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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 17 | package device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 25 | // deviceType mentions type of device like parent, child |
| 26 | type deviceType int32 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | |
| 28 | const ( |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 29 | parent deviceType = 0 |
| 30 | child deviceType = 1 |
| 31 | any deviceType = 2 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 34 | type matchResult uint8 |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 35 | |
| 36 | const ( |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 37 | noMatch matchResult = iota // current state has not match in the transition table |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 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 { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 45 | admin, oper, conn matchResult |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 63 | // deviceState has admin, operational and connection status of device |
| 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 |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 71 | type TransitionHandler func(context.Context, *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 { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 102 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
| 103 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 104 | handlers: []TransitionHandler{}}) |
| 105 | transitionMap.transitions = append(transitionMap.transitions, |
| 106 | Transition{ |
| 107 | deviceType: child, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 108 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
| 109 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 114 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 115 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 116 | handlers: []TransitionHandler{}}) |
| 117 | transitionMap.transitions = append(transitionMap.transitions, |
| 118 | Transition{ |
| 119 | deviceType: child, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 120 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 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}, |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 132 | previousState: deviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 133 | currentState: deviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
Andrea Campanella | 09400bd | 2020-04-02 11:58:04 +0200 | [diff] [blame] | 134 | handlers: []TransitionHandler{dMgr.DisableAllChildDevices, dMgr.DeleteAllUNILogicalPorts, dMgr.DeleteAllChildDevices, dMgr.DeleteAllLogicalPorts, 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, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 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}, |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 140 | handlers: []TransitionHandler{dMgr.DeleteAllLogicalPorts, dMgr.DeleteLogicalDevice, dMgr.DeleteAllChildDevices, dMgr.DeleteAllDeviceFlows}}) |
| 141 | transitionMap.transitions = append(transitionMap.transitions, |
| 142 | Transition{ |
| 143 | deviceType: parent, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 144 | previousState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_REACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
| 145 | currentState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNREACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
Chaitrashree G S | 7849b32 | 2020-03-29 19:25:49 -0400 | [diff] [blame] | 146 | handlers: []TransitionHandler{dMgr.DeleteAllLogicalPorts, dMgr.DeleteLogicalDevice, dMgr.DeleteAllChildDevices, dMgr.DeleteAllDeviceFlows}}) |
| 147 | transitionMap.transitions = append(transitionMap.transitions, |
| 148 | Transition{ |
| 149 | deviceType: parent, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 150 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNREACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
| 151 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_REACHABLE, Operational: voltha.OperStatus_ACTIVE}, |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 152 | handlers: []TransitionHandler{dMgr.CreateLogicalDevice}}) |
| 153 | transitionMap.transitions = append(transitionMap.transitions, |
| 154 | Transition{ |
Chaitrashree G S | 7849b32 | 2020-03-29 19:25:49 -0400 | [diff] [blame] | 155 | deviceType: parent, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 156 | previousState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNREACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
| 157 | currentState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_REACHABLE, Operational: voltha.OperStatus_UNKNOWN}, |
Chaitrashree G S | 7849b32 | 2020-03-29 19:25:49 -0400 | [diff] [blame] | 158 | handlers: []TransitionHandler{dMgr.CreateLogicalDevice}}) |
| 159 | transitionMap.transitions = append(transitionMap.transitions, |
| 160 | Transition{ |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 161 | deviceType: child, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 162 | previousState: deviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 163 | 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] | 164 | handlers: []TransitionHandler{dMgr.ChildDeviceLost, dMgr.DeleteLogicalPorts, dMgr.RunPostDeviceDelete}}) |
| 165 | transitionMap.transitions = append(transitionMap.transitions, |
| 166 | Transition{ |
| 167 | deviceType: child, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 168 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 169 | 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] | 170 | handlers: []TransitionHandler{dMgr.ChildDeviceLost, dMgr.DeleteLogicalPorts, dMgr.RunPostDeviceDelete}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 171 | transitionMap.transitions = append(transitionMap.transitions, |
| 172 | Transition{ |
| 173 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 174 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 175 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 176 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 177 | transitionMap.transitions = append(transitionMap.transitions, |
| 178 | Transition{ |
| 179 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 180 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 181 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 182 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 183 | transitionMap.transitions = append(transitionMap.transitions, |
| 184 | Transition{ |
| 185 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 186 | previousState: deviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 187 | currentState: deviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 188 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 189 | transitionMap.transitions = append(transitionMap.transitions, |
| 190 | Transition{ |
| 191 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 192 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 193 | currentState: deviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 194 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 195 | transitionMap.transitions = append(transitionMap.transitions, |
| 196 | Transition{ |
| 197 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 198 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 199 | currentState: deviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 200 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 201 | transitionMap.transitions = append(transitionMap.transitions, |
| 202 | Transition{ |
| 203 | deviceType: parent, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 204 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 205 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 206 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 207 | transitionMap.transitions = append(transitionMap.transitions, |
| 208 | Transition{ |
| 209 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 210 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 211 | currentState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 212 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 213 | transitionMap.transitions = append(transitionMap.transitions, |
| 214 | Transition{ |
| 215 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 216 | previousState: deviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 217 | currentState: deviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 218 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 219 | transitionMap.transitions = append(transitionMap.transitions, |
| 220 | Transition{ |
| 221 | deviceType: child, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 222 | previousState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 223 | currentState: deviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 224 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 225 | transitionMap.transitions = append(transitionMap.transitions, |
| 226 | Transition{ |
| 227 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 228 | previousState: deviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 229 | currentState: deviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 230 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 231 | transitionMap.transitions = append(transitionMap.transitions, |
| 232 | Transition{ |
| 233 | deviceType: any, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 234 | previousState: deviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 235 | 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] | 236 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 237 | |
| 238 | return &transitionMap |
| 239 | } |
| 240 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 241 | func getDeviceStates(device *voltha.Device) *deviceState { |
| 242 | return &deviceState{Admin: device.AdminState, Connection: device.ConnectStatus, Operational: device.OperStatus} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // isMatched matches a state transition. It returns whether there is a match and if there is whether it is an exact match |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 246 | func getHandler(previous *deviceState, current *deviceState, transition *Transition) ([]TransitionHandler, *match) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 247 | m := &match{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 248 | // Do we have an exact match? |
| 249 | if *previous == transition.previousState && *current == transition.currentState { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 250 | return transition.handlers, &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 251 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 252 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 253 | // Do we have Admin state match? |
| 254 | if current.Admin == transition.currentState.Admin && transition.currentState.Admin != voltha.AdminState_UNKNOWN { |
| 255 | if previous.Admin == transition.previousState.Admin { |
| 256 | m.admin = currPrevStateMatch |
| 257 | } else if transition.previousState.Admin == voltha.AdminState_UNKNOWN { |
| 258 | m.admin = currStateOnlyMatch |
| 259 | } |
| 260 | } else if current.Admin == transition.currentState.Admin && transition.currentState.Admin == voltha.AdminState_UNKNOWN { |
| 261 | if previous.Admin == transition.previousState.Admin || transition.previousState.Admin == voltha.AdminState_UNKNOWN { |
| 262 | m.admin = currWildcardMatch |
| 263 | } |
| 264 | } |
| 265 | if m.admin == noMatch { |
| 266 | // invalid transition - need to match on current admin state |
| 267 | return nil, m |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 268 | } |
| 269 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 270 | // Do we have an operational state match? |
| 271 | if current.Operational == transition.currentState.Operational && transition.previousState.Operational != voltha.OperStatus_UNKNOWN { |
| 272 | if previous.Operational == transition.previousState.Operational || transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 273 | m.oper = currPrevStateMatch |
| 274 | } else { |
| 275 | m.oper = currStateOnlyMatch |
| 276 | } |
| 277 | } else if current.Operational == transition.currentState.Operational && transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 278 | if previous.Operational == transition.previousState.Operational || transition.previousState.Operational == voltha.OperStatus_UNKNOWN { |
| 279 | m.oper = currWildcardMatch |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 282 | |
| 283 | // Do we have an connection state match? |
| 284 | if current.Connection == transition.currentState.Connection && transition.previousState.Connection != voltha.ConnectStatus_UNKNOWN { |
| 285 | if previous.Connection == transition.previousState.Connection || transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 286 | m.conn = currPrevStateMatch |
| 287 | } else { |
| 288 | m.conn = currStateOnlyMatch |
| 289 | } |
| 290 | } else if current.Connection == transition.currentState.Connection && transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 291 | if previous.Connection == transition.previousState.Connection || transition.previousState.Connection == voltha.ConnectStatus_UNKNOWN { |
| 292 | m.conn = currWildcardMatch |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 295 | |
| 296 | return transition.handlers, m |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 299 | // GetTransitionHandler returns transition handler & a flag that's set if the transition is invalid |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 300 | func (tMap *TransitionMap) GetTransitionHandler(device *voltha.Device, pState *deviceState) []TransitionHandler { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 301 | //1. Get the previous and current set of states |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 302 | cState := getDeviceStates(device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 303 | |
| 304 | // Do nothing is there are no states change |
| 305 | if *pState == *cState { |
| 306 | return nil |
| 307 | } |
| 308 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 309 | //logger.Infow("deviceType", log.Fields{"device": pDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 310 | deviceType := parent |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 311 | if !device.Root { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 312 | logger.Info("device is child") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 313 | deviceType = child |
| 314 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 315 | logger.Infof("deviceType:%d-deviceId:%s-previous:%v-current:%v", deviceType, device.Id, pState, cState) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 316 | |
| 317 | //2. Go over transition array to get the right transition |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 318 | var currentMatch []TransitionHandler |
| 319 | var tempHandler []TransitionHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 320 | var m *match |
| 321 | bestMatch := &match{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 322 | for _, aTransition := range tMap.transitions { |
| 323 | // consider transition only if it matches deviceType or is a wild card - any |
| 324 | if aTransition.deviceType != deviceType && aTransition.deviceType != any { |
| 325 | continue |
| 326 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 327 | tempHandler, m = getHandler(pState, cState, &aTransition) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 328 | if tempHandler != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 329 | if m.isExactMatch() && aTransition.deviceType == deviceType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 330 | return tempHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 331 | } else if m.isExactMatch() || m.isBetterMatch(bestMatch) { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 332 | currentMatch = tempHandler |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 333 | bestMatch = m |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
| 337 | return currentMatch |
| 338 | } |