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