blob: 1e932433ef46413f9a00f4a63c95bd0c062a877a [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"
Stephane Barbarie1039ec42019-02-04 10:43:16 -050021 "github.com/golang/protobuf/proto"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/google/uuid"
William Kurkiandaa6bb22019-03-07 12:26:28 -050023 "github.com/opencord/voltha-protos/go/common"
24 "github.com/opencord/voltha-protos/go/openflow_13"
25 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026 "math/rand"
khenaidoob9203542018-09-17 22:56:37 -040027 "reflect"
28 "strconv"
29 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040030)
31
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050033 TestProxy_Root *root
34 TestProxy_Root_LogicalDevice *Proxy
35 TestProxy_Root_Device *Proxy
Stephane Barbarieaa467942019-02-06 14:09:44 -050036 TestProxy_Root_Adapter *Proxy
Stephane Barbarie1039ec42019-02-04 10:43:16 -050037 TestProxy_DeviceId string
Stephane Barbarieaa467942019-02-06 14:09:44 -050038 TestProxy_AdapterId string
Stephane Barbarie1039ec42019-02-04 10:43:16 -050039 TestProxy_LogicalDeviceId string
40 TestProxy_TargetDeviceId string
41 TestProxy_TargetLogicalDeviceId string
42 TestProxy_LogicalPorts []*voltha.LogicalPort
43 TestProxy_Ports []*voltha.Port
44 TestProxy_Stats *openflow_13.OfpFlowStats
45 TestProxy_Flows *openflow_13.Flows
46 TestProxy_Device *voltha.Device
47 TestProxy_LogicalDevice *voltha.LogicalDevice
Stephane Barbarieaa467942019-02-06 14:09:44 -050048 TestProxy_Adapter *voltha.Adapter
Stephane Barbarieec0919b2018-09-05 14:14:29 -040049)
50
Stephane Barbariedc5022d2018-11-19 15:21:44 -050051func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050052 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
53 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
54 TestProxy_Root = NewRoot(&voltha.Voltha{}, nil)
55 TestProxy_Root_LogicalDevice = TestProxy_Root.CreateProxy("/", false)
56 TestProxy_Root_Device = TestProxy_Root.CreateProxy("/", false)
Stephane Barbarieaa467942019-02-06 14:09:44 -050057 TestProxy_Root_Adapter = TestProxy_Root.CreateProxy("/", false)
Stephane Barbarie1039ec42019-02-04 10:43:16 -050058
59 TestProxy_LogicalPorts = []*voltha.LogicalPort{
60 {
61 Id: "123",
62 DeviceId: "logicalport-0-device-id",
63 DevicePortNo: 123,
64 RootPort: false,
65 },
66 }
67 TestProxy_Ports = []*voltha.Port{
68 {
69 PortNo: 123,
70 Label: "test-port-0",
71 Type: voltha.Port_PON_OLT,
72 AdminState: common.AdminState_ENABLED,
73 OperStatus: common.OperStatus_ACTIVE,
74 DeviceId: "etcd_port-0-device-id",
75 Peers: []*voltha.Port_PeerPort{},
76 },
77 }
78
79 TestProxy_Stats = &openflow_13.OfpFlowStats{
80 Id: 1111,
81 }
82 TestProxy_Flows = &openflow_13.Flows{
83 Items: []*openflow_13.OfpFlowStats{TestProxy_Stats},
84 }
85 TestProxy_Device = &voltha.Device{
86 Id: TestProxy_DeviceId,
87 Type: "simulated_olt",
88 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
89 AdminState: voltha.AdminState_PREPROVISIONED,
90 Flows: TestProxy_Flows,
91 Ports: TestProxy_Ports,
92 }
93
94 TestProxy_LogicalDevice = &voltha.LogicalDevice{
95 Id: TestProxy_DeviceId,
96 DatapathId: 0,
97 Ports: TestProxy_LogicalPorts,
98 Flows: TestProxy_Flows,
99 }
Stephane Barbarieaa467942019-02-06 14:09:44 -0500100
101 TestProxy_Adapter = &voltha.Adapter{
102 Id: TestProxy_AdapterId,
103 Vendor: "test-adapter-vendor",
104 Version: "test-adapter-version",
105 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500106}
107
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500108func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500109 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500110 TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
111 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbarie126101e2018-10-11 16:18:48 -0400112
Stephane Barbariea188d942018-10-16 16:43:04 -0400113 preAddExecuted := false
114 postAddExecuted := false
115
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500116 devicesProxy := TestProxy_Root.node.CreateProxy("/devices", false)
117 devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes")
118 devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes")
119
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500120 // Register ADD instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500121 TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
122 TestProxy_Root_Device.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400123
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500124 // Add the device
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500125 if added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400126 t.Error("Failed to add device")
127 } else {
128 t.Logf("Added device : %+v", added)
129 }
130
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500131 // Verify that the added device can now be retrieved
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500132 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400133 t.Error("Failed to find added device")
134 } else {
135 djson, _ := json.Marshal(d)
136 t.Logf("Found device: %s", string(djson))
137 }
138
139 if !preAddExecuted {
140 t.Error("PRE_ADD callback was not executed")
141 }
142 if !postAddExecuted {
143 t.Error("POST_ADD callback was not executed")
144 }
145}
146
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500147func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
148 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500149
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500150 added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, "");
151 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() {
152 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400153 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400154}
155
Stephane Barbarieaa467942019-02-06 14:09:44 -0500156func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
157 TestProxy_AdapterId = "test-adapter"
158 TestProxy_Adapter.Id = TestProxy_AdapterId
159 preAddExecuted := false
160 postAddExecuted := false
161
162 // Register ADD instructions callbacks
163 TestProxy_Root_Adapter.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions for adapters", &preAddExecuted)
164 TestProxy_Root_Adapter.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions for adapters", &postAddExecuted)
165
166 // Add the adapter
167 if added := TestProxy_Root_Adapter.Add("/adapters", TestProxy_Adapter, ""); added == nil {
168 t.Error("Failed to add adapter")
169 } else {
170 t.Logf("Added adapter : %+v", added)
171 }
172
173 // Verify that the added device can now be retrieved
174 if d := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
175 t.Error("Failed to find added adapter")
176 } else {
177 djson, _ := json.Marshal(d)
178 t.Logf("Found adapter: %s", string(djson))
179 }
180
181 if !preAddExecuted {
182 t.Error("PRE_ADD callback was not executed")
183 }
184 if !postAddExecuted {
185 t.Error("POST_ADD callback was not executed")
186 }
187}
188
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500189func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
190 devices := TestProxy_Root_Device.Get("/devices", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400191
192 if len(devices.([]interface{})) == 0 {
193 t.Error("there are no available devices to retrieve")
194 } else {
195 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500196 TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -0400197 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400198 }
199}
200
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500201func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
202 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
203 t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400204 } else {
205 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400206 t.Logf("Found device: %s", string(djson))
207 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400208}
209
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500210func TestProxy_1_3_1_Update_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500211 var fwVersion int
212 preUpdateExecuted := false
213 postUpdateExecuted := false
214
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500215 if retrieved := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400216 t.Error("Failed to get device")
217 } else {
Stephane Barbariea188d942018-10-16 16:43:04 -0400218 t.Logf("Found raw device (root proxy): %+v", retrieved)
219
Stephane Barbarie126101e2018-10-11 16:18:48 -0400220 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
221 fwVersion = 0
222 } else {
223 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500224 fwVersion++
Stephane Barbarie126101e2018-10-11 16:18:48 -0400225 }
226
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500227 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400228
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500229 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400230 PRE_UPDATE,
231 commonCallback,
232 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
233 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500234 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400235 POST_UPDATE,
236 commonCallback,
237 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
238 )
239
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500240 if afterUpdate := TestProxy_Root_Device.Update("/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400241 t.Error("Failed to update device")
242 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500243 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400244 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500245
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500246 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400247 t.Error("Failed to find updated device (root proxy)")
248 } else {
249 djson, _ := json.Marshal(d)
Stephane Barbariea188d942018-10-16 16:43:04 -0400250 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400251 }
252
253 if !preUpdateExecuted {
254 t.Error("PRE_UPDATE callback was not executed")
255 }
256 if !postUpdateExecuted {
257 t.Error("POST_UPDATE callback was not executed")
258 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400259 }
260}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400261
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500262func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400263 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500264 devFlowsProxy := TestProxy_Root.node.CreateProxy("/devices/"+TestProxy_DeviceId+"/flows", false)
265 flows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbariea188d942018-10-16 16:43:04 -0400266 flows.(*openflow_13.Flows).Items[0].TableId = 2244
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400267
Stephane Barbariea188d942018-10-16 16:43:04 -0400268 preUpdateExecuted := false
269 postUpdateExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400270
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500271 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400272 PRE_UPDATE,
273 commonCallback,
274 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted,
275 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500276 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400277 POST_UPDATE,
278 commonCallback,
279 "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted,
280 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400281
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500282 kvFlows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400283
284 if reflect.DeepEqual(flows, kvFlows) {
285 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
286 }
287
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500288 if updated := devFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400289 t.Error("Failed to update flow")
290 } else {
291 t.Logf("Updated flows : %+v", updated)
292 }
293
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500294 if d := devFlowsProxy.Get("/", 0, false, ""); d == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400295 t.Error("Failed to find updated flows (flows proxy)")
296 } else {
297 djson, _ := json.Marshal(d)
298 t.Logf("Found flows (flows proxy): %s", string(djson))
299 }
300
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500301 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400302 t.Error("Failed to find updated flows (root proxy)")
303 } else {
304 djson, _ := json.Marshal(d)
305 t.Logf("Found flows (root proxy): %s", string(djson))
306 }
307
308 if !preUpdateExecuted {
309 t.Error("PRE_UPDATE callback was not executed")
310 }
311 if !postUpdateExecuted {
312 t.Error("POST_UPDATE callback was not executed")
313 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400314}
315
Stephane Barbarieaa467942019-02-06 14:09:44 -0500316func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
317 preUpdateExecuted := false
318 postUpdateExecuted := false
319
320 adaptersProxy := TestProxy_Root.node.CreateProxy("/adapters", false)
321
322 if retrieved := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 1, false, ""); retrieved == nil {
323 t.Error("Failed to get adapter")
324 } else {
325 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
326
327 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
328
329 adaptersProxy.RegisterCallback(
330 PRE_UPDATE,
331 commonCallback,
332 "PRE_UPDATE instructions for adapters", &preUpdateExecuted,
333 )
334 adaptersProxy.RegisterCallback(
335 POST_UPDATE,
336 commonCallback,
337 "POST_UPDATE instructions for adapters", &postUpdateExecuted,
338 )
339
340 if afterUpdate := adaptersProxy.Update("/"+TestProxy_AdapterId, retrieved, false, ""); afterUpdate == nil {
341 t.Error("Failed to update adapter")
342 } else {
343 t.Logf("Updated adapter : %+v", afterUpdate)
344 }
345
346 if d := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
347 t.Error("Failed to find updated adapter (root proxy)")
348 } else {
349 djson, _ := json.Marshal(d)
350 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
351 }
352
353 if !preUpdateExecuted {
354 t.Error("PRE_UPDATE callback for adapter was not executed")
355 }
356 if !postUpdateExecuted {
357 t.Error("POST_UPDATE callback for adapter was not executed")
358 }
359 }
360}
361
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500362func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400363 preRemoveExecuted := false
364 postRemoveExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400365
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500366 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400367 PRE_REMOVE,
368 commonCallback,
369 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
370 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500371 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400372 POST_REMOVE,
373 commonCallback,
374 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
375 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400376
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500377 if removed := TestProxy_Root_Device.Remove("/devices/"+TestProxy_DeviceId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400378 t.Error("Failed to remove device")
379 } else {
380 t.Logf("Removed device : %+v", removed)
381 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500382 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400383 djson, _ := json.Marshal(d)
384 t.Errorf("Device was not removed - %s", djson)
385 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500386 t.Logf("Device was removed: %s", TestProxy_DeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500387 }
388
389 if !preRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500390 t.Error("PRE_REMOVE callback was not executed")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500391 }
392 if !postRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500393 t.Error("POST_REMOVE callback was not executed")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500394 }
395}
396
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500397func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500398
399 ldIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500400 TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12]
401 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500402
403 preAddExecuted := false
404 postAddExecuted := false
405
406 // Register
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500407 TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
408 TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500409
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500410 if added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, ""); added == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500411 t.Error("Failed to add logical device")
412 } else {
413 t.Logf("Added logical device : %+v", added)
414 }
415
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500416 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500417 t.Error("Failed to find added logical device")
418 } else {
419 ldJSON, _ := json.Marshal(ld)
420 t.Logf("Found logical device: %s", string(ldJSON))
421 }
422
423 if !preAddExecuted {
424 t.Error("PRE_ADD callback was not executed")
425 }
426 if !postAddExecuted {
427 t.Error("POST_ADD callback was not executed")
428 }
429}
430
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500431func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
432 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500433
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500434 added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, "");
435 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() {
436 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
437 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500438}
439
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500440func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
441 logicalDevices := TestProxy_Root_LogicalDevice.Get("/logical_devices", 1, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500442
443 if len(logicalDevices.([]interface{})) == 0 {
444 t.Error("there are no available logical devices to retrieve")
445 } else {
446 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500447 TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500448 t.Logf("retrieved all logical devices: %+v", logicalDevices)
449 }
450}
451
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500452func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
453 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
454 t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500455 } else {
456 ldJSON, _ := json.Marshal(ld)
457 t.Logf("Found logical device: %s", string(ldJSON))
458 }
459
460}
461
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500462func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500463 var fwVersion int
464 preUpdateExecuted := false
465 postUpdateExecuted := false
466
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500467 if retrieved := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500468 t.Error("Failed to get logical device")
469 } else {
470 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
471
472 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
473 fwVersion = 0
474 } else {
475 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
476 fwVersion++
477 }
478
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500479 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500480 PRE_UPDATE,
481 commonCallback,
482 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
483 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500484 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500485 POST_UPDATE,
486 commonCallback,
487 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
488 )
489
490 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
491
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500492 if afterUpdate := TestProxy_Root_LogicalDevice.Update("/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500493 ""); afterUpdate == nil {
494 t.Error("Failed to update logical device")
495 } else {
496 t.Logf("Updated logical device : %+v", afterUpdate)
497 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500498 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500499 t.Error("Failed to find updated logical device (root proxy)")
500 } else {
501 djson, _ := json.Marshal(d)
502
503 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
504 }
505
506 if !preUpdateExecuted {
507 t.Error("PRE_UPDATE callback was not executed")
508 }
509 if !postUpdateExecuted {
510 t.Error("POST_UPDATE callback was not executed")
511 }
512 }
513}
514
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500515func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500516 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500517 ldFlowsProxy := TestProxy_Root.node.CreateProxy("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500518 flows := ldFlowsProxy.Get("/", 0, false, "")
519 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
520 t.Logf("before updated flows: %+v", flows)
521
522 ldFlowsProxy.RegisterCallback(
523 PRE_UPDATE,
524 commonCallback2,
525 )
526 ldFlowsProxy.RegisterCallback(
527 POST_UPDATE,
528 commonCallback2,
529 )
530
531 kvFlows := ldFlowsProxy.Get("/", 0, false, "")
532
533 if reflect.DeepEqual(flows, kvFlows) {
534 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
535 }
536
537 if updated := ldFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
538 t.Error("Failed to update logical device flows")
539 } else {
540 t.Logf("Updated logical device flows : %+v", updated)
541 }
542
543 if d := ldFlowsProxy.Get("/", 0, false, ""); d == nil {
544 t.Error("Failed to find updated logical device flows (flows proxy)")
545 } else {
546 djson, _ := json.Marshal(d)
547 t.Logf("Found flows (flows proxy): %s", string(djson))
548 }
549
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500550 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500551 ""); !reflect.ValueOf(d).IsValid() {
552 t.Error("Failed to find updated logical device flows (root proxy)")
553 } else {
554 djson, _ := json.Marshal(d)
555 t.Logf("Found logical device flows (root proxy): %s", string(djson))
556 }
557}
558
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500559func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500560 preRemoveExecuted := false
561 postRemoveExecuted := false
562
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500563 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500564 PRE_REMOVE,
565 commonCallback,
566 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
567 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500568 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500569 POST_REMOVE,
570 commonCallback,
571 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
572 )
573
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500574 if removed := TestProxy_Root_LogicalDevice.Remove("/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500575 t.Error("Failed to remove logical device")
576 } else {
577 t.Logf("Removed device : %+v", removed)
578 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500579 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500580 djson, _ := json.Marshal(d)
581 t.Errorf("Device was not removed - %s", djson)
582 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500583 t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400584 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400585
586 if !preRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500587 t.Error("PRE_REMOVE callback was not executed")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400588 }
589 if !postRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500590 t.Error("POST_REMOVE callback was not executed")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400591 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400592}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400593
594// -----------------------------
595// Callback tests
596// -----------------------------
597
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500598func TestProxy_Callbacks_1_Register(t *testing.T) {
599 TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400600
601 m := make(map[string]string)
602 m["name"] = "fghij"
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500603 TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400604
605 d := &voltha.Device{Id: "12345"}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500606 TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400607}
608
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500609func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
610 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400611}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400612
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500613func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
614 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400615}
616
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500617func TestProxy_Callbacks_4_Unregister(t *testing.T) {
618 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback)
619 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback)
620 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400621}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400622
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500623//func TestProxy_Callbacks_5_Add(t *testing.T) {
624// TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400625//}
626//
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500627//func TestProxy_Callbacks_6_Execute(t *testing.T) {
628// TestProxy_Root_Device.Root.ExecuteCallbacks()
Stephane Barbarie126101e2018-10-11 16:18:48 -0400629//}