blob: 464ca42bb222ec1cf94f3e02b0fe2a1cf7aea4c8 [file] [log] [blame]
khenaidoo0a822f92019-05-08 15:15:57 -04001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package core
17
18import (
khenaidoo442e7c72020-03-10 16:13:48 -040019 "fmt"
npujar1d86a522019-11-14 17:11:16 +053020 "reflect"
21 "testing"
22
23 "github.com/opencord/voltha-go/rw_core/coreif"
khenaidooab1f7bd2019-11-14 14:00:27 -050024 "github.com/opencord/voltha-go/rw_core/mocks"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo0a822f92019-05-08 15:15:57 -040026 "github.com/stretchr/testify/assert"
khenaidoo0a822f92019-05-08 15:15:57 -040027)
28
29var transitionMap *TransitionMap
npujar1d86a522019-11-14 17:11:16 +053030var tdm coreif.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040031
32type testDeviceManager struct {
khenaidooab1f7bd2019-11-14 14:00:27 -050033 mocks.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040034}
35
36func newTestDeviceManager() *testDeviceManager {
37 return &testDeviceManager{}
38}
39
khenaidoo0a822f92019-05-08 15:15:57 -040040func init() {
khenaidoo0a822f92019-05-08 15:15:57 -040041 tdm = newTestDeviceManager()
42 transitionMap = NewTransitionMap(tdm)
43}
44
serkant.uluderya2ae470f2020-01-21 11:13:09 -080045func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
khenaidoo0a822f92019-05-08 15:15:57 -040046 return &voltha.Device{
47 Id: "test",
48 Root: root,
49 AdminState: admin,
50 ConnectStatus: conn,
51 OperStatus: oper,
52 }
53}
54
Kent Hagermand9cc2e92019-11-04 13:28:15 -050055func getDeviceState(admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *DeviceState {
56 return &DeviceState{
57 Admin: admin,
58 Connection: conn,
59 Operational: oper,
60 }
61}
62
63func assertInvalidTransition(t *testing.T, device *voltha.Device, previousState *DeviceState) {
64 handlers := transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040065 assert.Equal(t, 1, len(handlers))
66 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
67}
68
Kent Hagermand9cc2e92019-11-04 13:28:15 -050069func assertNoOpTransition(t *testing.T, device *voltha.Device, previousState *DeviceState) {
70 handlers := transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040071 assert.Equal(t, 0, len(handlers))
72}
73
74func TestValidTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -050075 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
76 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
77 handlers := transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040078 assert.Equal(t, 1, len(handlers))
khenaidoo59ef7be2019-06-21 12:40:28 -040079 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -040080
Kent Hagermand9cc2e92019-11-04 13:28:15 -050081 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
82 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
83 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040084 assert.Equal(t, 1, len(handlers))
85 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
86
Kent Hagermand9cc2e92019-11-04 13:28:15 -050087 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
88 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
89 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040090 assert.Equal(t, 1, len(handlers))
91 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
92
Kent Hagermand9cc2e92019-11-04 13:28:15 -050093 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
94 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
95 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040096 assert.Equal(t, 1, len(handlers))
97 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
98
Kent Hagermand9cc2e92019-11-04 13:28:15 -050099 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
100 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
101 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400102 assert.Equal(t, 1, len(handlers))
103 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
104
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500105 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
106 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
107 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400108 assert.Equal(t, 1, len(handlers))
109 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
110
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500111 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
112 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
113 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400114 assert.Equal(t, 1, len(handlers))
115 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
116
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500117 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
118 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
119 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400120 assert.Equal(t, 1, len(handlers))
121 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
122
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500123 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
124 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
125 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400126 assert.Equal(t, 1, len(handlers))
127 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
128
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500129 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED)
130 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
131 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400132 assert.Equal(t, 1, len(handlers))
133 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
134
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500135 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
136 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
137 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400138 assert.Equal(t, 1, len(handlers))
139 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
140
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500141 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
142 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
143 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400144 assert.Equal(t, 1, len(handlers))
145 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
146
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500147 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
148 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
149 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400150 assert.Equal(t, 1, len(handlers))
151 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
152
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500153 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
154 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
155 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400156 assert.Equal(t, 1, len(handlers))
157 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
158
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500159 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
160 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
161 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400162 assert.Equal(t, 1, len(handlers))
163 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
164
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500165 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
166 device = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
167 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400168 assert.Equal(t, 1, len(handlers))
169 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
170
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500171 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
172 device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
173 handlers = transitionMap.GetTransitionHandler(device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400174 assert.Equal(t, 1, len(handlers))
175 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
Girish Gowdra408cd962020-03-11 14:31:31 -0700176
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500177 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
178 device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED)
179 handlers = transitionMap.GetTransitionHandler(device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700180 assert.Equal(t, 3, len(handlers))
181 assert.True(t, reflect.ValueOf(tdm.ChildDeviceLost).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
182 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
183 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
184
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500185 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
186 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
187 handlers = transitionMap.GetTransitionHandler(device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700188 assert.Equal(t, 4, len(handlers))
189 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
190 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
191 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
192 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
193
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500194 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
195 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
196 handlers = transitionMap.GetTransitionHandler(device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700197 assert.Equal(t, 1, len(handlers))
198 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -0400199
khenaidoo442e7c72020-03-10 16:13:48 -0400200 var deleteDeviceTest = struct {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500201 previousStates []*DeviceState
202 devices []*voltha.Device
khenaidoo442e7c72020-03-10 16:13:48 -0400203 expectedParentHandlers []TransitionHandler
204 expectedChildHandlers []TransitionHandler
205 }{
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500206 previousStates: []*DeviceState{
207 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
208 getDeviceState(voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
209 getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
210 getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
211 getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
212 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
213 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
khenaidoo442e7c72020-03-10 16:13:48 -0400214 },
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500215 devices: []*voltha.Device{
khenaidoo442e7c72020-03-10 16:13:48 -0400216 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
217 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
218 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
219 },
220 expectedParentHandlers: []TransitionHandler{
221 tdm.DisableAllChildDevices,
222 tdm.DeleteAllUNILogicalPorts,
223 tdm.DeleteAllChildDevices,
Andrea Campanella09400bd2020-04-02 11:58:04 +0200224 tdm.DeleteAllLogicalPorts,
khenaidoo442e7c72020-03-10 16:13:48 -0400225 tdm.DeleteLogicalDevice,
226 tdm.RunPostDeviceDelete,
227 },
228 expectedChildHandlers: []TransitionHandler{
229 tdm.ChildDeviceLost,
230 tdm.DeleteLogicalPorts,
231 tdm.RunPostDeviceDelete,
232 },
233 }
khenaidoo0a822f92019-05-08 15:15:57 -0400234
khenaidoo442e7c72020-03-10 16:13:48 -0400235 testName := "delete-parent-device-post-provisioning"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500236 for _, previousState := range deleteDeviceTest.previousStates {
237 for _, device := range deleteDeviceTest.devices {
238 device.Root = true
khenaidoo442e7c72020-03-10 16:13:48 -0400239 t.Run(testName, func(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500240 handlers = transitionMap.GetTransitionHandler(device, previousState)
Andrea Campanella09400bd2020-04-02 11:58:04 +0200241 assert.Equal(t, 6, len(handlers))
khenaidoo442e7c72020-03-10 16:13:48 -0400242 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
243 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
244 }
245 })
246 }
247 }
khenaidoo59ef7be2019-06-21 12:40:28 -0400248
khenaidoo442e7c72020-03-10 16:13:48 -0400249 testName = "delete-child-device"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500250 for _, deviceState := range deleteDeviceTest.previousStates {
251 for _, device := range deleteDeviceTest.devices {
252 device.Root = false
khenaidoo442e7c72020-03-10 16:13:48 -0400253 t.Run(testName, func(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500254 handlers = transitionMap.GetTransitionHandler(device, deviceState)
khenaidoo442e7c72020-03-10 16:13:48 -0400255 assert.Equal(t, 3, len(handlers))
256 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
257 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
258 }
259 })
260 }
261 }
khenaidoo0a822f92019-05-08 15:15:57 -0400262}
263
264func TestInvalidTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500265 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
266 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
267 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400268
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500269 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
270 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
271 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400272
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500273 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
274 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
275 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400276
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500277 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
278 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
279 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400280
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500281 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
282 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
283 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400284
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500285 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
286 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
287 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400288
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500289 previousState = getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
290 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
291 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400292}
293
294func TestNoOpTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500295 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
296 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
297 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400298
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500299 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
300 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
301 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400302
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500303 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
304 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
305 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400306
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500307 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
308 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
309 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400310
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500311 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
312 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
313 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400314
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500315 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
316 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
317 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400318}
khenaidoo442e7c72020-03-10 16:13:48 -0400319
320func TestMatch(t *testing.T) {
321 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch}
322 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch}
323 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
324}