blob: a085f97f5ea8db4c20cf2031f3c2d8dad407e54d [file] [log] [blame]
Kent Hagerman6031aad2020-07-29 16:36:33 -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 */
16package state
17
18import (
19 "context"
20 "fmt"
21 "reflect"
22 "testing"
23
Maninderdfadc982020-10-28 14:04:33 +053024 "github.com/opencord/voltha-protos/v4/go/voltha"
Kent Hagerman6031aad2020-07-29 16:36:33 -040025 "github.com/stretchr/testify/assert"
26)
27
28var transitionMap *TransitionMap
29var tdm DeviceManager
30
31type testDeviceManager struct {
32 DeviceManager
33}
34
35func newTestDeviceManager() *testDeviceManager {
36 return &testDeviceManager{}
37}
38
39func init() {
40 tdm = newTestDeviceManager()
41 transitionMap = NewTransitionMap(tdm)
42}
43
44func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
45 return &voltha.Device{
46 Id: "test",
47 Root: root,
48 AdminState: admin,
49 ConnectStatus: conn,
50 OperStatus: oper,
51 }
52}
53
54func assertInvalidTransition(t *testing.T, device, prevDevice *voltha.Device) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053055 handlers := transitionMap.getTransitionHandler(context.Background(), device, prevDevice, voltha.DeviceTransientState_NONE,
56 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040057 assert.Equal(t, 1, len(handlers))
58 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
59}
60
61func assertNoOpTransition(t *testing.T, device, prevDevice *voltha.Device) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053062 handlers := transitionMap.getTransitionHandler(context.Background(), device, prevDevice, voltha.DeviceTransientState_NONE,
63 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040064 assert.Equal(t, 0, len(handlers))
65}
66
67func TestValidTransitions(t *testing.T) {
68 ctx := context.Background()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053069
Kent Hagerman6031aad2020-07-29 16:36:33 -040070 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
71 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053072 handlers := transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
73 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040074 assert.Equal(t, 1, len(handlers))
75 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
76
77 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
78 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053079 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
80 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040081 assert.Equal(t, 1, len(handlers))
82 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
83
84 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
85 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053086 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
87 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040088 assert.Equal(t, 1, len(handlers))
89 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
90
91 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
92 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053093 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
94 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040095 assert.Equal(t, 1, len(handlers))
96 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
97
98 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
99 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530100 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
101 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400102 assert.Equal(t, 1, len(handlers))
103 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
104
105 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
106 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530107 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
108 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400109 assert.Equal(t, 1, len(handlers))
110 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
111
112 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
113 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530114 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
115 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400116 assert.Equal(t, 1, len(handlers))
117 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
118
119 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
120 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530121 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
122 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400123 assert.Equal(t, 1, len(handlers))
124 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
125
126 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
127 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530128 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
129 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400130 assert.Equal(t, 1, len(handlers))
131 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
132
133 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED)
134 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530135 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
136 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400137 assert.Equal(t, 1, len(handlers))
138 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
139
140 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
141 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530142 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
143 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400144 assert.Equal(t, 1, len(handlers))
145 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
146
147 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
148 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530149 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
150 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400151 assert.Equal(t, 1, len(handlers))
152 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
153
154 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
155 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530156 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
157 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400158 assert.Equal(t, 1, len(handlers))
159 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
160
161 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
162 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530163 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
164 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400165 assert.Equal(t, 1, len(handlers))
166 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
167
168 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
169 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530170 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
171 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400172 assert.Equal(t, 1, len(handlers))
173 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
174
175 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530176 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
177 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
178 voltha.DeviceTransientState_ANY)
179 assert.Equal(t, 1, len(handlers))
180 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
181
182 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
183 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
184 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE,
185 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400186 assert.Equal(t, 1, len(handlers))
187 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
188
189 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530190 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
191 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
192 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400193 assert.Equal(t, 1, len(handlers))
194 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
195
196 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530197 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED)
198 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
199 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400200 assert.Equal(t, 3, len(handlers))
201 assert.True(t, reflect.ValueOf(tdm.ChildDeviceLost).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
202 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
203 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
204
205 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
206 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530207 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
208 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400209 assert.Equal(t, 4, len(handlers))
210 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
211 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
212 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
213 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
214
215 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
216 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530217 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
218 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400219 assert.Equal(t, 1, len(handlers))
220 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
221
222 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
223 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530224 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
225 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400226 assert.Equal(t, 4, len(handlers))
227 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
228 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
229 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
230 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
231
232 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
233 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530234 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_NONE,
235 voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400236 assert.Equal(t, 1, len(handlers))
237 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
238
239 var deleteDeviceTest = struct {
240 previousDevices []*voltha.Device
241 devices []*voltha.Device
242 expectedParentHandlers []transitionHandler
243 expectedChildHandlers []transitionHandler
244 }{
245 previousDevices: []*voltha.Device{
246 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
247 getDevice(false, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
248 getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
249 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
250 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
251 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
252 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
253 },
254 devices: []*voltha.Device{
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530255 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
256 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
257 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
Kent Hagerman6031aad2020-07-29 16:36:33 -0400258 },
259 expectedParentHandlers: []transitionHandler{
260 tdm.DeleteAllLogicalPorts,
261 tdm.DeleteAllChildDevices,
262 tdm.DeleteLogicalDevice,
263 tdm.RunPostDeviceDelete,
264 },
265 expectedChildHandlers: []transitionHandler{
266 tdm.ChildDeviceLost,
267 tdm.DeleteLogicalPorts,
268 tdm.RunPostDeviceDelete,
269 },
270 }
271
272 testName := "delete-parent-device-post-provisioning"
273 for _, previousDevice := range deleteDeviceTest.previousDevices {
274 for _, device := range deleteDeviceTest.devices {
275 device.Root = true
276 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530277 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
278 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400279 assert.Equal(t, 4, len(handlers))
280 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
281 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
282 }
283 })
284 }
285 }
286
287 testName = "delete-child-device"
288 for _, previousDevice := range deleteDeviceTest.previousDevices {
289 for _, device := range deleteDeviceTest.devices {
290 device.Root = false
291 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530292 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
293 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400294 assert.Equal(t, 3, len(handlers))
295 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
296 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
297 }
298 })
299 }
300 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530301
Kent Hagerman6031aad2020-07-29 16:36:33 -0400302}
303
304func TestInvalidTransitions(t *testing.T) {
305 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
306 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
307 assertInvalidTransition(t, device, previousDevice)
308
309 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
310 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
311 assertInvalidTransition(t, device, previousDevice)
312
313 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
314 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
315 assertInvalidTransition(t, device, previousDevice)
316
317 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
318 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
319 assertInvalidTransition(t, device, previousDevice)
320
321 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
322 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
323 assertInvalidTransition(t, device, previousDevice)
324
325 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
326 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
327 assertInvalidTransition(t, device, previousDevice)
328
329 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
330 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
331 assertInvalidTransition(t, device, previousDevice)
332}
333
334func TestNoOpTransitions(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530335
Kent Hagerman6031aad2020-07-29 16:36:33 -0400336 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
337 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
338 assertNoOpTransition(t, device, previousDevice)
339
340 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
341 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
342 assertNoOpTransition(t, device, previousDevice)
343
344 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
345 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
346 assertNoOpTransition(t, device, previousDevice)
347
348 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
349 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
350 assertNoOpTransition(t, device, previousDevice)
351
352 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
353 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
354 assertNoOpTransition(t, device, previousDevice)
355
356 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
357 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
358 assertNoOpTransition(t, device, previousDevice)
359}
360
361func TestMatch(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530362 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch, transient: currWildcardMatch}
363 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch, transient: currWildcardMatch}
Kent Hagerman6031aad2020-07-29 16:36:33 -0400364 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
365}