blob: 77a0dea4cf406d1b2dca133f85315e4ba5b2655e [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"
Thomas Lee Se5a44012019-11-07 20:32:24 +053030 "github.com/opencord/voltha-lib-go/v2/pkg/log"
sbarbari17d7e222019-11-05 10:02:29 -050031 "github.com/opencord/voltha-protos/v2/go/common"
32 "github.com/opencord/voltha-protos/v2/go/openflow_13"
33 "github.com/opencord/voltha-protos/v2/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() {
57 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
58 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
Thomas Lee Se5a44012019-11-07 20:32:24 +053059 var err error
npujar9a30c702019-11-14 17:06:39 +053060 TestProxyRoot = NewRoot(&voltha.Voltha{}, nil)
61 if TestProxyRootLogicalDevice, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053062 log.With(log.Fields{"error": err}).Fatal("Cannot create logical device proxy")
63 }
npujar9a30c702019-11-14 17:06:39 +053064 if TestProxyRootDevice, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053065 log.With(log.Fields{"error": err}).Fatal("Cannot create device proxy")
66 }
npujar9a30c702019-11-14 17:06:39 +053067 if TestProxyRootAdapter, err = TestProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +053068 log.With(log.Fields{"error": err}).Fatal("Cannot create adapter proxy")
69 }
sbarbari17d7e222019-11-05 10:02:29 -050070
npujar9a30c702019-11-14 17:06:39 +053071 TestProxyLogicalPorts = []*voltha.LogicalPort{
sbarbari17d7e222019-11-05 10:02:29 -050072 {
73 Id: "123",
74 DeviceId: "logicalport-0-device-id",
75 DevicePortNo: 123,
76 RootPort: false,
77 },
78 }
npujar9a30c702019-11-14 17:06:39 +053079 TestProxyPorts = []*voltha.Port{
sbarbari17d7e222019-11-05 10:02:29 -050080 {
81 PortNo: 123,
82 Label: "test-port-0",
83 Type: voltha.Port_PON_OLT,
84 AdminState: common.AdminState_ENABLED,
85 OperStatus: common.OperStatus_ACTIVE,
86 DeviceId: "etcd_port-0-device-id",
87 Peers: []*voltha.Port_PeerPort{},
88 },
89 }
90
npujar9a30c702019-11-14 17:06:39 +053091 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050092 Id: 1111,
93 }
npujar9a30c702019-11-14 17:06:39 +053094 TestProxyFlows = &openflow_13.Flows{
95 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
sbarbari17d7e222019-11-05 10:02:29 -050096 }
npujar9a30c702019-11-14 17:06:39 +053097 TestProxyDevice = &voltha.Device{
98 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050099 Type: "simulated_olt",
100 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
101 AdminState: voltha.AdminState_PREPROVISIONED,
npujar9a30c702019-11-14 17:06:39 +0530102 Flows: TestProxyFlows,
103 Ports: TestProxyPorts,
sbarbari17d7e222019-11-05 10:02:29 -0500104 }
105
npujar9a30c702019-11-14 17:06:39 +0530106 TestProxyLogicalDevice = &voltha.LogicalDevice{
107 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500108 DatapathId: 0,
npujar9a30c702019-11-14 17:06:39 +0530109 Ports: TestProxyLogicalPorts,
110 Flows: TestProxyFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500111 }
112
npujar9a30c702019-11-14 17:06:39 +0530113 TestProxyAdapter = &voltha.Adapter{
114 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -0500115 Vendor: "test-adapter-vendor",
116 Version: "test-adapter-version",
117 }
118}
119
120func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
121 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530122 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
123 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500124
125 preAddExecuted := make(chan struct{})
126 postAddExecuted := make(chan struct{})
127 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
128
npujar9a30c702019-11-14 17:06:39 +0530129 devicesProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/devices", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530130 if err != nil {
131 log.With(log.Fields{"error": err}).Fatal("Cannot create devices proxy")
132 }
npujar9a30c702019-11-14 17:06:39 +0530133 devicesProxy.RegisterCallback(PreAdd, commonCallback2, "PRE_ADD Device container changes")
134 devicesProxy.RegisterCallback(PostAdd, commonCallback2, "POST_ADD Device container changes")
sbarbari17d7e222019-11-05 10:02:29 -0500135
136 // Register ADD instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530137 TestProxyRootDevice.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions", &preAddExecutedPtr)
138 TestProxyRootDevice.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500139
npujar9a30c702019-11-14 17:06:39 +0530140 added, err := TestProxyRootDevice.Add(context.Background(), "/devices", TestProxyDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530141 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530142 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530143 assert.NotNil(t, err)
144 }
145 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500146 t.Error("Failed to add device")
147 } else {
148 t.Logf("Added device : %+v", added)
149 }
150
151 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530152 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500153 }
154 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530155 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500156 }
157
158 // Verify that the added device can now be retrieved
npujar9a30c702019-11-14 17:06:39 +0530159 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530160 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530161 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530162 assert.NotNil(t, err)
163 }
164 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500165 t.Error("Failed to find added device")
166 } else {
167 djson, _ := json.Marshal(d)
168 t.Logf("Found device: %s", string(djson))
169 }
170}
171
172func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530173 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500174
npujar9a30c702019-11-14 17:06:39 +0530175 added, err := TestProxyRootDevice.Add(context.Background(), "/devices", TestProxyDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530176 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530177 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530178 assert.NotNil(t, err)
179 }
npujar9a30c702019-11-14 17:06:39 +0530180 if added.(proto.Message).String() != reflect.ValueOf(TestProxyDevice).Interface().(proto.Message).String() {
181 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, added)
sbarbari17d7e222019-11-05 10:02:29 -0500182 }
183}
184
185func verifyGotResponse(callbackIndicator <-chan struct{}) bool {
186 timeout := time.After(1 * time.Second)
187 // Wait until the channel closes, or we time out
188 select {
189 case <-callbackIndicator:
190 // Received response successfully
191 return true
192
193 case <-timeout:
194 // Got a timeout! fail with a timeout error
195 return false
196 }
197}
198
199func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530200 TestProxyAdapterID = "test-adapter"
201 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500202 preAddExecuted := make(chan struct{})
203 postAddExecuted := make(chan struct{})
204 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
205
206 // Register ADD instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530207 TestProxyRootAdapter.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions for adapters", &preAddExecutedPtr)
208 TestProxyRootAdapter.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions for adapters", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500209
210 // Add the adapter
npujar9a30c702019-11-14 17:06:39 +0530211 added, err := TestProxyRootAdapter.Add(context.Background(), "/adapters", TestProxyAdapter, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530212 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530213 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530214 assert.NotNil(t, err)
215 }
216 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500217 t.Error("Failed to add adapter")
218 } else {
219 t.Logf("Added adapter : %+v", added)
220 }
221
222 verifyGotResponse(postAddExecuted)
223
224 // Verify that the added device can now be retrieved
npujar9a30c702019-11-14 17:06:39 +0530225 d, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530226 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530227 BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530228 assert.NotNil(t, err)
229 }
230 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500231 t.Error("Failed to find added adapter")
232 } else {
233 djson, _ := json.Marshal(d)
234 t.Logf("Found adapter: %s", string(djson))
235 }
236
237 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530238 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500239 }
240 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530241 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500242 }
243}
244
245func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530246 devices, err := TestProxyRootDevice.Get(context.Background(), "/devices", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530247 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530248 BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530249 assert.NotNil(t, err)
250 }
sbarbari17d7e222019-11-05 10:02:29 -0500251 if len(devices.([]interface{})) == 0 {
252 t.Error("there are no available devices to retrieve")
253 } else {
254 // Save the target device id for later tests
npujar9a30c702019-11-14 17:06:39 +0530255 TestProxyTargetDeviceID = devices.([]interface{})[0].(*voltha.Device).Id
sbarbari17d7e222019-11-05 10:02:29 -0500256 t.Logf("retrieved all devices: %+v", devices)
257 }
258}
259
260func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530261 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530262 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530263 BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530264 assert.NotNil(t, err)
265 }
266 if !reflect.ValueOf(d).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530267 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500268 } else {
269 djson, _ := json.Marshal(d)
270 t.Logf("Found device: %s", string(djson))
271 }
272}
273
274func TestProxy_1_3_1_Update_Device(t *testing.T) {
275 var fwVersion int
276
277 preUpdateExecuted := make(chan struct{})
278 postUpdateExecuted := make(chan struct{})
279 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
280
npujar9a30c702019-11-14 17:06:39 +0530281 retrieved, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530282 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530283 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530284 assert.NotNil(t, err)
285 }
286 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500287 t.Error("Failed to get device")
288 } else {
289 t.Logf("Found raw device (root proxy): %+v", retrieved)
290
291 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
292 fwVersion = 0
293 } else {
294 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
295 fwVersion++
296 }
297
298 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
299
npujar9a30c702019-11-14 17:06:39 +0530300 TestProxyRootDevice.RegisterCallback(
301 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500302 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530303 "PreUpdate instructions (root proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500304 )
npujar9a30c702019-11-14 17:06:39 +0530305 TestProxyRootDevice.RegisterCallback(
306 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500307 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530308 "PostUpdate instructions (root proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500309 )
310
npujar9a30c702019-11-14 17:06:39 +0530311 afterUpdate, err := TestProxyRootDevice.Update(context.Background(), "/devices/"+TestProxyTargetDeviceID, retrieved, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530312 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530313 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530314 assert.NotNil(t, err)
315 }
316 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500317 t.Error("Failed to update device")
318 } else {
319 t.Logf("Updated device : %+v", afterUpdate)
320 }
321
322 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530323 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500324 }
325 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530326 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500327 }
328
npujar9a30c702019-11-14 17:06:39 +0530329 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyTargetDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530330 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530331 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530332 assert.NotNil(t, err)
333 }
334 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500335 t.Error("Failed to find updated device (root proxy)")
336 } else {
337 djson, _ := json.Marshal(d)
338 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
339 }
340 }
341}
342
343func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
344 // Get a device proxy and update a specific port
npujar9a30c702019-11-14 17:06:39 +0530345 devFlowsProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/devices/"+TestProxyDeviceID+"/flows", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530346 if err != nil {
347 log.With(log.Fields{"error": err}).Fatal("Cannot create device flows proxy")
348 }
349 flows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
350 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530351 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530352 assert.NotNil(t, err)
353 }
sbarbari17d7e222019-11-05 10:02:29 -0500354 flows.(*openflow_13.Flows).Items[0].TableId = 2244
355
356 preUpdateExecuted := make(chan struct{})
357 postUpdateExecuted := make(chan struct{})
358 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
359
360 devFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530361 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500362 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530363 "PreUpdate instructions (flows proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500364 )
365 devFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530366 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500367 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530368 "PostUpdate instructions (flows proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500369 )
370
Thomas Lee Se5a44012019-11-07 20:32:24 +0530371 kvFlows, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
372 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530373 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530374 assert.NotNil(t, err)
375 }
sbarbari17d7e222019-11-05 10:02:29 -0500376
377 if reflect.DeepEqual(flows, kvFlows) {
378 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
379 }
380
Thomas Lee Se5a44012019-11-07 20:32:24 +0530381 updated, err := devFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
382 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530383 BenchmarkProxyLogger.Errorf("Failed to update flows in device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530384 assert.NotNil(t, err)
385 }
386 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500387 t.Error("Failed to update flow")
388 } else {
389 t.Logf("Updated flows : %+v", updated)
390 }
391
392 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530393 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500394 }
395 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530396 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500397 }
398
Thomas Lee Se5a44012019-11-07 20:32:24 +0530399 d, err := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
400 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530401 BenchmarkProxyLogger.Errorf("Failed to get flows in device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530402 assert.NotNil(t, err)
403 }
404 if d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500405 t.Error("Failed to find updated flows (flows proxy)")
406 } else {
407 djson, _ := json.Marshal(d)
408 t.Logf("Found flows (flows proxy): %s", string(djson))
409 }
410
npujar9a30c702019-11-14 17:06:39 +0530411 d, err = TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID+"/flows", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530412 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530413 BenchmarkProxyLogger.Errorf("Failed to get flows from device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530414 assert.NotNil(t, err)
415 }
416 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500417 t.Error("Failed to find updated flows (root proxy)")
418 } else {
419 djson, _ := json.Marshal(d)
420 t.Logf("Found flows (root proxy): %s", string(djson))
421 }
422}
423
424func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
425 preUpdateExecuted := make(chan struct{})
426 postUpdateExecuted := make(chan struct{})
427 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
428
npujar9a30c702019-11-14 17:06:39 +0530429 adaptersProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/adapters", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530430 if err != nil {
431 log.With(log.Fields{"error": err}).Fatal("Cannot create adapters proxy")
432 }
npujar9a30c702019-11-14 17:06:39 +0530433 retrieved, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530434 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530435 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530436 assert.NotNil(t, err)
437 }
438 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500439 t.Error("Failed to get adapter")
440 } else {
441 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
442
443 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
444
445 adaptersProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530446 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500447 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530448 "PreUpdate instructions for adapters", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500449 )
450 adaptersProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530451 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500452 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530453 "PostUpdate instructions for adapters", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500454 )
455
npujar9a30c702019-11-14 17:06:39 +0530456 afterUpdate, err := adaptersProxy.Update(context.Background(), "/"+TestProxyAdapterID, retrieved, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530457 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530458 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530459 assert.NotNil(t, err)
460 }
461 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500462 t.Error("Failed to update adapter")
463 } else {
464 t.Logf("Updated adapter : %+v", afterUpdate)
465 }
466
467 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530468 t.Error("PreUpdate callback for adapter was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500469 }
470 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530471 t.Error("PostUpdate callback for adapter was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500472 }
473
npujar9a30c702019-11-14 17:06:39 +0530474 d, err := TestProxyRootAdapter.Get(context.Background(), "/adapters/"+TestProxyAdapterID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530475 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530476 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530477 assert.NotNil(t, err)
478 }
479 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500480 t.Error("Failed to find updated adapter (root proxy)")
481 } else {
482 djson, _ := json.Marshal(d)
483 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
484 }
485 }
486}
487
488func TestProxy_1_4_1_Remove_Device(t *testing.T) {
489 preRemoveExecuted := make(chan struct{})
490 postRemoveExecuted := make(chan struct{})
491 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
492
npujar9a30c702019-11-14 17:06:39 +0530493 TestProxyRootDevice.RegisterCallback(
494 PreRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500495 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530496 "PreRemove instructions (root proxy)", &preRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500497 )
npujar9a30c702019-11-14 17:06:39 +0530498 TestProxyRootDevice.RegisterCallback(
499 PostRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500500 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530501 "PostRemove instructions (root proxy)", &postRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500502 )
503
npujar9a30c702019-11-14 17:06:39 +0530504 removed, err := TestProxyRootDevice.Remove(context.Background(), "/devices/"+TestProxyDeviceID, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530505 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530506 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530507 assert.NotNil(t, err)
508 }
509 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500510 t.Error("Failed to remove device")
511 } else {
512 t.Logf("Removed device : %+v", removed)
513 }
514
515 if !verifyGotResponse(preRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530516 t.Error("PreRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500517 }
518 if !verifyGotResponse(postRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530519 t.Error("PostRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500520 }
521
npujar9a30c702019-11-14 17:06:39 +0530522 d, err := TestProxyRootDevice.Get(context.Background(), "/devices/"+TestProxyDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530523 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530524 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530525 assert.NotNil(t, err)
526 }
527 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500528 djson, _ := json.Marshal(d)
529 t.Errorf("Device was not removed - %s", djson)
530 } else {
npujar9a30c702019-11-14 17:06:39 +0530531 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500532 }
533}
534
535func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
536
537 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530538 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
539 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500540
541 preAddExecuted := make(chan struct{})
542 postAddExecuted := make(chan struct{})
543 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
544
545 // Register
npujar9a30c702019-11-14 17:06:39 +0530546 TestProxyRootLogicalDevice.RegisterCallback(PreAdd, commonChanCallback, "PreAdd instructions", &preAddExecutedPtr)
547 TestProxyRootLogicalDevice.RegisterCallback(PostAdd, commonChanCallback, "PostAdd instructions", &postAddExecutedPtr)
sbarbari17d7e222019-11-05 10:02:29 -0500548
npujar9a30c702019-11-14 17:06:39 +0530549 added, err := TestProxyRootLogicalDevice.Add(context.Background(), "/logical_devices", TestProxyLogicalDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530550 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530551 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530552 assert.NotNil(t, err)
553 }
554 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500555 t.Error("Failed to add logical device")
556 } else {
557 t.Logf("Added logical device : %+v", added)
558 }
559
560 verifyGotResponse(postAddExecuted)
561
npujar9a30c702019-11-14 17:06:39 +0530562 ld, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530563 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530564 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 +0530565 assert.NotNil(t, err)
566 }
567 if !reflect.ValueOf(ld).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500568 t.Error("Failed to find added logical device")
569 } else {
570 ldJSON, _ := json.Marshal(ld)
571 t.Logf("Found logical device: %s", string(ldJSON))
572 }
573
574 if !verifyGotResponse(preAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530575 t.Error("PreAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500576 }
577 if !verifyGotResponse(postAddExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530578 t.Error("PostAdd callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500579 }
580}
581
582func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530583 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500584
npujar9a30c702019-11-14 17:06:39 +0530585 added, err := TestProxyRootLogicalDevice.Add(context.Background(), "/logical_devices", TestProxyLogicalDevice, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530586 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530587 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530588 assert.NotNil(t, err)
589 }
npujar9a30c702019-11-14 17:06:39 +0530590 if added.(proto.Message).String() != reflect.ValueOf(TestProxyLogicalDevice).Interface().(proto.Message).String() {
591 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, added)
sbarbari17d7e222019-11-05 10:02:29 -0500592 }
593}
594
595func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530596 logicalDevices, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices", 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530597 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530598 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530599 assert.NotNil(t, err)
600 }
sbarbari17d7e222019-11-05 10:02:29 -0500601 if len(logicalDevices.([]interface{})) == 0 {
602 t.Error("there are no available logical devices to retrieve")
603 } else {
604 // Save the target device id for later tests
npujar9a30c702019-11-14 17:06:39 +0530605 TestProxyTargetLogicalDeviceID = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
sbarbari17d7e222019-11-05 10:02:29 -0500606 t.Logf("retrieved all logical devices: %+v", logicalDevices)
607 }
608}
609
610func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530611 ld, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530612 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530613 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530614 assert.NotNil(t, err)
615 }
616 if !reflect.ValueOf(ld).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530617 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500618 } else {
619 ldJSON, _ := json.Marshal(ld)
620 t.Logf("Found logical device: %s", string(ldJSON))
621 }
622
623}
624
625func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
626 var fwVersion int
627 preUpdateExecuted := make(chan struct{})
628 postUpdateExecuted := make(chan struct{})
629 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
630
npujar9a30c702019-11-14 17:06:39 +0530631 retrieved, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530632 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530633 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530634 assert.NotNil(t, err)
635 }
636 if retrieved == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500637 t.Error("Failed to get logical device")
638 } else {
639 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
640
641 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
642 fwVersion = 0
643 } else {
644 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
645 fwVersion++
646 }
647
npujar9a30c702019-11-14 17:06:39 +0530648 TestProxyRootLogicalDevice.RegisterCallback(
649 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500650 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530651 "PreUpdate instructions (root proxy)", &preUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500652 )
npujar9a30c702019-11-14 17:06:39 +0530653 TestProxyRootLogicalDevice.RegisterCallback(
654 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500655 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530656 "PostUpdate instructions (root proxy)", &postUpdateExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500657 )
658
659 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
660
npujar9a30c702019-11-14 17:06:39 +0530661 afterUpdate, err := TestProxyRootLogicalDevice.Update(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved, false,
Thomas Lee Se5a44012019-11-07 20:32:24 +0530662 "")
663 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530664 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530665 assert.NotNil(t, err)
666 }
667 if afterUpdate == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500668 t.Error("Failed to update logical device")
669 } else {
670 t.Logf("Updated logical device : %+v", afterUpdate)
671 }
672
673 if !verifyGotResponse(preUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530674 t.Error("PreUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500675 }
676 if !verifyGotResponse(postUpdateExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530677 t.Error("PostUpdate callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500678 }
679
npujar9a30c702019-11-14 17:06:39 +0530680 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyTargetLogicalDeviceID, 1, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530681 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530682 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530683 assert.NotNil(t, err)
684 }
685 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500686 t.Error("Failed to find updated logical device (root proxy)")
687 } else {
688 djson, _ := json.Marshal(d)
689
690 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
691 }
692 }
693}
694
695func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
696 // Get a device proxy and update a specific port
npujar9a30c702019-11-14 17:06:39 +0530697 ldFlowsProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID+"/flows", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530698 if err != nil {
699 log.With(log.Fields{"error": err}).Fatal("Failed to create logical device flows proxy")
700 }
701 flows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
702 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530703 BenchmarkProxyLogger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530704 assert.NotNil(t, err)
705 }
sbarbari17d7e222019-11-05 10:02:29 -0500706 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
707 t.Logf("before updated flows: %+v", flows)
708
709 ldFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530710 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500711 commonCallback2,
712 )
713 ldFlowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530714 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500715 commonCallback2,
716 )
717
Thomas Lee Se5a44012019-11-07 20:32:24 +0530718 kvFlows, err := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
719 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530720 BenchmarkProxyLogger.Errorf("Faield to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530721 assert.NotNil(t, err)
722 }
sbarbari17d7e222019-11-05 10:02:29 -0500723 if reflect.DeepEqual(flows, kvFlows) {
724 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
725 }
726
Thomas Lee Se5a44012019-11-07 20:32:24 +0530727 updated, err := ldFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, "")
728 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530729 BenchmarkProxyLogger.Errorf("Failed to update flows in logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530730 assert.NotNil(t, err)
731 }
732 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500733 t.Error("Failed to update logical device flows")
734 } else {
735 t.Logf("Updated logical device flows : %+v", updated)
736 }
737
Thomas Lee Se5a44012019-11-07 20:32:24 +0530738 if d, _ := ldFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500739 t.Error("Failed to find updated logical device flows (flows proxy)")
740 } else {
741 djson, _ := json.Marshal(d)
742 t.Logf("Found flows (flows proxy): %s", string(djson))
743 }
744
npujar9a30c702019-11-14 17:06:39 +0530745 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID+"/flows", 0, false,
Thomas Lee Se5a44012019-11-07 20:32:24 +0530746 "")
747 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530748 BenchmarkProxyLogger.Errorf("Failed to get flows from logical device flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530749 assert.NotNil(t, err)
750 }
751 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500752 t.Error("Failed to find updated logical device flows (root proxy)")
753 } else {
754 djson, _ := json.Marshal(d)
755 t.Logf("Found logical device flows (root proxy): %s", string(djson))
756 }
757}
758
759func TestProxy_2_4_1_Remove_Device(t *testing.T) {
760 preRemoveExecuted := make(chan struct{})
761 postRemoveExecuted := make(chan struct{})
762 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
763
npujar9a30c702019-11-14 17:06:39 +0530764 TestProxyRootLogicalDevice.RegisterCallback(
765 PreRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500766 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530767 "PreRemove instructions (root proxy)", &preRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500768 )
npujar9a30c702019-11-14 17:06:39 +0530769 TestProxyRootLogicalDevice.RegisterCallback(
770 PostRemove,
sbarbari17d7e222019-11-05 10:02:29 -0500771 commonChanCallback,
npujar9a30c702019-11-14 17:06:39 +0530772 "PostRemove instructions (root proxy)", &postRemoveExecutedPtr,
sbarbari17d7e222019-11-05 10:02:29 -0500773 )
774
npujar9a30c702019-11-14 17:06:39 +0530775 removed, err := TestProxyRootLogicalDevice.Remove(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530776 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530777 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530778 assert.NotNil(t, err)
779 }
780 if removed == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500781 t.Error("Failed to remove logical device")
782 } else {
783 t.Logf("Removed device : %+v", removed)
784 }
785
786 if !verifyGotResponse(preRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530787 t.Error("PreRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500788 }
789 if !verifyGotResponse(postRemoveExecuted) {
npujar9a30c702019-11-14 17:06:39 +0530790 t.Error("PostRemove callback was not executed")
sbarbari17d7e222019-11-05 10:02:29 -0500791 }
792
npujar9a30c702019-11-14 17:06:39 +0530793 d, err := TestProxyRootLogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxyLogicalDeviceID, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530794 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530795 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530796 assert.NotNil(t, err)
797 }
798 if reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500799 djson, _ := json.Marshal(d)
800 t.Errorf("Device was not removed - %s", djson)
801 } else {
npujar9a30c702019-11-14 17:06:39 +0530802 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500803 }
804}
805
806// -----------------------------
807// Callback tests
808// -----------------------------
809
810func TestProxy_Callbacks_1_Register(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530811 TestProxyRootDevice.RegisterCallback(PreAdd, firstCallback, "abcde", "12345")
sbarbari17d7e222019-11-05 10:02:29 -0500812
813 m := make(map[string]string)
814 m["name"] = "fghij"
npujar9a30c702019-11-14 17:06:39 +0530815 TestProxyRootDevice.RegisterCallback(PreAdd, secondCallback, m, 1.2345)
sbarbari17d7e222019-11-05 10:02:29 -0500816
817 d := &voltha.Device{Id: "12345"}
npujar9a30c702019-11-14 17:06:39 +0530818 TestProxyRootDevice.RegisterCallback(PreAdd, thirdCallback, "klmno", d)
sbarbari17d7e222019-11-05 10:02:29 -0500819}
820
821func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530822 TestProxyRootDevice.InvokeCallbacks(PreAdd, false, nil)
sbarbari17d7e222019-11-05 10:02:29 -0500823}
824
825func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530826 TestProxyRootDevice.InvokeCallbacks(PreAdd, true, nil)
sbarbari17d7e222019-11-05 10:02:29 -0500827}
828
829func TestProxy_Callbacks_4_Unregister(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530830 TestProxyRootDevice.UnregisterCallback(PreAdd, firstCallback)
831 TestProxyRootDevice.UnregisterCallback(PreAdd, secondCallback)
832 TestProxyRootDevice.UnregisterCallback(PreAdd, thirdCallback)
sbarbari17d7e222019-11-05 10:02:29 -0500833}
834
835//func TestProxy_Callbacks_5_Add(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530836// TestProxyRootDevice.Root.AddCallback(TestProxyRootDevice.InvokeCallbacks, PostUpdate, false, "some data", "some new data")
sbarbari17d7e222019-11-05 10:02:29 -0500837//}
838//
839//func TestProxy_Callbacks_6_Execute(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530840// TestProxyRootDevice.Root.ExecuteCallbacks()
sbarbari17d7e222019-11-05 10:02:29 -0500841//}