blob: 28af6b41d95865e8137863feedb88b74fcd89e95 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
Joey Armstronge8c091f2023-01-17 16:56:26 -05002 * Copyright 2020-2023 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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package omcitst provides the omci test functionality
18package 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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000034//OmciTestRequest structure holds the information for the OMCI test
35type OmciTestRequest struct {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000036 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000037 pDevOmciCC *cmn.OmciCC
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000038 extended bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000039 started bool
40 result bool
Himani Chawla4d908332020-08-31 12:30:20 +053041 exclusiveCc bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000042 allowFailure bool
43 txSeqNo uint16
44 verifyDone chan<- bool
45}
46
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000047// CTestRequestOmciTimeout - Special OMCI timeout for low prio test request
48const CTestRequestOmciTimeout = 5
49
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000050//NewOmciTestRequest returns a new instance of OmciTestRequest
51func 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")
55 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
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000088 go oo.pDevOmciCC.Send(ctx, onu2gGet, CTestRequestOmciTimeout, cmn.CDefaultRetries, false, omciRxCallbackPair)
Holger Hildebrandtfa074992020-03-27 15:42:06 +000089
90 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +000091 logger.Errorw(ctx, "performOmciTest: Device does not exist", log.Fields{"for device-id": oo.deviceID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +000092 }
93}
94
95// these are OMCI related functions, could/should be collected in a separate file? TODO!!!
96// for a simple start just included in here
97//basic approach copied from bbsim, cmp /devices/onu.go and /internal/common/omci/mibpackets.go
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +000098func (oo *OmciTestRequest) createOnu2gGet(ctx context.Context, tid uint16) ([]byte, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000099
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000100 meParams := me.ParamData{
101 EntityID: 0,
102 Attributes: me.AttributeValueMap{
103 me.Onu2G_EquipmentId: "",
104 me.Onu2G_OpticalNetworkUnitManagementAndControlChannelOmccVersion: 0},
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000105 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000106 meInstance, omciErr := me.NewOnu2G(meParams)
107 if omciErr.GetError() == nil {
108 var messageSet omci.DeviceIdent = omci.BaselineIdent
109 if oo.extended {
110 messageSet = omci.ExtendedIdent
111 }
112 omciLayer, msgLayer, err := oframe.EncodeFrame(meInstance, omci.GetRequestType, oframe.TransactionID(tid),
113 meframe.FrameFormat(messageSet))
114 if err != nil {
115 logger.Errorw(ctx, "Cannot encode ONU2-G instance for get", log.Fields{
116 "Err": err, "device-id": oo.deviceID})
117 return nil, err
118 }
119 oo.txSeqNo = tid
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000120
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000121 pkt, err := cmn.SerializeOmciLayer(ctx, omciLayer, msgLayer)
122 if err != nil {
123 logger.Errorw(ctx, "Cannot serialize ONU2-G get", log.Fields{
124 "Err": err, "device-id": oo.deviceID})
125 return nil, err
126 }
127 return pkt, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000128 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000129 logger.Errorw(ctx, "Cannot generate ONU2-G", log.Fields{
130 "Err": omciErr.GetError(), "device-id": oo.deviceID})
131 return nil, omciErr.GetError()
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000132}
133
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000134//ReceiveOmciVerifyResponse supply a response handler - in this testobject the message is evaluated directly, no response channel used
135func (oo *OmciTestRequest) ReceiveOmciVerifyResponse(ctx context.Context, omciMsg *omci.OMCI, packet *gp.Packet, respChan chan cmn.Message) error {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000136
dbainbri4d3a0dc2020-12-02 00:33:42 +0000137 logger.Debugw(ctx, "verify-omci-message-response received:", log.Fields{"omciMsgType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000138 "transCorrId": omciMsg.TransactionID, "DeviceIdent": omciMsg.DeviceIdentifier})
139
140 if omciMsg.TransactionID == oo.txSeqNo {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000141 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct TransCorrId": omciMsg.TransactionID})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000142 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000143 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect TransCorrId": omciMsg.TransactionID,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000144 "expected": oo.txSeqNo})
145 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200146 return fmt.Errorf("unexpected TransCorrId %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000147 }
148 if omciMsg.MessageType == omci.GetResponseType {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000149 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct RespType": omciMsg.MessageType})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000150 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000151 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect RespType": omciMsg.MessageType,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000152 "expected": omci.GetResponseType})
153 oo.verifyDone <- false
Andrea Campanella6515c582020-10-05 11:25:00 +0200154 return fmt.Errorf("unexpected MessageType %s", oo.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000155 }
Holger Hildebrandta6ef0e82022-04-06 13:11:32 +0000156 if oo.extended {
157 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
158 logger.Debugw(ctx, "verify-omci-message-response", log.Fields{"correct DeviceIdentifier": omciMsg.DeviceIdentifier})
159 } else {
160 logger.Debugw(ctx, "verify-omci-message-response error", log.Fields{"incorrect DeviceIdentifier": omciMsg.DeviceIdentifier,
161 "expected": omci.ExtendedIdent})
162 oo.verifyDone <- false
163 return fmt.Errorf("unexpected DeviceIdentifier %s", oo.deviceID)
164 }
165 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000166
167 //TODO!!! further tests on the payload should be done here ...
168
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000169 oo.pDevOmciCC.RLockMutexMonReq()
170 if _, exist := oo.pDevOmciCC.GetMonitoredRequest(omciMsg.TransactionID); exist {
171 oo.pDevOmciCC.SetChMonitoredRequest(omciMsg.TransactionID, true)
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000172 } else {
173 logger.Infow(ctx, "reqMon: map entry does not exist!",
174 log.Fields{"tid": omciMsg.TransactionID, "device-id": oo.deviceID})
175 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000176 oo.pDevOmciCC.RUnlockMutexMonReq()
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000177
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000178 oo.result = true
179 oo.verifyDone <- true
180
181 return nil
182}