blob: 5cb3aa96a86ac6b3dd564d55961716c132636aee [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"
sbarbari17d7e222019-11-05 10:02:29 -050022 "github.com/google/uuid"
Kent Hagerman4f355f52020-03-30 16:01:33 -040023 "github.com/opencord/voltha-lib-go/v3/pkg/db"
24 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
26 "github.com/opencord/voltha-protos/v3/go/common"
27 "github.com/opencord/voltha-protos/v3/go/openflow_13"
28 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053029 "github.com/stretchr/testify/assert"
Kent Hagerman4f355f52020-03-30 16:01:33 -040030 "strconv"
31 "strings"
32 "sync"
33 "testing"
sbarbari17d7e222019-11-05 10:02:29 -050034)
35
36var (
Kent Hagerman4f355f52020-03-30 16:01:33 -040037 BenchmarkProxyLogger log.Logger
npujar9a30c702019-11-14 17:06:39 +053038 TestProxyRootLogicalDevice *Proxy
39 TestProxyRootDevice *Proxy
40 TestProxyRootAdapter *Proxy
41 TestProxyDeviceID string
42 TestProxyAdapterID string
43 TestProxyLogicalDeviceID string
44 TestProxyTargetDeviceID string
45 TestProxyTargetLogicalDeviceID string
46 TestProxyLogicalPorts []*voltha.LogicalPort
47 TestProxyPorts []*voltha.Port
48 TestProxyStats *openflow_13.OfpFlowStats
49 TestProxyFlows *openflow_13.Flows
50 TestProxyDevice *voltha.Device
51 TestProxyLogicalDevice *voltha.LogicalDevice
52 TestProxyAdapter *voltha.Adapter
sbarbari17d7e222019-11-05 10:02:29 -050053)
54
55func init() {
Neha Sharmabe485932020-05-25 21:52:55 +000056 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -040057 BenchmarkProxyLogger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
58 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
59 //Setup default logger - applies for packages that do not have specific logger set
60 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
61 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053062 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040063
64 // Update all loggers (provisioned via init) with a common field
65 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
66 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053067 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040068 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
69
Neha Sharmabe485932020-05-25 21:52:55 +000070 TestProxyRootLogicalDevice = NewProxy(ctx, mockBackend, "/")
71 TestProxyRootDevice = NewProxy(ctx, mockBackend, "/")
72 TestProxyRootAdapter = NewProxy(ctx, mockBackend, "/")
sbarbari17d7e222019-11-05 10:02:29 -050073
npujar9a30c702019-11-14 17:06:39 +053074 TestProxyLogicalPorts = []*voltha.LogicalPort{
sbarbari17d7e222019-11-05 10:02:29 -050075 {
76 Id: "123",
77 DeviceId: "logicalport-0-device-id",
78 DevicePortNo: 123,
79 RootPort: false,
80 },
81 }
npujar9a30c702019-11-14 17:06:39 +053082 TestProxyPorts = []*voltha.Port{
sbarbari17d7e222019-11-05 10:02:29 -050083 {
84 PortNo: 123,
85 Label: "test-port-0",
86 Type: voltha.Port_PON_OLT,
87 AdminState: common.AdminState_ENABLED,
88 OperStatus: common.OperStatus_ACTIVE,
89 DeviceId: "etcd_port-0-device-id",
90 Peers: []*voltha.Port_PeerPort{},
91 },
92 }
93
npujar9a30c702019-11-14 17:06:39 +053094 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050095 Id: 1111,
96 }
npujar9a30c702019-11-14 17:06:39 +053097 TestProxyFlows = &openflow_13.Flows{
98 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
sbarbari17d7e222019-11-05 10:02:29 -050099 }
npujar9a30c702019-11-14 17:06:39 +0530100 TestProxyDevice = &voltha.Device{
101 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500102 Type: "simulated_olt",
103 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
104 AdminState: voltha.AdminState_PREPROVISIONED,
npujar9a30c702019-11-14 17:06:39 +0530105 Flows: TestProxyFlows,
106 Ports: TestProxyPorts,
sbarbari17d7e222019-11-05 10:02:29 -0500107 }
108
npujar9a30c702019-11-14 17:06:39 +0530109 TestProxyLogicalDevice = &voltha.LogicalDevice{
110 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500111 DatapathId: 0,
npujar9a30c702019-11-14 17:06:39 +0530112 Ports: TestProxyLogicalPorts,
113 Flows: TestProxyFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500114 }
115
npujar9a30c702019-11-14 17:06:39 +0530116 TestProxyAdapter = &voltha.Adapter{
117 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -0500118 Vendor: "test-adapter-vendor",
119 Version: "test-adapter-version",
120 }
121}
122
Kent Hagerman4f355f52020-03-30 16:01:33 -0400123type mockKV struct {
124 kvstore.Client // pretend we implement everything
125
126 mutex sync.RWMutex
127 data map[string]interface{}
128}
129
130func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) {
131 kv.mutex.RLock()
132 defer kv.mutex.RUnlock()
133
134 ret := make(map[string]*kvstore.KVPair, len(kv.data))
135 for k, v := range kv.data {
136 if strings.HasPrefix(k, key) {
137 ret[k] = &kvstore.KVPair{Key: k, Value: v}
138 }
139 }
140 return ret, nil
141}
142func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) {
143 kv.mutex.RLock()
144 defer kv.mutex.RUnlock()
145
146 if val, have := kv.data[key]; have {
147 return &kvstore.KVPair{Key: key, Value: val}, nil
148 }
149 return nil, nil
150}
151func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error {
152 kv.mutex.Lock()
153 defer kv.mutex.Unlock()
154
155 kv.data[key] = value
156 return nil
157}
158func (kv *mockKV) Delete(_ context.Context, key string) error {
159 kv.mutex.Lock()
160 defer kv.mutex.Unlock()
161
162 delete(kv.data, key)
163 return nil
164}
165
166var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}}
167
sbarbari17d7e222019-11-05 10:02:29 -0500168func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
169 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530170 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
171 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500172
Kent Hagerman4f355f52020-03-30 16:01:33 -0400173 if err := TestProxyRootDevice.AddWithID(context.Background(), "devices", TestProxyDeviceID, TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530174 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400175 t.Errorf("failed to add device: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530176 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400177 t.Logf("Added device : %+v", TestProxyDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500178
179 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400180 d := &voltha.Device{}
181 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530182 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530183 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400184 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500185 t.Error("Failed to find added device")
186 } else {
187 djson, _ := json.Marshal(d)
188 t.Logf("Found device: %s", string(djson))
189 }
190}
191
192func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530193 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500194
Kent Hagerman4f355f52020-03-30 16:01:33 -0400195 if err := TestProxyRootDevice.add(context.Background(), "devices", TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530196 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530197 assert.NotNil(t, err)
198 }
sbarbari17d7e222019-11-05 10:02:29 -0500199
Kent Hagerman4f355f52020-03-30 16:01:33 -0400200 d := &voltha.Device{}
201 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d); err != nil {
202 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
203 assert.NotNil(t, err)
204 } else if !have {
205 t.Error("Failed to find added device")
206 } else {
207 if d.String() != TestProxyDevice.String() {
208 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
209 }
210 djson, _ := json.Marshal(d)
211 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500212 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400213
sbarbari17d7e222019-11-05 10:02:29 -0500214}
215
216func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530217 TestProxyAdapterID = "test-adapter"
218 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500219
220 // Add the adapter
Kent Hagerman4f355f52020-03-30 16:01:33 -0400221 if err := TestProxyRootAdapter.AddWithID(context.Background(), "adapters", TestProxyAdapterID, TestProxyAdapter); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530222 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530223 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500224 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400225 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500226 }
227
sbarbari17d7e222019-11-05 10:02:29 -0500228 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400229 d := &voltha.Device{}
230 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530231 BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530232 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400233 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500234 t.Error("Failed to find added adapter")
235 } else {
236 djson, _ := json.Marshal(d)
237 t.Logf("Found adapter: %s", string(djson))
238 }
239
sbarbari17d7e222019-11-05 10:02:29 -0500240}
241
242func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400243 var devices []*voltha.Device
244 if err := TestProxyRootDevice.List(context.Background(), "devices", &devices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530245 BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530246 assert.NotNil(t, err)
247 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400248 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500249 t.Error("there are no available devices to retrieve")
250 } else {
251 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400252 TestProxyTargetDeviceID = devices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500253 t.Logf("retrieved all devices: %+v", devices)
254 }
255}
256
257func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400258 d := &voltha.Device{}
259 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530260 BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530261 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400262 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530263 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500264 } else {
265 djson, _ := json.Marshal(d)
266 t.Logf("Found device: %s", string(djson))
267 }
268}
269
270func TestProxy_1_3_1_Update_Device(t *testing.T) {
271 var fwVersion int
272
Kent Hagerman4f355f52020-03-30 16:01:33 -0400273 retrieved := &voltha.Device{}
274 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530275 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530276 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400277 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500278 t.Error("Failed to get device")
279 } else {
280 t.Logf("Found raw device (root proxy): %+v", retrieved)
281
Kent Hagerman4f355f52020-03-30 16:01:33 -0400282 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500283 fwVersion = 0
284 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400285 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500286 fwVersion++
287 }
288
Kent Hagerman4f355f52020-03-30 16:01:33 -0400289 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500290
Kent Hagerman4f355f52020-03-30 16:01:33 -0400291 if err := TestProxyRootDevice.Update(context.Background(), "devices/"+TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530292 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530293 assert.NotNil(t, err)
294 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400295 afterUpdate := &voltha.Device{}
296 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, afterUpdate); err != nil {
297 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
298 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500299 t.Error("Failed to update device")
300 } else {
301 t.Logf("Updated device : %+v", afterUpdate)
302 }
303
Kent Hagerman4f355f52020-03-30 16:01:33 -0400304 d := &voltha.Device{}
305 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530306 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530307 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400308 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500309 t.Error("Failed to find updated device (root proxy)")
310 } else {
311 djson, _ := json.Marshal(d)
312 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
313 }
314 }
315}
316
sbarbari17d7e222019-11-05 10:02:29 -0500317func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
Neha Sharmabe485932020-05-25 21:52:55 +0000318 ctx := context.Background()
319 adaptersProxy := NewProxy(ctx, mockBackend, "/adapters")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400320
321 retrieved := &voltha.Adapter{}
322 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530323 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530324 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400325 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500326 t.Error("Failed to get adapter")
327 } else {
328 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
329
Kent Hagerman4f355f52020-03-30 16:01:33 -0400330 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500331
Kent Hagerman4f355f52020-03-30 16:01:33 -0400332 if err := adaptersProxy.Update(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530333 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530334 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500335 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400336 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500337 }
338
Kent Hagerman4f355f52020-03-30 16:01:33 -0400339 d := &voltha.Adapter{}
340 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530341 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530342 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400343 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500344 t.Error("Failed to find updated adapter (root proxy)")
345 } else {
346 djson, _ := json.Marshal(d)
347 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
348 }
349 }
350}
351
352func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400353 if err := TestProxyRootDevice.Remove(context.Background(), "devices/"+TestProxyDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530354 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400355 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500356 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400357 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500358 }
359
Kent Hagerman4f355f52020-03-30 16:01:33 -0400360 d := &voltha.Device{}
361 have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530362 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530363 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530364 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400365 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500366 djson, _ := json.Marshal(d)
367 t.Errorf("Device was not removed - %s", djson)
368 } else {
npujar9a30c702019-11-14 17:06:39 +0530369 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500370 }
371}
372
373func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
374
375 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530376 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
377 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500378
Kent Hagerman4f355f52020-03-30 16:01:33 -0400379 if err := TestProxyRootLogicalDevice.AddWithID(context.Background(), "logical_devices", TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530380 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530381 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500382 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400383 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500384 }
385
Kent Hagerman4f355f52020-03-30 16:01:33 -0400386 ld := &voltha.LogicalDevice{}
387 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530388 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 +0530389 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400390 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500391 t.Error("Failed to find added logical device")
392 } else {
393 ldJSON, _ := json.Marshal(ld)
394 t.Logf("Found logical device: %s", string(ldJSON))
395 }
sbarbari17d7e222019-11-05 10:02:29 -0500396}
397
398func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530399 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500400
Kent Hagerman4f355f52020-03-30 16:01:33 -0400401 if err := TestProxyRootLogicalDevice.add(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530402 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530403 assert.NotNil(t, err)
404 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400405
406 device := &voltha.LogicalDevice{}
407 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil {
408 BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
409 assert.NotNil(t, err)
410 } else if !have {
411 t.Error("Failed to find added logical device")
412 } else {
413 if device.String() != TestProxyLogicalDevice.String() {
414 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
415 }
sbarbari17d7e222019-11-05 10:02:29 -0500416 }
417}
418
419func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400420 var logicalDevices []*voltha.LogicalDevice
421 if err := TestProxyRootLogicalDevice.List(context.Background(), "logical_devices", &logicalDevices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530422 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530423 assert.NotNil(t, err)
424 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400425 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500426 t.Error("there are no available logical devices to retrieve")
427 } else {
428 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400429 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500430 t.Logf("retrieved all logical devices: %+v", logicalDevices)
431 }
432}
433
434func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400435 ld := &voltha.LogicalDevice{}
436 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530437 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530438 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400439 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530440 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500441 } else {
442 ldJSON, _ := json.Marshal(ld)
443 t.Logf("Found logical device: %s", string(ldJSON))
444 }
445
446}
447
448func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
449 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500450
Kent Hagerman4f355f52020-03-30 16:01:33 -0400451 retrieved := &voltha.LogicalDevice{}
452 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530453 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530454 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400455 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500456 t.Error("Failed to get logical device")
457 } else {
458 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
459
Kent Hagerman4f355f52020-03-30 16:01:33 -0400460 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500461 fwVersion = 0
462 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400463 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500464 fwVersion++
465 }
466
Kent Hagerman4f355f52020-03-30 16:01:33 -0400467 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500468
Kent Hagerman4f355f52020-03-30 16:01:33 -0400469 if err := TestProxyRootLogicalDevice.Update(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530470 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530471 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500472 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400473 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500474 }
475
Kent Hagerman4f355f52020-03-30 16:01:33 -0400476 d := &voltha.LogicalDevice{}
477 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530478 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530479 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400480 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500481 t.Error("Failed to find updated logical device (root proxy)")
482 } else {
483 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500484 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
485 }
486 }
487}
488
sbarbari17d7e222019-11-05 10:02:29 -0500489func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400490 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530491 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400492 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500493 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400494 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500495 }
496
Kent Hagerman4f355f52020-03-30 16:01:33 -0400497 d := &voltha.LogicalDevice{}
498 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530499 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530500 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400501 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500502 djson, _ := json.Marshal(d)
503 t.Errorf("Device was not removed - %s", djson)
504 } else {
npujar9a30c702019-11-14 17:06:39 +0530505 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500506 }
507}