blob: 550260fa9216f751bfc8057a2e046532da78979d [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 (
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{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400174 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000175 BenchmarkProxyLogger.Errorf(ctx, "Failed get device info from test proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400176 assert.NotNil(t, err)
177 } else if !have {
178 t.Error("Failed to find added device")
179 } else {
180 if d.String() != TestProxyDevice.String() {
181 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
182 }
183 djson, _ := json.Marshal(d)
184 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500185 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400186
sbarbari17d7e222019-11-05 10:02:29 -0500187}
188
189func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000190 ctx := context.Background()
npujar9a30c702019-11-14 17:06:39 +0530191 TestProxyAdapterID = "test-adapter"
192 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500193
194 // Add the adapter
Kent Hagermanf5a67352020-04-30 15:15:26 -0400195 if err := TestProxyRootAdapter.Set(context.Background(), TestProxyAdapterID, TestProxyAdapter); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000196 BenchmarkProxyLogger.Errorf(ctx, "Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530197 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500198 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400199 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500200 }
201
sbarbari17d7e222019-11-05 10:02:29 -0500202 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400203 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400204 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000205 BenchmarkProxyLogger.Errorf(ctx, "Failed to retrieve device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530206 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400207 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500208 t.Error("Failed to find added adapter")
209 } else {
210 djson, _ := json.Marshal(d)
211 t.Logf("Found adapter: %s", string(djson))
212 }
213
sbarbari17d7e222019-11-05 10:02:29 -0500214}
215
216func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000217 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400218 var devices []*voltha.Device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400219 if err := TestProxyRootDevice.List(context.Background(), &devices); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000220 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 +0530221 assert.NotNil(t, err)
222 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400223 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500224 t.Error("there are no available devices to retrieve")
225 } else {
226 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400227 TestProxyTargetDeviceID = devices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500228 t.Logf("retrieved all devices: %+v", devices)
229 }
230}
231
232func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000233 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400234 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400235 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000236 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 +0530237 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400238 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530239 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500240 } else {
241 djson, _ := json.Marshal(d)
242 t.Logf("Found device: %s", string(djson))
243 }
244}
245
246func TestProxy_1_3_1_Update_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000247 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500248 var fwVersion int
249
Kent Hagerman4f355f52020-03-30 16:01:33 -0400250 retrieved := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400251 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000252 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530253 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400254 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500255 t.Error("Failed to get device")
256 } else {
257 t.Logf("Found raw device (root proxy): %+v", retrieved)
258
Kent Hagerman4f355f52020-03-30 16:01:33 -0400259 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500260 fwVersion = 0
261 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400262 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500263 fwVersion++
264 }
265
Kent Hagerman4f355f52020-03-30 16:01:33 -0400266 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500267
Kent Hagermanf5a67352020-04-30 15:15:26 -0400268 if err := TestProxyRootDevice.Set(context.Background(), TestProxyTargetDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000269 BenchmarkProxyLogger.Errorf(ctx, "Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530270 assert.NotNil(t, err)
271 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400272 afterUpdate := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400273 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, afterUpdate); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000274 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400275 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500276 t.Error("Failed to update device")
277 } else {
278 t.Logf("Updated device : %+v", afterUpdate)
279 }
280
Kent Hagerman4f355f52020-03-30 16:01:33 -0400281 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400282 if have, err := TestProxyRootDevice.Get(context.Background(), TestProxyTargetDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000283 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530284 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400285 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500286 t.Error("Failed to find updated device (root proxy)")
287 } else {
288 djson, _ := json.Marshal(d)
289 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
290 }
291 }
292}
293
sbarbari17d7e222019-11-05 10:02:29 -0500294func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000295 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500296
Kent Hagermanf5a67352020-04-30 15:15:26 -0400297 adaptersProxy := NewDBPath(mockBackend).Proxy("adapters")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400298
299 retrieved := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400300 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000301 BenchmarkProxyLogger.Errorf(ctx, "Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530302 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400303 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500304 t.Error("Failed to get adapter")
305 } else {
306 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
307
Kent Hagerman4f355f52020-03-30 16:01:33 -0400308 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500309
Kent Hagermanf5a67352020-04-30 15:15:26 -0400310 if err := adaptersProxy.Set(context.Background(), TestProxyAdapterID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000311 BenchmarkProxyLogger.Errorf(ctx, "Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530312 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500313 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400314 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500315 }
316
Kent Hagerman4f355f52020-03-30 16:01:33 -0400317 d := &voltha.Adapter{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400318 if have, err := TestProxyRootAdapter.Get(context.Background(), TestProxyAdapterID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000319 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 +0530320 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400321 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500322 t.Error("Failed to find updated adapter (root proxy)")
323 } else {
324 djson, _ := json.Marshal(d)
325 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
326 }
327 }
328}
329
330func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000331 ctx := context.Background()
Kent Hagermanf5a67352020-04-30 15:15:26 -0400332 if err := TestProxyRootDevice.Remove(context.Background(), TestProxyDeviceID); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000333 BenchmarkProxyLogger.Errorf(ctx, "Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400334 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500335 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400336 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500337 }
338
Kent Hagerman4f355f52020-03-30 16:01:33 -0400339 d := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400340 have, err := TestProxyRootDevice.Get(context.Background(), TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530341 if err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000342 BenchmarkProxyLogger.Errorf(ctx, "Failed to get device info from devices 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 djson, _ := json.Marshal(d)
346 t.Errorf("Device was not removed - %s", djson)
347 } else {
npujar9a30c702019-11-14 17:06:39 +0530348 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500349 }
350}
351
352func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000353 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500354
355 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530356 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
357 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500358
Kent Hagermanf5a67352020-04-30 15:15:26 -0400359 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000360 BenchmarkProxyLogger.Errorf(ctx, "Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530361 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500362 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400363 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500364 }
365
Kent Hagerman4f355f52020-03-30 16:01:33 -0400366 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400367 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyLogicalDeviceID, ld); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000368 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 +0530369 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400370 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500371 t.Error("Failed to find added logical device")
372 } else {
373 ldJSON, _ := json.Marshal(ld)
374 t.Logf("Found logical device: %s", string(ldJSON))
375 }
sbarbari17d7e222019-11-05 10:02:29 -0500376}
377
378func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000379 ctx := context.Background()
npujar9a30c702019-11-14 17:06:39 +0530380 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500381
Kent Hagermanf5a67352020-04-30 15:15:26 -0400382 if err := TestProxyRootLogicalDevice.Set(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000383 BenchmarkProxyLogger.Errorf(ctx, "Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530384 assert.NotNil(t, err)
385 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400386
387 device := &voltha.LogicalDevice{}
388 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000389 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 -0400390 assert.NotNil(t, err)
391 } else if !have {
392 t.Error("Failed to find added logical device")
393 } else {
394 if device.String() != TestProxyLogicalDevice.String() {
395 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
396 }
sbarbari17d7e222019-11-05 10:02:29 -0500397 }
398}
399
400func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000401 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400402 var logicalDevices []*voltha.LogicalDevice
Kent Hagermanf5a67352020-04-30 15:15:26 -0400403 if err := TestProxyRootLogicalDevice.List(context.Background(), &logicalDevices); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000404 BenchmarkProxyLogger.Errorf(ctx, "Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530405 assert.NotNil(t, err)
406 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400407 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500408 t.Error("there are no available logical devices to retrieve")
409 } else {
410 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400411 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500412 t.Logf("retrieved all logical devices: %+v", logicalDevices)
413 }
414}
415
416func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000417 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400418 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400419 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, ld); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000420 BenchmarkProxyLogger.Errorf(ctx, "Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530421 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400422 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530423 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500424 } else {
425 ldJSON, _ := json.Marshal(ld)
426 t.Logf("Found logical device: %s", string(ldJSON))
427 }
428
429}
430
431func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000432 ctx := context.Background()
sbarbari17d7e222019-11-05 10:02:29 -0500433 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500434
Kent Hagerman4f355f52020-03-30 16:01:33 -0400435 retrieved := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400436 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000437 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical devices 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 {
sbarbari17d7e222019-11-05 10:02:29 -0500440 t.Error("Failed to get logical device")
441 } else {
442 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
443
Kent Hagerman4f355f52020-03-30 16:01:33 -0400444 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500445 fwVersion = 0
446 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400447 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500448 fwVersion++
449 }
450
Kent Hagerman4f355f52020-03-30 16:01:33 -0400451 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500452
Kent Hagermanf5a67352020-04-30 15:15:26 -0400453 if err := TestProxyRootLogicalDevice.Set(context.Background(), TestProxyTargetLogicalDeviceID, retrieved); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000454 BenchmarkProxyLogger.Errorf(ctx, "Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530455 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500456 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400457 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500458 }
459
Kent Hagerman4f355f52020-03-30 16:01:33 -0400460 d := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400461 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), TestProxyTargetLogicalDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000462 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530463 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400464 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500465 t.Error("Failed to find updated logical device (root proxy)")
466 } else {
467 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500468 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
469 }
470 }
471}
472
sbarbari17d7e222019-11-05 10:02:29 -0500473func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000474 ctx := context.Background()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400475 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000476 BenchmarkProxyLogger.Errorf(ctx, "Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400477 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500478 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400479 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500480 }
481
Kent Hagerman4f355f52020-03-30 16:01:33 -0400482 d := &voltha.LogicalDevice{}
483 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000484 BenchmarkProxyLogger.Errorf(ctx, "Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530485 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400486 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500487 djson, _ := json.Marshal(d)
488 t.Errorf("Device was not removed - %s", djson)
489 } else {
npujar9a30c702019-11-14 17:06:39 +0530490 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500491 }
492}