blob: ad535264de81b5d024ff762f32c7c80cf0f04ca1 [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
Maninder581cf4b2021-06-16 22:42:07 +0530274 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
275 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
276 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
277 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
278 assert.Equal(t, 1, len(handlers))
279 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
280
281 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
282 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
283 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
284 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
285 assert.Equal(t, 1, len(handlers))
286 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
287
288 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
289 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING_FAILED)
290 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
291 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
292 assert.Equal(t, 1, len(handlers))
293 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
294
295 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
296 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING_FAILED)
297 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
298 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
299 assert.Equal(t, 1, len(handlers))
300 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
301
302 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
303 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_RECONCILING_FAILED)
304 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS,
305 voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
306 assert.Equal(t, 1, len(handlers))
307 assert.True(t, reflect.ValueOf(tdm.ReconcilingCleanup).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
308
Kent Hagerman6031aad2020-07-29 16:36:33 -0400309 var deleteDeviceTest = struct {
310 previousDevices []*voltha.Device
311 devices []*voltha.Device
312 expectedParentHandlers []transitionHandler
313 expectedChildHandlers []transitionHandler
314 }{
315 previousDevices: []*voltha.Device{
316 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
317 getDevice(false, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
318 getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
319 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
320 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
321 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
322 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
323 },
324 devices: []*voltha.Device{
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530325 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
326 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
327 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
Kent Hagerman6031aad2020-07-29 16:36:33 -0400328 },
329 expectedParentHandlers: []transitionHandler{
330 tdm.DeleteAllLogicalPorts,
331 tdm.DeleteAllChildDevices,
332 tdm.DeleteLogicalDevice,
333 tdm.RunPostDeviceDelete,
334 },
335 expectedChildHandlers: []transitionHandler{
336 tdm.ChildDeviceLost,
337 tdm.DeleteLogicalPorts,
338 tdm.RunPostDeviceDelete,
339 },
340 }
341
342 testName := "delete-parent-device-post-provisioning"
343 for _, previousDevice := range deleteDeviceTest.previousDevices {
344 for _, device := range deleteDeviceTest.devices {
345 device.Root = true
346 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530347 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
348 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400349 assert.Equal(t, 4, len(handlers))
350 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
351 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
352 }
353 })
354 }
355 }
356
357 testName = "delete-child-device"
358 for _, previousDevice := range deleteDeviceTest.previousDevices {
359 for _, device := range deleteDeviceTest.devices {
360 device.Root = false
361 t.Run(testName, func(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530362 handlers = transitionMap.getTransitionHandler(ctx, device, previousDevice, voltha.DeviceTransientState_FORCE_DELETING,
363 voltha.DeviceTransientState_ANY)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400364 assert.Equal(t, 3, len(handlers))
365 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
366 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
367 }
368 })
369 }
370 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530371
Kent Hagerman6031aad2020-07-29 16:36:33 -0400372}
373
374func TestInvalidTransitions(t *testing.T) {
375 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
376 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
377 assertInvalidTransition(t, device, previousDevice)
378
379 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
380 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
381 assertInvalidTransition(t, device, previousDevice)
382
383 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
384 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
385 assertInvalidTransition(t, device, previousDevice)
386
387 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400388 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
389 assertInvalidTransition(t, device, previousDevice)
390
391 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
392 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
393 assertInvalidTransition(t, device, previousDevice)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400394}
395
396func TestNoOpTransitions(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530397
Kent Hagerman6031aad2020-07-29 16:36:33 -0400398 previousDevice := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
399 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530400 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400401
402 previousDevice = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
403 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530404 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400405
406 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
407 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530408 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400409
410 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
411 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530412 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400413
414 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
415 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
Maninder0aabf0c2021-03-17 14:55:14 +0530416 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400417
418 previousDevice = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
419 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
Maninder0aabf0c2021-03-17 14:55:14 +0530420 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100421
422 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
423 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530424 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
Andrea Campanella3614a922021-02-25 12:40:42 +0100425
426 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
427 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Maninder0aabf0c2021-03-17 14:55:14 +0530428 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_NONE)
429
430 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
431 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
432 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
433
434 previousDevice = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
435 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
436 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
437
438 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
439 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
440 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
441
442 previousDevice = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
443 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
444 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
445
446 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
447 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
448 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
449
450 previousDevice = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
451 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
452 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
453
454 previousDevice = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
455 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
456 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
457
458 previousDevice = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
459 device = getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_RECONCILING)
460 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
461
462 previousDevice = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
463 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_RECONCILING)
464 assertNoOpTransition(t, device, previousDevice, voltha.DeviceTransientState_RECONCILE_IN_PROGRESS)
Kent Hagerman6031aad2020-07-29 16:36:33 -0400465}
466
467func TestMatch(t *testing.T) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530468 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch, transient: currWildcardMatch}
469 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch, transient: currWildcardMatch}
Kent Hagerman6031aad2020-07-29 16:36:33 -0400470 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
471}