blob: 5065a60aed88321b3435fdd7d0fc6bf67b151a3b [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
khenaidood948f772021-08-11 17:49:24 -040024 "github.com/opencord/voltha-protos/v5/go/core"
25
26 "github.com/opencord/voltha-protos/v5/go/voltha"
Kent Hagerman6031aad2020-07-29 16:36:33 -040027 "github.com/stretchr/testify/assert"
28)
29
30var transitionMap *TransitionMap
31var tdm DeviceManager
32
33type testDeviceManager struct {
34 DeviceManager
35}
36
37func newTestDeviceManager() *testDeviceManager {
38 return &testDeviceManager{}
39}
40
41func init() {
42 tdm = newTestDeviceManager()
43 transitionMap = NewTransitionMap(tdm)
44}
45
46func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
47 return &voltha.Device{
48 Id: "test",
49 Root: root,
50 AdminState: admin,
51 ConnectStatus: conn,
52 OperStatus: oper,
53 }
54}
55
56func assertInvalidTransition(t *testing.T, device, prevDevice *voltha.Device) {
khenaidood948f772021-08-11 17:49:24 -040057 handlers := transitionMap.getTransitionHandler(context.Background(), device, prevDevice, core.DeviceTransientState_NONE,
58 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040059 assert.Equal(t, 1, len(handlers))
60 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
61}
62
khenaidood948f772021-08-11 17:49:24 -040063func assertNoOpTransition(t *testing.T, device, prevDevice *voltha.Device, transientState core.DeviceTransientState_Types) {
Maninder0aabf0c2021-03-17 14:55:14 +053064 handlers := transitionMap.getTransitionHandler(context.Background(), device, prevDevice, transientState,
65 transientState)
Kent Hagerman6031aad2020-07-29 16:36:33 -040066 assert.Equal(t, 0, len(handlers))
67}
68
69func TestValidTransitions(t *testing.T) {
70 ctx := context.Background()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053071
Kent Hagerman6031aad2020-07-29 16:36:33 -040072 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
73 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -040074 handlers := transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
75 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040076 assert.Equal(t, 1, len(handlers))
77 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
78
79 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
80 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -040081 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
82 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040083 assert.Equal(t, 1, len(handlers))
84 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
85
86 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
87 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -040088 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
89 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040090 assert.Equal(t, 1, len(handlers))
91 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
92
93 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
94 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -040095 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
96 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -040097 assert.Equal(t, 1, len(handlers))
98 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
99
100 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
101 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400102 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
103 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400104 assert.Equal(t, 1, len(handlers))
105 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
106
107 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
108 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400109 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
110 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400111 assert.Equal(t, 1, len(handlers))
112 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
113
114 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
115 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400116 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
117 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400118 assert.Equal(t, 1, len(handlers))
119 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
120
121 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
122 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400123 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
124 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400125 assert.Equal(t, 1, len(handlers))
126 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
127
128 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
129 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400130 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
131 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400132 assert.Equal(t, 1, len(handlers))
133 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
134
135 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED)
136 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400137 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
138 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400139 assert.Equal(t, 1, len(handlers))
140 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
141
142 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
143 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400144 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
145 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400146 assert.Equal(t, 1, len(handlers))
147 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
148
149 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
150 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400151 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
152 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400153 assert.Equal(t, 1, len(handlers))
154 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
155
156 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
157 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400158 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
159 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400160 assert.Equal(t, 1, len(handlers))
161 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
162
163 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
164 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400165 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
166 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400167 assert.Equal(t, 1, len(handlers))
168 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
169
170 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
171 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400172 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
173 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400174 assert.Equal(t, 1, len(handlers))
175 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
176
177 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530178 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400179 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_FORCE_DELETING,
180 core.DeviceTransientState_ANY)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530181 assert.Equal(t, 1, len(handlers))
182 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
183
184 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
185 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400186 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE,
187 core.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400188 assert.Equal(t, 1, len(handlers))
189 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
190
191 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530192 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400193 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_FORCE_DELETING,
194 core.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400195 assert.Equal(t, 1, len(handlers))
196 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
197
198 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530199 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400200 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_FORCE_DELETING,
201 core.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400202 assert.Equal(t, 3, len(handlers))
203 assert.True(t, reflect.ValueOf(tdm.ChildDeviceLost).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
204 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
205 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
206
207 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
208 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400209 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
210 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400211 assert.Equal(t, 4, len(handlers))
212 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
213 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
214 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
215 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
216
217 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
218 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400219 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
220 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400221 assert.Equal(t, 1, len(handlers))
222 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
223
224 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
225 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400226 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
227 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400228 assert.Equal(t, 4, len(handlers))
229 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
230 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
231 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
232 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
233
234 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
235 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400236 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_NONE,
237 core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400238 assert.Equal(t, 1, len(handlers))
239 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
240
Maninder0aabf0c2021-03-17 14:55:14 +0530241 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
242 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400243 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
244 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530245 assert.Equal(t, 1, len(handlers))
246 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
247
248 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
249 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400250 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
251 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530252 assert.Equal(t, 1, len(handlers))
253 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
254
255 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
256 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
khenaidood948f772021-08-11 17:49:24 -0400257 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
258 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530259 assert.Equal(t, 1, len(handlers))
260 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
261
262 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
263 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400264 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
265 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530266 assert.Equal(t, 1, len(handlers))
267 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
268
269 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
270 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400271 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
272 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530273 assert.Equal(t, 1, len(handlers))
274 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
275
Maninder581cf4b2021-06-16 22:42:07 +0530276 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
277 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400278 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
279 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder581cf4b2021-06-16 22:42:07 +0530280 assert.Equal(t, 1, len(handlers))
281 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
282
283 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
284 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400285 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
286 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder581cf4b2021-06-16 22:42:07 +0530287 assert.Equal(t, 1, len(handlers))
288 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
289
290 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
291 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400292 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
293 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder581cf4b2021-06-16 22:42:07 +0530294 assert.Equal(t, 1, len(handlers))
295 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
296
297 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
298 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400299 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
300 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder581cf4b2021-06-16 22:42:07 +0530301 assert.Equal(t, 1, len(handlers))
302 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
303
304 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
305 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_RECONCILING_FAILED)
khenaidood948f772021-08-11 17:49:24 -0400306 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS,
307 core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder581cf4b2021-06-16 22:42:07 +0530308 assert.Equal(t, 1, len(handlers))
309 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
310
Kent Hagerman6031aad2020-07-29 16:36:33 -0400311 var deleteDeviceTest = struct {
312 previousDevices []*voltha.Device
313 devices []*voltha.Device
314 expectedParentHandlers []transitionHandler
315 expectedChildHandlers []transitionHandler
316 }{
317 previousDevices: []*voltha.Device{
318 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
319 getDevice(false, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
320 getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
321 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
322 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
323 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
324 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
325 },
326 devices: []*voltha.Device{
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530327 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
328 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
329 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
Kent Hagerman6031aad2020-07-29 16:36:33 -0400330 },
331 expectedParentHandlers: []transitionHandler{
332 tdm.DeleteAllLogicalPorts,
333 tdm.DeleteAllChildDevices,
334 tdm.DeleteLogicalDevice,
335 tdm.RunPostDeviceDelete,
336 },
337 expectedChildHandlers: []transitionHandler{
338 tdm.ChildDeviceLost,
339 tdm.DeleteLogicalPorts,
340 tdm.RunPostDeviceDelete,
341 },
342 }
343
344 testName := "delete-parent-device-post-provisioning"
345 for _, previousDevice := range deleteDeviceTest.previousDevices {
346 for _, device := range deleteDeviceTest.devices {
347 device.Root = true
348 t.Run(testName, func(t *testing.T) {
khenaidood948f772021-08-11 17:49:24 -0400349 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_FORCE_DELETING,
350 core.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400351 assert.Equal(t, 4, len(handlers))
352 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
353 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
354 }
355 })
356 }
357 }
358
359 testName = "delete-child-device"
360 for _, previousDevice := range deleteDeviceTest.previousDevices {
361 for _, device := range deleteDeviceTest.devices {
362 device.Root = false
363 t.Run(testName, func(t *testing.T) {
khenaidood948f772021-08-11 17:49:24 -0400364 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, core.DeviceTransientState_FORCE_DELETING,
365 core.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400366 assert.Equal(t, 3, len(handlers))
367 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
368 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
369 }
370 })
371 }
372 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530373
Kent Hagerman6031aad2020-07-29 16:36:33 -0400374}
375
376func TestInvalidTransitions(t *testing.T) {
377 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
378 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
379 assertInvalidTransition(t, device, previousDevice)
380
381 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
382 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
383 assertInvalidTransition(t, device, previousDevice)
384
385 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
386 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
387 assertInvalidTransition(t, device, previousDevice)
388
389 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400390 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
391 assertInvalidTransition(t, device, previousDevice)
392
393 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
394 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
395 assertInvalidTransition(t, device, previousDevice)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400396}
397
398func TestNoOpTransitions(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530399
Kent Hagerman6031aad2020-07-29 16:36:33 -0400400 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
401 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400402 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400403
404 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
405 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400406 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400407
408 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
409 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400410 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400411
412 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
413 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400414 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400415
416 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
417 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
khenaidood948f772021-08-11 17:49:24 -0400418 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400419
420 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
421 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
khenaidood948f772021-08-11 17:49:24 -0400422 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100423
424 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
425 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400426 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100427
428 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
429 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
khenaidood948f772021-08-11 17:49:24 -0400430 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_NONE)
Maninder0aabf0c2021-03-17 14:55:14 +0530431
432 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
433 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400434 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530435
436 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
437 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400438 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530439
440 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
441 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400442 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530443
444 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
445 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400446 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530447
448 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
449 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400450 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530451
452 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
453 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400454 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530455
456 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
457 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400458 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530459
460 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
461 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400462 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Maninder0aabf0c2021-03-17 14:55:14 +0530463
464 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
465 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
khenaidood948f772021-08-11 17:49:24 -0400466 assertNoOpTransition(t, device, previousDevice, core.DeviceTransientState_RECONCILE_IN_PROGRESS)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400467}
468
469func TestMatch(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530470 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch, transient: currWildcardMatch}
471 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch, transient: currWildcardMatch}
Kent Hagerman6031aad2020-07-29 16:36:33 -0400472 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
473}