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 | */ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 16 | package device |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 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 | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 55 | func getDeviceState(admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *deviceState { |
| 56 | return &deviceState{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 57 | Admin: admin, |
| 58 | Connection: conn, |
| 59 | Operational: oper, |
| 60 | } |
| 61 | } |
| 62 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 63 | func assertInvalidTransition(t *testing.T, device *voltha.Device, previousState *deviceState) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 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 | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 69 | func assertNoOpTransition(t *testing.T, device *voltha.Device, previousState *deviceState) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 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 | |
Chaitrashree G S | 7849b32 | 2020-03-29 19:25:49 -0400 | [diff] [blame] | 200 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN) |
| 201 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN) |
| 202 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
| 203 | assert.Equal(t, 4, len(handlers)) |
| 204 | assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 205 | assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[1]).Pointer()) |
| 206 | assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[2]).Pointer()) |
| 207 | assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer()) |
| 208 | |
| 209 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN) |
| 210 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN) |
| 211 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
| 212 | assert.Equal(t, 1, len(handlers)) |
| 213 | assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer()) |
| 214 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 215 | var deleteDeviceTest = struct { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 216 | previousStates []*deviceState |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 217 | devices []*voltha.Device |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 218 | expectedParentHandlers []TransitionHandler |
| 219 | expectedChildHandlers []TransitionHandler |
| 220 | }{ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame^] | 221 | previousStates: []*deviceState{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 222 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED), |
| 223 | getDeviceState(voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 224 | getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 225 | getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 226 | getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE), |
| 227 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN), |
| 228 | getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN), |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 229 | }, |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 230 | devices: []*voltha.Device{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 231 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN), |
| 232 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN), |
| 233 | getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED), |
| 234 | }, |
| 235 | expectedParentHandlers: []TransitionHandler{ |
| 236 | tdm.DisableAllChildDevices, |
| 237 | tdm.DeleteAllUNILogicalPorts, |
| 238 | tdm.DeleteAllChildDevices, |
Andrea Campanella | 09400bd | 2020-04-02 11:58:04 +0200 | [diff] [blame] | 239 | tdm.DeleteAllLogicalPorts, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 240 | tdm.DeleteLogicalDevice, |
| 241 | tdm.RunPostDeviceDelete, |
| 242 | }, |
| 243 | expectedChildHandlers: []TransitionHandler{ |
| 244 | tdm.ChildDeviceLost, |
| 245 | tdm.DeleteLogicalPorts, |
| 246 | tdm.RunPostDeviceDelete, |
| 247 | }, |
| 248 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 249 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 250 | testName := "delete-parent-device-post-provisioning" |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 251 | for _, previousState := range deleteDeviceTest.previousStates { |
| 252 | for _, device := range deleteDeviceTest.devices { |
| 253 | device.Root = true |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 254 | t.Run(testName, func(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 255 | handlers = transitionMap.GetTransitionHandler(device, previousState) |
Andrea Campanella | 09400bd | 2020-04-02 11:58:04 +0200 | [diff] [blame] | 256 | assert.Equal(t, 6, len(handlers)) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 257 | for idx, expHandler := range deleteDeviceTest.expectedParentHandlers { |
| 258 | assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer()) |
| 259 | } |
| 260 | }) |
| 261 | } |
| 262 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 263 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 264 | testName = "delete-child-device" |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 265 | for _, deviceState := range deleteDeviceTest.previousStates { |
| 266 | for _, device := range deleteDeviceTest.devices { |
| 267 | device.Root = false |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 268 | t.Run(testName, func(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 269 | handlers = transitionMap.GetTransitionHandler(device, deviceState) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 270 | assert.Equal(t, 3, len(handlers)) |
| 271 | for idx, expHandler := range deleteDeviceTest.expectedChildHandlers { |
| 272 | assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer()) |
| 273 | } |
| 274 | }) |
| 275 | } |
| 276 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | func TestInvalidTransitions(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 280 | previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE) |
| 281 | device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 282 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 283 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 284 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 285 | device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 286 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 287 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 288 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 289 | device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 290 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 291 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 292 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 293 | device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 294 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 295 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 296 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 297 | device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 298 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 299 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 300 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 301 | device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 302 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 303 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 304 | previousState = getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 305 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 306 | assertInvalidTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | func TestNoOpTransitions(t *testing.T) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 310 | previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 311 | device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 312 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 313 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 314 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 315 | device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 316 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 317 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 318 | previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 319 | device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 320 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 321 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 322 | previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 323 | device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN) |
| 324 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 325 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 326 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING) |
| 327 | device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED) |
| 328 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 329 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 330 | previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED) |
| 331 | device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING) |
| 332 | assertNoOpTransition(t, device, previousState) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 333 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 334 | |
| 335 | func TestMatch(t *testing.T) { |
| 336 | best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch} |
| 337 | m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch} |
| 338 | fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt()) |
| 339 | } |