blob: c5148cb23d248be5290b611e229d61ded055dc71 [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
Sridhar Ravindraf1331ad2024-02-15 16:13:37 +053072 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},
bseeniva6db14a22024-12-07 19:41:01 +0530113 {Name: selfTestEventAbort, Src: []string{selfTestStHandleSelfTestReq, selfTestStHandleSelfTestResp, selfTestStHandleTestResult,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000114 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)
bseeniva6db14a22024-12-07 19:41:01 +0530151 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, pFsmCb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700152 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,
bseeniva6db14a22024-12-07 19:41:01 +0530188 errorCode extension.GetValueResponse_ErrorReason, statusCode extension.GetValueResponse_Status, reqMsg extension.SingleGetValueRequest) {
189 meClassID, err := selfTestCb.getMeClassID(ctx, reqMsg)
190 if err != nil {
191 return
192 }
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700193 singleValResp := extension.SingleGetValueResponse{
194 Response: &extension.GetValueResponse{
195 Status: statusCode,
196 ErrReason: errorCode,
197 },
198 }
199 logger.Infow(ctx, "OMCI test response failure - pushing failure response", log.Fields{"device-id": selfTestCb.deviceID})
bseeniva6db14a22024-12-07 19:41:01 +0530200 // Clear the fsmCb from the map
201 delete(selfTestCb.selfTestFsmMap, meClassID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700202 respChan <- singleValResp
203 logger.Infow(ctx, "OMCI test response failure - pushing failure response complete", log.Fields{"device-id": selfTestCb.deviceID})
204}
205
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000206func (selfTestCb *SelfTestControlBlock) handleOmciMessage(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700207 logger.Debugw(ctx, "omci Msg", log.Fields{"device-id": selfTestCb.deviceID, "msgType": msg.OmciMsg.MessageType, "msg": msg})
208 switch msg.OmciMsg.MessageType {
209 case omci.TestResponseType:
210 selfTestCb.handleOmciTestResponse(ctx, msg, cb, classID)
211 case omci.TestResultType:
212 selfTestCb.handleOmciTestResult(ctx, msg, cb, classID)
213 default:
214 logger.Warnw(ctx, "Unknown Message Type", log.Fields{"msgType": msg.OmciMsg.MessageType})
215 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530216 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700217 }
218}
219
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000220func (selfTestCb *SelfTestControlBlock) handleOmciTestResponse(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700221 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeTestResponse)
222 if msgLayer == nil {
223 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 +0000224 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700225 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530226 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700227 return
228 }
229 msgObj, msgOk := msgLayer.(*omci.TestResponse)
230 if !msgOk {
231 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 +0000232 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700233 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530234 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700235 return
236 }
237 logger.Debugw(ctx, "OMCI test response Data", log.Fields{"device-id": selfTestCb.deviceID, "data-fields": msgObj})
238 if msgObj.Result == generated.Success && msgObj.EntityClass == classID {
239 logger.Infow(ctx, "OMCI test response success", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
240 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventTestResponseSuccess, classID)
241 return
242 }
243
244 logger.Infow(ctx, "OMCI test response failure", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000245 selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700246 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530247 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700248}
249
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000250func (selfTestCb *SelfTestControlBlock) handleOmciTestResult(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700251 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeTestResult)
252 if msgLayer == nil {
253 logger.Errorw(ctx, "omci Msg layer nil self test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
254 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530255 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700256 return
257 }
258 var msgObj *omci.OpticalLineSupervisionTestResult
259 var msgOk bool
260 switch classID {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000261 case me.AniGClassID:
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700262 msgObj, msgOk = msgLayer.(*omci.OpticalLineSupervisionTestResult)
263 default:
264 // We should not really land here
265 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530266 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700267 return
268 }
269 if !msgOk {
270 logger.Errorw(ctx, "omci Msg layer could not be detected for self test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
271 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530272 selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700273 return
274 }
275 logger.Debugw(ctx, "raw omci values of ani-g test result",
276 log.Fields{"device-id": selfTestCb.deviceID,
277 "power-feed-voltage": msgObj.PowerFeedVoltage,
278 "rx-power": msgObj.ReceivedOpticalPower,
279 "tx-power": msgObj.MeanOpticalLaunch,
280 "laser-bias-current": msgObj.LaserBiasCurrent,
281 "temperature": msgObj.Temperature})
282 singleValResp := extension.SingleGetValueResponse{
283 Response: &extension.GetValueResponse{
284 Status: extension.GetValueResponse_OK,
285 Response: &extension.GetValueResponse_OnuOpticalInfo{
286 OnuOpticalInfo: &extension.GetOnuPonOpticalInfoResponse{
287 // OMCI representation is Volts, 2s compliment, 20mV resolution
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000288 PowerFeedVoltage: float32(cmn.TwosComplementToSignedInt16(msgObj.PowerFeedVoltage)) * 0.02,
Girish Gowdra444522f2021-05-12 14:32:24 -0700289 // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution.
290 // Subtract 30 to convert the unit from dBu to dBm (as expected by proto interface)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000291 ReceivedOpticalPower: float32(cmn.TwosComplementToSignedInt16(msgObj.ReceivedOpticalPower))*0.002 - 30,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700292 // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution
Girish Gowdra444522f2021-05-12 14:32:24 -0700293 // Subtract 30 to convert the unit from dBu to dBm (as expected by proto interface)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000294 MeanOpticalLaunchPower: float32(cmn.TwosComplementToSignedInt16(msgObj.MeanOpticalLaunch))*0.002 - 30,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700295 // OMCI representation is unsigned int, 2uA resolution
296 // units of gRPC interface is mA.
297 LaserBiasCurrent: float32(msgObj.LaserBiasCurrent) * 0.000002 * 1000, // multiply by 1000 to get units in mA
298 // OMCI representation is 2s complement, 1/256 degree Celsius resolution
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000299 Temperature: float32(cmn.TwosComplementToSignedInt16(msgObj.Temperature)) / 256.0,
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700300 },
301 },
302 },
303 }
304 logger.Debugw(ctx, "ani-g test result after type/value conversion",
305 log.Fields{"device-id": selfTestCb.deviceID,
306 "power-feed-voltage": singleValResp.Response.GetOnuOpticalInfo().PowerFeedVoltage,
307 "rx-power": singleValResp.Response.GetOnuOpticalInfo().ReceivedOpticalPower,
308 "tx-power": singleValResp.Response.GetOnuOpticalInfo().MeanOpticalLaunchPower,
309 "laser-bias-current": singleValResp.Response.GetOnuOpticalInfo().LaserBiasCurrent,
310 "temperature": singleValResp.Response.GetOnuOpticalInfo().Temperature})
311 selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventTestResultSuccess)
312 logger.Infow(ctx, "OMCI test result success - pushing results", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
313 cb.respChan <- singleValResp
314 selfTestCb.selfTestRequestComplete(ctx, cb.reqMsg)
315 logger.Infow(ctx, "OMCI test result success - pushing results complete", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
316}
317
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000318func (selfTestCb *SelfTestControlBlock) handleOmciResponse(ctx context.Context, classID generated.ClassID) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700319 selfTestCb.selfTestFsmLock.RLock()
320 pFsmCb, ok := selfTestCb.selfTestFsmMap[classID]
321 selfTestCb.selfTestFsmLock.RUnlock()
322 if !ok {
323 logger.Errorw(ctx, "fsb control block unavailable", log.Fields{"device-id": selfTestCb.deviceID, "class-id": classID})
324 return
325 }
326 select {
327 case <-pFsmCb.stopOmciChan:
328 logger.Infow(ctx, "omci processing stopped", log.Fields{"device-id": selfTestCb.deviceID, "class-id": classID})
329 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530330 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR, pFsmCb.reqMsg)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000331 case message, ok := <-pFsmCb.fsm.CommChan:
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700332 if !ok {
333 logger.Errorw(ctx, "Message couldn't be read from channel", log.Fields{"device-id": selfTestCb.deviceID})
334 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530335 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, pFsmCb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700336 }
337 logger.Debugw(ctx, "Received message on self test result channel", log.Fields{"device-id": selfTestCb.deviceID})
338
339 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000340 case cmn.OMCI:
341 msg, _ := message.Data.(cmn.OmciMessage)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700342 selfTestCb.handleOmciMessage(ctx, msg, pFsmCb, classID)
343 default:
344 logger.Errorw(ctx, "Unknown message type received", log.Fields{"device-id": selfTestCb.deviceID, "message.Type": message.Type})
bseeniva6db14a22024-12-07 19:41:01 +0530345 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, pFsmCb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700346 }
347 case <-time.After(time.Duration(SelfTestResponseWaitTimeout) * time.Second):
348 logger.Errorw(ctx, "timeout waiting for test result", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID})
349 selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530350 selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_TIMEOUT, extension.GetValueResponse_ERROR, pFsmCb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700351 }
352}
353
354// selfTestRequestComplete removes the fsmCb from the local cache if found
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000355func (selfTestCb *SelfTestControlBlock) selfTestRequestComplete(ctx context.Context, reqMsg extension.SingleGetValueRequest) {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700356 meClassID, err := selfTestCb.getMeClassID(ctx, reqMsg)
357 if err != nil {
358 return
359 }
360 logger.Infow(ctx, "self test req handling complete", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": meClassID})
361 // Clear the fsmCb from the map
362 delete(selfTestCb.selfTestFsmMap, meClassID)
363}
364
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000365func (selfTestCb *SelfTestControlBlock) waitForStopSelfTestModuleSignal(ctx context.Context) {
Girish Gowdra10123c02021-08-30 11:52:06 -0700366 selfTestCb.SetSelfTestHandlerIsRunning(true)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000367 <-selfTestCb.StopSelfTestModule // block on stop signal
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700368
369 logger.Infow(ctx, "received stop signal - clean up start", log.Fields{"device-id": selfTestCb.deviceID})
370 selfTestCb.selfTestFsmLock.Lock()
371 for classID, fsmCb := range selfTestCb.selfTestFsmMap {
372 select {
373 case fsmCb.stopOmciChan <- true: // stop omci processing routine if one was active. It eventually aborts the fsm
374 logger.Debugw(ctx, "stopped omci processing", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": classID})
375 default:
376 selfTestCb.triggerFsmEvent(fsmCb.fsm, selfTestEventAbort)
bseeniva6db14a22024-12-07 19:41:01 +0530377 selfTestCb.submitFailureGetValueResponse(ctx, fsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR, fsmCb.reqMsg)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700378 }
379 }
380 selfTestCb.selfTestFsmMap = make(map[generated.ClassID]*fsmCb) // reset map
381 selfTestCb.selfTestFsmLock.Unlock()
382 logger.Infow(ctx, "received stop signal - clean up end", log.Fields{"device-id": selfTestCb.deviceID})
383}
384
385//// Exported functions
386
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000387// SetSelfTestHandlerIsRunning sets the value to selfTestCb.selfTestHandlerActive
388func (selfTestCb *SelfTestControlBlock) SetSelfTestHandlerIsRunning(active bool) {
Sridhar Ravindraf1331ad2024-02-15 16:13:37 +0530389 selfTestCb.SelfTestHandlerLock.Lock()
390 defer selfTestCb.SelfTestHandlerLock.Unlock()
391 selfTestCb.SelfTestHandlerActive = active
Girish Gowdra10123c02021-08-30 11:52:06 -0700392}
393
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000394// GetSelfTestHandlerIsRunning gets selfTestCb.selfTestHandlerActive
395func (selfTestCb *SelfTestControlBlock) GetSelfTestHandlerIsRunning() bool {
Sridhar Ravindraf1331ad2024-02-15 16:13:37 +0530396 selfTestCb.SelfTestHandlerLock.RLock()
397 defer selfTestCb.SelfTestHandlerLock.RUnlock()
398 return selfTestCb.SelfTestHandlerActive
Girish Gowdra10123c02021-08-30 11:52:06 -0700399}
400
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000401// SelfTestRequestStart initiate Test Request handling procedure. The results are asynchronously conveyed on the respChan.
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700402// If the return from selfTestRequest is NOT nil, the caller shall not wait for async response.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000403func (selfTestCb *SelfTestControlBlock) SelfTestRequestStart(ctx context.Context, reqMsg extension.SingleGetValueRequest,
404 CommChan chan cmn.Message, respChan chan extension.SingleGetValueResponse) error {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700405 meClassID, err := selfTestCb.getMeClassID(ctx, reqMsg)
406 if err != nil {
407 return err
408 }
409 if _, ok := selfTestCb.selfTestFsmMap[meClassID]; ok {
410 logger.Errorw(ctx, "self test already in progress for class id", log.Fields{"device-id": selfTestCb.deviceID, "class-id": meClassID})
411 return fmt.Errorf("self-test-already-in-progress-for-class-id-%v-device-id-%v", meClassID, selfTestCb.deviceID)
412 }
413 logger.Infow(ctx, "self test request initiated", log.Fields{"device-id": selfTestCb.deviceID, "meClassID": meClassID})
414 // indicates only if the FSM was initiated correctly. Response is asynchronous on respChan.
415 // If the return from here is NOT nil, the caller shall not wait for async response.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000416 return selfTestCb.initiateNewSelfTestFsm(ctx, reqMsg, CommChan, meClassID, respChan)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700417}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +0000418
419// PrepareForGarbageCollection - remove references to prepare for garbage collection
420func (selfTestCb *SelfTestControlBlock) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
421 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
422 selfTestCb.pDeviceHandler = nil
423 selfTestCb.pDevEntry = nil
424}