blob: c89c6843b0c28977fe5ddc634ccefa51c187123d [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 (
19 "github.com/opencord/voltha-go/common/log"
20 "github.com/opencord/voltha-go/rw_core/coreIf"
21 "github.com/opencord/voltha-protos/go/voltha"
22 "github.com/stretchr/testify/assert"
23 "reflect"
24 "testing"
25)
26
27var transitionMap *TransitionMap
28var tdm coreIf.DeviceManager
29
30type testDeviceManager struct {
31}
32
33func newTestDeviceManager() *testDeviceManager {
34 return &testDeviceManager{}
35}
36
37func (tdm *testDeviceManager) GetDevice(string) (*voltha.Device, error) {
38 return nil, nil
39}
40
41func (tdm *testDeviceManager) IsRootDevice(string) (bool, error) {
42 return false, nil
43}
44
45func (tdm *testDeviceManager) NotifyInvalidTransition(pto *voltha.Device) error {
46 return nil
47}
48
49func (tdm *testDeviceManager) SetAdminStateToEnable(to *voltha.Device) error {
50 return nil
51}
52
53func (tdm *testDeviceManager) CreateLogicalDevice(to *voltha.Device) error {
54 return nil
55}
56
57func (tdm *testDeviceManager) SetupUNILogicalPorts(to *voltha.Device) error {
58 return nil
59}
60
61func (tdm *testDeviceManager) DisableAllChildDevices(to *voltha.Device) error {
62 return nil
63}
64
65func (tdm *testDeviceManager) DeleteLogicalDevice(to *voltha.Device) error {
66 return nil
67}
68
69func (tdm *testDeviceManager) DeleteLogicalPorts(to *voltha.Device) error {
70 return nil
71}
72
73func (tdm *testDeviceManager) DeleteAllChildDevices(to *voltha.Device) error {
74 return nil
75}
76
77func (tdm *testDeviceManager) RunPostDeviceDelete(to *voltha.Device) error {
78 return nil
79}
80
81func init() {
82 log.AddPackage(log.JSON, log.WarnLevel, nil)
83 //log.UpdateAllLoggers(log.Fields{"instanceId": "device-state-transition"})
84 //log.SetAllLogLevel(log.DebugLevel)
85 tdm = newTestDeviceManager()
86 transitionMap = NewTransitionMap(tdm)
87}
88
89func getDevice(root bool, admin voltha.AdminState_AdminState, conn voltha.ConnectStatus_ConnectStatus, oper voltha.OperStatus_OperStatus) *voltha.Device {
90 return &voltha.Device{
91 Id: "test",
92 Root: root,
93 AdminState: admin,
94 ConnectStatus: conn,
95 OperStatus: oper,
96 }
97}
98
99func assertInvalidTransition(t *testing.T, from *voltha.Device, to *voltha.Device) {
100 handlers := transitionMap.GetTransitionHandler(from, to)
101 assert.Equal(t, 1, len(handlers))
102 assert.True(t, reflect.ValueOf(tdm.NotifyInvalidTransition).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
103}
104
105func assertNoOpTransition(t *testing.T, from *voltha.Device, to *voltha.Device) {
106 handlers := transitionMap.GetTransitionHandler(from, to)
107 assert.Equal(t, 0, len(handlers))
108}
109
110func TestValidTransitions(t *testing.T) {
111 from := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
112 to := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
113 handlers := transitionMap.GetTransitionHandler(from, to)
114 assert.Equal(t, 1, len(handlers))
115 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
116
117 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
118 to = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
119 handlers = transitionMap.GetTransitionHandler(from, to)
120 assert.Equal(t, 1, len(handlers))
121 assert.True(t, reflect.ValueOf(tdm.CreateLogicalDevice).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
122
123 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
124 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
125 handlers = transitionMap.GetTransitionHandler(from, to)
126 assert.Equal(t, 1, len(handlers))
127 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
128
khenaidoo0458db62019-06-20 08:50:36 -0400129 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
130 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVE)
131 handlers = transitionMap.GetTransitionHandler(from, to)
132 assert.Equal(t, 1, len(handlers))
133 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
134
khenaidoo0a822f92019-05-08 15:15:57 -0400135 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
136 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
137 handlers = transitionMap.GetTransitionHandler(from, to)
138 assert.Equal(t, 1, len(handlers))
139 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
140
141 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
142 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
143 handlers = transitionMap.GetTransitionHandler(from, to)
144 assert.Equal(t, 1, len(handlers))
145 assert.True(t, reflect.ValueOf(tdm.DisableAllChildDevices).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
146
147 from = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
148 to = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
149 handlers = transitionMap.GetTransitionHandler(from, to)
150 assert.Equal(t, 1, len(handlers))
151 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
152
153 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
154 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
155 handlers = transitionMap.GetTransitionHandler(from, to)
156 assert.Equal(t, 1, len(handlers))
157 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
158
159 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
160 to = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
161 handlers = transitionMap.GetTransitionHandler(from, to)
162 assert.Equal(t, 3, len(handlers))
163 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
164 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
165 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
166
167 from = getDevice(false, voltha.AdminState_DISABLED, 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, 2, len(handlers))
171 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
172 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
173}
174
175func TestInvalidTransitions(t *testing.T) {
176 from := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
177 to := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
178 assertInvalidTransition(t, from, to)
179
180 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
181 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
182 assertInvalidTransition(t, from, to)
183
184 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
185 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
186 assertInvalidTransition(t, from, to)
187
188 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
189 to = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
190 assertInvalidTransition(t, from, to)
191
192 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
193 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
194 assertInvalidTransition(t, from, to)
195
196 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
197 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
198 assertInvalidTransition(t, from, to)
199
200 from = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
201 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
202 assertInvalidTransition(t, from, to)
203}
204
205func TestNoOpTransitions(t *testing.T) {
206 from := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
207 to := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
208 assertNoOpTransition(t, from, to)
209
210 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
211 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
212 assertNoOpTransition(t, from, to)
213
214 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
215 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
216 assertNoOpTransition(t, from, to)
217
218 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
219 to = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
220 assertNoOpTransition(t, from, to)
221
222 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
223 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
224 assertNoOpTransition(t, from, to)
225
226 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
227 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
228 assertNoOpTransition(t, from, to)
229}