blob: 24458550816e9e47330d94c4f9e1d64afaf7fa07 [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"
29 oframe "github.com/opencord/omci-lib-go/v2/meframe"
khenaidoo7d3c5582021-08-11 18:09:44 -040030 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000031 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000032)
33
Joey Armstrong89c812c2024-01-12 19:00:20 -050034// OmciTestRequest structure holds the information for the OMCI test
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000035type OmciTestRequest struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000036 pDevOmciCC *cmn.OmciCC
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053037 verifyDone chan<- bool
38 deviceID string
39 txSeqNo uint16
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000040 extended bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000041 started bool
42 result bool
Himani Chawla4d908332020-08-31 12:30:20 +053043 exclusiveCc bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000044 allowFailure bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000045}
46
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000047// CTestRequestOmciTimeout - Special OMCI timeout for low prio test request
48const CTestRequestOmciTimeout = 5
49
Joey Armstrong89c812c2024-01-12 19:00:20 -050050// NewOmciTestRequest returns a new instance of OmciTestRequest
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000051func NewOmciTestRequest(ctx context.Context,
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000052 deviceID string, omciCc *cmn.OmciCC, extended bool,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000053 exclusive bool, allowFailure bool) *OmciTestRequest {
54 logger.Debug(ctx, "OmciTestRequest-init")
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053055 //nolint:govet
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000056 var OmciTestRequest OmciTestRequest
57 OmciTestRequest.deviceID = deviceID
58 OmciTestRequest.pDevOmciCC = omciCc
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000059 OmciTestRequest.extended = extended
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000060 OmciTestRequest.started = false
61 OmciTestRequest.result = false
62 OmciTestRequest.exclusiveCc = exclusive
63 OmciTestRequest.allowFailure = allowFailure
Holger Hildebrandtfa074992020-03-27 15:42:06 +000064
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000065 return &OmciTestRequest
Holger Hildebrandtfa074992020-03-27 15:42:06 +000066}
67
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000068// PerformOmciTest - TODO: add comment
69func (oo *OmciTestRequest) PerformOmciTest(ctx context.Context, execChannel chan<- bool) {
70 logger.Debug(ctx, "OmciTestRequest-start-test")
Holger Hildebrandtfa074992020-03-27 15:42:06 +000071
72 if oo.pDevOmciCC != nil {
Himani Chawla4d908332020-08-31 12:30:20 +053073 oo.verifyDone = execChannel
Holger Hildebrandtfa074992020-03-27 15:42:06 +000074 // test functionality is limited to ONU-2G get request for the moment
75 // without yet checking the received response automatically here (might be improved ??)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000076 tid := oo.pDevOmciCC.GetNextTid(false)
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000077 onu2gGet, _ := oo.createOnu2gGet(ctx, tid)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000078 omciRxCallbackPair := cmn.CallbackPair{
79 CbKey: tid,
80 CbEntry: cmn.CallbackPairEntry{
81 CbRespChannel: nil,
82 CbFunction: oo.ReceiveOmciVerifyResponse,
83 FramePrint: true,
84 },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000085 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000086 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 +000087 // send with default timeout and normal prio
Girish Gowdra0b235842021-03-09 13:06:46 -080088 // Note: No reference to fetch the OMCI timeout value from configuration, so hardcode it to 10s
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000089 go oo.pDevOmciCC.Send(ctx, onu2gGet, CTestRequestOmciTimeout, cmn.CDefaultRetries, false, omciRxCallbackPair)
Holger Hildebrandtfa074992020-03-27 15:42:06 +000090
91 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +000092 logger.Errorw(ctx, "performOmciTest: Device does not exist", log.Fields{"for device-id": oo.deviceID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +000093 }
94}
95
96// these are OMCI related functions, could/should be collected in a separate file? TODO!!!
97// for a simple start just included in here
Joey Armstrong89c812c2024-01-12 19:00:20 -050098// basic approach copied from bbsim, cmp /devices/onu.go and /internal/common/omci/mibpackets.go
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000099func (oo *OmciTestRequest) createOnu2gGet(ctx context.Context, tid uint16) ([]byte, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000100
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000101 meParams := me.ParamData{
102 EntityID: 0,
103 Attributes: me.AttributeValueMap{
104 me.Onu2G_EquipmentId: "",
105 me.Onu2G_OpticalNetworkUnitManagementAndControlChannelOmccVersion: 0},
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000106 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000107 meInstance, omciErr := me.NewOnu2G(meParams)
108 if omciErr.GetError() == nil {
109 var messageSet omci.DeviceIdent = omci.BaselineIdent
110 if oo.extended {
111 messageSet = omci.ExtendedIdent
112 }
113 omciLayer, msgLayer, err := oframe.EncodeFrame(meInstance, omci.GetRequestType, oframe.TransactionID(tid),
114 meframe.FrameFormat(messageSet))
115 if err != nil {
116 logger.Errorw(ctx, "Cannot encode ONU2-G instance for get", log.Fields{
117 "Err": err, "device-id": oo.deviceID})
118 return nil, err
119 }
120 oo.txSeqNo = tid
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000121
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000122 pkt, err := cmn.SerializeOmciLayer(ctx, omciLayer, msgLayer)
123 if err != nil {
124 logger.Errorw(ctx, "Cannot serialize ONU2-G get", log.Fields{
125 "Err": err, "device-id": oo.deviceID})
126 return nil, err
127 }
128 return pkt, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000129 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000130 logger.Errorw(ctx, "Cannot generate ONU2-G", log.Fields{
131 "Err": omciErr.GetError(), "device-id": oo.deviceID})
132 return nil, omciErr.GetError()
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000133}
134
Joey Armstrong89c812c2024-01-12 19:00:20 -0500135// ReceiveOmciVerifyResponse supply a response handler - in this testobject the message is evaluated directly, no response channel used
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000136func (oo *OmciTestRequest) ReceiveOmciVerifyResponse(ctx context.Context, omciMsg *omci.OMCI, packet *gp.Packet, respChan chan cmn.Message) error {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000137
dbainbri4d3a0dc2020-12-02 00:33:42 +0000138 logger.Debugw(ctx, "verify-omci-message-response received:", log.Fields{"omciMsgType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000139 "transCorrId": omciMsg.TransactionID, "DeviceIdent": omciMsg.DeviceIdentifier})
140
141 if omciMsg.TransactionID == oo.txSeqNo {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000142 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct TransCorrId": omciMsg.TransactionID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000143 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000144 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect TransCorrId": omciMsg.TransactionID,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000145 "expected": oo.txSeqNo})
146 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200147 return fmt.Errorf("unexpected TransCorrId %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000148 }
149 if omciMsg.MessageType == omci.GetResponseType {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000150 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct RespType": omciMsg.MessageType})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000151 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000152 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect RespType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000153 "expected": omci.GetResponseType})
154 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200155 return fmt.Errorf("unexpected MessageType %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000156 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000157 if oo.extended {
158 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
159 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct DeviceIdentifier": omciMsg.DeviceIdentifier})
160 } else {
161 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect DeviceIdentifier": omciMsg.DeviceIdentifier,
162 "expected": omci.ExtendedIdent})
163 oo.verifyDone <- false
164 return fmt.Errorf("unexpected DeviceIdentifier %s", oo.deviceID)
165 }
166 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000167
168 //TODO!!! further tests on the payload should be done here ...
169
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000170 oo.pDevOmciCC.RLockMutexMonReq()
171 if _, exist := oo.pDevOmciCC.GetMonitoredRequest(omciMsg.TransactionID); exist {
172 oo.pDevOmciCC.SetChMonitoredRequest(omciMsg.TransactionID, true)
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000173 } else {
174 logger.Infow(ctx, "reqMon: map entry does not exist!",
175 log.Fields{"tid": omciMsg.TransactionID, "device-id": oo.deviceID})
176 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000177 oo.pDevOmciCC.RUnlockMutexMonReq()
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000178
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000179 oo.result = true
180 oo.verifyDone <- true
181
182 return nil
183}