khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 ( |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 19 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | "github.com/opencord/voltha-go/rw_core/coreif" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/rw_core/mocks" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var transitionMap *TransitionMap |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 30 | var tdm coreif.DeviceManager |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 31 | |
| 32 | type testDeviceManager struct { |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 33 | mocks.DeviceManager |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func newTestDeviceManager() *testDeviceManager { |
| 37 | return &testDeviceManager{} |
| 38 | } |
| 39 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 40 | func init() { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 41 | tdm = newTestDeviceManager() |
| 42 | transitionMap = NewTransitionMap(tdm) |
| 43 | } |
| 44 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 45 | func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 46 | return &voltha.Device{ |
| 47 | Id: "test", |
| 48 | Root: root, |
| 49 | AdminState: admin, |
| 50 | ConnectStatus: conn, |
| 51 | OperStatus: oper, |
| 52 | } |
| 53 | } |
| 54 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 55 | func getDeviceState(admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *DeviceState { |
| 56 | return &DeviceState{ |
| 57 | Admin: admin, |
| 58 | Connection: conn, |
| 59 | Operational: oper, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func assertInvalidTransition(t *testing.T, device *voltha.Device, previousState *DeviceState) { |
| 64 | handlers := transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 65 | assert.Equal(t, 1, len(handlers)) |
| 66 | assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 67 | } |
| 68 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 69 | func assertNoOpTransition(t *testing.T, device *voltha.Device, previousState *DeviceState) { |
| 70 | handlers := transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 71 | assert.Equal(t, 0, len(handlers)) |
| 72 | } |
| 73 | |
| 74 | func TestValidTransitions(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 75 | previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 76 | device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 77 | handlers := transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 78 | assert.Equal(t, 1, len(handlers)) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 79 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 80 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 81 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 82 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 83 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 84 | assert.Equal(t, 1, len(handlers)) |
| 85 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 86 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 87 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 88 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 89 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 90 | assert.Equal(t, 1, len(handlers)) |
| 91 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 92 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 93 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING) |
| 94 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 95 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 96 | assert.Equal(t, 1, len(handlers)) |
| 97 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 98 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 99 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING) |
| 100 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 101 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 102 | assert.Equal(t, 1, len(handlers)) |
| 103 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 104 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 105 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED) |
| 106 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 107 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 108 | assert.Equal(t, 1, len(handlers)) |
| 109 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 110 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 111 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED) |
| 112 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 113 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 114 | assert.Equal(t, 1, len(handlers)) |
| 115 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 116 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 117 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED) |
| 118 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 119 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 120 | assert.Equal(t, 1, len(handlers)) |
| 121 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 122 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 123 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED) |
| 124 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 125 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 126 | assert.Equal(t, 1, len(handlers)) |
| 127 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 128 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 129 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED) |
| 130 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 131 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 132 | assert.Equal(t, 1, len(handlers)) |
| 133 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 134 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 135 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 136 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 137 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 138 | assert.Equal(t, 1, len(handlers)) |
| 139 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 140 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 141 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 142 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 143 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 144 | assert.Equal(t, 1, len(handlers)) |
| 145 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 146 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 147 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 148 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 149 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 150 | assert.Equal(t, 1, len(handlers)) |
| 151 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 152 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 153 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING) |
| 154 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 155 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 156 | assert.Equal(t, 1, len(handlers)) |
| 157 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 158 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 159 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING) |
| 160 | device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE) |
| 161 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 162 | assert.Equal(t, 1, len(handlers)) |
| 163 | assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 164 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 165 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 166 | device = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 167 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 168 | assert.Equal(t, 1, len(handlers)) |
| 169 | assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 170 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 171 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 172 | device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 173 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 174 | assert.Equal(t, 1, len(handlers)) |
| 175 | assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 176 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 177 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 178 | device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED) |
| 179 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 180 | assert.Equal(t, 3, len(handlers)) |
| 181 | assert.True(t, reflect.ValueOf(tdm.ChildDeviceLost).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 182 | assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[1]).Pointer()) |
| 183 | assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer()) |
| 184 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 185 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 186 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN) |
| 187 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 188 | assert.Equal(t, 4, len(handlers)) |
| 189 | assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 190 | assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[1]).Pointer()) |
| 191 | assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[2]).Pointer()) |
| 192 | assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer()) |
| 193 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 194 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN) |
| 195 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE) |
| 196 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 197 | assert.Equal(t, 1, len(handlers)) |
| 198 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 199 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 200 | var deleteDeviceTest = struct { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 201 | previousStates []*DeviceState |
| 202 | devices []*voltha.Device |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 203 | expectedParentHandlers []TransitionHandler |
| 204 | expectedChildHandlers []TransitionHandler |
| 205 | }{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 206 | previousStates: []*DeviceState{ |
| 207 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED), |
| 208 | getDeviceState(voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 209 | getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 210 | getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 211 | getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE), |
| 212 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN), |
| 213 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN), |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 214 | }, |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 215 | devices: []*voltha.Device{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 216 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 217 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN), |
| 218 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED), |
| 219 | }, |
| 220 | expectedParentHandlers: []TransitionHandler{ |
| 221 | tdm.DisableAllChildDevices, |
| 222 | tdm.DeleteAllUNILogicalPorts, |
| 223 | tdm.DeleteAllChildDevices, |
Andrea Campanella | 09400bd | 2020-04-02 11:58:04 +0200 | [diff] [blame] | 224 | tdm.DeleteAllLogicalPorts, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 225 | tdm.DeleteLogicalDevice, |
| 226 | tdm.RunPostDeviceDelete, |
| 227 | }, |
| 228 | expectedChildHandlers: []TransitionHandler{ |
| 229 | tdm.ChildDeviceLost, |
| 230 | tdm.DeleteLogicalPorts, |
| 231 | tdm.RunPostDeviceDelete, |
| 232 | }, |
| 233 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 234 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 235 | testName := "delete-parent-device-post-provisioning" |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 236 | for _, previousState := range deleteDeviceTest.previousStates { |
| 237 | for _, device := range deleteDeviceTest.devices { |
| 238 | device.Root = true |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 239 | t.Run(testName, func(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 240 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
Andrea Campanella | 09400bd | 2020-04-02 11:58:04 +0200 | [diff] [blame] | 241 | assert.Equal(t, 6, len(handlers)) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 242 | for idx, expHandler := range deleteDeviceTest.expectedParentHandlers { |
| 243 | assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer()) |
| 244 | } |
| 245 | }) |
| 246 | } |
| 247 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 248 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 249 | testName = "delete-child-device" |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 250 | for _, deviceState := range deleteDeviceTest.previousStates { |
| 251 | for _, device := range deleteDeviceTest.devices { |
| 252 | device.Root = false |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 253 | t.Run(testName, func(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 254 | handlers = transitionMap.GetTransitionHandler(device, deviceState) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 255 | assert.Equal(t, 3, len(handlers)) |
| 256 | for idx, expHandler := range deleteDeviceTest.expectedChildHandlers { |
| 257 | assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer()) |
| 258 | } |
| 259 | }) |
| 260 | } |
| 261 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | func TestInvalidTransitions(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 265 | previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 266 | device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 267 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 268 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 269 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 270 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 271 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 272 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 273 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 274 | device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 275 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 276 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 277 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 278 | device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 279 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 280 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 281 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 282 | device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 283 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 284 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 285 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 286 | device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 287 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 288 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 289 | previousState = getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 290 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 291 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | func TestNoOpTransitions(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 295 | previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 296 | device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 297 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 298 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 299 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 300 | device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 301 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 302 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 303 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 304 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 305 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 306 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 307 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 308 | device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 309 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 310 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 311 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 312 | device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED) |
| 313 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 314 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 315 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED) |
| 316 | device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING) |
| 317 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 318 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 319 | |
| 320 | func TestMatch(t *testing.T) { |
| 321 | best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch} |
| 322 | m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch} |
| 323 | fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt()) |
| 324 | } |