Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 17 | //Package omcitst provides the omci test functionality |
| 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" |
| 27 | "github.com/opencord/omci-lib-go" |
| 28 | "github.com/opencord/omci-lib-go/generated" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 29 | me "github.com/opencord/omci-lib-go/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 | |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [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}, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 113 | {Name: selfTestEventAbort, Src: []string{selfTestStHandleSelfTestReq, selfTestStHandleSelfTestReq, selfTestStHandleTestResult, |
| 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) |
| 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 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, |
| 188 | errorCode extension.GetValueResponse_ErrorReason, statusCode extension.GetValueResponse_Status) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 189 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 200 | 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] | 201 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 214 | 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] | 215 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 218 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 219 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 226 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 227 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 239 | selfTestCb.pDevEntry.GetDevOmciCC().ReleaseTid(ctx, msg.OmciMsg.TransactionID) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 240 | selfTestCb.triggerFsmEvent(cb.fsm, selfTestEventAbort) |
| 241 | selfTestCb.submitFailureGetValueResponse(ctx, cb.respChan, extension.GetValueResponse_UNSUPPORTED, extension.GetValueResponse_ERROR) |
| 242 | } |
| 243 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 244 | 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] | 245 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 255 | case me.AniGClassID: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 256 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 282 | PowerFeedVoltage: float32(cmn.TwosComplementToSignedInt16(msgObj.PowerFeedVoltage)) * 0.02, |
Girish Gowdra | 444522f | 2021-05-12 14:32:24 -0700 | [diff] [blame] | 283 | // 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 285 | ReceivedOpticalPower: float32(cmn.TwosComplementToSignedInt16(msgObj.ReceivedOpticalPower))*0.002 - 30, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 286 | // OMCI representation is Decibel-microwatts, 2s compliment, 0.002dB resolution |
Girish Gowdra | 444522f | 2021-05-12 14:32:24 -0700 | [diff] [blame] | 287 | // 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] | 288 | MeanOpticalLaunchPower: float32(cmn.TwosComplementToSignedInt16(msgObj.MeanOpticalLaunch))*0.002 - 30, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 289 | // 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 293 | Temperature: float32(cmn.TwosComplementToSignedInt16(msgObj.Temperature)) / 256.0, |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 294 | }, |
| 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 312 | func (selfTestCb *SelfTestControlBlock) handleOmciResponse(ctx context.Context, classID generated.ClassID) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 313 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 325 | case message, ok := <-pFsmCb.fsm.CommChan: |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 326 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 334 | case cmn.OMCI: |
| 335 | msg, _ := message.Data.(cmn.OmciMessage) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 336 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 349 | func (selfTestCb *SelfTestControlBlock) selfTestRequestComplete(ctx context.Context, reqMsg extension.SingleGetValueRequest) { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 350 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 359 | func (selfTestCb *SelfTestControlBlock) waitForStopSelfTestModuleSignal(ctx context.Context) { |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 360 | selfTestCb.SetSelfTestHandlerIsRunning(true) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 361 | <-selfTestCb.StopSelfTestModule // block on stop signal |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 362 | selfTestCb.SetSelfTestHandlerIsRunning(false) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 363 | |
| 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 382 | // SetSelfTestHandlerIsRunning sets the value to selfTestCb.selfTestHandlerActive |
| 383 | func (selfTestCb *SelfTestControlBlock) SetSelfTestHandlerIsRunning(active bool) { |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 384 | selfTestCb.selfTestHandlerLock.Lock() |
| 385 | defer selfTestCb.selfTestHandlerLock.Unlock() |
| 386 | selfTestCb.selfTestHandlerActive = active |
| 387 | } |
| 388 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 389 | // GetSelfTestHandlerIsRunning gets selfTestCb.selfTestHandlerActive |
| 390 | func (selfTestCb *SelfTestControlBlock) GetSelfTestHandlerIsRunning() bool { |
Girish Gowdra | 10123c0 | 2021-08-30 11:52:06 -0700 | [diff] [blame] | 391 | selfTestCb.selfTestHandlerLock.RLock() |
| 392 | defer selfTestCb.selfTestHandlerLock.RUnlock() |
| 393 | return selfTestCb.selfTestHandlerActive |
| 394 | } |
| 395 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 396 | // 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] | 397 | // 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] | 398 | func (selfTestCb *SelfTestControlBlock) SelfTestRequestStart(ctx context.Context, reqMsg extension.SingleGetValueRequest, |
| 399 | CommChan chan cmn.Message, respChan chan extension.SingleGetValueResponse) error { |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 400 | 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 411 | return selfTestCb.initiateNewSelfTestFsm(ctx, reqMsg, CommChan, meClassID, respChan) |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 412 | } |