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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltha-go/common/log" |
| 20 | "github.com/opencord/voltha-go/protos/voltha" |
| 21 | ) |
| 22 | |
| 23 | type DeviceType int32 |
| 24 | |
| 25 | const ( |
| 26 | parent DeviceType = 0 |
| 27 | child DeviceType = 1 |
| 28 | any DeviceType = 2 |
| 29 | ) |
| 30 | |
| 31 | type DeviceState struct { |
| 32 | Admin voltha.AdminState_AdminState |
| 33 | Connection voltha.ConnectStatus_ConnectStatus |
| 34 | Operational voltha.OperStatus_OperStatus |
| 35 | } |
| 36 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 37 | type TransitionHandler func(*voltha.Device) error |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | |
| 39 | type Transition struct { |
| 40 | deviceType DeviceType |
| 41 | previousState DeviceState |
| 42 | currentState DeviceState |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 43 | handlers []TransitionHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type TransitionMap struct { |
| 47 | transitions []Transition |
| 48 | } |
| 49 | |
| 50 | func NewTransitionMap(dMgr *DeviceManager) *TransitionMap { |
| 51 | var transitionMap TransitionMap |
| 52 | transitionMap.transitions = make([]Transition, 0) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 53 | //transitionMap.transitions = append(transitionMap.transitions, |
| 54 | // Transition{ |
| 55 | // deviceType: any, |
| 56 | // previousState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 57 | // currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 58 | // handlers: []TransitionHandler{dMgr.activateDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | transitionMap.transitions = append(transitionMap.transitions, |
| 60 | Transition{ |
| 61 | deviceType: any, |
| 62 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 63 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 64 | handlers: []TransitionHandler{dMgr.notAllowed}}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 65 | //transitionMap.transitions = append(transitionMap.transitions, |
| 66 | // Transition{ |
| 67 | // deviceType: any, |
| 68 | // previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 69 | // currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 70 | // handlers: []TransitionHandler{dMgr.activateDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | transitionMap.transitions = append(transitionMap.transitions, |
| 72 | Transition{ |
| 73 | deviceType: any, |
| 74 | previousState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 75 | currentState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 76 | handlers: []TransitionHandler{dMgr.notAllowed}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 77 | transitionMap.transitions = append(transitionMap.transitions, |
| 78 | Transition{ |
| 79 | deviceType: any, |
| 80 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 81 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 82 | handlers: []TransitionHandler{dMgr.notAllowed}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 83 | transitionMap.transitions = append(transitionMap.transitions, |
| 84 | Transition{ |
| 85 | deviceType: parent, |
| 86 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 87 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 88 | handlers: []TransitionHandler{dMgr.createLogicalDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 89 | transitionMap.transitions = append(transitionMap.transitions, |
| 90 | Transition{ |
| 91 | deviceType: child, |
| 92 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVATING}, |
| 93 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_ACTIVE}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 94 | handlers: []TransitionHandler{dMgr.addUNILogicalPort}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | transitionMap.transitions = append(transitionMap.transitions, |
| 96 | Transition{ |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 97 | deviceType: parent, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 99 | currentState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 100 | handlers: []TransitionHandler{dMgr.deleteLogicalDevice, dMgr.disableAllChildDevices}}) |
| 101 | transitionMap.transitions = append(transitionMap.transitions, |
| 102 | Transition{ |
| 103 | deviceType: child, |
| 104 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 105 | currentState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 106 | handlers: []TransitionHandler{dMgr.deleteLogicalPort}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 107 | transitionMap.transitions = append(transitionMap.transitions, |
| 108 | Transition{ |
| 109 | deviceType: any, |
| 110 | previousState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 111 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 112 | handlers: []TransitionHandler{dMgr.abandonDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 113 | transitionMap.transitions = append(transitionMap.transitions, |
| 114 | Transition{ |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 115 | deviceType: child, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 116 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 117 | currentState: DeviceState{Admin: voltha.AdminState_UNKNOWN, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 118 | handlers: []TransitionHandler{dMgr.notAllowed}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 119 | transitionMap.transitions = append(transitionMap.transitions, |
| 120 | Transition{ |
| 121 | deviceType: any, |
| 122 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 123 | currentState: DeviceState{Admin: voltha.AdminState_PREPROVISIONED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 124 | handlers: []TransitionHandler{dMgr.abandonDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 125 | transitionMap.transitions = append(transitionMap.transitions, |
| 126 | Transition{ |
| 127 | deviceType: any, |
| 128 | previousState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 129 | currentState: DeviceState{Admin: voltha.AdminState_ENABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 130 | handlers: []TransitionHandler{dMgr.reEnableDevice}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 131 | transitionMap.transitions = append(transitionMap.transitions, |
| 132 | Transition{ |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 133 | deviceType: parent, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 134 | 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] | 135 | currentState: DeviceState{Admin: voltha.AdminState_DELETED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 136 | handlers: []TransitionHandler{dMgr.deleteAllChildDevices}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 137 | transitionMap.transitions = append(transitionMap.transitions, |
| 138 | Transition{ |
| 139 | deviceType: any, |
| 140 | previousState: DeviceState{Admin: voltha.AdminState_DOWNLOADING_IMAGE, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
| 141 | currentState: DeviceState{Admin: voltha.AdminState_DISABLED, Connection: voltha.ConnectStatus_UNKNOWN, Operational: voltha.OperStatus_UNKNOWN}, |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 142 | handlers: []TransitionHandler{dMgr.notAllowed}}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 143 | |
| 144 | return &transitionMap |
| 145 | } |
| 146 | |
| 147 | func getDeviceStates(device *voltha.Device) *DeviceState { |
| 148 | return &DeviceState{Admin: device.AdminState, Connection: device.ConnectStatus, Operational: device.OperStatus} |
| 149 | } |
| 150 | |
| 151 | // 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] | 152 | func getHandler(previous *DeviceState, current *DeviceState, transition *Transition) ([]TransitionHandler, bool) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 153 | |
| 154 | // Do we have an exact match? |
| 155 | if *previous == transition.previousState && *current == transition.currentState { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 156 | return transition.handlers, true |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 157 | } |
| 158 | // If the admin state changed then prioritize it first |
| 159 | if previous.Admin != current.Admin { |
| 160 | if previous.Admin == transition.previousState.Admin && current.Admin == transition.currentState.Admin { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 161 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | // If the operational state changed then prioritize it in second position |
| 165 | if previous.Operational != current.Operational { |
| 166 | if previous.Operational == transition.previousState.Operational && current.Operational == transition.currentState.Operational { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 167 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | // If the connection state changed then prioritize it in third position |
| 171 | if previous.Connection != current.Connection { |
| 172 | if previous.Connection == transition.previousState.Connection && current.Connection == transition.currentState.Connection { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 173 | return transition.handlers, false |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return nil, false |
| 177 | } |
| 178 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 179 | func (tMap *TransitionMap) GetTransitionHandler(pDevice *voltha.Device, cDevice *voltha.Device) []TransitionHandler { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 180 | //1. Get the previous and current set of states |
| 181 | pState := getDeviceStates(pDevice) |
| 182 | cState := getDeviceStates(cDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 183 | //log.Infow("DeviceType", log.Fields{"device": pDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 184 | deviceType := parent |
| 185 | if !pDevice.Root { |
| 186 | log.Info("device is child") |
| 187 | deviceType = child |
| 188 | } |
| 189 | log.Infof("deviceType:%d-deviceId:%s-previous:%v-current:%v", deviceType, pDevice.Id, pState, cState) |
| 190 | |
| 191 | //2. Go over transition array to get the right transition |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 192 | var currentMatch []TransitionHandler |
| 193 | var tempHandler []TransitionHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 194 | var exactMatch bool |
| 195 | var deviceTypeMatch bool |
| 196 | for _, aTransition := range tMap.transitions { |
| 197 | // consider transition only if it matches deviceType or is a wild card - any |
| 198 | if aTransition.deviceType != deviceType && aTransition.deviceType != any { |
| 199 | continue |
| 200 | } |
| 201 | tempHandler, exactMatch = getHandler(pState, cState, &aTransition) |
| 202 | if tempHandler != nil { |
| 203 | if exactMatch { |
| 204 | return tempHandler |
| 205 | } else { |
| 206 | if currentMatch == nil { |
| 207 | currentMatch = tempHandler |
| 208 | } else if aTransition.deviceType == deviceType { |
| 209 | currentMatch = tempHandler |
| 210 | deviceTypeMatch = true |
| 211 | } else if !deviceTypeMatch { |
| 212 | currentMatch = tempHandler |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return currentMatch |
| 218 | } |