blob: 498eb457d7b8a9ecab7338ee652fbe5daed71255 [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080031 "github.com/opencord/voltha-protos/v3/go/openflow_13"
32 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053033 "github.com/stretchr/testify/assert"
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
npujar9a30c702019-11-14 17:06:39 +053046 TestProxyStats *openflow_13.OfpFlowStats
npujar9a30c702019-11-14 17:06:39 +053047 TestProxyDevice *voltha.Device
48 TestProxyLogicalDevice *voltha.LogicalDevice
49 TestProxyAdapter *voltha.Adapter
sbarbari17d7e222019-11-05 10:02:29 -050050)
51
52func init() {
Kent Hagerman4f355f52020-03-30 16:01:33 -040053 BenchmarkProxyLogger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
54 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
55 //Setup default logger - applies for packages that do not have specific logger set
56 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
57 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053058 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040059
60 // Update all loggers (provisioned via init) with a common field
61 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
62 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053063 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040064 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
65
Kent Hagermanf5a67352020-04-30 15:15:26 -040066 proxy := NewDBPath(mockBackend)
67 TestProxyRootLogicalDevice = proxy.Proxy("logical_devices")
68 TestProxyRootDevice = proxy.Proxy("device")
69 TestProxyRootAdapter = proxy.Proxy("adapters")
sbarbari17d7e222019-11-05 10:02:29 -050070
npujar9a30c702019-11-14 17:06:39 +053071 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050072 Id: 1111,
73 }
npujar9a30c702019-11-14 17:06:39 +053074 TestProxyDevice = &voltha.Device{
75 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050076 Type: "simulated_olt",
77 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
78 AdminState: voltha.AdminState_PREPROVISIONED,
sbarbari17d7e222019-11-05 10:02:29 -050079 }
80
npujar9a30c702019-11-14 17:06:39 +053081 TestProxyLogicalDevice = &voltha.LogicalDevice{
82 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050083 DatapathId: 0,
sbarbari17d7e222019-11-05 10:02:29 -050084 }
85
npujar9a30c702019-11-14 17:06:39 +053086 TestProxyAdapter = &voltha.Adapter{
87 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -050088 Vendor: "test-adapter-vendor",
89 Version: "test-adapter-version",
90 }
91}
92
Kent Hagerman4f355f52020-03-30 16:01:33 -040093type mockKV struct {
94 kvstore.Client // pretend we implement everything
95
96 mutex sync.RWMutex
97 data map[string]interface{}
98}
99
100func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) {
101 kv.mutex.RLock()
102 defer kv.mutex.RUnlock()
103
104 ret := make(map[string]*kvstore.KVPair, len(kv.data))
105 for k, v := range kv.data {
106 if strings.HasPrefix(k, key) {
107 ret[k] = &kvstore.KVPair{Key: k, Value: v}
108 }
109 }
110 return ret, nil
111}
112func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) {
113 kv.mutex.RLock()
114 defer kv.mutex.RUnlock()
115
116 if val, have := kv.data[key]; have {
117 return &kvstore.KVPair{Key: key, Value: val}, nil
118 }
119 return nil, nil
120}
121func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error {
122 kv.mutex.Lock()
123 defer kv.mutex.Unlock()
124
125 kv.data[key] = value
126 return nil
127}
128func (kv *mockKV) Delete(_ context.Context, key string) error {
129 kv.mutex.Lock()
130 defer kv.mutex.Unlock()
131
132 delete(kv.data, key)
133 return nil
134}
135
136var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}}
137
sbarbari17d7e222019-11-05 10:02:29 -0500138func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
139 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530140 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
141 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500142
Kent Hagermanf5a67352020-04-30 15:15:26 -0400143 if err := TestProxyRootDevice.Set(context.Background(), TestProxyDeviceID, TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530144 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400145 t.Errorf("failed to add device: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530146 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400147 t.Logf("Added device : %+v", TestProxyDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500148
149 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400150 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400151 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530152 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530153 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400154 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500155 t.Error("Failed to find added device")
156 } else {
157 djson, _ := json.Marshal(d)
158 t.Logf("Found device: %s", string(djson))
159 }
160}
161
162func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530163 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500164
Kent Hagermanf5a67352020-04-30 15:15:26 -0400165 if err := TestProxyRootDevice.Set(context.Background(), "devices", TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530166 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530167 assert.NotNil(t, err)
168 }
sbarbari17d7e222019-11-05 10:02:29 -0500169
Kent Hagerman4f355f52020-03-30 16:01:33 -0400170 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400171 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400172 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
173 assert.NotNil(t, err)
174 } else if !have {
175 t.Error("Failed to find added device")
176 } else {
177 if d.String() != TestProxyDevice.String() {
178 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
179 }
180 djson, _ := json.Marshal(d)
181 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500182 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400183
sbarbari17d7e222019-11-05 10:02:29 -0500184}
185
186func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530187 TestProxyAdapterID = "test-adapter"
188 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500189
190 // Add the adapter
Kent Hagermanf5a67352020-04-30 15:15:26 -0400191 if err := TestProxyRootAdapter.Set(context.Background(), TestProxyAdapterID, TestProxyAdapter); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530192 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530193 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500194 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400195 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500196 }
197
sbarbari17d7e222019-11-05 10:02:29 -0500198 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400199 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400200 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530201 BenchmarkProxyLogger.Errorf("Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530202 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400203 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500204 t.Error("Failed to find added adapter")
205 } else {
206 djson, _ := json.Marshal(d)
207 t.Logf("Found adapter: %s", string(djson))
208 }
209
sbarbari17d7e222019-11-05 10:02:29 -0500210}
211
212func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400213 var devices []*voltha.Device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400214 if err := TestProxyRootDevice.List(context.Background(), &devices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530215 BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530216 assert.NotNil(t, err)
217 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400218 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500219 t.Error("there are no available devices to retrieve")
220 } else {
221 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400222 TestProxyTargetDeviceID = devices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500223 t.Logf("retrieved all devices: %+v", devices)
224 }
225}
226
227func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400228 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400229 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530230 BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530231 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400232 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530233 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500234 } else {
235 djson, _ := json.Marshal(d)
236 t.Logf("Found device: %s", string(djson))
237 }
238}
239
240func TestProxy_1_3_1_Update_Device(t *testing.T) {
241 var fwVersion int
242
Kent Hagerman4f355f52020-03-30 16:01:33 -0400243 retrieved := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400244 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530245 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530246 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400247 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500248 t.Error("Failed to get device")
249 } else {
250 t.Logf("Found raw device (root proxy): %+v", retrieved)
251
Kent Hagerman4f355f52020-03-30 16:01:33 -0400252 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500253 fwVersion = 0
254 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400255 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500256 fwVersion++
257 }
258
Kent Hagerman4f355f52020-03-30 16:01:33 -0400259 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500260
Kent Hagermanf5a67352020-04-30 15:15:26 -0400261 if err := TestProxyRootDevice.Set(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530262 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530263 assert.NotNil(t, err)
264 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400265 afterUpdate := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400266 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, afterUpdate); err != nil {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400267 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
268 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500269 t.Error("Failed to update device")
270 } else {
271 t.Logf("Updated device : %+v", afterUpdate)
272 }
273
Kent Hagerman4f355f52020-03-30 16:01:33 -0400274 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400275 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); 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 find updated device (root proxy)")
280 } else {
281 djson, _ := json.Marshal(d)
282 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
283 }
284 }
285}
286
sbarbari17d7e222019-11-05 10:02:29 -0500287func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
sbarbari17d7e222019-11-05 10:02:29 -0500288
Kent Hagermanf5a67352020-04-30 15:15:26 -0400289 adaptersProxy := NewDBPath(mockBackend).Proxy("adapters")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400290
291 retrieved := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400292 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530293 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530294 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400295 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500296 t.Error("Failed to get adapter")
297 } else {
298 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
299
Kent Hagerman4f355f52020-03-30 16:01:33 -0400300 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500301
Kent Hagermanf5a67352020-04-30 15:15:26 -0400302 if err := adaptersProxy.Set(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530303 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530304 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500305 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400306 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500307 }
308
Kent Hagerman4f355f52020-03-30 16:01:33 -0400309 d := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400310 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530311 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530312 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400313 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500314 t.Error("Failed to find updated adapter (root proxy)")
315 } else {
316 djson, _ := json.Marshal(d)
317 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
318 }
319 }
320}
321
322func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagermanf5a67352020-04-30 15:15:26 -0400323 if err := TestProxyRootDevice.Remove(context.Background(), TestProxyDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530324 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400325 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500326 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400327 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500328 }
329
Kent Hagerman4f355f52020-03-30 16:01:33 -0400330 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400331 have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530332 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530333 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530334 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400335 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500336 djson, _ := json.Marshal(d)
337 t.Errorf("Device was not removed - %s", djson)
338 } else {
npujar9a30c702019-11-14 17:06:39 +0530339 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500340 }
341}
342
343func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
344
345 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530346 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
347 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500348
Kent Hagermanf5a67352020-04-30 15:15:26 -0400349 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530350 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530351 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500352 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400353 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500354 }
355
Kent Hagerman4f355f52020-03-30 16:01:33 -0400356 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400357 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530358 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 +0530359 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400360 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500361 t.Error("Failed to find added logical device")
362 } else {
363 ldJSON, _ := json.Marshal(ld)
364 t.Logf("Found logical device: %s", string(ldJSON))
365 }
sbarbari17d7e222019-11-05 10:02:29 -0500366}
367
368func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530369 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500370
Kent Hagermanf5a67352020-04-30 15:15:26 -0400371 if err := TestProxyRootLogicalDevice.Set(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530372 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530373 assert.NotNil(t, err)
374 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400375
376 device := &voltha.LogicalDevice{}
377 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil {
378 BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
379 assert.NotNil(t, err)
380 } else if !have {
381 t.Error("Failed to find added logical device")
382 } else {
383 if device.String() != TestProxyLogicalDevice.String() {
384 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
385 }
sbarbari17d7e222019-11-05 10:02:29 -0500386 }
387}
388
389func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400390 var logicalDevices []*voltha.LogicalDevice
Kent Hagermanf5a67352020-04-30 15:15:26 -0400391 if err := TestProxyRootLogicalDevice.List(context.Background(), &logicalDevices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530392 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530393 assert.NotNil(t, err)
394 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400395 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500396 t.Error("there are no available logical devices to retrieve")
397 } else {
398 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400399 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500400 t.Logf("retrieved all logical devices: %+v", logicalDevices)
401 }
402}
403
404func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400405 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400406 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530407 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530408 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400409 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530410 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500411 } else {
412 ldJSON, _ := json.Marshal(ld)
413 t.Logf("Found logical device: %s", string(ldJSON))
414 }
415
416}
417
418func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
419 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500420
Kent Hagerman4f355f52020-03-30 16:01:33 -0400421 retrieved := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400422 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530423 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530424 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400425 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500426 t.Error("Failed to get logical device")
427 } else {
428 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
429
Kent Hagerman4f355f52020-03-30 16:01:33 -0400430 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500431 fwVersion = 0
432 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400433 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500434 fwVersion++
435 }
436
Kent Hagerman4f355f52020-03-30 16:01:33 -0400437 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500438
Kent Hagermanf5a67352020-04-30 15:15:26 -0400439 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530440 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530441 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500442 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400443 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500444 }
445
Kent Hagerman4f355f52020-03-30 16:01:33 -0400446 d := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400447 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530448 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530449 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400450 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500451 t.Error("Failed to find updated logical device (root proxy)")
452 } else {
453 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500454 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
455 }
456 }
457}
458
sbarbari17d7e222019-11-05 10:02:29 -0500459func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400460 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530461 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400462 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500463 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400464 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500465 }
466
Kent Hagerman4f355f52020-03-30 16:01:33 -0400467 d := &voltha.LogicalDevice{}
468 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530469 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530470 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400471 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500472 djson, _ := json.Marshal(d)
473 t.Errorf("Device was not removed - %s", djson)
474 } else {
npujar9a30c702019-11-14 17:06:39 +0530475 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500476 }
477}