Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
| 17 | //Package adaptercoreonu provides the utility for onu devices, flows and statistics |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
Andrea Campanella | 6515c58 | 2020-10-05 11:25:00 +0200 | [diff] [blame] | 22 | "fmt" |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 23 | "time" |
| 24 | |
| 25 | "github.com/looplab/fsm" |
| 26 | |
| 27 | "github.com/opencord/omci-lib-go" |
| 28 | me "github.com/opencord/omci-lib-go/generated" |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 30 | //ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 31 | //"github.com/opencord/voltha-protos/v4/go/openflow_13" |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 32 | ) |
| 33 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 34 | //lockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI |
| 35 | type lockStateFsm struct { |
Holger Hildebrandt | 8165eda | 2020-09-24 09:39:24 +0000 | [diff] [blame] | 36 | pDeviceHandler *deviceHandler |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 37 | deviceID string |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 38 | pOmciCC *omciCC |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 39 | adminState bool |
| 40 | requestEvent OnuDeviceEvent |
| 41 | omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses |
| 42 | pAdaptFsm *AdapterFsm |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 43 | pLastTxMeInstance *me.ManagedEntity |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 44 | } |
| 45 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 46 | const ( |
| 47 | // events of lock/unlock UNI port FSM |
| 48 | uniEvStart = "uniEvStart" |
| 49 | uniEvStartAdmin = "uniEvStartAdmin" |
| 50 | uniEvRxUnisResp = "uniEvRxUnisResp" |
| 51 | uniEvRxOnugResp = "uniEvRxOnugResp" |
| 52 | uniEvTimeoutSimple = "uniEvTimeoutSimple" |
| 53 | uniEvTimeoutUnis = "uniEvTimeoutUnis" |
| 54 | uniEvReset = "uniEvReset" |
| 55 | uniEvRestart = "uniEvRestart" |
| 56 | ) |
| 57 | const ( |
| 58 | // states of lock/unlock UNI port FSM |
| 59 | uniStDisabled = "uniStDisabled" |
| 60 | uniStStarting = "uniStStarting" |
| 61 | uniStSettingUnis = "uniStSettingUnis" |
| 62 | uniStSettingOnuG = "uniStSettingOnuG" |
| 63 | uniStAdminDone = "uniStAdminDone" |
| 64 | uniStResetting = "uniStResetting" |
| 65 | ) |
Holger Hildebrandt | 10d9819 | 2021-01-27 15:29:31 +0000 | [diff] [blame] | 66 | const cUniFsmIdleState = uniStDisabled |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 67 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 68 | //newLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 69 | func newLockStateFsm(ctx context.Context, apDevOmciCC *omciCC, aAdminState bool, aRequestEvent OnuDeviceEvent, |
Holger Hildebrandt | 8165eda | 2020-09-24 09:39:24 +0000 | [diff] [blame] | 70 | aName string, apDeviceHandler *deviceHandler, aCommChannel chan Message) *lockStateFsm { |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 71 | instFsm := &lockStateFsm{ |
Holger Hildebrandt | 8165eda | 2020-09-24 09:39:24 +0000 | [diff] [blame] | 72 | pDeviceHandler: apDeviceHandler, |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 73 | deviceID: apDeviceHandler.deviceID, |
Holger Hildebrandt | 8165eda | 2020-09-24 09:39:24 +0000 | [diff] [blame] | 74 | pOmciCC: apDevOmciCC, |
| 75 | adminState: aAdminState, |
| 76 | requestEvent: aRequestEvent, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 77 | } |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 78 | instFsm.pAdaptFsm = NewAdapterFsm(aName, instFsm.deviceID, aCommChannel) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 79 | if instFsm.pAdaptFsm == nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 80 | logger.Errorw(ctx, "LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{ |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 81 | "device-id": instFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 82 | return nil |
| 83 | } |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 84 | if aAdminState { //port locking requested |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 85 | instFsm.pAdaptFsm.pFsm = fsm.NewFSM( |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 86 | uniStDisabled, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 87 | fsm.Events{ |
| 88 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 89 | {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 90 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 91 | {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingUnis}, |
| 92 | // the settingUnis state is used for multi ME config for all UNI related ports |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 93 | // maybe such could be reflected in the state machine as well (port number parametrized) |
| 94 | // but that looks not straightforward here - so we keep it simple here for the beginning(?) |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 95 | {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStSettingOnuG}, |
| 96 | {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStAdminDone}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 97 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 98 | {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting}, |
| 99 | {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 100 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 101 | {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis, |
| 102 | uniStAdminDone}, Dst: uniStResetting}, |
| 103 | // exceptional treatment for all states except uniStResetting |
| 104 | {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis, |
| 105 | uniStAdminDone, uniStResetting}, Dst: uniStDisabled}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 106 | }, |
| 107 | |
| 108 | fsm.Callbacks{ |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 109 | "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) }, |
| 110 | ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) }, |
| 111 | ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) }, |
| 112 | ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) }, |
| 113 | ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) }, |
| 114 | ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) }, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 115 | }, |
| 116 | ) |
| 117 | } else { //port unlocking requested |
| 118 | instFsm.pAdaptFsm.pFsm = fsm.NewFSM( |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 119 | uniStDisabled, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 120 | fsm.Events{ |
| 121 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 122 | {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 123 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 124 | {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingOnuG}, |
| 125 | {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStSettingUnis}, |
| 126 | // the settingUnis state is used for multi ME config for all UNI related ports |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 127 | // maybe such could be reflected in the state machine as well (port number parametrized) |
| 128 | // but that looks not straightforward here - so we keep it simple here for the beginning(?) |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 129 | {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStAdminDone}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 130 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 131 | {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting}, |
| 132 | {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 133 | |
mpagenko | 1cc3cb4 | 2020-07-27 15:24:38 +0000 | [diff] [blame] | 134 | {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis, |
| 135 | uniStAdminDone}, Dst: uniStResetting}, |
| 136 | // exceptional treatment for all states except uniStResetting |
| 137 | {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis, |
| 138 | uniStAdminDone, uniStResetting}, Dst: uniStDisabled}, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 139 | }, |
| 140 | |
| 141 | fsm.Callbacks{ |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 142 | "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) }, |
| 143 | ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) }, |
| 144 | ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) }, |
| 145 | ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) }, |
| 146 | ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) }, |
| 147 | ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) }, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 148 | }, |
| 149 | ) |
| 150 | } |
| 151 | if instFsm.pAdaptFsm.pFsm == nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 152 | logger.Errorw(ctx, "LockStateFsm's Base FSM could not be instantiated!!", log.Fields{ |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 153 | "device-id": instFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 154 | return nil |
| 155 | } |
| 156 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 157 | logger.Debugw(ctx, "LockStateFsm created", log.Fields{"device-id": instFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 158 | return instFsm |
| 159 | } |
| 160 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 161 | //setSuccessEvent modifies the requested event notified on success |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 162 | //assumption is that this is only called in the disabled (idle) state of the FSM, hence no sem protection required |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 163 | func (oFsm *lockStateFsm) setSuccessEvent(aEvent OnuDeviceEvent) { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 164 | oFsm.requestEvent = aEvent |
| 165 | } |
| 166 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 167 | func (oFsm *lockStateFsm) enterAdminStartingState(ctx context.Context, e *fsm.Event) { |
| 168 | logger.Debugw(ctx, "LockStateFSM start", log.Fields{"in state": e.FSM.Current(), |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 169 | "device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 170 | // in case the used channel is not yet defined (can be re-used after restarts) |
| 171 | if oFsm.omciLockResponseReceived == nil { |
| 172 | oFsm.omciLockResponseReceived = make(chan bool) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 173 | logger.Debug(ctx, "LockStateFSM - OMCI UniLock RxChannel defined") |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 174 | } else { |
| 175 | // as we may 're-use' this instance of FSM and the connected channel |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 176 | // make sure there is no 'lingering' request in the already existing channels: |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 177 | // (simple loop sufficient as we are the only receiver) |
| 178 | for len(oFsm.omciLockResponseReceived) > 0 { |
| 179 | <-oFsm.omciLockResponseReceived |
| 180 | } |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 181 | for len(oFsm.pAdaptFsm.commChan) > 0 { |
| 182 | <-oFsm.pAdaptFsm.commChan |
| 183 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 184 | } |
| 185 | // start go routine for processing of LockState messages |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 186 | go oFsm.processOmciLockMessages(ctx) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 187 | |
| 188 | //let the state machine run forward from here directly |
| 189 | pLockStateAFsm := oFsm.pAdaptFsm |
| 190 | if pLockStateAFsm != nil { |
| 191 | // obviously calling some FSM event here directly does not work - so trying to decouple it ... |
| 192 | go func(a_pAFsm *AdapterFsm) { |
| 193 | if a_pAFsm != nil && a_pAFsm.pFsm != nil { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 194 | _ = a_pAFsm.pFsm.Event(uniEvStartAdmin) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 195 | } |
| 196 | }(pLockStateAFsm) |
| 197 | } |
| 198 | } |
| 199 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 200 | func (oFsm *lockStateFsm) enterSettingOnuGState(ctx context.Context, e *fsm.Event) { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 201 | var omciAdminState uint8 = 1 //default locked |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 202 | if !oFsm.adminState { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 203 | omciAdminState = 0 |
| 204 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 205 | logger.Debugw(ctx, "LockStateFSM Tx Set::ONU-G:admin", log.Fields{ |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 206 | "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 207 | requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState} |
Girish Gowdra | 0b23584 | 2021-03-09 13:06:46 -0800 | [diff] [blame] | 208 | meInstance := oFsm.pOmciCC.sendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, true, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 209 | requestedAttributes, oFsm.pAdaptFsm.commChan) |
| 210 | //accept also nil as (error) return value for writing to LastTx |
| 211 | // - this avoids misinterpretation of new received OMCI messages |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 212 | oFsm.pLastTxMeInstance = meInstance |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 213 | if oFsm.pLastTxMeInstance == nil { |
| 214 | logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{ |
| 215 | "device-id": oFsm.deviceID}) |
| 216 | //some more sophisticated approach is possible, e.g. repeating once, by now let's reset the state machine in order to release all resources now |
| 217 | pLockStateAFsm := oFsm.pAdaptFsm |
| 218 | if pLockStateAFsm != nil { |
| 219 | // obviously calling some FSM event here directly does not work - so trying to decouple it ... |
| 220 | go func(a_pAFsm *AdapterFsm) { |
| 221 | if a_pAFsm != nil && a_pAFsm.pFsm != nil { |
| 222 | _ = a_pAFsm.pFsm.Event(uniEvReset) |
| 223 | } |
| 224 | }(pLockStateAFsm) |
| 225 | } |
| 226 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 227 | } |
| 228 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 229 | func (oFsm *lockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) { |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 230 | logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{ |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 231 | "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState}) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 232 | go oFsm.performUniPortAdminSet(ctx) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 233 | } |
| 234 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 235 | func (oFsm *lockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) { |
| 236 | logger.Debugw(ctx, "LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 237 | //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 238 | oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent) |
Holger Hildebrandt | 8165eda | 2020-09-24 09:39:24 +0000 | [diff] [blame] | 239 | |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 240 | //let's reset the state machine in order to release all resources now |
| 241 | pLockStateAFsm := oFsm.pAdaptFsm |
| 242 | if pLockStateAFsm != nil { |
| 243 | // obviously calling some FSM event here directly does not work - so trying to decouple it ... |
| 244 | go func(a_pAFsm *AdapterFsm) { |
| 245 | if a_pAFsm != nil && a_pAFsm.pFsm != nil { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 246 | _ = a_pAFsm.pFsm.Event(uniEvReset) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 247 | } |
| 248 | }(pLockStateAFsm) |
| 249 | } |
| 250 | } |
| 251 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 252 | func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) { |
| 253 | logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 254 | pLockStateAFsm := oFsm.pAdaptFsm |
| 255 | if pLockStateAFsm != nil { |
| 256 | // abort running message processing |
| 257 | fsmAbortMsg := Message{ |
| 258 | Type: TestMsg, |
| 259 | Data: TestMessage{ |
| 260 | TestMessageVal: AbortMessageProcessing, |
| 261 | }, |
| 262 | } |
| 263 | pLockStateAFsm.commChan <- fsmAbortMsg |
| 264 | |
| 265 | //try to restart the FSM to 'disabled' |
| 266 | // see DownloadedState: decouple event transfer |
| 267 | go func(a_pAFsm *AdapterFsm) { |
| 268 | if a_pAFsm != nil && a_pAFsm.pFsm != nil { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 269 | _ = a_pAFsm.pFsm.Event(uniEvRestart) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 270 | } |
| 271 | }(pLockStateAFsm) |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 272 | oFsm.pLastTxMeInstance = nil |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 276 | func (oFsm *lockStateFsm) processOmciLockMessages(ctx context.Context) { |
| 277 | logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 278 | loop: |
| 279 | for { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 280 | // case <-ctx.Done(): |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 281 | // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 282 | // break loop |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 283 | message, ok := <-oFsm.pAdaptFsm.commChan |
| 284 | if !ok { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 285 | logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID}) |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 286 | // but then we have to ensure a restart of the FSM as well - as exceptional procedure |
| 287 | _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRestart) |
| 288 | break loop |
| 289 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 290 | logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID}) |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 291 | |
| 292 | switch message.Type { |
| 293 | case TestMsg: |
| 294 | msg, _ := message.Data.(TestMessage) |
| 295 | if msg.TestMessageVal == AbortMessageProcessing { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 296 | logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 297 | break loop |
| 298 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 299 | logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal}) |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 300 | case OMCI: |
| 301 | msg, _ := message.Data.(OmciMessage) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 302 | oFsm.handleOmciLockStateMessage(ctx, msg) |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 303 | default: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 304 | logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID, |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 305 | "message.Type": message.Type}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 308 | logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 309 | } |
| 310 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 311 | func (oFsm *lockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg OmciMessage) { |
| 312 | logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID, |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 313 | "msgType": msg.OmciMsg.MessageType}) |
| 314 | |
| 315 | if msg.OmciMsg.MessageType == omci.SetResponseType { |
| 316 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse) |
| 317 | if msgLayer == nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 318 | logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected for SetResponse", |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 319 | log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 320 | return |
| 321 | } |
| 322 | msgObj, msgOk := msgLayer.(*omci.SetResponse) |
| 323 | if !msgOk { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 324 | logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse", |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 325 | log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 326 | return |
| 327 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 328 | logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 329 | if msgObj.Result != me.Success { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 330 | logger.Errorw(ctx, "LockStateFsm - Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 331 | // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display? |
| 332 | return |
| 333 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 334 | |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 335 | //should never appear, left here for robustness |
Girish Gowdra | 3d5d675 | 2021-03-22 19:21:24 -0700 | [diff] [blame] | 336 | if oFsm.pLastTxMeInstance != nil { |
| 337 | // compare comments above for CreateResponse (apply also here ...) |
| 338 | if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() && |
| 339 | msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() { |
| 340 | //store the created ME into DB //TODO??? obviously the Python code does not store the config ... |
| 341 | // if, then something like: |
| 342 | //oFsm.pOnuDB.StoreMe(msgObj) |
| 343 | |
| 344 | switch oFsm.pLastTxMeInstance.GetName() { |
| 345 | case "OnuG": |
| 346 | { // let the FSM proceed ... |
| 347 | _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp) |
| 348 | } |
| 349 | case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint": |
| 350 | { // let the PPTP init proceed by stopping the wait function |
| 351 | oFsm.omciLockResponseReceived <- true |
| 352 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 353 | } |
Girish Gowdra | 3d5d675 | 2021-03-22 19:21:24 -0700 | [diff] [blame] | 354 | } else { |
| 355 | logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ", |
| 356 | log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 357 | } |
Matteo Scandolo | b250bec | 2021-01-27 17:35:45 -0800 | [diff] [blame] | 358 | } else { |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 359 | logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID}) |
Girish Gowdra | 3d5d675 | 2021-03-22 19:21:24 -0700 | [diff] [blame] | 360 | return |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 361 | } |
| 362 | } else { |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 363 | logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType}) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 364 | return |
| 365 | } |
| 366 | } |
| 367 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 368 | func (oFsm *lockStateFsm) performUniPortAdminSet(ctx context.Context) { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 369 | var omciAdminState uint8 = 1 //default locked |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 370 | if !oFsm.adminState { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 371 | omciAdminState = 0 |
| 372 | } |
Holger Hildebrandt | 2fb7089 | 2020-10-28 11:53:18 +0000 | [diff] [blame] | 373 | //set PPTPEthUni or VEIP AdminState |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 374 | requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState} |
| 375 | |
| 376 | for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap { |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 377 | // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port), |
| 378 | // compare also limitation for logical voltha port in dh.enableUniPortStateUpdate() |
| 379 | if (omciAdminState == 1) || (1<<uniPort.uniID)&activeUniPortStateUpdateMask == (1<<uniPort.uniID) { |
| 380 | var meInstance *me.ManagedEntity |
| 381 | if uniPort.portType == uniPPTP { |
| 382 | logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{ |
| 383 | "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState}) |
Girish Gowdra | 0b23584 | 2021-03-09 13:06:46 -0800 | [diff] [blame] | 384 | meInstance = oFsm.pOmciCC.sendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx), uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 385 | true, requestedAttributes, oFsm.pAdaptFsm.commChan) |
| 386 | oFsm.pLastTxMeInstance = meInstance |
| 387 | } else if uniPort.portType == uniVEIP { |
| 388 | logger.Debugw(ctx, "Setting VEIP admin state", log.Fields{ |
| 389 | "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState}) |
Girish Gowdra | 0b23584 | 2021-03-09 13:06:46 -0800 | [diff] [blame] | 390 | meInstance = oFsm.pOmciCC.sendSetVeipLS(log.WithSpanFromContext(context.TODO(), ctx), uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 391 | true, requestedAttributes, oFsm.pAdaptFsm.commChan) |
| 392 | oFsm.pLastTxMeInstance = meInstance |
| 393 | } else { |
| 394 | logger.Warnw(ctx, "Unsupported UniTP type - skip", |
| 395 | log.Fields{"device-id": oFsm.deviceID, "Port": uniNo}) |
| 396 | continue |
| 397 | } |
mpagenko | b06684f | 2021-03-23 14:39:04 +0000 | [diff] [blame] | 398 | if oFsm.pLastTxMeInstance == nil { |
| 399 | logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{ |
| 400 | "device-id": oFsm.deviceID, "Port": uniNo}) |
| 401 | //some more sophisticated approach is possible, e.g. repeating once, by now let's reset the state machine in order to release all resources now |
| 402 | _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset) |
| 403 | return |
| 404 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 405 | |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 406 | //verify response |
| 407 | err := oFsm.waitforOmciResponse(ctx, meInstance) |
| 408 | if err != nil { |
| 409 | logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!", |
| 410 | log.Fields{"device-id": oFsm.deviceID, "Port": uniNo}) |
| 411 | _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset) |
| 412 | return |
| 413 | } |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 414 | } |
| 415 | } //for all UNI ports |
| 416 | // if Config has been done for all UNI related instances let the FSM proceed |
| 417 | // while we did not check here, if there is some port at all - !? |
mpagenko | 8b5fdd2 | 2020-12-17 17:58:32 +0000 | [diff] [blame] | 418 | logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID}) |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 419 | _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 420 | } |
| 421 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 422 | func (oFsm *lockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error { |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 423 | select { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 424 | // maybe be also some outside cancel (but no context modeled for the moment ...) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 425 | // case <-ctx.Done(): |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 426 | // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID}) |
mpagenko | 3af1f03 | 2020-06-10 08:53:41 +0000 | [diff] [blame] | 427 | case <-time.After(30 * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 428 | logger.Warnw(ctx, "LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID}) |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 429 | return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 430 | case success := <-oFsm.omciLockResponseReceived: |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 431 | if success { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 432 | logger.Debug(ctx, "LockStateFSM uni-set response received") |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 433 | return nil |
| 434 | } |
| 435 | // should not happen so far |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 436 | logger.Warnw(ctx, "LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID}) |
mpagenko | 01e726e | 2020-10-23 09:45:29 +0000 | [diff] [blame] | 437 | return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID) |
Holger Hildebrandt | ccd390c | 2020-05-29 13:49:04 +0000 | [diff] [blame] | 438 | } |
| 439 | } |