blob: ee4986b57288c79fe664883611c8ef2440a944ec [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
Maninder0aabf0c2021-03-17 14:55:14 +053061func assertNoOpTransition(t *testing.T, device, prevDevice *voltha.Device, transientState voltha.DeviceTransientState_Types) {
62 handlers := transitionMap.getTransitionHandler(context.Background(), device, prevDevice, transientState,
63 transientState)
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
Maninder0aabf0c2021-03-17 14:55:14 +0530239 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
240 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
241 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
242 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
243 assert.Equal(t, 1, len(handlers))
244 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
245
246 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
247 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
248 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
249 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
250 assert.Equal(t, 1, len(handlers))
251 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
252
253 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
254 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
255 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
256 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
257 assert.Equal(t, 1, len(handlers))
258 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
259
260 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
261 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
262 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
263 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
264 assert.Equal(t, 1, len(handlers))
265 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
266
267 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
268 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
269 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
270 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
271 assert.Equal(t, 1, len(handlers))
272 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
273
Kent Hagerman6031aad2020-07-29 16:36:33 -0400274 var deleteDeviceTest = struct {
275 previousDevices []*voltha.Device
276 devices []*voltha.Device
277 expectedParentHandlers []transitionHandler
278 expectedChildHandlers []transitionHandler
279 }{
280 previousDevices: []*voltha.Device{
281 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
282 getDevice(false, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
283 getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
284 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
285 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
286 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
287 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
288 },
289 devices: []*voltha.Device{
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530290 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
291 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
292 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
Kent Hagerman6031aad2020-07-29 16:36:33 -0400293 },
294 expectedParentHandlers: []transitionHandler{
295 tdm.DeleteAllLogicalPorts,
296 tdm.DeleteAllChildDevices,
297 tdm.DeleteLogicalDevice,
298 tdm.RunPostDeviceDelete,
299 },
300 expectedChildHandlers: []transitionHandler{
301 tdm.ChildDeviceLost,
302 tdm.DeleteLogicalPorts,
303 tdm.RunPostDeviceDelete,
304 },
305 }
306
307 testName := "delete-parent-device-post-provisioning"
308 for _, previousDevice := range deleteDeviceTest.previousDevices {
309 for _, device := range deleteDeviceTest.devices {
310 device.Root = true
311 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530312 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
313 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400314 assert.Equal(t, 4, len(handlers))
315 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
316 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
317 }
318 })
319 }
320 }
321
322 testName = "delete-child-device"
323 for _, previousDevice := range deleteDeviceTest.previousDevices {
324 for _, device := range deleteDeviceTest.devices {
325 device.Root = false
326 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530327 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
328 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400329 assert.Equal(t, 3, len(handlers))
330 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
331 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
332 }
333 })
334 }
335 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530336
Kent Hagerman6031aad2020-07-29 16:36:33 -0400337}
338
339func TestInvalidTransitions(t *testing.T) {
340 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
341 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
342 assertInvalidTransition(t, device, previousDevice)
343
344 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
345 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
346 assertInvalidTransition(t, device, previousDevice)
347
348 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
349 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
350 assertInvalidTransition(t, device, previousDevice)
351
352 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400353 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
354 assertInvalidTransition(t, device, previousDevice)
355
356 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
357 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
358 assertInvalidTransition(t, device, previousDevice)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400359}
360
361func TestNoOpTransitions(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530362
Kent Hagerman6031aad2020-07-29 16:36:33 -0400363 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
364 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530365 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400366
367 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
368 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530369 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400370
371 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
372 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530373 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400374
375 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
376 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530377 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400378
379 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
380 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
Maninder0aabf0c2021-03-17 14:55:14 +0530381 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400382
383 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
384 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
Maninder0aabf0c2021-03-17 14:55:14 +0530385 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100386
387 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
388 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530389 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100390
391 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
392 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530393 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
394
395 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
396 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
397 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
398
399 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
400 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
401 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
402
403 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
404 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
405 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
406
407 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
408 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
409 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
410
411 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
412 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
413 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
414
415 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
416 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
417 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
418
419 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
420 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
421 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
422
423 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
424 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
425 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
426
427 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
428 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
429 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400430}
431
432func TestMatch(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530433 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch, transient: currWildcardMatch}
434 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch, transient: currWildcardMatch}
Kent Hagerman6031aad2020-07-29 16:36:33 -0400435 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
436}