blob: 458d17844a7bbb39f433adca7af121430447c043 [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 */
Kent Hagerman2b216042020-04-03 18:28:56 -040016package device
khenaidoo0a822f92019-05-08 15:15:57 -040017
18import (
Rohan Agrawal31f21802020-06-12 05:38:46 +000019 "context"
khenaidoo442e7c72020-03-10 16:13:48 -040020 "fmt"
npujar1d86a522019-11-14 17:11:16 +053021 "reflect"
22 "testing"
23
24 "github.com/opencord/voltha-go/rw_core/coreif"
khenaidooab1f7bd2019-11-14 14:00:27 -050025 "github.com/opencord/voltha-go/rw_core/mocks"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo0a822f92019-05-08 15:15:57 -040027 "github.com/stretchr/testify/assert"
khenaidoo0a822f92019-05-08 15:15:57 -040028)
29
30var transitionMap *TransitionMap
npujar1d86a522019-11-14 17:11:16 +053031var tdm coreif.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040032
33type testDeviceManager struct {
khenaidooab1f7bd2019-11-14 14:00:27 -050034 mocks.DeviceManager
khenaidoo0a822f92019-05-08 15:15:57 -040035}
36
37func newTestDeviceManager() *testDeviceManager {
38 return &testDeviceManager{}
39}
40
khenaidoo0a822f92019-05-08 15:15:57 -040041func init() {
khenaidoo0a822f92019-05-08 15:15:57 -040042 tdm = newTestDeviceManager()
43 transitionMap = NewTransitionMap(tdm)
44}
45
serkant.uluderya2ae470f2020-01-21 11:13:09 -080046func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
khenaidoo0a822f92019-05-08 15:15:57 -040047 return &voltha.Device{
48 Id: "test",
49 Root: root,
50 AdminState: admin,
51 ConnectStatus: conn,
52 OperStatus: oper,
53 }
54}
55
Kent Hagerman2b216042020-04-03 18:28:56 -040056func getDeviceState(admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *deviceState {
57 return &deviceState{
Kent Hagermand9cc2e92019-11-04 13:28:15 -050058 Admin: admin,
59 Connection: conn,
60 Operational: oper,
61 }
62}
63
Kent Hagerman2b216042020-04-03 18:28:56 -040064func assertInvalidTransition(t *testing.T, device *voltha.Device, previousState *deviceState) {
Rohan Agrawal31f21802020-06-12 05:38:46 +000065 handlers := transitionMap.GetTransitionHandler(context.Background(), device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040066 assert.Equal(t, 1, len(handlers))
67 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
68}
69
Kent Hagerman2b216042020-04-03 18:28:56 -040070func assertNoOpTransition(t *testing.T, device *voltha.Device, previousState *deviceState) {
Rohan Agrawal31f21802020-06-12 05:38:46 +000071 handlers := transitionMap.GetTransitionHandler(context.Background(), device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040072 assert.Equal(t, 0, len(handlers))
73}
74
75func TestValidTransitions(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +000076 ctx := context.Background()
Kent Hagermand9cc2e92019-11-04 13:28:15 -050077 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
78 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +000079 handlers := transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -040080 assert.Equal(t, 1, len(handlers))
khenaidoo59ef7be2019-06-21 12:40:28 -040081 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -040082
Kent Hagermand9cc2e92019-11-04 13:28:15 -050083 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
84 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +000085 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040086 assert.Equal(t, 1, len(handlers))
87 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
88
Kent Hagermand9cc2e92019-11-04 13:28:15 -050089 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
90 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +000091 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040092 assert.Equal(t, 1, len(handlers))
93 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
94
Kent Hagermand9cc2e92019-11-04 13:28:15 -050095 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
96 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +000097 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -040098 assert.Equal(t, 1, len(handlers))
99 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
100
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500101 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
102 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000103 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400104 assert.Equal(t, 1, len(handlers))
105 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
106
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500107 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
108 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000109 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400110 assert.Equal(t, 1, len(handlers))
111 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
112
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500113 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
114 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000115 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400116 assert.Equal(t, 1, len(handlers))
117 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
118
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500119 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
120 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000121 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400122 assert.Equal(t, 1, len(handlers))
123 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
124
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500125 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
126 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000127 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400128 assert.Equal(t, 1, len(handlers))
129 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
130
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500131 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED)
132 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000133 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400134 assert.Equal(t, 1, len(handlers))
135 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
136
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500137 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
138 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400140 assert.Equal(t, 1, len(handlers))
141 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
142
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500143 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
144 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000145 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400146 assert.Equal(t, 1, len(handlers))
147 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
148
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500149 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
150 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000151 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400152 assert.Equal(t, 1, len(handlers))
153 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
154
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500155 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
156 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000157 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400158 assert.Equal(t, 1, len(handlers))
159 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
160
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500161 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
162 device = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000163 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400164 assert.Equal(t, 1, len(handlers))
165 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
166
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500167 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
168 device = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000169 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400170 assert.Equal(t, 1, len(handlers))
171 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
172
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500173 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
174 device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000175 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
khenaidoo59ef7be2019-06-21 12:40:28 -0400176 assert.Equal(t, 1, len(handlers))
177 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
Girish Gowdra408cd962020-03-11 14:31:31 -0700178
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500179 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
180 device = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000181 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700182 assert.Equal(t, 3, len(handlers))
183 assert.True(t, reflect.ValueOf(tdm.ChildDeviceLost).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
184 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
185 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
186
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500187 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
188 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700190 assert.Equal(t, 4, len(handlers))
191 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
Chaitrashree G S3bbfa352020-05-02 02:32:07 -0400192 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
193 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
Girish Gowdra408cd962020-03-11 14:31:31 -0700194 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
195
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500196 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
197 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000198 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Girish Gowdra408cd962020-03-11 14:31:31 -0700199 assert.Equal(t, 1, len(handlers))
200 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -0400201
Chaitrashree G S7849b322020-03-29 19:25:49 -0400202 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
203 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000204 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Chaitrashree G S7849b322020-03-29 19:25:49 -0400205 assert.Equal(t, 4, len(handlers))
206 assert.True(t, reflect.ValueOf(tdm.DeleteAllLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
Chaitrashree G S3bbfa352020-05-02 02:32:07 -0400207 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
208 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
Chaitrashree G S7849b322020-03-29 19:25:49 -0400209 assert.True(t, reflect.ValueOf(tdm.DeleteAllDeviceFlows).Pointer() == reflect.ValueOf(handlers[3]).Pointer())
210
211 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN)
212 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000213 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Chaitrashree G S7849b322020-03-29 19:25:49 -0400214 assert.Equal(t, 1, len(handlers))
215 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
216
khenaidoo442e7c72020-03-10 16:13:48 -0400217 var deleteDeviceTest = struct {
Kent Hagerman2b216042020-04-03 18:28:56 -0400218 previousStates []*deviceState
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500219 devices []*voltha.Device
khenaidoo442e7c72020-03-10 16:13:48 -0400220 expectedParentHandlers []TransitionHandler
221 expectedChildHandlers []TransitionHandler
222 }{
Kent Hagerman2b216042020-04-03 18:28:56 -0400223 previousStates: []*deviceState{
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500224 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
225 getDeviceState(voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
226 getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
227 getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
228 getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
229 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
230 getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
khenaidoo442e7c72020-03-10 16:13:48 -0400231 },
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500232 devices: []*voltha.Device{
khenaidoo442e7c72020-03-10 16:13:48 -0400233 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
234 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
235 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
236 },
237 expectedParentHandlers: []TransitionHandler{
Andrea Campanella09400bd2020-04-02 11:58:04 +0200238 tdm.DeleteAllLogicalPorts,
Chaitrashree G S3bbfa352020-05-02 02:32:07 -0400239 tdm.DeleteAllChildDevices,
khenaidoo442e7c72020-03-10 16:13:48 -0400240 tdm.DeleteLogicalDevice,
241 tdm.RunPostDeviceDelete,
242 },
243 expectedChildHandlers: []TransitionHandler{
244 tdm.ChildDeviceLost,
245 tdm.DeleteLogicalPorts,
246 tdm.RunPostDeviceDelete,
247 },
248 }
khenaidoo0a822f92019-05-08 15:15:57 -0400249
khenaidoo442e7c72020-03-10 16:13:48 -0400250 testName := "delete-parent-device-post-provisioning"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500251 for _, previousState := range deleteDeviceTest.previousStates {
252 for _, device := range deleteDeviceTest.devices {
253 device.Root = true
khenaidoo442e7c72020-03-10 16:13:48 -0400254 t.Run(testName, func(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000255 handlers = transitionMap.GetTransitionHandler(ctx, device, previousState)
Chaitrashree G S3bbfa352020-05-02 02:32:07 -0400256 assert.Equal(t, 4, len(handlers))
khenaidoo442e7c72020-03-10 16:13:48 -0400257 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
258 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
259 }
260 })
261 }
262 }
khenaidoo59ef7be2019-06-21 12:40:28 -0400263
khenaidoo442e7c72020-03-10 16:13:48 -0400264 testName = "delete-child-device"
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500265 for _, deviceState := range deleteDeviceTest.previousStates {
266 for _, device := range deleteDeviceTest.devices {
267 device.Root = false
khenaidoo442e7c72020-03-10 16:13:48 -0400268 t.Run(testName, func(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000269 handlers = transitionMap.GetTransitionHandler(ctx, device, deviceState)
khenaidoo442e7c72020-03-10 16:13:48 -0400270 assert.Equal(t, 3, len(handlers))
271 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
272 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
273 }
274 })
275 }
276 }
khenaidoo0a822f92019-05-08 15:15:57 -0400277}
278
279func TestInvalidTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500280 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
281 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
282 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400283
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500284 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
285 device = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
286 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400287
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500288 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
289 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
290 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400291
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500292 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
293 device = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
294 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400295
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500296 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
297 device = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
298 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400299
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500300 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
301 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
302 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400303
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500304 previousState = getDeviceState(voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
305 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
306 assertInvalidTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400307}
308
309func TestNoOpTransitions(t *testing.T) {
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500310 previousState := getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
311 device := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
312 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400313
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500314 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
315 device = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
316 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400317
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500318 previousState = getDeviceState(voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
319 device = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
320 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400321
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500322 previousState = getDeviceState(voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
323 device = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
324 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400325
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500326 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
327 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
328 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400329
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500330 previousState = getDeviceState(voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
331 device = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
332 assertNoOpTransition(t, device, previousState)
khenaidoo0a822f92019-05-08 15:15:57 -0400333}
khenaidoo442e7c72020-03-10 16:13:48 -0400334
335func TestMatch(t *testing.T) {
336 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch}
337 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch}
338 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
339}