blob: a9ec326310000d3d415d42099a75b6b4a2b60d14 [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
129 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
130 to = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
131 handlers = transitionMap.GetTransitionHandler(from, to)
132 assert.Equal(t, 1, len(handlers))
133 assert.True(t, reflect.ValueOf(tdm.SetupUNILogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
134
135 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
136 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
137 handlers = transitionMap.GetTransitionHandler(from, to)
138 assert.Equal(t, 1, len(handlers))
139 assert.True(t, reflect.ValueOf(tdm.DisableAllChildDevices).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
140
141 from = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
142 to = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
143 handlers = transitionMap.GetTransitionHandler(from, to)
144 assert.Equal(t, 1, len(handlers))
145 assert.True(t, reflect.ValueOf(tdm.SetAdminStateToEnable).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
146
147 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
148 to = getDevice(true, 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_UNKNOWN)
154 to = getDevice(true, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
155 handlers = transitionMap.GetTransitionHandler(from, to)
156 assert.Equal(t, 3, len(handlers))
157 assert.True(t, reflect.ValueOf(tdm.DeleteAllChildDevices).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
158 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalDevice).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
159 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[2]).Pointer())
160
161 from = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
162 to = getDevice(false, voltha.AdminState_DELETED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
163 handlers = transitionMap.GetTransitionHandler(from, to)
164 assert.Equal(t, 2, len(handlers))
165 assert.True(t, reflect.ValueOf(tdm.DeleteLogicalPorts).Pointer() == reflect.ValueOf(handlers[0]).Pointer())
166 assert.True(t, reflect.ValueOf(tdm.RunPostDeviceDelete).Pointer() == reflect.ValueOf(handlers[1]).Pointer())
167}
168
169func TestInvalidTransitions(t *testing.T) {
170 from := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVE)
171 to := getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
172 assertInvalidTransition(t, from, to)
173
174 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
175 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
176 assertInvalidTransition(t, from, to)
177
178 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
179 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
180 assertInvalidTransition(t, from, to)
181
182 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
183 to = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
184 assertInvalidTransition(t, from, to)
185
186 from = getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
187 to = getDevice(true, voltha.AdminState_UNKNOWN, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
188 assertInvalidTransition(t, from, to)
189
190 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
191 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
192 assertInvalidTransition(t, from, to)
193
194 from = getDevice(true, voltha.AdminState_DOWNLOADING_IMAGE, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
195 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
196 assertInvalidTransition(t, from, to)
197}
198
199func TestNoOpTransitions(t *testing.T) {
200 from := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
201 to := getDevice(true, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
202 assertNoOpTransition(t, from, to)
203
204 from = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
205 to = getDevice(true, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
206 assertNoOpTransition(t, from, to)
207
208 from = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
209 to = getDevice(true, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
210 assertNoOpTransition(t, from, to)
211
212 from = getDevice(false, voltha.AdminState_ENABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
213 to = getDevice(false, voltha.AdminState_DISABLED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_UNKNOWN)
214 assertNoOpTransition(t, from, to)
215
216 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_UNKNOWN, voltha.OperStatus_ACTIVATING)
217 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
218 assertNoOpTransition(t, from, to)
219
220 from = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_DISCOVERED)
221 to = getDevice(false, voltha.AdminState_PREPROVISIONED, voltha.ConnectStatus_REACHABLE, voltha.OperStatus_ACTIVATING)
222 assertNoOpTransition(t, from, to)
223}