blob: 5bfd8d8835aa41be65301b895db57f1d036ddfbe [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"
npujar9a30c702019-11-14 17:06:39 +053022 "math/rand"
23 "reflect"
24 "strconv"
25 "testing"
26 "time"
27
sbarbari17d7e222019-11-05 10:02:29 -050028 "github.com/golang/protobuf/proto"
29 "github.com/google/uuid"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 "github.com/opencord/voltha-protos/v3/go/common"
32 "github.com/opencord/voltha-protos/v3/go/openflow_13"
33 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053034 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050035)
36
37var (
npujar9a30c702019-11-14 17:06:39 +053038 TestProxyRoot Root
39 TestProxyRootLogicalDevice *Proxy
40 TestProxyRootDevice *Proxy
41 TestProxyRootAdapter *Proxy
42 TestProxyDeviceID string
43 TestProxyAdapterID string
44 TestProxyLogicalDeviceID string
45 TestProxyTargetDeviceID string
46 TestProxyTargetLogicalDeviceID string
47 TestProxyLogicalPorts []*voltha.LogicalPort
48 TestProxyPorts []*voltha.Port
49 TestProxyStats *openflow_13.OfpFlowStats
50 TestProxyFlows *openflow_13.Flows
51 TestProxyDevice *voltha.Device
52 TestProxyLogicalDevice *voltha.LogicalDevice
53 TestProxyAdapter *voltha.Adapter
sbarbari17d7e222019-11-05 10:02:29 -050054)
55
56func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053057 var err error
npujar9a30c702019-11-14 17:06:39 +053058 TestProxyRoot = NewRoot(&voltha.Voltha{}, nil)
59 if TestProxyRootLogicalDevice, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053060 log.With(log.Fields{"error": err}).Fatal("Cannot create logical device proxy")
61 }
npujar9a30c702019-11-14 17:06:39 +053062 if TestProxyRootDevice, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053063 log.With(log.Fields{"error": err}).Fatal("Cannot create device proxy")
64 }
npujar9a30c702019-11-14 17:06:39 +053065 if TestProxyRootAdapter, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053066 log.With(log.Fields{"error": err}).Fatal("Cannot create adapter proxy")
67 }
sbarbari17d7e222019-11-05 10:02:29 -050068
npujar9a30c702019-11-14 17:06:39 +053069 TestProxyLogicalPorts = []*voltha.LogicalPort{
sbarbari17d7e222019-11-05 10:02:29 -050070 {
71 Id: "123",
72 DeviceId: "logicalport-0-device-id",
73 DevicePortNo: 123,
74 RootPort: false,
75 },
76 }
npujar9a30c702019-11-14 17:06:39 +053077 TestProxyPorts = []*voltha.Port{
sbarbari17d7e222019-11-05 10:02:29 -050078 {
79 PortNo: 123,
80 Label: "test-port-0",
81 Type: voltha.Port_PON_OLT,
82 AdminState: common.AdminState_ENABLED,
83 OperStatus: common.OperStatus_ACTIVE,
84 DeviceId: "etcd_port-0-device-id",
85 Peers: []*voltha.Port_PeerPort{},
86 },
87 }
88
npujar9a30c702019-11-14 17:06:39 +053089 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050090 Id: 1111,
91 }
npujar9a30c702019-11-14 17:06:39 +053092 TestProxyFlows = &openflow_13.Flows{
93 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
sbarbari17d7e222019-11-05 10:02:29 -050094 }
npujar9a30c702019-11-14 17:06:39 +053095 TestProxyDevice = &voltha.Device{
96 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050097 Type: "simulated_olt",
98 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
99 AdminState: voltha.AdminState_PREPROVISIONED,
npujar9a30c702019-11-14 17:06:39 +0530100 Flows: TestProxyFlows,
101 Ports: TestProxyPorts,
sbarbari17d7e222019-11-05 10:02:29 -0500102 }
103
npujar9a30c702019-11-14 17:06:39 +0530104 TestProxyLogicalDevice = &voltha.LogicalDevice{
105 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500106 DatapathId: 0,
npujar9a30c702019-11-14 17:06:39 +0530107 Ports: TestProxyLogicalPorts,
108 Flows: TestProxyFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500109 }
110
npujar9a30c702019-11-14 17:06:39 +0530111 TestProxyAdapter = &voltha.Adapter{
112 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -0500113 Vendor: "test-adapter-vendor",
114 Version: "test-adapter-version",
115 }
116}
117
118func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
119 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530120 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
121 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500122
123 preAddExecuted := make(chan struct{})
124 postAddExecuted := make(chan struct{})
125 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
126
npujar9a30c702019-11-14 17:06:39 +0530127 devicesProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/devices", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530128 if err != nil {
129 log.With(log.Fields{"error": err}).Fatal("Cannot create devices proxy")
130 }
npujar9a30c702019-11-14 17:06:39 +0530131 devicesProxy.RegisterCallback(PreAdd, commonCallback2, "PRE_ADD Device container changes")
132 devicesProxy.RegisterCallback(PostAdd, commonCallback2, "POST_ADD Device container changes")
sbarbari17d7e222019-11-05 10:02:29 -0500133
134 // Register ADD instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530135 TestProxyRootDevice.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions", &preAddExecutedPtr)
136 TestProxyRootDevice.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500137
npujar9a30c702019-11-14 17:06:39 +0530138 added, err := TestProxyRootDevice.Add(context.Background(), "/devices", TestProxyDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530139 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530140 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530141 assert.NotNil(t, err)
142 }
143 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500144 t.Error("Failed to add device")
145 } else {
146 t.Logf("Added device : %+v", added)
147 }
148
149 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530150 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500151 }
152 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530153 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500154 }
155
156 // Verify that the added device can now be retrieved
npujar9a30c702019-11-14 17:06:39 +0530157 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530158 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530159 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530160 assert.NotNil(t, err)
161 }
162 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500163 t.Error("Failed to find added device")
164 } else {
165 djson, _ := json.Marshal(d)
166 t.Logf("Found device: %s", string(djson))
167 }
168}
169
170func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530171 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500172
npujar9a30c702019-11-14 17:06:39 +0530173 added, err := TestProxyRootDevice.Add(context.Background(), "/devices", TestProxyDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530174 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530175 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530176 assert.NotNil(t, err)
177 }
npujar9a30c702019-11-14 17:06:39 +0530178 if added.(proto.Message).String() != reflect.ValueOf(TestProxyDevice).Interface().(proto.Message).String() {
179 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, added)
sbarbari17d7e222019-11-05 10:02:29 -0500180 }
181}
182
183func verifyGotResponse(callbackIndicator <-chan struct{}) bool {
184 timeout := time.After(1 * time.Second)
185 // Wait until the channel closes, or we time out
186 select {
187 case <-callbackIndicator:
188 // Received response successfully
189 return true
190
191 case <-timeout:
192 // Got a timeout! fail with a timeout error
193 return false
194 }
195}
196
197func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530198 TestProxyAdapterID = "test-adapter"
199 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500200 preAddExecuted := make(chan struct{})
201 postAddExecuted := make(chan struct{})
202 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
203
204 // Register ADD instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530205 TestProxyRootAdapter.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions for adapters", &preAddExecutedPtr)
206 TestProxyRootAdapter.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions for adapters", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500207
208 // Add the adapter
npujar9a30c702019-11-14 17:06:39 +0530209 added, err := TestProxyRootAdapter.Add(context.Background(), "/adapters", TestProxyAdapter, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530210 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530211 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530212 assert.NotNil(t, err)
213 }
214 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500215 t.Error("Failed to add adapter")
216 } else {
217 t.Logf("Added adapter : %+v", added)
218 }
219
220 verifyGotResponse(postAddExecuted)
221
222 // Verify that the added device can now be retrieved
npujar9a30c702019-11-14 17:06:39 +0530223 d, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530224 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530225 BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530226 assert.NotNil(t, err)
227 }
228 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500229 t.Error("Failed to find added adapter")
230 } else {
231 djson, _ := json.Marshal(d)
232 t.Logf("Found adapter: %s", string(djson))
233 }
234
235 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530236 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500237 }
238 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530239 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500240 }
241}
242
243func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530244 devices, err := TestProxyRootDevice.Get(context.Background(), "/devices", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530245 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530246 BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530247 assert.NotNil(t, err)
248 }
sbarbari17d7e222019-11-05 10:02:29 -0500249 if len(devices.([]interface{})) == 0 {
250 t.Error("there are no available devices to retrieve")
251 } else {
252 // Save the target device id for later tests
npujar9a30c702019-11-14 17:06:39 +0530253 TestProxyTargetDeviceID = devices.([]interface{})[0].(*voltha.Device).Id
sbarbari17d7e222019-11-05 10:02:29 -0500254 t.Logf("retrieved all devices: %+v", devices)
255 }
256}
257
258func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530259 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530260 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530261 BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530262 assert.NotNil(t, err)
263 }
264 if !reflect.ValueOf(d).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530265 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500266 } else {
267 djson, _ := json.Marshal(d)
268 t.Logf("Found device: %s", string(djson))
269 }
270}
271
272func TestProxy_1_3_1_Update_Device(t *testing.T) {
273 var fwVersion int
274
275 preUpdateExecuted := make(chan struct{})
276 postUpdateExecuted := make(chan struct{})
277 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
278
npujar9a30c702019-11-14 17:06:39 +0530279 retrieved, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530280 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530281 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530282 assert.NotNil(t, err)
283 }
284 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500285 t.Error("Failed to get device")
286 } else {
287 t.Logf("Found raw device (root proxy): %+v", retrieved)
288
289 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
290 fwVersion = 0
291 } else {
292 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
293 fwVersion++
294 }
295
296 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
297
npujar9a30c702019-11-14 17:06:39 +0530298 TestProxyRootDevice.RegisterCallback(
299 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500300 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530301 "PreUpdate instructions (root proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500302 )
npujar9a30c702019-11-14 17:06:39 +0530303 TestProxyRootDevice.RegisterCallback(
304 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500305 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530306 "PostUpdate instructions (root proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500307 )
308
npujar9a30c702019-11-14 17:06:39 +0530309 afterUpdate, err := TestProxyRootDevice.Update(context.Background(), "/devices/"+TestProxyTargetDeviceID, retrieved, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530310 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530311 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530312 assert.NotNil(t, err)
313 }
314 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500315 t.Error("Failed to update device")
316 } else {
317 t.Logf("Updated device : %+v", afterUpdate)
318 }
319
320 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530321 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500322 }
323 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530324 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500325 }
326
npujar9a30c702019-11-14 17:06:39 +0530327 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530328 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530329 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530330 assert.NotNil(t, err)
331 }
332 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500333 t.Error("Failed to find updated device (root proxy)")
334 } else {
335 djson, _ := json.Marshal(d)
336 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
337 }
338 }
339}
340
341func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
342 // Get a device proxy and update a specific port
npujar9a30c702019-11-14 17:06:39 +0530343 devFlowsProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/devices/"+TestProxyDeviceID+"/flows", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530344 if err != nil {
345 log.With(log.Fields{"error": err}).Fatal("Cannot create device flows proxy")
346 }
347 flows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
348 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530349 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530350 assert.NotNil(t, err)
351 }
sbarbari17d7e222019-11-05 10:02:29 -0500352 flows.(*openflow_13.Flows).Items[0].TableId = 2244
353
354 preUpdateExecuted := make(chan struct{})
355 postUpdateExecuted := make(chan struct{})
356 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
357
358 devFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530359 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500360 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530361 "PreUpdate instructions (flows proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500362 )
363 devFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530364 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500365 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530366 "PostUpdate instructions (flows proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500367 )
368
Thomas Lee Se5a44012019-11-07 20:32:24 +0530369 kvFlows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
370 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530371 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530372 assert.NotNil(t, err)
373 }
sbarbari17d7e222019-11-05 10:02:29 -0500374
375 if reflect.DeepEqual(flows, kvFlows) {
376 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
377 }
378
Thomas Lee Se5a44012019-11-07 20:32:24 +0530379 updated, err := devFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
380 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530381 BenchmarkProxyLogger.Errorf("Failed to update flows in device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530382 assert.NotNil(t, err)
383 }
384 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500385 t.Error("Failed to update flow")
386 } else {
387 t.Logf("Updated flows : %+v", updated)
388 }
389
390 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530391 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500392 }
393 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530394 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500395 }
396
Thomas Lee Se5a44012019-11-07 20:32:24 +0530397 d, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
398 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530399 BenchmarkProxyLogger.Errorf("Failed to get flows in device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530400 assert.NotNil(t, err)
401 }
402 if d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500403 t.Error("Failed to find updated flows (flows proxy)")
404 } else {
405 djson, _ := json.Marshal(d)
406 t.Logf("Found flows (flows proxy): %s", string(djson))
407 }
408
npujar9a30c702019-11-14 17:06:39 +0530409 d, err = TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID+"/flows", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530410 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530411 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530412 assert.NotNil(t, err)
413 }
414 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500415 t.Error("Failed to find updated flows (root proxy)")
416 } else {
417 djson, _ := json.Marshal(d)
418 t.Logf("Found flows (root proxy): %s", string(djson))
419 }
420}
421
422func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
423 preUpdateExecuted := make(chan struct{})
424 postUpdateExecuted := make(chan struct{})
425 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
426
npujar9a30c702019-11-14 17:06:39 +0530427 adaptersProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/adapters", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530428 if err != nil {
429 log.With(log.Fields{"error": err}).Fatal("Cannot create adapters proxy")
430 }
npujar9a30c702019-11-14 17:06:39 +0530431 retrieved, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530432 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530433 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530434 assert.NotNil(t, err)
435 }
436 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500437 t.Error("Failed to get adapter")
438 } else {
439 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
440
441 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
442
443 adaptersProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530444 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500445 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530446 "PreUpdate instructions for adapters", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500447 )
448 adaptersProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530449 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500450 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530451 "PostUpdate instructions for adapters", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500452 )
453
npujar9a30c702019-11-14 17:06:39 +0530454 afterUpdate, err := adaptersProxy.Update(context.Background(), "/"+TestProxyAdapterID, retrieved, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530455 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530456 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530457 assert.NotNil(t, err)
458 }
459 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500460 t.Error("Failed to update adapter")
461 } else {
462 t.Logf("Updated adapter : %+v", afterUpdate)
463 }
464
465 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530466 t.Error("PreUpdate callback for adapter was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500467 }
468 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530469 t.Error("PostUpdate callback for adapter was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500470 }
471
npujar9a30c702019-11-14 17:06:39 +0530472 d, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530473 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530474 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530475 assert.NotNil(t, err)
476 }
477 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500478 t.Error("Failed to find updated adapter (root proxy)")
479 } else {
480 djson, _ := json.Marshal(d)
481 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
482 }
483 }
484}
485
486func TestProxy_1_4_1_Remove_Device(t *testing.T) {
487 preRemoveExecuted := make(chan struct{})
488 postRemoveExecuted := make(chan struct{})
489 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
490
npujar9a30c702019-11-14 17:06:39 +0530491 TestProxyRootDevice.RegisterCallback(
492 PreRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500493 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530494 "PreRemove instructions (root proxy)", &preRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500495 )
npujar9a30c702019-11-14 17:06:39 +0530496 TestProxyRootDevice.RegisterCallback(
497 PostRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500498 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530499 "PostRemove instructions (root proxy)", &postRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500500 )
501
npujar9a30c702019-11-14 17:06:39 +0530502 removed, err := TestProxyRootDevice.Remove(context.Background(), "/devices/"+TestProxyDeviceID, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530503 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530504 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530505 assert.NotNil(t, err)
506 }
507 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500508 t.Error("Failed to remove device")
509 } else {
510 t.Logf("Removed device : %+v", removed)
511 }
512
513 if !verifyGotResponse(preRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530514 t.Error("PreRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500515 }
516 if !verifyGotResponse(postRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530517 t.Error("PostRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500518 }
519
npujar9a30c702019-11-14 17:06:39 +0530520 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530521 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530522 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530523 assert.NotNil(t, err)
524 }
525 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500526 djson, _ := json.Marshal(d)
527 t.Errorf("Device was not removed - %s", djson)
528 } else {
npujar9a30c702019-11-14 17:06:39 +0530529 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500530 }
531}
532
533func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
534
535 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530536 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
537 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500538
539 preAddExecuted := make(chan struct{})
540 postAddExecuted := make(chan struct{})
541 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
542
543 // Register
npujar9a30c702019-11-14 17:06:39 +0530544 TestProxyRootLogicalDevice.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions", &preAddExecutedPtr)
545 TestProxyRootLogicalDevice.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500546
npujar9a30c702019-11-14 17:06:39 +0530547 added, err := TestProxyRootLogicalDevice.Add(context.Background(), "/logical_devices", TestProxyLogicalDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530548 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530549 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530550 assert.NotNil(t, err)
551 }
552 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500553 t.Error("Failed to add logical device")
554 } else {
555 t.Logf("Added logical device : %+v", added)
556 }
557
558 verifyGotResponse(postAddExecuted)
559
npujar9a30c702019-11-14 17:06:39 +0530560 ld, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530561 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530562 BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530563 assert.NotNil(t, err)
564 }
565 if !reflect.ValueOf(ld).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500566 t.Error("Failed to find added logical device")
567 } else {
568 ldJSON, _ := json.Marshal(ld)
569 t.Logf("Found logical device: %s", string(ldJSON))
570 }
571
572 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530573 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500574 }
575 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530576 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500577 }
578}
579
580func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530581 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500582
npujar9a30c702019-11-14 17:06:39 +0530583 added, err := TestProxyRootLogicalDevice.Add(context.Background(), "/logical_devices", TestProxyLogicalDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530584 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530585 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530586 assert.NotNil(t, err)
587 }
npujar9a30c702019-11-14 17:06:39 +0530588 if added.(proto.Message).String() != reflect.ValueOf(TestProxyLogicalDevice).Interface().(proto.Message).String() {
589 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, added)
sbarbari17d7e222019-11-05 10:02:29 -0500590 }
591}
592
593func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530594 logicalDevices, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530595 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530596 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530597 assert.NotNil(t, err)
598 }
sbarbari17d7e222019-11-05 10:02:29 -0500599 if len(logicalDevices.([]interface{})) == 0 {
600 t.Error("there are no available logical devices to retrieve")
601 } else {
602 // Save the target device id for later tests
npujar9a30c702019-11-14 17:06:39 +0530603 TestProxyTargetLogicalDeviceID = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
sbarbari17d7e222019-11-05 10:02:29 -0500604 t.Logf("retrieved all logical devices: %+v", logicalDevices)
605 }
606}
607
608func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530609 ld, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530610 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530611 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530612 assert.NotNil(t, err)
613 }
614 if !reflect.ValueOf(ld).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530615 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500616 } else {
617 ldJSON, _ := json.Marshal(ld)
618 t.Logf("Found logical device: %s", string(ldJSON))
619 }
620
621}
622
623func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
624 var fwVersion int
625 preUpdateExecuted := make(chan struct{})
626 postUpdateExecuted := make(chan struct{})
627 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
628
npujar9a30c702019-11-14 17:06:39 +0530629 retrieved, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530630 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530631 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530632 assert.NotNil(t, err)
633 }
634 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500635 t.Error("Failed to get logical device")
636 } else {
637 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
638
639 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
640 fwVersion = 0
641 } else {
642 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
643 fwVersion++
644 }
645
npujar9a30c702019-11-14 17:06:39 +0530646 TestProxyRootLogicalDevice.RegisterCallback(
647 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500648 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530649 "PreUpdate instructions (root proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500650 )
npujar9a30c702019-11-14 17:06:39 +0530651 TestProxyRootLogicalDevice.RegisterCallback(
652 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500653 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530654 "PostUpdate instructions (root proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500655 )
656
657 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
658
npujar9a30c702019-11-14 17:06:39 +0530659 afterUpdate, err := TestProxyRootLogicalDevice.Update(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved, false,
Thomas Lee Se5a44012019-11-07 20:32:24 +0530660 "")
661 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530662 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530663 assert.NotNil(t, err)
664 }
665 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500666 t.Error("Failed to update logical device")
667 } else {
668 t.Logf("Updated logical device : %+v", afterUpdate)
669 }
670
671 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530672 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500673 }
674 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530675 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500676 }
677
npujar9a30c702019-11-14 17:06:39 +0530678 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530679 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530680 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530681 assert.NotNil(t, err)
682 }
683 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500684 t.Error("Failed to find updated logical device (root proxy)")
685 } else {
686 djson, _ := json.Marshal(d)
687
688 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
689 }
690 }
691}
692
693func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
694 // Get a device proxy and update a specific port
npujar9a30c702019-11-14 17:06:39 +0530695 ldFlowsProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID+"/flows", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530696 if err != nil {
697 log.With(log.Fields{"error": err}).Fatal("Failed to create logical device flows proxy")
698 }
699 flows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
700 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530701 BenchmarkProxyLogger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530702 assert.NotNil(t, err)
703 }
sbarbari17d7e222019-11-05 10:02:29 -0500704 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
705 t.Logf("before updated flows: %+v", flows)
706
707 ldFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530708 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500709 commonCallback2,
710 )
711 ldFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530712 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500713 commonCallback2,
714 )
715
Thomas Lee Se5a44012019-11-07 20:32:24 +0530716 kvFlows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
717 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530718 BenchmarkProxyLogger.Errorf("Faield to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530719 assert.NotNil(t, err)
720 }
sbarbari17d7e222019-11-05 10:02:29 -0500721 if reflect.DeepEqual(flows, kvFlows) {
722 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
723 }
724
Thomas Lee Se5a44012019-11-07 20:32:24 +0530725 updated, err := ldFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
726 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530727 BenchmarkProxyLogger.Errorf("Failed to update flows in logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530728 assert.NotNil(t, err)
729 }
730 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500731 t.Error("Failed to update logical device flows")
732 } else {
733 t.Logf("Updated logical device flows : %+v", updated)
734 }
735
Thomas Lee Se5a44012019-11-07 20:32:24 +0530736 if d, _ := ldFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500737 t.Error("Failed to find updated logical device flows (flows proxy)")
738 } else {
739 djson, _ := json.Marshal(d)
740 t.Logf("Found flows (flows proxy): %s", string(djson))
741 }
742
npujar9a30c702019-11-14 17:06:39 +0530743 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID+"/flows", 0, false,
Thomas Lee Se5a44012019-11-07 20:32:24 +0530744 "")
745 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530746 BenchmarkProxyLogger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530747 assert.NotNil(t, err)
748 }
749 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500750 t.Error("Failed to find updated logical device flows (root proxy)")
751 } else {
752 djson, _ := json.Marshal(d)
753 t.Logf("Found logical device flows (root proxy): %s", string(djson))
754 }
755}
756
757func TestProxy_2_4_1_Remove_Device(t *testing.T) {
758 preRemoveExecuted := make(chan struct{})
759 postRemoveExecuted := make(chan struct{})
760 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
761
npujar9a30c702019-11-14 17:06:39 +0530762 TestProxyRootLogicalDevice.RegisterCallback(
763 PreRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500764 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530765 "PreRemove instructions (root proxy)", &preRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500766 )
npujar9a30c702019-11-14 17:06:39 +0530767 TestProxyRootLogicalDevice.RegisterCallback(
768 PostRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500769 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530770 "PostRemove instructions (root proxy)", &postRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500771 )
772
npujar9a30c702019-11-14 17:06:39 +0530773 removed, err := TestProxyRootLogicalDevice.Remove(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530774 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530775 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530776 assert.NotNil(t, err)
777 }
778 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500779 t.Error("Failed to remove logical device")
780 } else {
781 t.Logf("Removed device : %+v", removed)
782 }
783
784 if !verifyGotResponse(preRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530785 t.Error("PreRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500786 }
787 if !verifyGotResponse(postRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530788 t.Error("PostRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500789 }
790
npujar9a30c702019-11-14 17:06:39 +0530791 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530792 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530793 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530794 assert.NotNil(t, err)
795 }
796 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500797 djson, _ := json.Marshal(d)
798 t.Errorf("Device was not removed - %s", djson)
799 } else {
npujar9a30c702019-11-14 17:06:39 +0530800 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500801 }
802}
803
804// -----------------------------
805// Callback tests
806// -----------------------------
807
808func TestProxy_Callbacks_1_Register(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530809 TestProxyRootDevice.RegisterCallback(PreAdd, firstCallback, "abcde", "12345")
sbarbari17d7e222019-11-05 10:02:29 -0500810
811 m := make(map[string]string)
812 m["name"] = "fghij"
npujar9a30c702019-11-14 17:06:39 +0530813 TestProxyRootDevice.RegisterCallback(PreAdd, secondCallback, m, 1.2345)
sbarbari17d7e222019-11-05 10:02:29 -0500814
815 d := &voltha.Device{Id: "12345"}
npujar9a30c702019-11-14 17:06:39 +0530816 TestProxyRootDevice.RegisterCallback(PreAdd, thirdCallback, "klmno", d)
sbarbari17d7e222019-11-05 10:02:29 -0500817}
818
819func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
npujar467fe752020-01-16 20:17:45 +0530820 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
821 defer cancel()
822 TestProxyRootDevice.InvokeCallbacks(ctx, PreAdd, false, nil)
sbarbari17d7e222019-11-05 10:02:29 -0500823}
824
825func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
npujar467fe752020-01-16 20:17:45 +0530826 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
827 defer cancel()
828 TestProxyRootDevice.InvokeCallbacks(ctx, PreAdd, true, nil)
sbarbari17d7e222019-11-05 10:02:29 -0500829}
830
831func TestProxy_Callbacks_4_Unregister(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530832 TestProxyRootDevice.UnregisterCallback(PreAdd, firstCallback)
833 TestProxyRootDevice.UnregisterCallback(PreAdd, secondCallback)
834 TestProxyRootDevice.UnregisterCallback(PreAdd, thirdCallback)
sbarbari17d7e222019-11-05 10:02:29 -0500835}
836
837//func TestProxy_Callbacks_5_Add(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530838// TestProxyRootDevice.Root.AddCallback(TestProxyRootDevice.InvokeCallbacks, PostUpdate, false, "some data", "some new data")
sbarbari17d7e222019-11-05 10:02:29 -0500839//}
840//
841//func TestProxy_Callbacks_6_Execute(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530842// TestProxyRootDevice.Root.ExecuteCallbacks()
sbarbari17d7e222019-11-05 10:02:29 -0500843//}