blob: 2d831c16948b78e7183704a6ed349dee18fb4f29 [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
2 * Copyright 2018-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 model
17
18import (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040019 "encoding/hex"
20 "encoding/json"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/google/uuid"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/opencord/voltha-go/protos/voltha"
Stephane Barbariea188d942018-10-16 16:43:04 -040023 "github.com/opencord/voltha-go/protos/openflow_13"
khenaidoob9203542018-09-17 22:56:37 -040024 "reflect"
25 "strconv"
26 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040027)
28
Stephane Barbarieec0919b2018-09-05 14:14:29 -040029var (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040030)
31
Stephane Barbarie126101e2018-10-11 16:18:48 -040032func Test_Proxy_1_1_Add_NewDevice(t *testing.T) {
33 devIdBin, _ := uuid.New().MarshalBinary()
34 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
35
Stephane Barbariea188d942018-10-16 16:43:04 -040036 preAddExecuted := false
37 postAddExecuted := false
38
39 modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
40 modelTestConfig.RootProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
Stephane Barbarie126101e2018-10-11 16:18:48 -040041
42 device.Id = devId
Stephane Barbariea188d942018-10-16 16:43:04 -040043 if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -040044 t.Error("Failed to add device")
45 } else {
46 t.Logf("Added device : %+v", added)
47 }
48
Stephane Barbariea188d942018-10-16 16:43:04 -040049 if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -040050 t.Error("Failed to find added device")
51 } else {
52 djson, _ := json.Marshal(d)
53 t.Logf("Found device: %s", string(djson))
54 }
55
56 if !preAddExecuted {
57 t.Error("PRE_ADD callback was not executed")
58 }
59 if !postAddExecuted {
60 t.Error("POST_ADD callback was not executed")
61 }
62}
63
64func Test_Proxy_1_2_Add_ExistingDevice(t *testing.T) {
65 device.Id = devId
Stephane Barbariea188d942018-10-16 16:43:04 -040066 if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -040067 t.Logf("Successfully detected that the device already exists: %s", devId)
68 } else {
69 t.Errorf("A new device should not have been created : %+v", added)
70 }
71
72}
73
74func Test_Proxy_2_1_Get_AllDevices(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -040075 devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -040076
77 if len(devices.([]interface{})) == 0 {
78 t.Error("there are no available devices to retrieve")
79 } else {
80 // Save the target device id for later tests
81 targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -040082 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040083 }
84}
85
Stephane Barbarie126101e2018-10-11 16:18:48 -040086func Test_Proxy_2_2_Get_SingleDevice(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -040087 if d := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -040088 t.Errorf("Failed to find device : %s", targetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040089 } else {
90 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -040091 t.Logf("Found device: %s", string(djson))
92 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -040093
Stephane Barbarie126101e2018-10-11 16:18:48 -040094}
95
96func Test_Proxy_3_1_Update_Device_WithRootProxy(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -040097 if retrieved := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -040098 t.Error("Failed to get device")
99 } else {
Stephane Barbariea188d942018-10-16 16:43:04 -0400100 t.Logf("Found raw device (root proxy): %+v", retrieved)
101
Stephane Barbarie126101e2018-10-11 16:18:48 -0400102 var fwVersion int
103 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
104 fwVersion = 0
105 } else {
106 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
107 fwVersion += 1
108 }
109
Stephane Barbariea188d942018-10-16 16:43:04 -0400110 preUpdateExecuted := false
111 postUpdateExecuted := false
Stephane Barbarie126101e2018-10-11 16:18:48 -0400112
Stephane Barbariea188d942018-10-16 16:43:04 -0400113 modelTestConfig.RootProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400114 PRE_UPDATE,
115 commonCallback,
116 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
117 )
Stephane Barbariea188d942018-10-16 16:43:04 -0400118 modelTestConfig.RootProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400119 POST_UPDATE,
120 commonCallback,
121 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
122 )
123
124 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
125 //cloned.FirmwareVersion = strconv.Itoa(fwVersion)
126 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
127 //t.Logf("Before update : %+v", cloned)
128
Stephane Barbariea188d942018-10-16 16:43:04 -0400129 if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+targetDeviceId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400130 t.Error("Failed to update device")
131 } else {
132 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
133 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400134 if d := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400135 t.Error("Failed to find updated device (root proxy)")
136 } else {
137 djson, _ := json.Marshal(d)
Stephane Barbariea188d942018-10-16 16:43:04 -0400138
139 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400140 }
141
142 if !preUpdateExecuted {
143 t.Error("PRE_UPDATE callback was not executed")
144 }
145 if !postUpdateExecuted {
146 t.Error("POST_UPDATE callback was not executed")
147 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400148 }
149}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400150
151func Test_Proxy_3_2_Update_Flow_WithSubProxy(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400152 // Get a device proxy and update a specific port
Stephane Barbariea188d942018-10-16 16:43:04 -0400153 devflowsProxy := modelTestConfig.Root.GetProxy("/devices/"+devId+"/flows", false)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400154 flows := devflowsProxy.Get("/", 0, false, "")
155 //flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2222
Stephane Barbariea188d942018-10-16 16:43:04 -0400156 //flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244
157 flows.(*openflow_13.Flows).Items[0].TableId = 2244
Stephane Barbarie126101e2018-10-11 16:18:48 -0400158 t.Logf("before updated flows: %+v", flows)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400159
Stephane Barbariea188d942018-10-16 16:43:04 -0400160 //devPortsProxy := modelTestConfig.RootProxy.node.GetProxy("/devices/"+devId+"/ports", false)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400161 //port123 := devPortsProxy.Get("/123", 0, false, "")
162 //t.Logf("got ports: %+v", port123)
163 //port123.(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400164
Stephane Barbariea188d942018-10-16 16:43:04 -0400165 preUpdateExecuted := false
166 postUpdateExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400167
Stephane Barbarie126101e2018-10-11 16:18:48 -0400168 devflowsProxy.RegisterCallback(
169 PRE_UPDATE,
170 commonCallback,
171 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted,
172 )
173 devflowsProxy.RegisterCallback(
174 POST_UPDATE,
175 commonCallback,
176 "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted,
177 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400178
Stephane Barbarie126101e2018-10-11 16:18:48 -0400179 kvFlows := devflowsProxy.Get("/", 0, false, "")
180
181 if reflect.DeepEqual(flows, kvFlows) {
182 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
183 }
184
Stephane Barbariea188d942018-10-16 16:43:04 -0400185 if updated := devflowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400186 t.Error("Failed to update flow")
187 } else {
188 t.Logf("Updated flows : %+v", updated)
189 }
190
191 if d := devflowsProxy.Get("/", 0, false, ""); d == nil {
192 t.Error("Failed to find updated flows (flows proxy)")
193 } else {
194 djson, _ := json.Marshal(d)
195 t.Logf("Found flows (flows proxy): %s", string(djson))
196 }
197
Stephane Barbariea188d942018-10-16 16:43:04 -0400198 if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400199 t.Error("Failed to find updated flows (root proxy)")
200 } else {
201 djson, _ := json.Marshal(d)
202 t.Logf("Found flows (root proxy): %s", string(djson))
203 }
204
205 if !preUpdateExecuted {
206 t.Error("PRE_UPDATE callback was not executed")
207 }
208 if !postUpdateExecuted {
209 t.Error("POST_UPDATE callback was not executed")
210 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400211
Stephane Barbariea188d942018-10-16 16:43:04 -0400212 //Get a device proxy and update all its ports
213
214
215 //devProxy := modelTestConfig.RootProxy.GetProxy("/devices/"+devId, false)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400216 //ports := devProxy.Get("/ports", 0, false, "")
217 //t.Logf("got ports: %+v", ports)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400218 //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400219 //
220 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
221 //
222 //devProxy.Update("/ports", ports, false, "")
223 //updated := devProxy.Get("/ports", 0, false, "")
224 //t.Logf("got updated ports: %+v", updated)
225
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400226 //
227 // Get a device proxy, retrieve all the ports and update a specific one
228 //
229
Stephane Barbariea188d942018-10-16 16:43:04 -0400230 //devProxy := modelTestConfig.RootProxy.GetProxy("/devices/"+devId, false)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400231 //ports := devProxy.Get("/ports", 0, false, "")
232 //t.Logf("got ports: %+v", ports)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400233 //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400234 //
235 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
236 //
237 //devProxy.Update("/ports/123", ports.([]interface{})[0], false, "")
238 //updated := devProxy.Get("/ports", 0, false, "")
239 //t.Logf("got updated ports: %+v", updated)
240}
241
Stephane Barbarie126101e2018-10-11 16:18:48 -0400242func Test_Proxy_4_1_Remove_Device(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400243 preRemoveExecuted := false
244 postRemoveExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400245
Stephane Barbariea188d942018-10-16 16:43:04 -0400246 modelTestConfig.RootProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400247 PRE_REMOVE,
248 commonCallback,
249 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
250 )
Stephane Barbariea188d942018-10-16 16:43:04 -0400251 modelTestConfig.RootProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400252 POST_REMOVE,
253 commonCallback,
254 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
255 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400256
Stephane Barbariea188d942018-10-16 16:43:04 -0400257 if removed := modelTestConfig.RootProxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400258 t.Error("Failed to remove device")
259 } else {
260 t.Logf("Removed device : %+v", removed)
261 }
Stephane Barbariea188d942018-10-16 16:43:04 -0400262 if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400263 djson, _ := json.Marshal(d)
264 t.Errorf("Device was not removed - %s", djson)
265 } else {
266 t.Logf("Device was removed: %s", devId)
267 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400268
269 if !preRemoveExecuted {
270 t.Error("PRE_UPDATE callback was not executed")
271 }
272 if !postRemoveExecuted {
273 t.Error("POST_UPDATE callback was not executed")
274 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400275}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400276
277// -----------------------------
278// Callback tests
279// -----------------------------
280
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400281func Test_Proxy_Callbacks_1_Register(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400282 modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400283
284 m := make(map[string]string)
285 m["name"] = "fghij"
Stephane Barbariea188d942018-10-16 16:43:04 -0400286 modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400287
288 d := &voltha.Device{Id: "12345"}
Stephane Barbariea188d942018-10-16 16:43:04 -0400289 modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400290}
291
292func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400293 modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400294}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400295
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400296func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400297 modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400298}
299
300func Test_Proxy_Callbacks_4_Unregister(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400301 modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, firstCallback)
302 modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, secondCallback)
303 modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, thirdCallback)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400304}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400305
306//func Test_Proxy_Callbacks_5_Add(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400307// modelTestConfig.RootProxy.Root.AddCallback(modelTestConfig.RootProxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400308//}
309//
310//func Test_Proxy_Callbacks_6_Execute(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400311// modelTestConfig.RootProxy.Root.ExecuteCallbacks()
Stephane Barbarie126101e2018-10-11 16:18:48 -0400312//}