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