blob: a73a071122ed3072f925af62538981ca9ed13caf [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"
Kent Hagermanf5a67352020-04-30 15:15:26 -040022 "strconv"
23 "strings"
24 "sync"
25 "testing"
26
sbarbari17d7e222019-11-05 10:02:29 -050027 "github.com/google/uuid"
Kent Hagerman4f355f52020-03-30 16:01:33 -040028 "github.com/opencord/voltha-lib-go/v3/pkg/db"
29 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
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 (
Kent Hagerman4f355f52020-03-30 16:01:33 -040038 BenchmarkProxyLogger log.Logger
npujar9a30c702019-11-14 17:06:39 +053039 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() {
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
Kent Hagermanf5a67352020-04-30 15:15:26 -040070 proxy := NewDBPath(mockBackend)
71 TestProxyRootLogicalDevice = proxy.Proxy("logical_devices")
72 TestProxyRootDevice = proxy.Proxy("device")
73 TestProxyRootAdapter = proxy.Proxy("adapters")
sbarbari17d7e222019-11-05 10:02:29 -050074
npujar9a30c702019-11-14 17:06:39 +053075 TestProxyLogicalPorts = []*voltha.LogicalPort{
sbarbari17d7e222019-11-05 10:02:29 -050076 {
77 Id: "123",
78 DeviceId: "logicalport-0-device-id",
79 DevicePortNo: 123,
80 RootPort: false,
81 },
82 }
npujar9a30c702019-11-14 17:06:39 +053083 TestProxyPorts = []*voltha.Port{
sbarbari17d7e222019-11-05 10:02:29 -050084 {
85 PortNo: 123,
86 Label: "test-port-0",
87 Type: voltha.Port_PON_OLT,
88 AdminState: common.AdminState_ENABLED,
89 OperStatus: common.OperStatus_ACTIVE,
90 DeviceId: "etcd_port-0-device-id",
91 Peers: []*voltha.Port_PeerPort{},
92 },
93 }
94
npujar9a30c702019-11-14 17:06:39 +053095 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050096 Id: 1111,
97 }
npujar9a30c702019-11-14 17:06:39 +053098 TestProxyFlows = &openflow_13.Flows{
99 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
sbarbari17d7e222019-11-05 10:02:29 -0500100 }
npujar9a30c702019-11-14 17:06:39 +0530101 TestProxyDevice = &voltha.Device{
102 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500103 Type: "simulated_olt",
104 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
105 AdminState: voltha.AdminState_PREPROVISIONED,
npujar9a30c702019-11-14 17:06:39 +0530106 Flows: TestProxyFlows,
107 Ports: TestProxyPorts,
sbarbari17d7e222019-11-05 10:02:29 -0500108 }
109
npujar9a30c702019-11-14 17:06:39 +0530110 TestProxyLogicalDevice = &voltha.LogicalDevice{
111 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500112 DatapathId: 0,
npujar9a30c702019-11-14 17:06:39 +0530113 Ports: TestProxyLogicalPorts,
114 Flows: TestProxyFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500115 }
116
npujar9a30c702019-11-14 17:06:39 +0530117 TestProxyAdapter = &voltha.Adapter{
118 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -0500119 Vendor: "test-adapter-vendor",
120 Version: "test-adapter-version",
121 }
122}
123
Kent Hagerman4f355f52020-03-30 16:01:33 -0400124type mockKV struct {
125 kvstore.Client // pretend we implement everything
126
127 mutex sync.RWMutex
128 data map[string]interface{}
129}
130
131func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) {
132 kv.mutex.RLock()
133 defer kv.mutex.RUnlock()
134
135 ret := make(map[string]*kvstore.KVPair, len(kv.data))
136 for k, v := range kv.data {
137 if strings.HasPrefix(k, key) {
138 ret[k] = &kvstore.KVPair{Key: k, Value: v}
139 }
140 }
141 return ret, nil
142}
143func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) {
144 kv.mutex.RLock()
145 defer kv.mutex.RUnlock()
146
147 if val, have := kv.data[key]; have {
148 return &kvstore.KVPair{Key: key, Value: val}, nil
149 }
150 return nil, nil
151}
152func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error {
153 kv.mutex.Lock()
154 defer kv.mutex.Unlock()
155
156 kv.data[key] = value
157 return nil
158}
159func (kv *mockKV) Delete(_ context.Context, key string) error {
160 kv.mutex.Lock()
161 defer kv.mutex.Unlock()
162
163 delete(kv.data, key)
164 return nil
165}
166
167var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}}
168
sbarbari17d7e222019-11-05 10:02:29 -0500169func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
170 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530171 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
172 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500173
Kent Hagermanf5a67352020-04-30 15:15:26 -0400174 if err := TestProxyRootDevice.Set(context.Background(), TestProxyDeviceID, TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530175 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400176 t.Errorf("failed to add device: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530177 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400178 t.Logf("Added device : %+v", TestProxyDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500179
180 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400181 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400182 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530183 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530184 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400185 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500186 t.Error("Failed to find added device")
187 } else {
188 djson, _ := json.Marshal(d)
189 t.Logf("Found device: %s", string(djson))
190 }
191}
192
193func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530194 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500195
Kent Hagermanf5a67352020-04-30 15:15:26 -0400196 if err := TestProxyRootDevice.Set(context.Background(), "devices", TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530197 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530198 assert.NotNil(t, err)
199 }
sbarbari17d7e222019-11-05 10:02:29 -0500200
Kent Hagerman4f355f52020-03-30 16:01:33 -0400201 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400202 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400203 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
204 assert.NotNil(t, err)
205 } else if !have {
206 t.Error("Failed to find added device")
207 } else {
208 if d.String() != TestProxyDevice.String() {
209 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
210 }
211 djson, _ := json.Marshal(d)
212 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500213 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400214
sbarbari17d7e222019-11-05 10:02:29 -0500215}
216
217func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530218 TestProxyAdapterID = "test-adapter"
219 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500220
221 // Add the adapter
Kent Hagermanf5a67352020-04-30 15:15:26 -0400222 if err := TestProxyRootAdapter.Set(context.Background(), TestProxyAdapterID, TestProxyAdapter); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530223 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530224 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500225 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400226 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500227 }
228
sbarbari17d7e222019-11-05 10:02:29 -0500229 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400230 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400231 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530232 BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530233 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400234 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500235 t.Error("Failed to find added adapter")
236 } else {
237 djson, _ := json.Marshal(d)
238 t.Logf("Found adapter: %s", string(djson))
239 }
240
sbarbari17d7e222019-11-05 10:02:29 -0500241}
242
243func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400244 var devices []*voltha.Device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400245 if err := TestProxyRootDevice.List(context.Background(), &devices); 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 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400249 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500250 t.Error("there are no available devices to retrieve")
251 } else {
252 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400253 TestProxyTargetDeviceID = devices[0].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) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400259 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400260 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); 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)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400263 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530264 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500265 } else {
266 djson, _ := json.Marshal(d)
267 t.Logf("Found device: %s", string(djson))
268 }
269}
270
271func TestProxy_1_3_1_Update_Device(t *testing.T) {
272 var fwVersion int
273
Kent Hagerman4f355f52020-03-30 16:01:33 -0400274 retrieved := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400275 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530276 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530277 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400278 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500279 t.Error("Failed to get device")
280 } else {
281 t.Logf("Found raw device (root proxy): %+v", retrieved)
282
Kent Hagerman4f355f52020-03-30 16:01:33 -0400283 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500284 fwVersion = 0
285 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400286 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500287 fwVersion++
288 }
289
Kent Hagerman4f355f52020-03-30 16:01:33 -0400290 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500291
Kent Hagermanf5a67352020-04-30 15:15:26 -0400292 if err := TestProxyRootDevice.Set(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530293 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530294 assert.NotNil(t, err)
295 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400296 afterUpdate := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400297 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, afterUpdate); err != nil {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400298 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
299 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500300 t.Error("Failed to update device")
301 } else {
302 t.Logf("Updated device : %+v", afterUpdate)
303 }
304
Kent Hagerman4f355f52020-03-30 16:01:33 -0400305 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400306 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530307 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530308 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400309 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500310 t.Error("Failed to find updated device (root proxy)")
311 } else {
312 djson, _ := json.Marshal(d)
313 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
314 }
315 }
316}
317
sbarbari17d7e222019-11-05 10:02:29 -0500318func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
sbarbari17d7e222019-11-05 10:02:29 -0500319
Kent Hagermanf5a67352020-04-30 15:15:26 -0400320 adaptersProxy := NewDBPath(mockBackend).Proxy("adapters")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400321
322 retrieved := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400323 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530324 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530325 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400326 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500327 t.Error("Failed to get adapter")
328 } else {
329 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
330
Kent Hagerman4f355f52020-03-30 16:01:33 -0400331 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500332
Kent Hagermanf5a67352020-04-30 15:15:26 -0400333 if err := adaptersProxy.Set(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530334 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530335 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500336 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400337 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500338 }
339
Kent Hagerman4f355f52020-03-30 16:01:33 -0400340 d := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400341 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530342 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530343 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400344 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500345 t.Error("Failed to find updated adapter (root proxy)")
346 } else {
347 djson, _ := json.Marshal(d)
348 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
349 }
350 }
351}
352
353func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagermanf5a67352020-04-30 15:15:26 -0400354 if err := TestProxyRootDevice.Remove(context.Background(), TestProxyDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530355 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400356 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500357 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400358 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500359 }
360
Kent Hagerman4f355f52020-03-30 16:01:33 -0400361 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400362 have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530363 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530364 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530365 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400366 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500367 djson, _ := json.Marshal(d)
368 t.Errorf("Device was not removed - %s", djson)
369 } else {
npujar9a30c702019-11-14 17:06:39 +0530370 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500371 }
372}
373
374func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
375
376 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530377 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
378 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500379
Kent Hagermanf5a67352020-04-30 15:15:26 -0400380 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530381 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530382 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500383 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400384 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500385 }
386
Kent Hagerman4f355f52020-03-30 16:01:33 -0400387 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400388 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530389 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 +0530390 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400391 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500392 t.Error("Failed to find added logical device")
393 } else {
394 ldJSON, _ := json.Marshal(ld)
395 t.Logf("Found logical device: %s", string(ldJSON))
396 }
sbarbari17d7e222019-11-05 10:02:29 -0500397}
398
399func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530400 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500401
Kent Hagermanf5a67352020-04-30 15:15:26 -0400402 if err := TestProxyRootLogicalDevice.Set(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530403 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530404 assert.NotNil(t, err)
405 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400406
407 device := &voltha.LogicalDevice{}
408 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil {
409 BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
410 assert.NotNil(t, err)
411 } else if !have {
412 t.Error("Failed to find added logical device")
413 } else {
414 if device.String() != TestProxyLogicalDevice.String() {
415 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
416 }
sbarbari17d7e222019-11-05 10:02:29 -0500417 }
418}
419
420func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400421 var logicalDevices []*voltha.LogicalDevice
Kent Hagermanf5a67352020-04-30 15:15:26 -0400422 if err := TestProxyRootLogicalDevice.List(context.Background(), &logicalDevices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530423 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530424 assert.NotNil(t, err)
425 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400426 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500427 t.Error("there are no available logical devices to retrieve")
428 } else {
429 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400430 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500431 t.Logf("retrieved all logical devices: %+v", logicalDevices)
432 }
433}
434
435func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400436 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400437 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530438 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530439 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400440 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530441 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500442 } else {
443 ldJSON, _ := json.Marshal(ld)
444 t.Logf("Found logical device: %s", string(ldJSON))
445 }
446
447}
448
449func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
450 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500451
Kent Hagerman4f355f52020-03-30 16:01:33 -0400452 retrieved := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400453 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530454 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530455 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400456 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500457 t.Error("Failed to get logical device")
458 } else {
459 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
460
Kent Hagerman4f355f52020-03-30 16:01:33 -0400461 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500462 fwVersion = 0
463 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400464 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500465 fwVersion++
466 }
467
Kent Hagerman4f355f52020-03-30 16:01:33 -0400468 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500469
Kent Hagermanf5a67352020-04-30 15:15:26 -0400470 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530471 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530472 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500473 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400474 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500475 }
476
Kent Hagerman4f355f52020-03-30 16:01:33 -0400477 d := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400478 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530479 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530480 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400481 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500482 t.Error("Failed to find updated logical device (root proxy)")
483 } else {
484 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500485 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
486 }
487 }
488}
489
sbarbari17d7e222019-11-05 10:02:29 -0500490func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400491 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530492 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400493 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500494 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400495 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500496 }
497
Kent Hagerman4f355f52020-03-30 16:01:33 -0400498 d := &voltha.LogicalDevice{}
499 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530500 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530501 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400502 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500503 djson, _ := json.Marshal(d)
504 t.Errorf("Device was not removed - %s", djson)
505 } else {
npujar9a30c702019-11-14 17:06:39 +0530506 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500507 }
508}