blob: f8def86743427bc9438651780cdc565a26343e91 [file] [log] [blame]
Girish Gowdra6afb56a2021-04-27 17:47:57 -07001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
Girish Gowdra6afb56a2021-04-27 17:47:57 -07003 *
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
Girish Gowdra6afb56a2021-04-27 17:47:57 -070019
20import (
21 "context"
22 "fmt"
khenaidoo7d3c5582021-08-11 18:09:44 -040023 "sync"
24 "time"
25
Girish Gowdra6afb56a2021-04-27 17:47:57 -070026 "github.com/looplab/fsm"
mpagenko836a1fd2021-11-01 16:12:42 +000027 "github.com/opencord/omci-lib-go/v2"
28 "github.com/opencord/omci-lib-go/v2/generated"
29 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040030 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000031 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
khenaidoo7d3c5582021-08-11 18:09:44 -040032 "github.com/opencord/voltha-protos/v5/go/extension"
Girish Gowdra6afb56a2021-04-27 17:47:57 -070033)
34
35const (
36 // events of Self Test FSM
37 selfTestEventTestRequest = "selfTestEventTestRequest"
38 selfTestEventTestResponseSuccess = "selfTestEventTestResponseSuccess"
39 selfTestEventTestResultSuccess = "selfTestEventTestResultSuccess"
40 selfTestEventAbort = "selfTestEventAbort"
41)
42const (
43 // states of Self Test FSM
44 selfTestStNull = "selfTestStNull"
45 selfTestStHandleSelfTestReq = "selfTestStHandleSelfTestReq"
46 selfTestStHandleSelfTestResp = "selfTestStHandleSelfTestResp"
47 selfTestStHandleTestResult = "selfTestStHandleTestResult"
48)
49
50const (
51 //SelfTestResponseWaitTimeout specifies timeout value waiting for self test response. Unit in seconds
52 SelfTestResponseWaitTimeout = 2
53)
54
55// We initiate an fsmCb per Self Test Request
56type fsmCb struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000057 fsm *cmn.AdapterFsm
Girish Gowdra6afb56a2021-04-27 17:47:57 -070058 reqMsg extension.SingleGetValueRequest
59 respChan chan extension.SingleGetValueResponse
60 stopOmciChan chan bool
61}
62
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000063// SelfTestControlBlock - TODO: add comment
64type SelfTestControlBlock struct {
Girish Gowdra6afb56a2021-04-27 17:47:57 -070065 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000066 pDeviceHandler cmn.IdeviceHandler
67 pDevEntry cmn.IonuDeviceEntry
Girish Gowdra6afb56a2021-04-27 17:47:57 -070068
69 selfTestFsmMap map[generated.ClassID]*fsmCb // The fsmCb is indexed by ME Class ID of the Test Action procedure
70 selfTestFsmLock sync.RWMutex
71
Girish Gowdra10123c02021-08-30 11:52:06 -070072 selfTestHandlerLock sync.RWMutex
73 selfTestHandlerActive bool
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000074 StopSelfTestModule chan bool
Girish Gowdra6afb56a2021-04-27 17:47:57 -070075}
76
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000077// NewSelfTestMsgHandlerCb creates the SelfTestControlBlock
Girish Gowdra6afb56a2021-04-27 17:47:57 -070078// Self Test Handler module supports sending SelfTestRequest and handling of SelfTestResponse/SelfTestResults
79// An ephemeral Self Test FSM is initiated for every Self Test request and multiple Self Tests on different
80// MEs (that support it) can be handled in parallel.
81// At the time of creating this module, only ANI-G self-test is supported.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000082func NewSelfTestMsgHandlerCb(ctx context.Context, dh cmn.IdeviceHandler, devEntry cmn.IonuDeviceEntry) *SelfTestControlBlock {
83 selfTestCb := SelfTestControlBlock{
84 deviceID: dh.GetDeviceID(),
85 pDeviceHandler: dh,
86 pDevEntry: devEntry,
87 }
Girish Gowdra6afb56a2021-04-27 17:47:57 -070088 selfTestCb.selfTestFsmMap = make(map[generated.ClassID]*fsmCb)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000089 selfTestCb.StopSelfTestModule = make(chan bool)
Girish Gowdra6afb56a2021-04-27 17:47:57 -070090
91 go selfTestCb.waitForStopSelfTestModuleSignal(ctx)
92
93 return &selfTestCb
94}
95
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000096func (selfTestCb *SelfTestControlBlock) initiateNewSelfTestFsm(ctx context.Context, reqMsg extension.SingleGetValueRequest,
97 CommChan chan cmn.Message, classID generated.ClassID, respChan chan extension.SingleGetValueResponse) error {
98 aFsm := cmn.NewAdapterFsm("selfTestFsm", selfTestCb.deviceID, CommChan)
Girish Gowdra6afb56a2021-04-27 17:47:57 -070099
100 if aFsm == nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000101 logger.Errorw(ctx, "selfTestFsm cmn.AdapterFsm could not be instantiated!!", log.Fields{
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700102 "device-id": selfTestCb.deviceID})
103 return fmt.Errorf("nil-adapter-fsm")
104 }
105 // Self Test FSM related state machine
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000106 aFsm.PFsm = fsm.NewFSM(
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700107
108 selfTestStNull,
109 fsm.Events{
110 {Name: selfTestEventTestRequest, Src: []string{selfTestStNull}, Dst: selfTestStHandleSelfTestReq},
111 {Name: selfTestEventTestResponseSuccess, Src: []string{selfTestStHandleSelfTestReq}, Dst: selfTestStHandleSelfTestResp},
112 {Name: selfTestEventTestResultSuccess, Src: []string{selfTestStHandleSelfTestResp}, Dst: selfTestStNull},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000113 {Name: selfTestEventAbort, Src: []string{selfTestStHandleSelfTestReq, selfTestStHandleSelfTestReq, selfTestStHandleTestResult,
114 selfTestStNull}, Dst: selfTestStNull},
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700115 },
116 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000117 "enter_state": func(e *fsm.Event) { aFsm.LogFsmStateChange(ctx, e) },
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700118 "enter_" + selfTestStHandleSelfTestReq: func(e *fsm.Event) { selfTestCb.selfTestFsmHandleSelfTestRequest(ctx, e) },
119 "enter_" + selfTestStHandleSelfTestResp: func(e *fsm.Event) { selfTestCb.selfTestFsmHandleSelfTestResponse(ctx, e) },
120 },
121 )
122 selfTestCb.selfTestFsmLock.Lock()
123 selfTestCb.selfTestFsmMap[classID] = &fsmCb{fsm: aFsm, reqMsg: reqMsg, respChan: respChan, stopOmciChan: make(chan bool)}
124 // Initiate the selfTestEventTestRequest on the FSM. Also pass the additional argument - classID.
125 // This is useful for the the FSM handler function to pull out fsmCb from the selfTestCb.selfTestFsmMap map.
126 selfTestCb.triggerFsmEvent(aFsm, selfTestEventTestRequest, classID)
127 selfTestCb.selfTestFsmLock.Unlock()
128
129 return nil
130}
131
132///// FSM Handlers
133
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000134func (selfTestCb *SelfTestControlBlock) selfTestFsmHandleSelfTestRequest(ctx context.Context, e *fsm.Event) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700135 classID := e.Args[0].(generated.ClassID)
136 selfTestCb.selfTestFsmLock.RLock()
137 pFsmCb, ok := selfTestCb.selfTestFsmMap[classID]
138 selfTestCb.selfTestFsmLock.RUnlock()
139 if !ok {
140 // This case is impossible. Would be curious to see if this happens
141 logger.Fatalw(ctx, "class-id-not-found", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
142 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000143 instKeys := selfTestCb.pDevEntry.GetOnuDB().GetSortedInstKeys(ctx, classID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700144
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000145 // TODO: Choosing the first index from the instance keys. For ANI-G, this is fine as there is only one ANI-G instance.
146 // How do we handle and report self test for multiple instances?
147 if err := selfTestCb.pDevEntry.GetDevOmciCC().SendSelfTestReq(ctx, classID, instKeys[0], selfTestCb.pDeviceHandler.GetOmciTimeout(),
148 false, pFsmCb.fsm.CommChan); err != nil {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700149 logger.Errorw(ctx, "error sending self test request", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
150 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
151 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
152 return
153 }
154
155 go selfTestCb.handleOmciResponse(ctx, classID)
156}
157
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000158func (selfTestCb *SelfTestControlBlock) selfTestFsmHandleSelfTestResponse(ctx context.Context, e *fsm.Event) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700159 classID := e.Args[0].(generated.ClassID)
160 // Pass the test result processing to another routine
161 go selfTestCb.handleOmciResponse(ctx, classID)
162
163}
164
165///// Utility functions
166
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000167func (selfTestCb *SelfTestControlBlock) getMeClassID(ctx context.Context, reqMsg extension.SingleGetValueRequest) (generated.ClassID, error) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700168 switch reqMsg.GetRequest().GetRequest().(type) {
169 case *extension.GetValueRequest_OnuOpticalInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000170 return me.AniGClassID, nil
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700171 default:
172 logger.Warnw(ctx, "unsupported me class id for self test", log.Fields{"device-id": selfTestCb.deviceID})
173 return 0, fmt.Errorf("unsupported me class id for self test %v", selfTestCb.deviceID)
174 }
175}
176
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000177func (selfTestCb *SelfTestControlBlock) triggerFsmEvent(pSelfTestFsm *cmn.AdapterFsm, event string, args ...generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700178 go func() {
179 if len(args) > 0 {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000180 _ = pSelfTestFsm.PFsm.Event(event, args[0])
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700181 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000182 _ = pSelfTestFsm.PFsm.Event(event)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700183 }
184 }()
185}
186
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000187func (selfTestCb *SelfTestControlBlock) submitFailureGetValueResponse(ctx context.Context, respChan chan extension.SingleGetValueResponse,
188 errorCode extension.GetValueResponse_ErrorReason, statusCode extension.GetValueResponse_Status) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700189 singleValResp := extension.SingleGetValueResponse{
190 Response: &extension.GetValueResponse{
191 Status: statusCode,
192 ErrReason: errorCode,
193 },
194 }
195 logger.Infow(ctx, "OMCI test response failure - pushing failure response", log.Fields{"device-id": selfTestCb.deviceID})
196 respChan <- singleValResp
197 logger.Infow(ctx, "OMCI test response failure - pushing failure response complete", log.Fields{"device-id": selfTestCb.deviceID})
198}
199
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000200func (selfTestCb *SelfTestControlBlock) handleOmciMessage(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700201 logger.Debugw(ctx, "omci Msg", log.Fields{"device-id": selfTestCb.deviceID, "msgType": msg.OmciMsg.MessageType, "msg": msg})
202 switch msg.OmciMsg.MessageType {
203 case omci.TestResponseType:
204 selfTestCb.handleOmciTestResponse(ctx, msg, cb, classID)
205 case omci.TestResultType:
206 selfTestCb.handleOmciTestResult(ctx, msg, cb, classID)
207 default:
208 logger.Warnw(ctx, "Unknown Message Type", log.Fields{"msgType": msg.OmciMsg.MessageType})
209 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
210 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR)
211 }
212}
213
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000214func (selfTestCb *SelfTestControlBlock) handleOmciTestResponse(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700215 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeTestResponse)
216 if msgLayer == nil {
217 logger.Errorw(ctx, "omci Msg layer nil self test response", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000218 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700219 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
220 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
221 return
222 }
223 msgObj, msgOk := msgLayer.(*omci.TestResponse)
224 if !msgOk {
225 logger.Errorw(ctx, "omci Msg layer could not be detected for self test response", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000226 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700227 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
228 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
229 return
230 }
231 logger.Debugw(ctx, "OMCI test response Data", log.Fields{"device-id": selfTestCb.deviceID, "data-fields": msgObj})
232 if msgObj.Result == generated.Success && msgObj.EntityClass == classID {
233 logger.Infow(ctx, "OMCI test response success", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
234 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventTestResponseSuccess, classID)
235 return
236 }
237
238 logger.Infow(ctx, "OMCI test response failure", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000239 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700240 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
241 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR)
242}
243
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000244func (selfTestCb *SelfTestControlBlock) handleOmciTestResult(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700245 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeTestResult)
246 if msgLayer == nil {
247 logger.Errorw(ctx, "omci Msg layer nil self test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
248 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
249 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
250 return
251 }
252 var msgObj *omci.OpticalLineSupervisionTestResult
253 var msgOk bool
254 switch classID {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000255 case me.AniGClassID:
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700256 msgObj, msgOk = msgLayer.(*omci.OpticalLineSupervisionTestResult)
257 default:
258 // We should not really land here
259 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
260 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
261 return
262 }
263 if !msgOk {
264 logger.Errorw(ctx, "omci Msg layer could not be detected for self test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
265 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
266 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
267 return
268 }
269 logger.Debugw(ctx, "raw omci values of ani-g test result",
270 log.Fields{"device-id": selfTestCb.deviceID,
271 "power-feed-voltage": msgObj.PowerFeedVoltage,
272 "rx-power": msgObj.ReceivedOpticalPower,
273 "tx-power": msgObj.MeanOpticalLaunch,
274 "laser-bias-current": msgObj.LaserBiasCurrent,
275 "temperature": msgObj.Temperature})
276 singleValResp := extension.SingleGetValueResponse{
277 Response: &extension.GetValueResponse{
278 Status: extension.GetValueResponse_OK,
279 Response: &extension.GetValueResponse_OnuOpticalInfo{
280 OnuOpticalInfo: &extension.GetOnuPonOpticalInfoResponse{
281 // OMCI representation is Volts, 2s compliment, 20mV resolution
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000282 PowerFeedVoltage: float32(cmn.TwosComplementToSignedInt16(msgObj.PowerFeedVoltage)) * 0.02,
Girish Gowdra444522f2021-05-12 14:32:24 -0700283 // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution.
284 // Subtract 30 to convert the unit from dBu to dBm (as expected by proto interface)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000285 ReceivedOpticalPower: float32(cmn.TwosComplementToSignedInt16(msgObj.ReceivedOpticalPower))*0.002 - 30,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700286 // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution
Girish Gowdra444522f2021-05-12 14:32:24 -0700287 // Subtract 30 to convert the unit from dBu to dBm (as expected by proto interface)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000288 MeanOpticalLaunchPower: float32(cmn.TwosComplementToSignedInt16(msgObj.MeanOpticalLaunch))*0.002 - 30,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700289 // OMCI representation is unsigned int, 2uA resolution
290 // units of gRPC interface is mA.
291 LaserBiasCurrent: float32(msgObj.LaserBiasCurrent) * 0.000002 * 1000, // multiply by 1000 to get units in mA
292 // OMCI representation is 2s complement, 1/256 degree Celsius resolution
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000293 Temperature: float32(cmn.TwosComplementToSignedInt16(msgObj.Temperature)) / 256.0,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700294 },
295 },
296 },
297 }
298 logger.Debugw(ctx, "ani-g test result after type/value conversion",
299 log.Fields{"device-id": selfTestCb.deviceID,
300 "power-feed-voltage": singleValResp.Response.GetOnuOpticalInfo().PowerFeedVoltage,
301 "rx-power": singleValResp.Response.GetOnuOpticalInfo().ReceivedOpticalPower,
302 "tx-power": singleValResp.Response.GetOnuOpticalInfo().MeanOpticalLaunchPower,
303 "laser-bias-current": singleValResp.Response.GetOnuOpticalInfo().LaserBiasCurrent,
304 "temperature": singleValResp.Response.GetOnuOpticalInfo().Temperature})
305 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventTestResultSuccess)
306 logger.Infow(ctx, "OMCI test result success - pushing results", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
307 cb.respChan <- singleValResp
308 selfTestCb.selfTestRequestComplete(ctx, cb.reqMsg)
309 logger.Infow(ctx, "OMCI test result success - pushing results complete", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
310}
311
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000312func (selfTestCb *SelfTestControlBlock) handleOmciResponse(ctx context.Context, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700313 selfTestCb.selfTestFsmLock.RLock()
314 pFsmCb, ok := selfTestCb.selfTestFsmMap[classID]
315 selfTestCb.selfTestFsmLock.RUnlock()
316 if !ok {
317 logger.Errorw(ctx, "fsb control block unavailable", log.Fields{"device-id": selfTestCb.deviceID, "class-id": classID})
318 return
319 }
320 select {
321 case <-pFsmCb.stopOmciChan:
322 logger.Infow(ctx, "omci processing stopped", log.Fields{"device-id": selfTestCb.deviceID, "class-id": classID})
323 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
324 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000325 case message, ok := <-pFsmCb.fsm.CommChan:
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700326 if !ok {
327 logger.Errorw(ctx, "Message couldn't be read from channel", log.Fields{"device-id": selfTestCb.deviceID})
328 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
329 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR)
330 }
331 logger.Debugw(ctx, "Received message on self test result channel", log.Fields{"device-id": selfTestCb.deviceID})
332
333 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000334 case cmn.OMCI:
335 msg, _ := message.Data.(cmn.OmciMessage)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700336 selfTestCb.handleOmciMessage(ctx, msg, pFsmCb, classID)
337 default:
338 logger.Errorw(ctx, "Unknown message type received", log.Fields{"device-id": selfTestCb.deviceID, "message.Type": message.Type})
339 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR)
340 }
341 case <-time.After(time.Duration(SelfTestResponseWaitTimeout) * time.Second):
342 logger.Errorw(ctx, "timeout waiting for test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
343 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
344 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_TIMEOUT, extension.GetValueResponse_ERROR)
345 }
346}
347
348// selfTestRequestComplete removes the fsmCb from the local cache if found
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000349func (selfTestCb *SelfTestControlBlock) selfTestRequestComplete(ctx context.Context, reqMsg extension.SingleGetValueRequest) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700350 meClassID, err := selfTestCb.getMeClassID(ctx, reqMsg)
351 if err != nil {
352 return
353 }
354 logger.Infow(ctx, "self test req handling complete", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": meClassID})
355 // Clear the fsmCb from the map
356 delete(selfTestCb.selfTestFsmMap, meClassID)
357}
358
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000359func (selfTestCb *SelfTestControlBlock) waitForStopSelfTestModuleSignal(ctx context.Context) {
Girish Gowdra10123c02021-08-30 11:52:06 -0700360 selfTestCb.SetSelfTestHandlerIsRunning(true)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000361 <-selfTestCb.StopSelfTestModule // block on stop signal
Girish Gowdra10123c02021-08-30 11:52:06 -0700362 selfTestCb.SetSelfTestHandlerIsRunning(false)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700363
364 logger.Infow(ctx, "received stop signal - clean up start", log.Fields{"device-id": selfTestCb.deviceID})
365 selfTestCb.selfTestFsmLock.Lock()
366 for classID, fsmCb := range selfTestCb.selfTestFsmMap {
367 select {
368 case fsmCb.stopOmciChan <- true: // stop omci processing routine if one was active. It eventually aborts the fsm
369 logger.Debugw(ctx, "stopped omci processing", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": classID})
370 default:
371 selfTestCb.triggerFsmEvent(fsmCb.fsm, selfTestEventAbort)
372 selfTestCb.submitFailureGetValueResponse(ctx, fsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR)
373 }
374 }
375 selfTestCb.selfTestFsmMap = make(map[generated.ClassID]*fsmCb) // reset map
376 selfTestCb.selfTestFsmLock.Unlock()
377 logger.Infow(ctx, "received stop signal - clean up end", log.Fields{"device-id": selfTestCb.deviceID})
378}
379
380//// Exported functions
381
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000382// SetSelfTestHandlerIsRunning sets the value to selfTestCb.selfTestHandlerActive
383func (selfTestCb *SelfTestControlBlock) SetSelfTestHandlerIsRunning(active bool) {
Girish Gowdra10123c02021-08-30 11:52:06 -0700384 selfTestCb.selfTestHandlerLock.Lock()
385 defer selfTestCb.selfTestHandlerLock.Unlock()
386 selfTestCb.selfTestHandlerActive = active
387}
388
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000389// GetSelfTestHandlerIsRunning gets selfTestCb.selfTestHandlerActive
390func (selfTestCb *SelfTestControlBlock) GetSelfTestHandlerIsRunning() bool {
Girish Gowdra10123c02021-08-30 11:52:06 -0700391 selfTestCb.selfTestHandlerLock.RLock()
392 defer selfTestCb.selfTestHandlerLock.RUnlock()
393 return selfTestCb.selfTestHandlerActive
394}
395
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000396// SelfTestRequestStart initiate Test Request handling procedure. The results are asynchronously conveyed on the respChan.
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700397// If the return from selfTestRequest is NOT nil, the caller shall not wait for async response.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000398func (selfTestCb *SelfTestControlBlock) SelfTestRequestStart(ctx context.Context, reqMsg extension.SingleGetValueRequest,
399 CommChan chan cmn.Message, respChan chan extension.SingleGetValueResponse) error {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700400 meClassID, err := selfTestCb.getMeClassID(ctx, reqMsg)
401 if err != nil {
402 return err
403 }
404 if _, ok := selfTestCb.selfTestFsmMap[meClassID]; ok {
405 logger.Errorw(ctx, "self test already in progress for class id", log.Fields{"device-id": selfTestCb.deviceID, "class-id": meClassID})
406 return fmt.Errorf("self-test-already-in-progress-for-class-id-%v-device-id-%v", meClassID, selfTestCb.deviceID)
407 }
408 logger.Infow(ctx, "self test request initiated", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": meClassID})
409 // indicates only if the FSM was initiated correctly. Response is asynchronous on respChan.
410 // If the return from here is NOT nil, the caller shall not wait for async response.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000411 return selfTestCb.initiateNewSelfTestFsm(ctx, reqMsg, CommChan, meClassID, respChan)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700412}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +0000413
414// PrepareForGarbageCollection - remove references to prepare for garbage collection
415func (selfTestCb *SelfTestControlBlock) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
416 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
417 selfTestCb.pDeviceHandler = nil
418 selfTestCb.pDevEntry = nil
419}