blob: 0a1b43b7cddb61ff021e552c9845c4b39b634c31 [file] [log] [blame]
khenaidoo0a822f92019-05-08 15:15:57 -04001/*
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 Hagerman2b216042020-04-03 18:28:56 -040016package device
khenaidoo0a822f92019-05-08 15:15:57 -040017
18import (
khenaidoo442e7c72020-03-10 16:13:48 -040019 "fmt"
npujar1d86a522019-11-14 17:11:16 +053020 "reflect"
21 "testing"
22
23 "github.com/opencord/voltha-go/rw_core/coreif"
khenaidooab1f7bd2019-11-14 14:00:27 -050024 "github.com/opencord/voltha-go/rw_core/mocks"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo0a822f92019-05-08 15:15:57 -040026 "github.com/stretchr/testify/assert"
khenaidoo0a822f92019-05-08 15:15:57 -040027)
28
29var transitionMap *TransitionMap
npujar1d86a522019-11-14 17:11:16 +053030var tdm coreif.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040031
32type testDeviceManager struct {
khenaidooab1f7bd2019-11-14 14:00:27 -050033 mocks.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040034}
35
36func newTestDeviceManager() *testDeviceManager {
37 return &testDeviceManager{}
38}
39
khenaidoo0a822f92019-05-08 15:15:57 -040040func init() {
khenaidoo0a822f92019-05-08 15:15:57 -040041 tdm = newTestDeviceManager()
42 transitionMap = NewTransitionMap(tdm)
43}
44
serkant.uluderya2ae470f2020-01-21 11:13:09 -080045func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
khenaidoo0a822f92019-05-08 15:15:57 -040046 return &voltha.Device{
47 Id: "test",
48 Root: root,
49 AdminState: admin,
50 ConnectStatus: conn,
51 OperStatus: oper,
52 }
53}
54
Kent Hagerman2b216042020-04-03 18:28:56 -040055func getDeviceState(admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *deviceState {
56 return &deviceState{
Kent Hagermand9cc2e92019-11-04 13:28:15 -050057 Admin: admin,
58 Connection: conn,
59 Operational: oper,
60 }
61}
62
Kent Hagerman2b216042020-04-03 18:28:56 -040063func assertInvalidTransition(t *testing.T, device *voltha.Device, previousState *deviceState) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -050064 handlers := transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040065 assert.Equal(t, 1, len(handlers))
66 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
67}
68
Kent Hagerman2b216042020-04-03 18:28:56 -040069func assertNoOpTransition(t *testing.T, device *voltha.Device, previousState *deviceState) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -050070 handlers := transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040071 assert.Equal(t, 0, len(handlers))
72}
73
74func TestValidTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -050075 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)
khenaidoo0a822f92019-05-08 15:15:57 -040078 assert.Equal(t, 1, len(handlers))
khenaidoo59ef7be2019-06-21 12:40:28 -040079 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -040080
Kent Hagermand9cc2e92019-11-04 13:28:15 -050081 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)
khenaidoo59ef7be2019-06-21 12:40:28 -040084 assert.Equal(t, 1, len(handlers))
85 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
86
Kent Hagermand9cc2e92019-11-04 13:28:15 -050087 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)
khenaidoo59ef7be2019-06-21 12:40:28 -040090 assert.Equal(t, 1, len(handlers))
91 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
92
Kent Hagermand9cc2e92019-11-04 13:28:15 -050093 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)
khenaidoo59ef7be2019-06-21 12:40:28 -040096 assert.Equal(t, 1, len(handlers))
97 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
98
Kent Hagermand9cc2e92019-11-04 13:28:15 -050099 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400102 assert.Equal(t, 1, len(handlers))
103 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
104
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500105 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400108 assert.Equal(t, 1, len(handlers))
109 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
110
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500111 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400114 assert.Equal(t, 1, len(handlers))
115 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
116
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500117 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400120 assert.Equal(t, 1, len(handlers))
121 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
122
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500123 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400126 assert.Equal(t, 1, len(handlers))
127 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
128
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500129 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400132 assert.Equal(t, 1, len(handlers))
133 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
134
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500135 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400138 assert.Equal(t, 1, len(handlers))
139 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
140
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500141 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400144 assert.Equal(t, 1, len(handlers))
145 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
146
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500147 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400150 assert.Equal(t, 1, len(handlers))
151 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
152
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500153 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400156 assert.Equal(t, 1, len(handlers))
157 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
158
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500159 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400162 assert.Equal(t, 1, len(handlers))
163 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
164
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500165 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400168 assert.Equal(t, 1, len(handlers))
169 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
170
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500171 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)
khenaidoo59ef7be2019-06-21 12:40:28 -0400174 assert.Equal(t, 1, len(handlers))
175 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
Girish Gowdra408cd962020-03-11 14:31:31 -0700176
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500177 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 Gowdra408cd962020-03-11 14:31:31 -0700180 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 Hagermand9cc2e92019-11-04 13:28:15 -0500185 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 Gowdra408cd962020-03-11 14:31:31 -0700188 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 Hagermand9cc2e92019-11-04 13:28:15 -0500194 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 Gowdra408cd962020-03-11 14:31:31 -0700197 assert.Equal(t, 1, len(handlers))
198 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -0400199
Chaitrashree G S7849b322020-03-29 19:25:49 -0400200 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
khenaidoo442e7c72020-03-10 16:13:48 -0400215 var deleteDeviceTest = struct {
Kent Hagerman2b216042020-04-03 18:28:56 -0400216 previousStates []*deviceState
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500217 devices []*voltha.Device
khenaidoo442e7c72020-03-10 16:13:48 -0400218 expectedParentHandlers []TransitionHandler
219 expectedChildHandlers []TransitionHandler
220 }{
Kent Hagerman2b216042020-04-03 18:28:56 -0400221 previousStates: []*deviceState{
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500222 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),
khenaidoo442e7c72020-03-10 16:13:48 -0400229 },
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500230 devices: []*voltha.Device{
khenaidoo442e7c72020-03-10 16:13:48 -0400231 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 Campanella09400bd2020-04-02 11:58:04 +0200239 tdm.DeleteAllLogicalPorts,
khenaidoo442e7c72020-03-10 16:13:48 -0400240 tdm.DeleteLogicalDevice,
241 tdm.RunPostDeviceDelete,
242 },
243 expectedChildHandlers: []TransitionHandler{
244 tdm.ChildDeviceLost,
245 tdm.DeleteLogicalPorts,
246 tdm.RunPostDeviceDelete,
247 },
248 }
khenaidoo0a822f92019-05-08 15:15:57 -0400249
khenaidoo442e7c72020-03-10 16:13:48 -0400250 testName := "delete-parent-device-post-provisioning"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500251 for _, previousState := range deleteDeviceTest.previousStates {
252 for _, device := range deleteDeviceTest.devices {
253 device.Root = true
khenaidoo442e7c72020-03-10 16:13:48 -0400254 t.Run(testName, func(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500255 handlers = transitionMap.GetTransitionHandler(device, previousState)
Andrea Campanella09400bd2020-04-02 11:58:04 +0200256 assert.Equal(t, 6, len(handlers))
khenaidoo442e7c72020-03-10 16:13:48 -0400257 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
258 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
259 }
260 })
261 }
262 }
khenaidoo59ef7be2019-06-21 12:40:28 -0400263
khenaidoo442e7c72020-03-10 16:13:48 -0400264 testName = "delete-child-device"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500265 for _, deviceState := range deleteDeviceTest.previousStates {
266 for _, device := range deleteDeviceTest.devices {
267 device.Root = false
khenaidoo442e7c72020-03-10 16:13:48 -0400268 t.Run(testName, func(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500269 handlers = transitionMap.GetTransitionHandler(device, deviceState)
khenaidoo442e7c72020-03-10 16:13:48 -0400270 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 }
khenaidoo0a822f92019-05-08 15:15:57 -0400277}
278
279func TestInvalidTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500280 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400283
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500284 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400287
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500288 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400291
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500292 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400295
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500296 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400299
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500300 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400303
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500304 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400307}
308
309func TestNoOpTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500310 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400313
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500314 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400317
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500318 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400321
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500322 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400325
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500326 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400329
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500330 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)
khenaidoo0a822f92019-05-08 15:15:57 -0400333}
khenaidoo442e7c72020-03-10 16:13:48 -0400334
335func 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}