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 | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/rw_core/coreif" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 34 | // DeviceState has admin, operational and connection status of device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 35 | type DeviceState struct { |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 36 | Admin voltha.AdminState_Types |
| 37 | Connection voltha.ConnectStatus_Types |
| 38 | Operational voltha.OperStatus_Types |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | } |
| 40 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 41 | // TransitionHandler function type which takes device as input parameter |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 42 | type TransitionHandler func(*voltha.Device) error |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 44 | // Transition represent transition related attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | type Transition struct { |
| 46 | deviceType DeviceType |
| 47 | previousState DeviceState |
| 48 | currentState DeviceState |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 49 | handlers []TransitionHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 50 | } |
| 51 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 52 | // TransitionMap represent map of transitions and device manager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | type TransitionMap struct { |
| 54 | transitions []Transition |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 55 | dMgr coreif.DeviceManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 56 | } |
| 57 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 58 | // NewTransitionMap creates transition map |
| 59 | func NewTransitionMap(dMgr coreif.DeviceManager) *TransitionMap { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | var transitionMap TransitionMap |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 61 | transitionMap.dMgr = dMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 62 | transitionMap.transitions = make([]Transition, 0) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 63 | transitionMap.transitions = append(transitionMap.transitions, |
| 64 | Transition{ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 65 | deviceType: parent, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 66 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 67 | 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] | 68 | handlers: []TransitionHandler{dMgr.CreateLogicalDevice}}) |
| 69 | transitionMap.transitions = append(transitionMap.transitions, |
| 70 | Transition{ |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 71 | deviceType: child, |
| 72 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_DISCOVERED}, |
| 73 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 74 | handlers: []TransitionHandler{dMgr.SetupUNILogicalPorts}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | transitionMap.transitions = append(transitionMap.transitions, |
| 76 | Transition{ |
| 77 | deviceType: child, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 78 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 79 | 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] | 80 | handlers: []TransitionHandler{dMgr.SetupUNILogicalPorts}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 81 | transitionMap.transitions = append(transitionMap.transitions, |
| 82 | Transition{ |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 83 | deviceType: any, |
| 84 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 85 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 86 | handlers: []TransitionHandler{dMgr.RunPostDeviceDelete}}) |
| 87 | transitionMap.transitions = append(transitionMap.transitions, |
| 88 | Transition{ |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 89 | deviceType: parent, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 90 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 91 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
Devmalya Paul | 84169b5 | 2019-08-27 19:31:44 -0400 | [diff] [blame] | 92 | handlers: []TransitionHandler{dMgr.DisableAllChildDevices, dMgr.DeleteAllChildDevices, dMgr.DeleteLogicalDevice, dMgr.RunPostDeviceDelete}}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 93 | transitionMap.transitions = append(transitionMap.transitions, |
| 94 | Transition{ |
| 95 | deviceType: child, |
| 96 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 97 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 98 | handlers: []TransitionHandler{dMgr.DeleteLogicalPorts, dMgr.RunPostDeviceDelete}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 99 | transitionMap.transitions = append(transitionMap.transitions, |
| 100 | Transition{ |
| 101 | deviceType: any, |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 102 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 103 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 104 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 105 | transitionMap.transitions = append(transitionMap.transitions, |
| 106 | Transition{ |
| 107 | deviceType: any, |
| 108 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 109 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 110 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 111 | transitionMap.transitions = append(transitionMap.transitions, |
| 112 | Transition{ |
| 113 | deviceType: any, |
| 114 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 115 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 116 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 117 | transitionMap.transitions = append(transitionMap.transitions, |
| 118 | Transition{ |
| 119 | deviceType: any, |
| 120 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 121 | currentState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 122 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 123 | transitionMap.transitions = append(transitionMap.transitions, |
| 124 | Transition{ |
| 125 | deviceType: any, |
| 126 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 127 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 128 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 129 | transitionMap.transitions = append(transitionMap.transitions, |
| 130 | Transition{ |
| 131 | deviceType: parent, |
| 132 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
| 133 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 134 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 135 | transitionMap.transitions = append(transitionMap.transitions, |
| 136 | Transition{ |
| 137 | deviceType: any, |
| 138 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 139 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 140 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 141 | transitionMap.transitions = append(transitionMap.transitions, |
| 142 | Transition{ |
| 143 | deviceType: any, |
| 144 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 145 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 146 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 147 | transitionMap.transitions = append(transitionMap.transitions, |
| 148 | Transition{ |
| 149 | deviceType: child, |
| 150 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 151 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 152 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 153 | transitionMap.transitions = append(transitionMap.transitions, |
| 154 | Transition{ |
| 155 | deviceType: any, |
| 156 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 157 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 158 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
| 159 | transitionMap.transitions = append(transitionMap.transitions, |
| 160 | Transition{ |
| 161 | deviceType: any, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | previousState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 163 | 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] | 164 | handlers: []TransitionHandler{dMgr.NotifyInvalidTransition}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 165 | |
| 166 | return &transitionMap |
| 167 | } |
| 168 | |
| 169 | func getDeviceStates(device *voltha.Device) *DeviceState { |
| 170 | return &DeviceState{Admin: device.AdminState, Connection: device.ConnectStatus, Operational: device.OperStatus} |
| 171 | } |
| 172 | |
| 173 | // isMatched matches a state transition. It returns whether there is a match and if there is whether it is an exact match |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 174 | func getHandler(previous *DeviceState, current *DeviceState, transition *Transition) ([]TransitionHandler, bool) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 175 | |
| 176 | // Do we have an exact match? |
| 177 | if *previous == transition.previousState && *current == transition.currentState { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 178 | return transition.handlers, true |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 179 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 180 | |
| 181 | // Admin states must match |
| 182 | if previous.Admin != transition.previousState.Admin || current.Admin != transition.currentState.Admin { |
| 183 | return nil, false |
| 184 | } |
| 185 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 186 | // If the admin state changed then prioritize it first |
| 187 | if previous.Admin != current.Admin { |
| 188 | if previous.Admin == transition.previousState.Admin && current.Admin == transition.currentState.Admin { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 189 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | // If the operational state changed then prioritize it in second position |
| 193 | if previous.Operational != current.Operational { |
| 194 | if previous.Operational == transition.previousState.Operational && current.Operational == transition.currentState.Operational { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 195 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | // If the connection state changed then prioritize it in third position |
| 199 | if previous.Connection != current.Connection { |
| 200 | if previous.Connection == transition.previousState.Connection && current.Connection == transition.currentState.Connection { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 201 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | return nil, false |
| 205 | } |
| 206 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 207 | // GetTransitionHandler returns transition handler |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 208 | func (tMap *TransitionMap) GetTransitionHandler(pDevice *voltha.Device, cDevice *voltha.Device) []TransitionHandler { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 209 | //1. Get the previous and current set of states |
| 210 | pState := getDeviceStates(pDevice) |
| 211 | cState := getDeviceStates(cDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 212 | //log.Infow("DeviceType", log.Fields{"device": pDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 213 | deviceType := parent |
| 214 | if !pDevice.Root { |
| 215 | log.Info("device is child") |
| 216 | deviceType = child |
| 217 | } |
| 218 | log.Infof("deviceType:%d-deviceId:%s-previous:%v-current:%v", deviceType, pDevice.Id, pState, cState) |
| 219 | |
| 220 | //2. Go over transition array to get the right transition |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 221 | var currentMatch []TransitionHandler |
| 222 | var tempHandler []TransitionHandler |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 223 | var exactStateMatch bool |
| 224 | var stateMatchFound bool |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 225 | for _, aTransition := range tMap.transitions { |
| 226 | // consider transition only if it matches deviceType or is a wild card - any |
| 227 | if aTransition.deviceType != deviceType && aTransition.deviceType != any { |
| 228 | continue |
| 229 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 230 | tempHandler, exactStateMatch = getHandler(pState, cState, &aTransition) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 231 | if tempHandler != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 232 | if exactStateMatch && aTransition.deviceType == deviceType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 233 | return tempHandler |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 234 | } else if exactStateMatch { |
| 235 | currentMatch = tempHandler |
| 236 | stateMatchFound = true |
| 237 | } else if currentMatch == nil && !stateMatchFound { |
| 238 | currentMatch = tempHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | return currentMatch |
| 243 | } |