blob: 4c210370e7e5259ef4e9c912cf33c3befbc5c3e0 [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"
yasin sapli5458a1c2021-06-14 22:24:38 +000028 "github.com/opencord/voltha-lib-go/v5/pkg/db"
29 "github.com/opencord/voltha-lib-go/v5/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Maninderdfadc982020-10-28 14:04:33 +053031 "github.com/opencord/voltha-protos/v4/go/openflow_13"
32 "github.com/opencord/voltha-protos/v4/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 (
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000037 BenchmarkProxyLogger log.CLogger
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() {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000053 BenchmarkProxyLogger, _ = log.RegisterPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
54 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -040055 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
56 //Setup default logger - applies for packages that do not have specific logger set
57 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000058 BenchmarkProxyLogger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053059 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040060
61 // Update all loggers (provisioned via init) with a common field
62 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000063 BenchmarkProxyLogger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053064 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040065 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
66
Kent Hagermanf5a67352020-04-30 15:15:26 -040067 proxy := NewDBPath(mockBackend)
68 TestProxyRootLogicalDevice = proxy.Proxy("logical_devices")
69 TestProxyRootDevice = proxy.Proxy("device")
70 TestProxyRootAdapter = proxy.Proxy("adapters")
sbarbari17d7e222019-11-05 10:02:29 -050071
npujar9a30c702019-11-14 17:06:39 +053072 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050073 Id: 1111,
74 }
npujar9a30c702019-11-14 17:06:39 +053075 TestProxyDevice = &voltha.Device{
76 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050077 Type: "simulated_olt",
78 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
79 AdminState: voltha.AdminState_PREPROVISIONED,
sbarbari17d7e222019-11-05 10:02:29 -050080 }
81
npujar9a30c702019-11-14 17:06:39 +053082 TestProxyLogicalDevice = &voltha.LogicalDevice{
83 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -050084 DatapathId: 0,
sbarbari17d7e222019-11-05 10:02:29 -050085 }
86
npujar9a30c702019-11-14 17:06:39 +053087 TestProxyAdapter = &voltha.Adapter{
88 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -050089 Vendor: "test-adapter-vendor",
90 Version: "test-adapter-version",
91 }
92}
93
Kent Hagerman4f355f52020-03-30 16:01:33 -040094type mockKV struct {
95 kvstore.Client // pretend we implement everything
96
97 mutex sync.RWMutex
98 data map[string]interface{}
99}
100
101func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) {
102 kv.mutex.RLock()
103 defer kv.mutex.RUnlock()
104
105 ret := make(map[string]*kvstore.KVPair, len(kv.data))
106 for k, v := range kv.data {
107 if strings.HasPrefix(k, key) {
108 ret[k] = &kvstore.KVPair{Key: k, Value: v}
109 }
110 }
111 return ret, nil
112}
113func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) {
114 kv.mutex.RLock()
115 defer kv.mutex.RUnlock()
116
117 if val, have := kv.data[key]; have {
118 return &kvstore.KVPair{Key: key, Value: val}, nil
119 }
120 return nil, nil
121}
122func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error {
123 kv.mutex.Lock()
124 defer kv.mutex.Unlock()
125
126 kv.data[key] = value
127 return nil
128}
129func (kv *mockKV) Delete(_ context.Context, key string) error {
130 kv.mutex.Lock()
131 defer kv.mutex.Unlock()
132
133 delete(kv.data, key)
134 return nil
135}
136
137var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}}
138
sbarbari17d7e222019-11-05 10:02:29 -0500139func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000140 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500141 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530142 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
143 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500144
Kent Hagermanf5a67352020-04-30 15:15:26 -0400145 if err := TestProxyRootDevice.Set(context.Background(), TestProxyDeviceID, TestProxyDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000146 BenchmarkProxyLogger.Errorf(ctx, "Failed to add test proxy device due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400147 t.Errorf("failed to add device: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530148 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400149 t.Logf("Added device : %+v", TestProxyDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500150
151 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400152 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400153 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000154 BenchmarkProxyLogger.Errorf(ctx, "Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530155 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400156 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500157 t.Error("Failed to find added device")
158 } else {
159 djson, _ := json.Marshal(d)
160 t.Logf("Found device: %s", string(djson))
161 }
162}
163
164func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000165 ctx := context.Background()
npujar9a30c702019-11-14 17:06:39 +0530166 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500167
Kent Hagermanf5a67352020-04-30 15:15:26 -0400168 if err := TestProxyRootDevice.Set(context.Background(), "devices", TestProxyDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000169 BenchmarkProxyLogger.Errorf(ctx, "Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530170 assert.NotNil(t, err)
171 }
sbarbari17d7e222019-11-05 10:02:29 -0500172
Kent Hagerman4f355f52020-03-30 16:01:33 -0400173 d := &voltha.Device{}
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300174
175 have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d)
176 if err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000177 BenchmarkProxyLogger.Errorf(ctx, "Failed get device info from test proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400178 assert.NotNil(t, err)
179 } else if !have {
180 t.Error("Failed to find added device")
181 } else {
182 if d.String() != TestProxyDevice.String() {
183 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
184 }
185 djson, _ := json.Marshal(d)
186 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500187 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400188
sbarbari17d7e222019-11-05 10:02:29 -0500189}
190
191func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000192 ctx := context.Background()
npujar9a30c702019-11-14 17:06:39 +0530193 TestProxyAdapterID = "test-adapter"
194 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500195
196 // Add the adapter
Kent Hagermanf5a67352020-04-30 15:15:26 -0400197 if err := TestProxyRootAdapter.Set(context.Background(), TestProxyAdapterID, TestProxyAdapter); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000198 BenchmarkProxyLogger.Errorf(ctx, "Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530199 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500200 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400201 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500202 }
203
sbarbari17d7e222019-11-05 10:02:29 -0500204 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400205 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400206 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000207 BenchmarkProxyLogger.Errorf(ctx, "Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530208 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400209 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500210 t.Error("Failed to find added adapter")
211 } else {
212 djson, _ := json.Marshal(d)
213 t.Logf("Found adapter: %s", string(djson))
214 }
215
sbarbari17d7e222019-11-05 10:02:29 -0500216}
217
218func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000219 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400220 var devices []*voltha.Device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400221 if err := TestProxyRootDevice.List(context.Background(), &devices); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000222 BenchmarkProxyLogger.Errorf(ctx, "Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530223 assert.NotNil(t, err)
224 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400225 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500226 t.Error("there are no available devices to retrieve")
227 } else {
228 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400229 TestProxyTargetDeviceID = devices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500230 t.Logf("retrieved all devices: %+v", devices)
231 }
232}
233
234func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000235 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400236 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400237 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000238 BenchmarkProxyLogger.Errorf(ctx, "Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530239 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400240 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530241 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500242 } else {
243 djson, _ := json.Marshal(d)
244 t.Logf("Found device: %s", string(djson))
245 }
246}
247
248func TestProxy_1_3_1_Update_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000249 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500250 var fwVersion int
251
Kent Hagerman4f355f52020-03-30 16:01:33 -0400252 retrieved := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400253 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000254 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530255 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400256 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500257 t.Error("Failed to get device")
258 } else {
259 t.Logf("Found raw device (root proxy): %+v", retrieved)
260
Kent Hagerman4f355f52020-03-30 16:01:33 -0400261 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500262 fwVersion = 0
263 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400264 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500265 fwVersion++
266 }
267
Kent Hagerman4f355f52020-03-30 16:01:33 -0400268 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500269
Kent Hagermanf5a67352020-04-30 15:15:26 -0400270 if err := TestProxyRootDevice.Set(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000271 BenchmarkProxyLogger.Errorf(ctx, "Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530272 assert.NotNil(t, err)
273 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400274 afterUpdate := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400275 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, afterUpdate); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000276 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400277 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500278 t.Error("Failed to update device")
279 } else {
280 t.Logf("Updated device : %+v", afterUpdate)
281 }
282
Kent Hagerman4f355f52020-03-30 16:01:33 -0400283 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400284 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000285 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530286 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400287 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500288 t.Error("Failed to find updated device (root proxy)")
289 } else {
290 djson, _ := json.Marshal(d)
291 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
292 }
293 }
294}
295
sbarbari17d7e222019-11-05 10:02:29 -0500296func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000297 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500298
Kent Hagermanf5a67352020-04-30 15:15:26 -0400299 adaptersProxy := NewDBPath(mockBackend).Proxy("adapters")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400300
301 retrieved := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400302 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000303 BenchmarkProxyLogger.Errorf(ctx, "Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530304 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400305 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500306 t.Error("Failed to get adapter")
307 } else {
308 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
309
Kent Hagerman4f355f52020-03-30 16:01:33 -0400310 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500311
Kent Hagermanf5a67352020-04-30 15:15:26 -0400312 if err := adaptersProxy.Set(context.Background(), TestProxyAdapterID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000313 BenchmarkProxyLogger.Errorf(ctx, "Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530314 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500315 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400316 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500317 }
318
Kent Hagerman4f355f52020-03-30 16:01:33 -0400319 d := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400320 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000321 BenchmarkProxyLogger.Errorf(ctx, "Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530322 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400323 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500324 t.Error("Failed to find updated adapter (root proxy)")
325 } else {
326 djson, _ := json.Marshal(d)
327 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
328 }
329 }
330}
331
332func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000333 ctx := context.Background()
Kent Hagermanf5a67352020-04-30 15:15:26 -0400334 if err := TestProxyRootDevice.Remove(context.Background(), TestProxyDeviceID); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000335 BenchmarkProxyLogger.Errorf(ctx, "Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400336 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500337 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400338 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500339 }
340
Kent Hagerman4f355f52020-03-30 16:01:33 -0400341 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400342 have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530343 if err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000344 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530345 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400346 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500347 djson, _ := json.Marshal(d)
348 t.Errorf("Device was not removed - %s", djson)
349 } else {
npujar9a30c702019-11-14 17:06:39 +0530350 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500351 }
352}
353
354func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000355 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500356
357 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530358 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
359 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500360
Kent Hagermanf5a67352020-04-30 15:15:26 -0400361 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000362 BenchmarkProxyLogger.Errorf(ctx, "Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530363 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500364 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400365 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500366 }
367
Kent Hagerman4f355f52020-03-30 16:01:33 -0400368 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400369 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyLogicalDeviceID, ld); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000370 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info from logical device proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530371 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400372 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500373 t.Error("Failed to find added logical device")
374 } else {
375 ldJSON, _ := json.Marshal(ld)
376 t.Logf("Found logical device: %s", string(ldJSON))
377 }
sbarbari17d7e222019-11-05 10:02:29 -0500378}
379
380func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000381 ctx := context.Background()
npujar9a30c702019-11-14 17:06:39 +0530382 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500383
Kent Hagermanf5a67352020-04-30 15:15:26 -0400384 if err := TestProxyRootLogicalDevice.Set(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000385 BenchmarkProxyLogger.Errorf(ctx, "Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530386 assert.NotNil(t, err)
387 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400388
389 device := &voltha.LogicalDevice{}
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300390 have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device)
391 if err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000392 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info from logical device proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400393 assert.NotNil(t, err)
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300394 return
395 }
396 if !have {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400397 t.Error("Failed to find added logical device")
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300398 return
399 }
400 if device.String() != TestProxyLogicalDevice.String() {
401 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
sbarbari17d7e222019-11-05 10:02:29 -0500402 }
403}
404
405func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000406 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400407 var logicalDevices []*voltha.LogicalDevice
Kent Hagermanf5a67352020-04-30 15:15:26 -0400408 if err := TestProxyRootLogicalDevice.List(context.Background(), &logicalDevices); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000409 BenchmarkProxyLogger.Errorf(ctx, "Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530410 assert.NotNil(t, err)
411 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400412 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500413 t.Error("there are no available logical devices to retrieve")
414 } else {
415 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400416 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500417 t.Logf("retrieved all logical devices: %+v", logicalDevices)
418 }
419}
420
421func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000422 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400423 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400424 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, ld); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000425 BenchmarkProxyLogger.Errorf(ctx, "Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530426 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400427 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530428 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500429 } else {
430 ldJSON, _ := json.Marshal(ld)
431 t.Logf("Found logical device: %s", string(ldJSON))
432 }
433
434}
435
436func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000437 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500438 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500439
Kent Hagerman4f355f52020-03-30 16:01:33 -0400440 retrieved := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400441 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000442 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530443 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400444 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500445 t.Error("Failed to get logical device")
446 } else {
447 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
448
Kent Hagerman4f355f52020-03-30 16:01:33 -0400449 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500450 fwVersion = 0
451 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400452 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500453 fwVersion++
454 }
455
Kent Hagerman4f355f52020-03-30 16:01:33 -0400456 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500457
Kent Hagermanf5a67352020-04-30 15:15:26 -0400458 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000459 BenchmarkProxyLogger.Errorf(ctx, "Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530460 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500461 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400462 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500463 }
464
Kent Hagerman4f355f52020-03-30 16:01:33 -0400465 d := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400466 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000467 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530468 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400469 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500470 t.Error("Failed to find updated logical device (root proxy)")
471 } else {
472 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500473 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
474 }
475 }
476}
477
sbarbari17d7e222019-11-05 10:02:29 -0500478func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000479 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400480 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000481 BenchmarkProxyLogger.Errorf(ctx, "Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400482 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500483 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400484 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500485 }
486
Kent Hagerman4f355f52020-03-30 16:01:33 -0400487 d := &voltha.LogicalDevice{}
488 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000489 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530490 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400491 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500492 djson, _ := json.Marshal(d)
493 t.Errorf("Device was not removed - %s", djson)
494 } else {
npujar9a30c702019-11-14 17:06:39 +0530495 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500496 }
497}