blob: 0ed8af9dc2053d5a3bcdf526c778852ef87c05c3 [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
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 (
19 "context"
20 "encoding/hex"
21 "encoding/json"
22 "github.com/golang/protobuf/proto"
23 "github.com/google/uuid"
Thomas Lee Se5a44012019-11-07 20:32:24 +053024 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sbarbari17d7e222019-11-05 10:02:29 -050025 "github.com/opencord/voltha-protos/v2/go/common"
26 "github.com/opencord/voltha-protos/v2/go/openflow_13"
27 "github.com/opencord/voltha-protos/v2/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053028 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050029 "math/rand"
30 "reflect"
31 "strconv"
32 "testing"
33 "time"
34)
35
36var (
37 TestProxy_Root *root
38 TestProxy_Root_LogicalDevice *Proxy
39 TestProxy_Root_Device *Proxy
40 TestProxy_Root_Adapter *Proxy
41 TestProxy_DeviceId string
42 TestProxy_AdapterId string
43 TestProxy_LogicalDeviceId string
44 TestProxy_TargetDeviceId string
45 TestProxy_TargetLogicalDeviceId string
46 TestProxy_LogicalPorts []*voltha.LogicalPort
47 TestProxy_Ports []*voltha.Port
48 TestProxy_Stats *openflow_13.OfpFlowStats
49 TestProxy_Flows *openflow_13.Flows
50 TestProxy_Device *voltha.Device
51 TestProxy_LogicalDevice *voltha.LogicalDevice
52 TestProxy_Adapter *voltha.Adapter
53)
54
55func init() {
56 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
57 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
Thomas Lee Se5a44012019-11-07 20:32:24 +053058 var err error
sbarbari17d7e222019-11-05 10:02:29 -050059 TestProxy_Root = NewRoot(&voltha.Voltha{}, nil)
Thomas Lee Se5a44012019-11-07 20:32:24 +053060 if TestProxy_Root_LogicalDevice, err = TestProxy_Root.CreateProxy(context.Background(), "/", false); err != nil {
61 log.With(log.Fields{"error": err}).Fatal("Cannot create logical device proxy")
62 }
63 if TestProxy_Root_Device, err = TestProxy_Root.CreateProxy(context.Background(), "/", false); err != nil {
64 log.With(log.Fields{"error": err}).Fatal("Cannot create device proxy")
65 }
66 if TestProxy_Root_Adapter, err = TestProxy_Root.CreateProxy(context.Background(), "/", false); err != nil {
67 log.With(log.Fields{"error": err}).Fatal("Cannot create adapter proxy")
68 }
sbarbari17d7e222019-11-05 10:02:29 -050069
70 TestProxy_LogicalPorts = []*voltha.LogicalPort{
71 {
72 Id: "123",
73 DeviceId: "logicalport-0-device-id",
74 DevicePortNo: 123,
75 RootPort: false,
76 },
77 }
78 TestProxy_Ports = []*voltha.Port{
79 {
80 PortNo: 123,
81 Label: "test-port-0",
82 Type: voltha.Port_PON_OLT,
83 AdminState: common.AdminState_ENABLED,
84 OperStatus: common.OperStatus_ACTIVE,
85 DeviceId: "etcd_port-0-device-id",
86 Peers: []*voltha.Port_PeerPort{},
87 },
88 }
89
90 TestProxy_Stats = &openflow_13.OfpFlowStats{
91 Id: 1111,
92 }
93 TestProxy_Flows = &openflow_13.Flows{
94 Items: []*openflow_13.OfpFlowStats{TestProxy_Stats},
95 }
96 TestProxy_Device = &voltha.Device{
97 Id: TestProxy_DeviceId,
98 Type: "simulated_olt",
99 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
100 AdminState: voltha.AdminState_PREPROVISIONED,
101 Flows: TestProxy_Flows,
102 Ports: TestProxy_Ports,
103 }
104
105 TestProxy_LogicalDevice = &voltha.LogicalDevice{
106 Id: TestProxy_DeviceId,
107 DatapathId: 0,
108 Ports: TestProxy_LogicalPorts,
109 Flows: TestProxy_Flows,
110 }
111
112 TestProxy_Adapter = &voltha.Adapter{
113 Id: TestProxy_AdapterId,
114 Vendor: "test-adapter-vendor",
115 Version: "test-adapter-version",
116 }
117}
118
119func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
120 devIDBin, _ := uuid.New().MarshalBinary()
121 TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
122 TestProxy_Device.Id = TestProxy_DeviceId
123
124 preAddExecuted := make(chan struct{})
125 postAddExecuted := make(chan struct{})
126 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
127
Thomas Lee Se5a44012019-11-07 20:32:24 +0530128 devicesProxy, err := TestProxy_Root.node.CreateProxy(context.Background(), "/devices", false)
129 if err != nil {
130 log.With(log.Fields{"error": err}).Fatal("Cannot create devices proxy")
131 }
sbarbari17d7e222019-11-05 10:02:29 -0500132 devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes")
133 devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes")
134
135 // Register ADD instructions callbacks
136 TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
137 TestProxy_Root_Device.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
138
Thomas Lee Se5a44012019-11-07 20:32:24 +0530139 added, err := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, "")
140 if err != nil {
141 BenchmarkProxy_Logger.Errorf("Failed to add test proxy device due to error: %v", err)
142 assert.NotNil(t, err)
143 }
144 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500145 t.Error("Failed to add device")
146 } else {
147 t.Logf("Added device : %+v", added)
148 }
149
150 if !verifyGotResponse(preAddExecuted) {
151 t.Error("PRE_ADD callback was not executed")
152 }
153 if !verifyGotResponse(postAddExecuted) {
154 t.Error("POST_ADD callback was not executed")
155 }
156
157 // Verify that the added device can now be retrieved
Thomas Lee Se5a44012019-11-07 20:32:24 +0530158 d, err := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, "")
159 if err != nil {
160 BenchmarkProxy_Logger.Errorf("Failed get device info from test proxy due to error: %v", err)
161 assert.NotNil(t, err)
162 }
163 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500164 t.Error("Failed to find added device")
165 } else {
166 djson, _ := json.Marshal(d)
167 t.Logf("Found device: %s", string(djson))
168 }
169}
170
171func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
172 TestProxy_Device.Id = TestProxy_DeviceId
173
Thomas Lee Se5a44012019-11-07 20:32:24 +0530174 added, err := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, "")
175 if err != nil {
176 BenchmarkProxy_Logger.Errorf("Failed to add device to test proxy due to error: %v", err)
177 assert.NotNil(t, err)
178 }
sbarbari17d7e222019-11-05 10:02:29 -0500179 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() {
180 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
181 }
182}
183
184func verifyGotResponse(callbackIndicator <-chan struct{}) bool {
185 timeout := time.After(1 * time.Second)
186 // Wait until the channel closes, or we time out
187 select {
188 case <-callbackIndicator:
189 // Received response successfully
190 return true
191
192 case <-timeout:
193 // Got a timeout! fail with a timeout error
194 return false
195 }
196}
197
198func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
199 TestProxy_AdapterId = "test-adapter"
200 TestProxy_Adapter.Id = TestProxy_AdapterId
201 preAddExecuted := make(chan struct{})
202 postAddExecuted := make(chan struct{})
203 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
204
205 // Register ADD instructions callbacks
206 TestProxy_Root_Adapter.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions for adapters", &preAddExecutedPtr)
207 TestProxy_Root_Adapter.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions for adapters", &postAddExecutedPtr)
208
209 // Add the adapter
Thomas Lee Se5a44012019-11-07 20:32:24 +0530210 added, err := TestProxy_Root_Adapter.Add(context.Background(), "/adapters", TestProxy_Adapter, "")
211 if err != nil {
212 BenchmarkProxy_Logger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
213 assert.NotNil(t, err)
214 }
215 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500216 t.Error("Failed to add adapter")
217 } else {
218 t.Logf("Added adapter : %+v", added)
219 }
220
221 verifyGotResponse(postAddExecuted)
222
223 // Verify that the added device can now be retrieved
Thomas Lee Se5a44012019-11-07 20:32:24 +0530224 d, err := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 0, false, "")
225 if err != nil {
226 BenchmarkProxy_Logger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
227 assert.NotNil(t, err)
228 }
229 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500230 t.Error("Failed to find added adapter")
231 } else {
232 djson, _ := json.Marshal(d)
233 t.Logf("Found adapter: %s", string(djson))
234 }
235
236 if !verifyGotResponse(preAddExecuted) {
237 t.Error("PRE_ADD callback was not executed")
238 }
239 if !verifyGotResponse(postAddExecuted) {
240 t.Error("POST_ADD callback was not executed")
241 }
242}
243
244func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530245 devices, err := TestProxy_Root_Device.Get(context.Background(), "/devices", 1, false, "")
246 if err != nil {
247 BenchmarkProxy_Logger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
248 assert.NotNil(t, err)
249 }
sbarbari17d7e222019-11-05 10:02:29 -0500250 if len(devices.([]interface{})) == 0 {
251 t.Error("there are no available devices to retrieve")
252 } else {
253 // Save the target device id for later tests
254 TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
255 t.Logf("retrieved all devices: %+v", devices)
256 }
257}
258
259func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530260 d, err := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 0, false, "")
261 if err != nil {
262 BenchmarkProxy_Logger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
263 assert.NotNil(t, err)
264 }
265 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500266 t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId)
267 } else {
268 djson, _ := json.Marshal(d)
269 t.Logf("Found device: %s", string(djson))
270 }
271}
272
273func TestProxy_1_3_1_Update_Device(t *testing.T) {
274 var fwVersion int
275
276 preUpdateExecuted := make(chan struct{})
277 postUpdateExecuted := make(chan struct{})
278 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
279
Thomas Lee Se5a44012019-11-07 20:32:24 +0530280 retrieved, err := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, "")
281 if err != nil {
282 BenchmarkProxy_Logger.Errorf("Failed to get device info from test proxy due to error: %v", err)
283 assert.NotNil(t, err)
284 }
285 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500286 t.Error("Failed to get device")
287 } else {
288 t.Logf("Found raw device (root proxy): %+v", retrieved)
289
290 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
291 fwVersion = 0
292 } else {
293 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
294 fwVersion++
295 }
296
297 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
298
299 TestProxy_Root_Device.RegisterCallback(
300 PRE_UPDATE,
301 commonChanCallback,
302 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
303 )
304 TestProxy_Root_Device.RegisterCallback(
305 POST_UPDATE,
306 commonChanCallback,
307 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
308 )
309
Thomas Lee Se5a44012019-11-07 20:32:24 +0530310 afterUpdate, err := TestProxy_Root_Device.Update(context.Background(), "/devices/"+TestProxy_TargetDeviceId, retrieved, false, "")
311 if err != nil {
312 BenchmarkProxy_Logger.Errorf("Failed to update device info test proxy due to error: %v", err)
313 assert.NotNil(t, err)
314 }
315 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500316 t.Error("Failed to update device")
317 } else {
318 t.Logf("Updated device : %+v", afterUpdate)
319 }
320
321 if !verifyGotResponse(preUpdateExecuted) {
322 t.Error("PRE_UPDATE callback was not executed")
323 }
324 if !verifyGotResponse(postUpdateExecuted) {
325 t.Error("POST_UPDATE callback was not executed")
326 }
327
Thomas Lee Se5a44012019-11-07 20:32:24 +0530328 d, err := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, "")
329 if err != nil {
330 BenchmarkProxy_Logger.Errorf("Failed to get device info from test proxy due to error: %v", err)
331 assert.NotNil(t, err)
332 }
333 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500334 t.Error("Failed to find updated device (root proxy)")
335 } else {
336 djson, _ := json.Marshal(d)
337 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
338 }
339 }
340}
341
342func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
343 // Get a device proxy and update a specific port
Thomas Lee Se5a44012019-11-07 20:32:24 +0530344 devFlowsProxy, err := TestProxy_Root.node.CreateProxy(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", false)
345 if err != nil {
346 log.With(log.Fields{"error": err}).Fatal("Cannot create device flows proxy")
347 }
348 flows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
349 if err != nil {
350 BenchmarkProxy_Logger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
351 assert.NotNil(t, err)
352 }
sbarbari17d7e222019-11-05 10:02:29 -0500353 flows.(*openflow_13.Flows).Items[0].TableId = 2244
354
355 preUpdateExecuted := make(chan struct{})
356 postUpdateExecuted := make(chan struct{})
357 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
358
359 devFlowsProxy.RegisterCallback(
360 PRE_UPDATE,
361 commonChanCallback,
362 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecutedPtr,
363 )
364 devFlowsProxy.RegisterCallback(
365 POST_UPDATE,
366 commonChanCallback,
367 "POST_UPDATE instructions (flows proxy)", &postUpdateExecutedPtr,
368 )
369
Thomas Lee Se5a44012019-11-07 20:32:24 +0530370 kvFlows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
371 if err != nil {
372 BenchmarkProxy_Logger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
373 assert.NotNil(t, err)
374 }
sbarbari17d7e222019-11-05 10:02:29 -0500375
376 if reflect.DeepEqual(flows, kvFlows) {
377 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
378 }
379
Thomas Lee Se5a44012019-11-07 20:32:24 +0530380 updated, err := devFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
381 if err != nil {
382 BenchmarkProxy_Logger.Errorf("Failed to update flows in device flows proxy due to error: %v", err)
383 assert.NotNil(t, err)
384 }
385 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500386 t.Error("Failed to update flow")
387 } else {
388 t.Logf("Updated flows : %+v", updated)
389 }
390
391 if !verifyGotResponse(preUpdateExecuted) {
392 t.Error("PRE_UPDATE callback was not executed")
393 }
394 if !verifyGotResponse(postUpdateExecuted) {
395 t.Error("POST_UPDATE callback was not executed")
396 }
397
Thomas Lee Se5a44012019-11-07 20:32:24 +0530398 d, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
399 if err != nil {
400 BenchmarkProxy_Logger.Errorf("Failed to get flows in device flows proxy due to error: %v", err)
401 assert.NotNil(t, err)
402 }
403 if d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500404 t.Error("Failed to find updated flows (flows proxy)")
405 } else {
406 djson, _ := json.Marshal(d)
407 t.Logf("Found flows (flows proxy): %s", string(djson))
408 }
409
Thomas Lee Se5a44012019-11-07 20:32:24 +0530410 d, err = TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", 1, false, "")
411 if err != nil {
412 BenchmarkProxy_Logger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
413 assert.NotNil(t, err)
414 }
415 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500416 t.Error("Failed to find updated flows (root proxy)")
417 } else {
418 djson, _ := json.Marshal(d)
419 t.Logf("Found flows (root proxy): %s", string(djson))
420 }
421}
422
423func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
424 preUpdateExecuted := make(chan struct{})
425 postUpdateExecuted := make(chan struct{})
426 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
427
Thomas Lee Se5a44012019-11-07 20:32:24 +0530428 adaptersProxy, err := TestProxy_Root.node.CreateProxy(context.Background(), "/adapters", false)
429 if err != nil {
430 log.With(log.Fields{"error": err}).Fatal("Cannot create adapters proxy")
431 }
432 retrieved, err := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, "")
433 if err != nil {
434 BenchmarkProxy_Logger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
435 assert.NotNil(t, err)
436 }
437 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500438 t.Error("Failed to get adapter")
439 } else {
440 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
441
442 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
443
444 adaptersProxy.RegisterCallback(
445 PRE_UPDATE,
446 commonChanCallback,
447 "PRE_UPDATE instructions for adapters", &preUpdateExecutedPtr,
448 )
449 adaptersProxy.RegisterCallback(
450 POST_UPDATE,
451 commonChanCallback,
452 "POST_UPDATE instructions for adapters", &postUpdateExecutedPtr,
453 )
454
Thomas Lee Se5a44012019-11-07 20:32:24 +0530455 afterUpdate, err := adaptersProxy.Update(context.Background(), "/"+TestProxy_AdapterId, retrieved, false, "")
456 if err != nil {
457 BenchmarkProxy_Logger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
458 assert.NotNil(t, err)
459 }
460 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500461 t.Error("Failed to update adapter")
462 } else {
463 t.Logf("Updated adapter : %+v", afterUpdate)
464 }
465
466 if !verifyGotResponse(preUpdateExecuted) {
467 t.Error("PRE_UPDATE callback for adapter was not executed")
468 }
469 if !verifyGotResponse(postUpdateExecuted) {
470 t.Error("POST_UPDATE callback for adapter was not executed")
471 }
472
Thomas Lee Se5a44012019-11-07 20:32:24 +0530473 d, err := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, "")
474 if err != nil {
475 BenchmarkProxy_Logger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
476 assert.NotNil(t, err)
477 }
478 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500479 t.Error("Failed to find updated adapter (root proxy)")
480 } else {
481 djson, _ := json.Marshal(d)
482 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
483 }
484 }
485}
486
487func TestProxy_1_4_1_Remove_Device(t *testing.T) {
488 preRemoveExecuted := make(chan struct{})
489 postRemoveExecuted := make(chan struct{})
490 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
491
492 TestProxy_Root_Device.RegisterCallback(
493 PRE_REMOVE,
494 commonChanCallback,
495 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
496 )
497 TestProxy_Root_Device.RegisterCallback(
498 POST_REMOVE,
499 commonChanCallback,
500 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
501 )
502
Thomas Lee Se5a44012019-11-07 20:32:24 +0530503 removed, err := TestProxy_Root_Device.Remove(context.Background(), "/devices/"+TestProxy_DeviceId, "")
504 if err != nil {
505 BenchmarkProxy_Logger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
506 assert.NotNil(t, err)
507 }
508 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500509 t.Error("Failed to remove device")
510 } else {
511 t.Logf("Removed device : %+v", removed)
512 }
513
514 if !verifyGotResponse(preRemoveExecuted) {
515 t.Error("PRE_REMOVE callback was not executed")
516 }
517 if !verifyGotResponse(postRemoveExecuted) {
518 t.Error("POST_REMOVE callback was not executed")
519 }
520
Thomas Lee Se5a44012019-11-07 20:32:24 +0530521 d, err := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, "")
522 if err != nil {
523 BenchmarkProxy_Logger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
524 assert.NotNil(t, err)
525 }
526 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500527 djson, _ := json.Marshal(d)
528 t.Errorf("Device was not removed - %s", djson)
529 } else {
530 t.Logf("Device was removed: %s", TestProxy_DeviceId)
531 }
532}
533
534func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
535
536 ldIDBin, _ := uuid.New().MarshalBinary()
537 TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12]
538 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
539
540 preAddExecuted := make(chan struct{})
541 postAddExecuted := make(chan struct{})
542 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
543
544 // Register
545 TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
546 TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
547
Thomas Lee Se5a44012019-11-07 20:32:24 +0530548 added, err := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, "")
549 if err != nil {
550 BenchmarkProxy_Logger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
551 assert.NotNil(t, err)
552 }
553 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500554 t.Error("Failed to add logical device")
555 } else {
556 t.Logf("Added logical device : %+v", added)
557 }
558
559 verifyGotResponse(postAddExecuted)
560
Thomas Lee Se5a44012019-11-07 20:32:24 +0530561 ld, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, "")
562 if err != nil {
563 BenchmarkProxy_Logger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
564 assert.NotNil(t, err)
565 }
566 if !reflect.ValueOf(ld).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500567 t.Error("Failed to find added logical device")
568 } else {
569 ldJSON, _ := json.Marshal(ld)
570 t.Logf("Found logical device: %s", string(ldJSON))
571 }
572
573 if !verifyGotResponse(preAddExecuted) {
574 t.Error("PRE_ADD callback was not executed")
575 }
576 if !verifyGotResponse(postAddExecuted) {
577 t.Error("POST_ADD callback was not executed")
578 }
579}
580
581func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
582 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
583
Thomas Lee Se5a44012019-11-07 20:32:24 +0530584 added, err := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, "")
585 if err != nil {
586 BenchmarkProxy_Logger.Errorf("Failed to add logical device due to error: %v", err)
587 assert.NotNil(t, err)
588 }
sbarbari17d7e222019-11-05 10:02:29 -0500589 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() {
590 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
591 }
592}
593
594func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530595 logicalDevices, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices", 1, false, "")
596 if err != nil {
597 BenchmarkProxy_Logger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
598 assert.NotNil(t, err)
599 }
sbarbari17d7e222019-11-05 10:02:29 -0500600 if len(logicalDevices.([]interface{})) == 0 {
601 t.Error("there are no available logical devices to retrieve")
602 } else {
603 // Save the target device id for later tests
604 TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
605 t.Logf("retrieved all logical devices: %+v", logicalDevices)
606 }
607}
608
609func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530610 ld, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, "")
611 if err != nil {
612 BenchmarkProxy_Logger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
613 assert.NotNil(t, err)
614 }
615 if !reflect.ValueOf(ld).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500616 t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId)
617 } else {
618 ldJSON, _ := json.Marshal(ld)
619 t.Logf("Found logical device: %s", string(ldJSON))
620 }
621
622}
623
624func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
625 var fwVersion int
626 preUpdateExecuted := make(chan struct{})
627 postUpdateExecuted := make(chan struct{})
628 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
629
Thomas Lee Se5a44012019-11-07 20:32:24 +0530630 retrieved, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, "")
631 if err != nil {
632 BenchmarkProxy_Logger.Errorf("Failed to get logical devices due to error: %v", err)
633 assert.NotNil(t, err)
634 }
635 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500636 t.Error("Failed to get logical device")
637 } else {
638 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
639
640 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
641 fwVersion = 0
642 } else {
643 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
644 fwVersion++
645 }
646
647 TestProxy_Root_LogicalDevice.RegisterCallback(
648 PRE_UPDATE,
649 commonChanCallback,
650 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
651 )
652 TestProxy_Root_LogicalDevice.RegisterCallback(
653 POST_UPDATE,
654 commonChanCallback,
655 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
656 )
657
658 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
659
Thomas Lee Se5a44012019-11-07 20:32:24 +0530660 afterUpdate, err := TestProxy_Root_LogicalDevice.Update(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false,
661 "")
662 if err != nil {
663 BenchmarkProxy_Logger.Errorf("Faield to update logical device info due to error: %v", err)
664 assert.NotNil(t, err)
665 }
666 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500667 t.Error("Failed to update logical device")
668 } else {
669 t.Logf("Updated logical device : %+v", afterUpdate)
670 }
671
672 if !verifyGotResponse(preUpdateExecuted) {
673 t.Error("PRE_UPDATE callback was not executed")
674 }
675 if !verifyGotResponse(postUpdateExecuted) {
676 t.Error("POST_UPDATE callback was not executed")
677 }
678
Thomas Lee Se5a44012019-11-07 20:32:24 +0530679 d, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, "")
680 if err != nil {
681 BenchmarkProxy_Logger.Errorf("Failed to get logical device info due to error: %v", err)
682 assert.NotNil(t, err)
683 }
684 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500685 t.Error("Failed to find updated logical device (root proxy)")
686 } else {
687 djson, _ := json.Marshal(d)
688
689 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
690 }
691 }
692}
693
694func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
695 // Get a device proxy and update a specific port
Thomas Lee Se5a44012019-11-07 20:32:24 +0530696 ldFlowsProxy, err := TestProxy_Root.node.CreateProxy(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false)
697 if err != nil {
698 log.With(log.Fields{"error": err}).Fatal("Failed to create logical device flows proxy")
699 }
700 flows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
701 if err != nil {
702 BenchmarkProxy_Logger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
703 assert.NotNil(t, err)
704 }
sbarbari17d7e222019-11-05 10:02:29 -0500705 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
706 t.Logf("before updated flows: %+v", flows)
707
708 ldFlowsProxy.RegisterCallback(
709 PRE_UPDATE,
710 commonCallback2,
711 )
712 ldFlowsProxy.RegisterCallback(
713 POST_UPDATE,
714 commonCallback2,
715 )
716
Thomas Lee Se5a44012019-11-07 20:32:24 +0530717 kvFlows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
718 if err != nil {
719 BenchmarkProxy_Logger.Errorf("Faield to get flows from logical device flows proxy due to error: %v", err)
720 assert.NotNil(t, err)
721 }
sbarbari17d7e222019-11-05 10:02:29 -0500722 if reflect.DeepEqual(flows, kvFlows) {
723 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
724 }
725
Thomas Lee Se5a44012019-11-07 20:32:24 +0530726 updated, err := ldFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
727 if err != nil {
728 BenchmarkProxy_Logger.Errorf("Failed to update flows in logical device flows proxy due to error: %v", err)
729 assert.NotNil(t, err)
730 }
731 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500732 t.Error("Failed to update logical device flows")
733 } else {
734 t.Logf("Updated logical device flows : %+v", updated)
735 }
736
Thomas Lee Se5a44012019-11-07 20:32:24 +0530737 if d, _ := ldFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500738 t.Error("Failed to find updated logical device flows (flows proxy)")
739 } else {
740 djson, _ := json.Marshal(d)
741 t.Logf("Found flows (flows proxy): %s", string(djson))
742 }
743
Thomas Lee Se5a44012019-11-07 20:32:24 +0530744 d, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false,
745 "")
746 if err != nil {
747 BenchmarkProxy_Logger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
748 assert.NotNil(t, err)
749 }
750 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500751 t.Error("Failed to find updated logical device flows (root proxy)")
752 } else {
753 djson, _ := json.Marshal(d)
754 t.Logf("Found logical device flows (root proxy): %s", string(djson))
755 }
756}
757
758func TestProxy_2_4_1_Remove_Device(t *testing.T) {
759 preRemoveExecuted := make(chan struct{})
760 postRemoveExecuted := make(chan struct{})
761 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
762
763 TestProxy_Root_LogicalDevice.RegisterCallback(
764 PRE_REMOVE,
765 commonChanCallback,
766 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
767 )
768 TestProxy_Root_LogicalDevice.RegisterCallback(
769 POST_REMOVE,
770 commonChanCallback,
771 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
772 )
773
Thomas Lee Se5a44012019-11-07 20:32:24 +0530774 removed, err := TestProxy_Root_LogicalDevice.Remove(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, "")
775 if err != nil {
776 BenchmarkProxy_Logger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
777 assert.NotNil(t, err)
778 }
779 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500780 t.Error("Failed to remove logical device")
781 } else {
782 t.Logf("Removed device : %+v", removed)
783 }
784
785 if !verifyGotResponse(preRemoveExecuted) {
786 t.Error("PRE_REMOVE callback was not executed")
787 }
788 if !verifyGotResponse(postRemoveExecuted) {
789 t.Error("POST_REMOVE callback was not executed")
790 }
791
Thomas Lee Se5a44012019-11-07 20:32:24 +0530792 d, err := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, "")
793 if err != nil {
794 BenchmarkProxy_Logger.Errorf("Failed to get logical device info due to error: %v", err)
795 assert.NotNil(t, err)
796 }
797 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500798 djson, _ := json.Marshal(d)
799 t.Errorf("Device was not removed - %s", djson)
800 } else {
801 t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId)
802 }
803}
804
805// -----------------------------
806// Callback tests
807// -----------------------------
808
809func TestProxy_Callbacks_1_Register(t *testing.T) {
810 TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
811
812 m := make(map[string]string)
813 m["name"] = "fghij"
814 TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
815
816 d := &voltha.Device{Id: "12345"}
817 TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
818}
819
820func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
821 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil)
822}
823
824func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
825 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil)
826}
827
828func TestProxy_Callbacks_4_Unregister(t *testing.T) {
829 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback)
830 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback)
831 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback)
832}
833
834//func TestProxy_Callbacks_5_Add(t *testing.T) {
835// TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
836//}
837//
838//func TestProxy_Callbacks_6_Execute(t *testing.T) {
839// TestProxy_Root_Device.Root.ExecuteCallbacks()
840//}