blob: 8eecd7026d2e97737bd40001283f72458486b1e3 [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-lib-go/v3/pkg/log"
26 "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() {
khenaidooab1f7bd2019-11-14 14:00:27 -050042 if _, err := log.AddPackage(log.JSON, log.WarnLevel, nil); err != nil {
43 log.Fatal("failure-adding-package-core")
44 }
khenaidoo0a822f92019-05-08 15:15:57 -040045 tdm = newTestDeviceManager()
46 transitionMap = NewTransitionMap(tdm)
47}
48
serkant.uluderya2ae470f2020-01-21 11:13:09 -080049func getDevice(root bool, admin voltha.AdminState_Types, conn voltha.ConnectStatus_Types, oper voltha.OperStatus_Types) *voltha.Device {
khenaidoo0a822f92019-05-08 15:15:57 -040050 return &voltha.Device{
51 Id: "test",
52 Root: root,
53 AdminState: admin,
54 ConnectStatus: conn,
55 OperStatus: oper,
56 }
57}
58
59func assertInvalidTransition(t *testing.T, from *voltha.Device, to *voltha.Device) {
60 handlers := transitionMap.GetTransitionHandler(from, to)
61 assert.Equal(t, 1, len(handlers))
62 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
63}
64
65func assertNoOpTransition(t *testing.T, from *voltha.Device, to *voltha.Device) {
66 handlers := transitionMap.GetTransitionHandler(from, to)
67 assert.Equal(t, 0, len(handlers))
68}
69
70func TestValidTransitions(t *testing.T) {
khenaidoo59ef7be2019-06-21 12:40:28 -040071 from := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
72 to := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
khenaidoo0a822f92019-05-08 15:15:57 -040073 handlers := transitionMap.GetTransitionHandler(from, to)
74 assert.Equal(t, 1, len(handlers))
khenaidoo59ef7be2019-06-21 12:40:28 -040075 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -040076
khenaidoo59ef7be2019-06-21 12:40:28 -040077 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
78 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
79 handlers = transitionMap.GetTransitionHandler(from, to)
80 assert.Equal(t, 1, len(handlers))
81 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
82
83 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
84 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
85 handlers = transitionMap.GetTransitionHandler(from, to)
86 assert.Equal(t, 1, len(handlers))
87 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
88
89 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
90 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
91 handlers = transitionMap.GetTransitionHandler(from, to)
92 assert.Equal(t, 1, len(handlers))
93 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
94
95 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
khenaidoo0a822f92019-05-08 15:15:57 -040096 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
97 handlers = transitionMap.GetTransitionHandler(from, to)
98 assert.Equal(t, 1, len(handlers))
99 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
100
khenaidoo59ef7be2019-06-21 12:40:28 -0400101 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
khenaidoo0a822f92019-05-08 15:15:57 -0400102 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
103 handlers = transitionMap.GetTransitionHandler(from, to)
104 assert.Equal(t, 1, len(handlers))
105 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
106
khenaidoo59ef7be2019-06-21 12:40:28 -0400107 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
108 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
109 handlers = transitionMap.GetTransitionHandler(from, to)
110 assert.Equal(t, 1, len(handlers))
111 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
112
113 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_DISCOVERED)
114 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
115 handlers = transitionMap.GetTransitionHandler(from, to)
116 assert.Equal(t, 1, len(handlers))
117 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
118
119 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
120 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
121 handlers = transitionMap.GetTransitionHandler(from, to)
122 assert.Equal(t, 1, len(handlers))
123 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
124
125 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_DISCOVERED)
126 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
127 handlers = transitionMap.GetTransitionHandler(from, to)
128 assert.Equal(t, 1, len(handlers))
129 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
130
131 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
132 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
133 handlers = transitionMap.GetTransitionHandler(from, to)
134 assert.Equal(t, 1, len(handlers))
135 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
136
137 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
138 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
139 handlers = transitionMap.GetTransitionHandler(from, to)
140 assert.Equal(t, 1, len(handlers))
141 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
142
143 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
144 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
145 handlers = transitionMap.GetTransitionHandler(from, to)
146 assert.Equal(t, 1, len(handlers))
147 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
148
149 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
150 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
151 handlers = transitionMap.GetTransitionHandler(from, to)
152 assert.Equal(t, 1, len(handlers))
153 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
154
155 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVATING)
156 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_ACTIVE)
157 handlers = transitionMap.GetTransitionHandler(from, to)
158 assert.Equal(t, 1, len(handlers))
159 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
160
khenaidoo59ef7be2019-06-21 12:40:28 -0400161 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
162 to = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
163 handlers = transitionMap.GetTransitionHandler(from, to)
164 assert.Equal(t, 1, len(handlers))
165 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
166
167 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
168 to = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
169 handlers = transitionMap.GetTransitionHandler(from, to)
170 assert.Equal(t, 1, len(handlers))
171 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
khenaidoo0a822f92019-05-08 15:15:57 -0400172
khenaidoo442e7c72020-03-10 16:13:48 -0400173 var deleteDeviceTest = struct {
174 from []*voltha.Device
175 to []*voltha.Device
176 expectedParentHandlers []TransitionHandler
177 expectedChildHandlers []TransitionHandler
178 }{
179 from: []*voltha.Device{
180 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_FAILED),
181 getDevice(false, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
182 getDevice(false, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
183 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
184 getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE),
185 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_UNKNOWN),
186 getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
187 },
188 to: []*voltha.Device{
189 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN),
190 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN),
191 getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_FAILED),
192 },
193 expectedParentHandlers: []TransitionHandler{
194 tdm.DisableAllChildDevices,
195 tdm.DeleteAllUNILogicalPorts,
196 tdm.DeleteAllChildDevices,
197 tdm.DeleteLogicalDevice,
198 tdm.RunPostDeviceDelete,
199 },
200 expectedChildHandlers: []TransitionHandler{
201 tdm.ChildDeviceLost,
202 tdm.DeleteLogicalPorts,
203 tdm.RunPostDeviceDelete,
204 },
205 }
khenaidoo0a822f92019-05-08 15:15:57 -0400206
khenaidoo442e7c72020-03-10 16:13:48 -0400207 testName := "delete-parent-device-post-provisioning"
208 for _, from := range deleteDeviceTest.from {
209 from.Root = true
210 for _, to := range deleteDeviceTest.to {
211 to.Root = true
212 t.Run(testName, func(t *testing.T) {
213 handlers = transitionMap.GetTransitionHandler(from, to)
214 assert.Equal(t, 5, len(handlers))
215 for idx, expHandler := range deleteDeviceTest.expectedParentHandlers {
216 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
217 }
218 })
219 }
220 }
khenaidoo59ef7be2019-06-21 12:40:28 -0400221
khenaidoo442e7c72020-03-10 16:13:48 -0400222 testName = "delete-child-device"
223 for _, from := range deleteDeviceTest.from {
224 from.Root = false
225 for _, to := range deleteDeviceTest.to {
226 to.Root = false
227 t.Run(testName, func(t *testing.T) {
228 handlers = transitionMap.GetTransitionHandler(from, to)
229 assert.Equal(t, 3, len(handlers))
230 for idx, expHandler := range deleteDeviceTest.expectedChildHandlers {
231 assert.True(t, reflect.ValueOf(expHandler).Pointer() == reflect.ValueOf(handlers[idx]).Pointer())
232 }
233 })
234 }
235 }
khenaidoo0a822f92019-05-08 15:15:57 -0400236}
237
238func TestInvalidTransitions(t *testing.T) {
khenaidoo59ef7be2019-06-21 12:40:28 -0400239 from := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
240 to := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
khenaidoo0a822f92019-05-08 15:15:57 -0400241 assertInvalidTransition(t, from, to)
242
khenaidoo59ef7be2019-06-21 12:40:28 -0400243 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
244 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidoo0a822f92019-05-08 15:15:57 -0400245 assertInvalidTransition(t, from, to)
246
khenaidoo59ef7be2019-06-21 12:40:28 -0400247 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidoo0a822f92019-05-08 15:15:57 -0400248 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
249 assertInvalidTransition(t, from, to)
250
khenaidoo59ef7be2019-06-21 12:40:28 -0400251 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
khenaidoo0a822f92019-05-08 15:15:57 -0400252 to = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
253 assertInvalidTransition(t, from, to)
254
255 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
256 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
257 assertInvalidTransition(t, from, to)
258
259 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
260 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
261 assertInvalidTransition(t, from, to)
262
263 from = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
264 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
265 assertInvalidTransition(t, from, to)
266}
267
268func TestNoOpTransitions(t *testing.T) {
269 from := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
270 to := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
271 assertNoOpTransition(t, from, to)
272
273 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
274 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
275 assertNoOpTransition(t, from, to)
276
277 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
278 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
279 assertNoOpTransition(t, from, to)
280
281 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
282 to = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
283 assertNoOpTransition(t, from, to)
284
285 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
286 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
287 assertNoOpTransition(t, from, to)
288
289 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
290 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
291 assertNoOpTransition(t, from, to)
292}
khenaidoo442e7c72020-03-10 16:13:48 -0400293
294func TestMatch(t *testing.T) {
295 best := &match{admin: currPrevStateMatch, oper: currPrevStateMatch, conn: currPrevStateMatch}
296 m := &match{admin: currStateOnlyMatch, oper: currWildcardMatch, conn: currWildcardMatch}
297 fmt.Println(m.isBetterMatch(best), m.toInt(), best.toInt())
298}