Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 3 | * |
| 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 Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 17 | // Package omcitst provides the omci test functionality |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 18 | package omcitst |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "fmt" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 23 | "sync" |
| 24 | "time" |
| 25 | |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 26 | "github.com/looplab/fsm" |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 27 | "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" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 31 | cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-protos/v5/go/extension" |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | const ( |
| 36 | // events of Self Test FSM |
| 37 | selfTestEventTestRequest = "selfTestEventTestRequest" |
| 38 | selfTestEventTestResponseSuccess = "selfTestEventTestResponseSuccess" |
| 39 | selfTestEventTestResultSuccess = "selfTestEventTestResultSuccess" |
| 40 | selfTestEventAbort = "selfTestEventAbort" |
| 41 | ) |
| 42 | const ( |
| 43 | // states of Self Test FSM |
| 44 | selfTestStNull = "selfTestStNull" |
| 45 | selfTestStHandleSelfTestReq = "selfTestStHandleSelfTestReq" |
| 46 | selfTestStHandleSelfTestResp = "selfTestStHandleSelfTestResp" |
| 47 | selfTestStHandleTestResult = "selfTestStHandleTestResult" |
| 48 | ) |
| 49 | |
| 50 | const ( |
| 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 |
| 56 | type fsmCb struct { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 57 | fsm *cmn.AdapterFsm |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 58 | reqMsg extension.SingleGetValueRequest |
| 59 | respChan chan extension.SingleGetValueResponse |
| 60 | stopOmciChan chan bool |
| 61 | } |
| 62 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 63 | // SelfTestControlBlock - TODO: add comment |
| 64 | type SelfTestControlBlock struct { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 65 | deviceID string |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 66 | pDeviceHandler cmn.IdeviceHandler |
| 67 | pDevEntry cmn.IonuDeviceEntry |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 68 | |
| 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 Ravindra | f1331ad | 2024-02-15 16:13:37 +0530 | [diff] [blame] | 72 | SelfTestHandlerLock sync.RWMutex |
| 73 | SelfTestHandlerActive bool |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 74 | StopSelfTestModule chan bool |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 77 | // NewSelfTestMsgHandlerCb creates the SelfTestControlBlock |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 78 | // 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 82 | func 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 Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 88 | selfTestCb.selfTestFsmMap = make(map[generated.ClassID]*fsmCb) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 89 | selfTestCb.StopSelfTestModule = make(chan bool) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 90 | |
| 91 | go selfTestCb.waitForStopSelfTestModuleSignal(ctx) |
| 92 | |
| 93 | return &selfTestCb |
| 94 | } |
| 95 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 96 | func (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 Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 99 | |
| 100 | if aFsm == nil { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 101 | logger.Errorw(ctx, "selfTestFsm cmn.AdapterFsm could not be instantiated!!", log.Fields{ |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 102 | "device-id": selfTestCb.deviceID}) |
| 103 | return fmt.Errorf("nil-adapter-fsm") |
| 104 | } |
| 105 | // Self Test FSM related state machine |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 106 | aFsm.PFsm = fsm.NewFSM( |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 107 | |
| 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}, |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 113 | {Name: selfTestEventAbort, Src: []string{selfTestStHandleSelfTestReq, selfTestStHandleSelfTestResp, selfTestStHandleTestResult, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 114 | selfTestStNull}, Dst: selfTestStNull}, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 115 | }, |
| 116 | fsm.Callbacks{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 117 | "enter_state": func(e *fsm.Event) { aFsm.LogFsmStateChange(ctx, e) }, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 118 | "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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 134 | func (selfTestCb *SelfTestControlBlock) selfTestFsmHandleSelfTestRequest(ctx context.Context, e *fsm.Event) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 135 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 143 | instKeys := selfTestCb.pDevEntry.GetOnuDB().GetSortedInstKeys(ctx, classID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 144 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 145 | // 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 Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 149 | logger.Errorw(ctx, "error sending self test request", log.Fields{"device-id": selfTestCb.deviceID, "classID": classID}) |
| 150 | selfTestCb.triggerFsmEvent(pFsmCb.fsm, selfTestEventAbort) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 151 | selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, pFsmCb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 152 | return |
| 153 | } |
| 154 | |
| 155 | go selfTestCb.handleOmciResponse(ctx, classID) |
| 156 | } |
| 157 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 158 | func (selfTestCb *SelfTestControlBlock) selfTestFsmHandleSelfTestResponse(ctx context.Context, e *fsm.Event) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 159 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 167 | func (selfTestCb *SelfTestControlBlock) getMeClassID(ctx context.Context, reqMsg extension.SingleGetValueRequest) (generated.ClassID, error) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 168 | switch reqMsg.GetRequest().GetRequest().(type) { |
| 169 | case *extension.GetValueRequest_OnuOpticalInfo: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 170 | return me.AniGClassID, nil |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 171 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 177 | func (selfTestCb *SelfTestControlBlock) triggerFsmEvent(pSelfTestFsm *cmn.AdapterFsm, event string, args ...generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 178 | go func() { |
| 179 | if len(args) > 0 { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 180 | _ = pSelfTestFsm.PFsm.Event(event, args[0]) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 181 | } else { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 182 | _ = pSelfTestFsm.PFsm.Event(event) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 183 | } |
| 184 | }() |
| 185 | } |
| 186 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 187 | func (selfTestCb *SelfTestControlBlock) submitFailureGetValueResponse(ctx context.Context, respChan chan extension.SingleGetValueResponse, |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 188 | 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 Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 193 | 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}) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 200 | // Clear the fsmCb from the map |
| 201 | delete(selfTestCb.selfTestFsmMap, meClassID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 202 | respChan <- singleValResp |
| 203 | logger.Infow(ctx, "OMCI test response failure - pushing failure response complete", log.Fields{"device-id": selfTestCb.deviceID}) |
| 204 | } |
| 205 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 206 | func (selfTestCb *SelfTestControlBlock) handleOmciMessage(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 207 | 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 216 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 220 | func (selfTestCb *SelfTestControlBlock) handleOmciTestResponse(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 221 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 224 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 225 | selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 226 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 227 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 232 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 233 | selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 234 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 235 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 245 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 246 | selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 247 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 250 | func (selfTestCb *SelfTestControlBlock) handleOmciTestResult(ctx context.Context, msg cmn.OmciMessage, cb *fsmCb, classID generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 251 | 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 255 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 256 | return |
| 257 | } |
| 258 | var msgObj *omci.OpticalLineSupervisionTestResult |
| 259 | var msgOk bool |
| 260 | switch classID { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 261 | case me.AniGClassID: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 262 | msgObj, msgOk = msgLayer.(*omci.OpticalLineSupervisionTestResult) |
| 263 | default: |
| 264 | // We should not really land here |
| 265 | selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 266 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 267 | 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 272 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, cb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 273 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 288 | PowerFeedVoltage: float32(cmn.TwosComplementToSignedInt16(msgObj.PowerFeedVoltage)) * 0.02, |
Girish Gowdra | 444522f | 2021-05-12 14:32:24 -0700 | [diff] [blame] | 289 | // 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 291 | ReceivedOpticalPower: float32(cmn.TwosComplementToSignedInt16(msgObj.ReceivedOpticalPower))*0.002 - 30, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 292 | // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution |
Girish Gowdra | 444522f | 2021-05-12 14:32:24 -0700 | [diff] [blame] | 293 | // Subtract 30 to convert the unit from dBu to dBm (as expected by proto interface) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 294 | MeanOpticalLaunchPower: float32(cmn.TwosComplementToSignedInt16(msgObj.MeanOpticalLaunch))*0.002 - 30, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 295 | // 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 299 | Temperature: float32(cmn.TwosComplementToSignedInt16(msgObj.Temperature)) / 256.0, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 300 | }, |
| 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 318 | func (selfTestCb *SelfTestControlBlock) handleOmciResponse(ctx context.Context, classID generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 319 | 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 330 | selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR, pFsmCb.reqMsg) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 331 | case message, ok := <-pFsmCb.fsm.CommChan: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 332 | 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 335 | selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_INTERNAL_ERROR, extension.GetValueResponse_ERROR, pFsmCb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 336 | } |
| 337 | logger.Debugw(ctx, "Received message on self test result channel", log.Fields{"device-id": selfTestCb.deviceID}) |
| 338 | |
| 339 | switch message.Type { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 340 | case cmn.OMCI: |
| 341 | msg, _ := message.Data.(cmn.OmciMessage) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 342 | 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}) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 345 | selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR, pFsmCb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 346 | } |
| 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 350 | selfTestCb.submitFailureGetValueResponse(ctx, pFsmCb.respChan, extension.GetValueResponse_TIMEOUT, extension.GetValueResponse_ERROR, pFsmCb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | // selfTestRequestComplete removes the fsmCb from the local cache if found |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 355 | func (selfTestCb *SelfTestControlBlock) selfTestRequestComplete(ctx context.Context, reqMsg extension.SingleGetValueRequest) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 356 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 365 | func (selfTestCb *SelfTestControlBlock) waitForStopSelfTestModuleSignal(ctx context.Context) { |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 366 | selfTestCb.SetSelfTestHandlerIsRunning(true) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 367 | <-selfTestCb.StopSelfTestModule // block on stop signal |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 368 | |
| 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) |
bseeniva | 6db14a2 | 2024-12-07 19:41:01 +0530 | [diff] [blame^] | 377 | selfTestCb.submitFailureGetValueResponse(ctx, fsmCb.respChan, extension.GetValueResponse_REASON_UNDEFINED, extension.GetValueResponse_ERROR, fsmCb.reqMsg) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 378 | } |
| 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 387 | // SetSelfTestHandlerIsRunning sets the value to selfTestCb.selfTestHandlerActive |
| 388 | func (selfTestCb *SelfTestControlBlock) SetSelfTestHandlerIsRunning(active bool) { |
Sridhar Ravindra | f1331ad | 2024-02-15 16:13:37 +0530 | [diff] [blame] | 389 | selfTestCb.SelfTestHandlerLock.Lock() |
| 390 | defer selfTestCb.SelfTestHandlerLock.Unlock() |
| 391 | selfTestCb.SelfTestHandlerActive = active |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 394 | // GetSelfTestHandlerIsRunning gets selfTestCb.selfTestHandlerActive |
| 395 | func (selfTestCb *SelfTestControlBlock) GetSelfTestHandlerIsRunning() bool { |
Sridhar Ravindra | f1331ad | 2024-02-15 16:13:37 +0530 | [diff] [blame] | 396 | selfTestCb.SelfTestHandlerLock.RLock() |
| 397 | defer selfTestCb.SelfTestHandlerLock.RUnlock() |
| 398 | return selfTestCb.SelfTestHandlerActive |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 401 | // SelfTestRequestStart initiate Test Request handling procedure. The results are asynchronously conveyed on the respChan. |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 402 | // If the return from selfTestRequest is NOT nil, the caller shall not wait for async response. |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 403 | func (selfTestCb *SelfTestControlBlock) SelfTestRequestStart(ctx context.Context, reqMsg extension.SingleGetValueRequest, |
| 404 | CommChan chan cmn.Message, respChan chan extension.SingleGetValueResponse) error { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 405 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 416 | return selfTestCb.initiateNewSelfTestFsm(ctx, reqMsg, CommChan, meClassID, respChan) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 417 | } |
Holger Hildebrandt | e7cc609 | 2022-02-01 11:37:03 +0000 | [diff] [blame] | 418 | |
| 419 | // PrepareForGarbageCollection - remove references to prepare for garbage collection |
| 420 | func (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 | } |