khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 20 | "context" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/rw_core/coreif" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 25 | // DeviceType mentions type of device like parent, child |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 26 | type DeviceType int32 |
| 27 | |
| 28 | const ( |
| 29 | parent DeviceType = 0 |
| 30 | child DeviceType = 1 |
| 31 | any DeviceType = 2 |
| 32 | ) |
| 33 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 34 | type MatchResult uint8 |
| 35 | |
| 36 | const ( |
| 37 | noMatch MatchResult = iota // current state has not match in the transition table |
| 38 | currWildcardMatch // current state matches the wildcard *_UNKNOWN state in the transition table |
| 39 | currStateOnlyMatch // current state matches the current state and previous state matches the wildcard in the transition table |
| 40 | currPrevStateMatch // both current and previous states match in the transition table |
| 41 | ) |
| 42 | |
| 43 | // match is used to keep the current match states |
| 44 | type match struct { |
| 45 | admin, oper, conn MatchResult |
| 46 | } |
| 47 | |
| 48 | // toInt returns an integer representing the matching level of the match (the larger the number the better) |
| 49 | func (m *match) toInt() int { |
| 50 | return int(m.admin<<4 | m.oper<<2 | m.conn) |
| 51 | } |
| 52 | |
| 53 | // isExactMatch returns true if match is an exact match |
| 54 | func (m *match) isExactMatch() bool { |
| 55 | return m.admin == currPrevStateMatch && m.oper == currPrevStateMatch && m.conn == currPrevStateMatch |
| 56 | } |
| 57 | |
| 58 | // isBetterMatch returns true if newMatch is a worse match |
| 59 | func (m *match) isBetterMatch(newMatch *match) bool { |
| 60 | return m.toInt() > newMatch.toInt() |
| 61 | } |
| 62 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 63 | // DeviceState has admin, operational and connection status of device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 64 | type DeviceState struct { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 65 | Admin voltha.AdminState_Types |
| 66 | Connection voltha.ConnectStatus_Types |
| 67 | Operational voltha.OperStatus_Types |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 68 | } |
| 69 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 70 | // TransitionHandler function type which takes the current and previous device info as input parameter |
| 71 | type TransitionHandler func(ctx context.Context, curr *voltha.Device, prev *voltha.Device) error |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 73 | // Transition represent transition related attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 74 | type Transition struct { |
| 75 | deviceType DeviceType |
| 76 | previousState DeviceState |
| 77 | currentState DeviceState |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 78 | handlers []TransitionHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | } |
| 80 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 81 | // TransitionMap represent map of transitions and device manager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | type TransitionMap struct { |
| 83 | transitions []Transition |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 84 | dMgr coreif.DeviceManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 85 | } |
| 86 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 87 | // NewTransitionMap creates transition map |
| 88 | func NewTransitionMap(dMgr coreif.DeviceManager) *TransitionMap { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 89 | var transitionMap TransitionMap |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 90 | transitionMap.dMgr = dMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | transitionMap.transitions = make([]Transition, 0) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 92 | transitionMap.transitions = append( |
| 93 | transitionMap.transitions, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 94 | Transition{ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | deviceType: parent, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 96 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 97 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 98 | handlers: []TransitionHandler{dMgr.CreateLogicalDevice}}) |
| 99 | transitionMap.transitions = append(transitionMap.transitions, |
| 100 | Transition{ |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 101 | deviceType: child, |
| 102 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 103 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 104 | handlers: []TransitionHandler{}}) |
| 105 | transitionMap.transitions = append(transitionMap.transitions, |
| 106 | Transition{ |
| 107 | deviceType: child, |
| 108 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 109 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 110 | handlers: []TransitionHandler{dMgr.SetupUNILogicalPorts}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 111 | transitionMap.transitions = append(transitionMap.transitions, |
| 112 | Transition{ |
| 113 | deviceType: child, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 114 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 115 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
| 116 | handlers: []TransitionHandler{}}) |
| 117 | transitionMap.transitions = append(transitionMap.transitions, |
| 118 | Transition{ |
| 119 | deviceType: child, |
| 120 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 121 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 122 | handlers: []TransitionHandler{dMgr.SetupUNILogicalPorts}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | transitionMap.transitions = append(transitionMap.transitions, |
| 124 | Transition{ |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 125 | deviceType: any, |
| 126 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 127 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 128 | handlers: []TransitionHandler{dMgr.RunPostDeviceDelete}}) |
| 129 | transitionMap.transitions = append(transitionMap.transitions, |
| 130 | Transition{ |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 131 | deviceType: parent, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 132 | previousState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 133 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 134 | handlers: []TransitionHandler{dMgr.DisableAllChildDevices, dMgr.DeleteAllUNILogicalPorts, dMgr.DeleteAllChildDevices, dMgr.DeleteLogicalDevice, dMgr.RunPostDeviceDelete}}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 135 | transitionMap.transitions = append(transitionMap.transitions, |
| 136 | Transition{ |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 137 | deviceType: parent, |
| 138 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_REACHABLE, Operational: voltha.OperStatus_ACTIVE}, |
| 139 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNREACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
| 140 | handlers: []TransitionHandler{dMgr.DeleteAllLogicalPorts, dMgr.DeleteLogicalDevice, dMgr.DeleteAllChildDevices, dMgr.DeleteAllDeviceFlows}}) |
| 141 | transitionMap.transitions = append(transitionMap.transitions, |
| 142 | Transition{ |
| 143 | deviceType: parent, |
| 144 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNREACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
| 145 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_REACHABLE, Operational: voltha.OperStatus_ACTIVE}, |
| 146 | handlers: []TransitionHandler{dMgr.CreateLogicalDevice}}) |
| 147 | transitionMap.transitions = append(transitionMap.transitions, |
| 148 | Transition{ |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 149 | deviceType: child, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 150 | previousState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 151 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 152 | handlers: []TransitionHandler{dMgr.ChildDeviceLost, dMgr.DeleteLogicalPorts, dMgr.RunPostDeviceDelete}}) |
| 153 | transitionMap.transitions = append(transitionMap.transitions, |
| 154 | Transition{ |
| 155 | deviceType: child, |
| 156 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 157 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 158 | handlers: []TransitionHandler{dMgr.ChildDeviceLost, dMgr.DeleteLogicalPorts, dMgr.RunPostDeviceDelete}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 159 | transitionMap.transitions = append(transitionMap.transitions, |
| 160 | Transition{ |
| 161 | deviceType: any, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 162 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 163 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 164 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 165 | transitionMap.transitions = append(transitionMap.transitions, |
| 166 | Transition{ |
| 167 | deviceType: any, |
| 168 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 169 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 170 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 171 | transitionMap.transitions = append(transitionMap.transitions, |
| 172 | Transition{ |
| 173 | deviceType: any, |
| 174 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 175 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 176 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 177 | transitionMap.transitions = append(transitionMap.transitions, |
| 178 | Transition{ |
| 179 | deviceType: any, |
| 180 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 181 | currentState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 182 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 183 | transitionMap.transitions = append(transitionMap.transitions, |
| 184 | Transition{ |
| 185 | deviceType: any, |
| 186 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 187 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 188 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 189 | transitionMap.transitions = append(transitionMap.transitions, |
| 190 | Transition{ |
| 191 | deviceType: parent, |
| 192 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 193 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 194 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 195 | transitionMap.transitions = append(transitionMap.transitions, |
| 196 | Transition{ |
| 197 | deviceType: any, |
| 198 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 199 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 200 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 201 | transitionMap.transitions = append(transitionMap.transitions, |
| 202 | Transition{ |
| 203 | deviceType: any, |
| 204 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 205 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 206 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 207 | transitionMap.transitions = append(transitionMap.transitions, |
| 208 | Transition{ |
| 209 | deviceType: child, |
| 210 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 211 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 212 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 213 | transitionMap.transitions = append(transitionMap.transitions, |
| 214 | Transition{ |
| 215 | deviceType: any, |
| 216 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 217 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 218 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 219 | transitionMap.transitions = append(transitionMap.transitions, |
| 220 | Transition{ |
| 221 | deviceType: any, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 222 | previousState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 223 | currentState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 224 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 225 | |
| 226 | return &transitionMap |
| 227 | } |
| 228 | |
| 229 | func getDeviceStates(device *voltha.Device) *DeviceState { |
| 230 | return &DeviceState{Admin: device.AdminState, Connection: device.ConnectStatus, Operational: device.OperStatus} |
| 231 | } |
| 232 | |
| 233 | // isMatched matches a state transition. It returns whether there is a match and if there is whether it is an exact match |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 234 | func getHandler(previous *DeviceState, current *DeviceState, transition *Transition) ([]TransitionHandler, *match) { |
| 235 | m := &match{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 236 | // Do we have an exact match? |
| 237 | if *previous == transition.previousState && *current == transition.currentState { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 238 | return transition.handlers, &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 239 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 240 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 241 | // Do we have Admin state match? |
| 242 | if current.Admin == transition.currentState.Admin && transition.currentState.Admin != voltha.AdminState_UNKNOWN { |
| 243 | if previous.Admin == transition.previousState.Admin { |
| 244 | m.admin = currPrevStateMatch |
| 245 | } else if transition.previousState.Admin == voltha.AdminState_UNKNOWN { |
| 246 | m.admin = currStateOnlyMatch |
| 247 | } |
| 248 | } else if current.Admin == transition.currentState.Admin && transition.currentState.Admin == voltha.AdminState_UNKNOWN { |
| 249 | if previous.Admin == transition.previousState.Admin || transition.previousState.Admin == voltha.AdminState_UNKNOWN { |
| 250 | m.admin = currWildcardMatch |
| 251 | } |
| 252 | } |
| 253 | if m.admin == noMatch { |
| 254 | // invalid transition - need to match on current admin state |
| 255 | return nil, m |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 256 | } |
| 257 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 258 | // Do we have an operational state match? |
| 259 | if current.Operational == transition.currentState.Operational && transition.previousState.Operational != voltha.OperStatus_UNKNOWN { |
| 260 | if previous.Operational == transition.previousState.Operational || transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 261 | m.oper = currPrevStateMatch |
| 262 | } else { |
| 263 | m.oper = currStateOnlyMatch |
| 264 | } |
| 265 | } else if current.Operational == transition.currentState.Operational && transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 266 | if previous.Operational == transition.previousState.Operational || transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 267 | m.oper = currWildcardMatch |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 268 | } |
| 269 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 270 | |
| 271 | // Do we have an connection state match? |
| 272 | if current.Connection == transition.currentState.Connection && transition.previousState.Connection != voltha.ConnectStatus_UNKNOWN { |
| 273 | if previous.Connection == transition.previousState.Connection || transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 274 | m.conn = currPrevStateMatch |
| 275 | } else { |
| 276 | m.conn = currStateOnlyMatch |
| 277 | } |
| 278 | } else if current.Connection == transition.currentState.Connection && transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 279 | if previous.Connection == transition.previousState.Connection || transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 280 | m.conn = currWildcardMatch |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 283 | |
| 284 | return transition.handlers, m |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 285 | } |
| 286 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 287 | // GetTransitionHandler returns transition handler |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 288 | func (tMap *TransitionMap) GetTransitionHandler(pDevice *voltha.Device, cDevice *voltha.Device) []TransitionHandler { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 289 | //1. Get the previous and current set of states |
| 290 | pState := getDeviceStates(pDevice) |
| 291 | cState := getDeviceStates(cDevice) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 292 | |
| 293 | // Do nothing is there are no states change |
| 294 | if *pState == *cState { |
| 295 | return nil |
| 296 | } |
| 297 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 298 | //logger.Infow("DeviceType", log.Fields{"device": pDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 299 | deviceType := parent |
| 300 | if !pDevice.Root { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 301 | logger.Info("device is child") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 302 | deviceType = child |
| 303 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 304 | logger.Infof("deviceType:%d-deviceId:%s-previous:%v-current:%v", deviceType, pDevice.Id, pState, cState) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 305 | |
| 306 | //2. Go over transition array to get the right transition |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 307 | var currentMatch []TransitionHandler |
| 308 | var tempHandler []TransitionHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 309 | var m *match |
| 310 | bestMatch := &match{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 311 | for _, aTransition := range tMap.transitions { |
| 312 | // consider transition only if it matches deviceType or is a wild card - any |
| 313 | if aTransition.deviceType != deviceType && aTransition.deviceType != any { |
| 314 | continue |
| 315 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 316 | tempHandler, m = getHandler(pState, cState, &aTransition) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 317 | if tempHandler != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 318 | if m.isExactMatch() && aTransition.deviceType == deviceType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 319 | return tempHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 320 | } else if m.isExactMatch() || m.isBetterMatch(bestMatch) { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 321 | currentMatch = tempHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 322 | bestMatch = m |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | return currentMatch |
| 327 | } |