blob: cf8e005ac74be03fca1134a0fd1c76f7ee00de70 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors
Holger Hildebrandtfa074992020-03-27 15:42:06 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joey Armstrong89c812c2024-01-12 19:00:20 -050017// Package omcitst provides the omci test functionality
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000018package omcitst
Holger Hildebrandtfa074992020-03-27 15:42:06 +000019
20import (
21 "context"
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000022 "encoding/hex"
Andrea Campanella6515c582020-10-05 11:25:00 +020023 "fmt"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000024
Holger Hildebrandtfa074992020-03-27 15:42:06 +000025 gp "github.com/google/gopacket"
mpagenko836a1fd2021-11-01 16:12:42 +000026 "github.com/opencord/omci-lib-go/v2"
27 me "github.com/opencord/omci-lib-go/v2/generated"
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000028 "github.com/opencord/omci-lib-go/v2/meframe"
khenaidoo7d3c5582021-08-11 18:09:44 -040029 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000030 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000031)
32
Joey Armstrong89c812c2024-01-12 19:00:20 -050033// OmciTestRequest structure holds the information for the OMCI test
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000034type OmciTestRequest struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000035 pDevOmciCC *cmn.OmciCC
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053036 verifyDone chan<- bool
37 deviceID string
38 txSeqNo uint16
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000039 extended bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000040 started bool
41 result bool
Himani Chawla4d908332020-08-31 12:30:20 +053042 exclusiveCc bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000043 allowFailure bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000044}
45
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000046// CTestRequestOmciTimeout - Special OMCI timeout for low prio test request
47const CTestRequestOmciTimeout = 5
48
Joey Armstrong89c812c2024-01-12 19:00:20 -050049// NewOmciTestRequest returns a new instance of OmciTestRequest
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000050func NewOmciTestRequest(ctx context.Context,
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000051 deviceID string, omciCc *cmn.OmciCC, extended bool,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000052 exclusive bool, allowFailure bool) *OmciTestRequest {
53 logger.Debug(ctx, "OmciTestRequest-init")
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053054 //nolint:govet
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000055 var OmciTestRequest OmciTestRequest
56 OmciTestRequest.deviceID = deviceID
57 OmciTestRequest.pDevOmciCC = omciCc
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000058 OmciTestRequest.extended = extended
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000059 OmciTestRequest.started = false
60 OmciTestRequest.result = false
61 OmciTestRequest.exclusiveCc = exclusive
62 OmciTestRequest.allowFailure = allowFailure
Holger Hildebrandtfa074992020-03-27 15:42:06 +000063
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000064 return &OmciTestRequest
Holger Hildebrandtfa074992020-03-27 15:42:06 +000065}
66
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000067// PerformOmciTest - TODO: add comment
68func (oo *OmciTestRequest) PerformOmciTest(ctx context.Context, execChannel chan<- bool) {
69 logger.Debug(ctx, "OmciTestRequest-start-test")
Holger Hildebrandtfa074992020-03-27 15:42:06 +000070
71 if oo.pDevOmciCC != nil {
Himani Chawla4d908332020-08-31 12:30:20 +053072 oo.verifyDone = execChannel
Holger Hildebrandtfa074992020-03-27 15:42:06 +000073 // test functionality is limited to ONU-2G get request for the moment
74 // without yet checking the received response automatically here (might be improved ??)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000075 tid := oo.pDevOmciCC.GetNextTid(false)
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000076 onu2gGet, _ := oo.createOnu2gGet(ctx, tid)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000077 omciRxCallbackPair := cmn.CallbackPair{
78 CbKey: tid,
79 CbEntry: cmn.CallbackPairEntry{
80 CbRespChannel: nil,
81 CbFunction: oo.ReceiveOmciVerifyResponse,
82 FramePrint: true,
83 },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000084 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000085 logger.Debugw(ctx, "performOmciTest-start sending frame", log.Fields{"for device-id": oo.deviceID, "onu2gGet": hex.EncodeToString(onu2gGet)})
Holger Hildebrandtfa074992020-03-27 15:42:06 +000086 // send with default timeout and normal prio
Girish Gowdra0b235842021-03-09 13:06:46 -080087 // Note: No reference to fetch the OMCI timeout value from configuration, so hardcode it to 10s
mgoudad611f4c2025-10-30 14:49:27 +053088 go func() {
89 _ = oo.pDevOmciCC.Send(ctx, onu2gGet, CTestRequestOmciTimeout, cmn.CDefaultRetries, false, omciRxCallbackPair)
90 }()
Holger Hildebrandtfa074992020-03-27 15:42:06 +000091
92 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +000093 logger.Errorw(ctx, "performOmciTest: Device does not exist", log.Fields{"for device-id": oo.deviceID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +000094 }
95}
96
97// these are OMCI related functions, could/should be collected in a separate file? TODO!!!
98// for a simple start just included in here
Joey Armstrong89c812c2024-01-12 19:00:20 -050099// basic approach copied from bbsim, cmp /devices/onu.go and /internal/common/omci/mibpackets.go
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000100func (oo *OmciTestRequest) createOnu2gGet(ctx context.Context, tid uint16) ([]byte, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000101
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000102 meParams := me.ParamData{
103 EntityID: 0,
104 Attributes: me.AttributeValueMap{
105 me.Onu2G_EquipmentId: "",
106 me.Onu2G_OpticalNetworkUnitManagementAndControlChannelOmccVersion: 0},
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000107 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000108 meInstance, omciErr := me.NewOnu2G(meParams)
109 if omciErr.GetError() == nil {
mgoudad611f4c2025-10-30 14:49:27 +0530110 var messageSet = omci.BaselineIdent
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000111 if oo.extended {
112 messageSet = omci.ExtendedIdent
113 }
mgoudad611f4c2025-10-30 14:49:27 +0530114 omciLayer, msgLayer, err := meframe.EncodeFrame(meInstance, omci.GetRequestType, meframe.TransactionID(tid),
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000115 meframe.FrameFormat(messageSet))
116 if err != nil {
117 logger.Errorw(ctx, "Cannot encode ONU2-G instance for get", log.Fields{
118 "Err": err, "device-id": oo.deviceID})
119 return nil, err
120 }
121 oo.txSeqNo = tid
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000122
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000123 pkt, err := cmn.SerializeOmciLayer(ctx, omciLayer, msgLayer)
124 if err != nil {
125 logger.Errorw(ctx, "Cannot serialize ONU2-G get", log.Fields{
126 "Err": err, "device-id": oo.deviceID})
127 return nil, err
128 }
129 return pkt, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000130 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000131 logger.Errorw(ctx, "Cannot generate ONU2-G", log.Fields{
132 "Err": omciErr.GetError(), "device-id": oo.deviceID})
133 return nil, omciErr.GetError()
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000134}
135
Joey Armstrong89c812c2024-01-12 19:00:20 -0500136// ReceiveOmciVerifyResponse supply a response handler - in this testobject the message is evaluated directly, no response channel used
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000137func (oo *OmciTestRequest) ReceiveOmciVerifyResponse(ctx context.Context, omciMsg *omci.OMCI, packet *gp.Packet, respChan chan cmn.Message) error {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000138
dbainbri4d3a0dc2020-12-02 00:33:42 +0000139 logger.Debugw(ctx, "verify-omci-message-response received:", log.Fields{"omciMsgType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000140 "transCorrId": omciMsg.TransactionID, "DeviceIdent": omciMsg.DeviceIdentifier})
141
142 if omciMsg.TransactionID == oo.txSeqNo {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000143 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct TransCorrId": omciMsg.TransactionID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000144 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000145 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect TransCorrId": omciMsg.TransactionID,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000146 "expected": oo.txSeqNo})
147 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200148 return fmt.Errorf("unexpected TransCorrId %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000149 }
150 if omciMsg.MessageType == omci.GetResponseType {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000151 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct RespType": omciMsg.MessageType})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000152 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000153 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect RespType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000154 "expected": omci.GetResponseType})
155 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200156 return fmt.Errorf("unexpected MessageType %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000157 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000158 if oo.extended {
159 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
160 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct DeviceIdentifier": omciMsg.DeviceIdentifier})
161 } else {
162 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect DeviceIdentifier": omciMsg.DeviceIdentifier,
163 "expected": omci.ExtendedIdent})
164 oo.verifyDone <- false
165 return fmt.Errorf("unexpected DeviceIdentifier %s", oo.deviceID)
166 }
167 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000168
169 //TODO!!! further tests on the payload should be done here ...
170
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000171 oo.pDevOmciCC.RLockMutexMonReq()
172 if _, exist := oo.pDevOmciCC.GetMonitoredRequest(omciMsg.TransactionID); exist {
173 oo.pDevOmciCC.SetChMonitoredRequest(omciMsg.TransactionID, true)
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000174 } else {
175 logger.Infow(ctx, "reqMon: map entry does not exist!",
176 log.Fields{"tid": omciMsg.TransactionID, "device-id": oo.deviceID})
177 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000178 oo.pDevOmciCC.RUnlockMutexMonReq()
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000179
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000180 oo.result = true
181 oo.verifyDone <- true
182
183 return nil
184}