blob: 683e0a424ccc4805d2191d5629f59f6bf024ef9e [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package model
17
18import (
19 "context"
20 "encoding/hex"
21 "encoding/json"
sbarbari17d7e222019-11-05 10:02:29 -050022 "github.com/google/uuid"
Kent Hagerman4f355f52020-03-30 16:01:33 -040023 "github.com/opencord/voltha-lib-go/v3/pkg/db"
24 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
26 "github.com/opencord/voltha-protos/v3/go/common"
27 "github.com/opencord/voltha-protos/v3/go/openflow_13"
28 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053029 "github.com/stretchr/testify/assert"
Kent Hagerman4f355f52020-03-30 16:01:33 -040030 "strconv"
31 "strings"
32 "sync"
33 "testing"
sbarbari17d7e222019-11-05 10:02:29 -050034)
35
36var (
Kent Hagerman4f355f52020-03-30 16:01:33 -040037 BenchmarkProxyLogger log.Logger
npujar9a30c702019-11-14 17:06:39 +053038 TestProxyRootLogicalDevice *Proxy
39 TestProxyRootDevice *Proxy
40 TestProxyRootAdapter *Proxy
41 TestProxyDeviceID string
42 TestProxyAdapterID string
43 TestProxyLogicalDeviceID string
44 TestProxyTargetDeviceID string
45 TestProxyTargetLogicalDeviceID string
46 TestProxyLogicalPorts []*voltha.LogicalPort
47 TestProxyPorts []*voltha.Port
48 TestProxyStats *openflow_13.OfpFlowStats
49 TestProxyFlows *openflow_13.Flows
50 TestProxyDevice *voltha.Device
51 TestProxyLogicalDevice *voltha.LogicalDevice
52 TestProxyAdapter *voltha.Adapter
sbarbari17d7e222019-11-05 10:02:29 -050053)
54
55func init() {
Kent Hagerman4f355f52020-03-30 16:01:33 -040056 BenchmarkProxyLogger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
57 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
58 //Setup default logger - applies for packages that do not have specific logger set
59 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
60 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053061 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040062
63 // Update all loggers (provisioned via init) with a common field
64 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
65 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Thomas Lee Se5a44012019-11-07 20:32:24 +053066 }
Kent Hagerman4f355f52020-03-30 16:01:33 -040067 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
68
69 TestProxyRootLogicalDevice = NewProxy(mockBackend, "/")
70 TestProxyRootDevice = NewProxy(mockBackend, "/")
71 TestProxyRootAdapter = NewProxy(mockBackend, "/")
sbarbari17d7e222019-11-05 10:02:29 -050072
npujar9a30c702019-11-14 17:06:39 +053073 TestProxyLogicalPorts = []*voltha.LogicalPort{
sbarbari17d7e222019-11-05 10:02:29 -050074 {
75 Id: "123",
76 DeviceId: "logicalport-0-device-id",
77 DevicePortNo: 123,
78 RootPort: false,
79 },
80 }
npujar9a30c702019-11-14 17:06:39 +053081 TestProxyPorts = []*voltha.Port{
sbarbari17d7e222019-11-05 10:02:29 -050082 {
83 PortNo: 123,
84 Label: "test-port-0",
85 Type: voltha.Port_PON_OLT,
86 AdminState: common.AdminState_ENABLED,
87 OperStatus: common.OperStatus_ACTIVE,
88 DeviceId: "etcd_port-0-device-id",
89 Peers: []*voltha.Port_PeerPort{},
90 },
91 }
92
npujar9a30c702019-11-14 17:06:39 +053093 TestProxyStats = &openflow_13.OfpFlowStats{
sbarbari17d7e222019-11-05 10:02:29 -050094 Id: 1111,
95 }
npujar9a30c702019-11-14 17:06:39 +053096 TestProxyFlows = &openflow_13.Flows{
97 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
sbarbari17d7e222019-11-05 10:02:29 -050098 }
npujar9a30c702019-11-14 17:06:39 +053099 TestProxyDevice = &voltha.Device{
100 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500101 Type: "simulated_olt",
102 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
103 AdminState: voltha.AdminState_PREPROVISIONED,
npujar9a30c702019-11-14 17:06:39 +0530104 Flows: TestProxyFlows,
105 Ports: TestProxyPorts,
sbarbari17d7e222019-11-05 10:02:29 -0500106 }
107
npujar9a30c702019-11-14 17:06:39 +0530108 TestProxyLogicalDevice = &voltha.LogicalDevice{
109 Id: TestProxyDeviceID,
sbarbari17d7e222019-11-05 10:02:29 -0500110 DatapathId: 0,
npujar9a30c702019-11-14 17:06:39 +0530111 Ports: TestProxyLogicalPorts,
112 Flows: TestProxyFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500113 }
114
npujar9a30c702019-11-14 17:06:39 +0530115 TestProxyAdapter = &voltha.Adapter{
116 Id: TestProxyAdapterID,
sbarbari17d7e222019-11-05 10:02:29 -0500117 Vendor: "test-adapter-vendor",
118 Version: "test-adapter-version",
119 }
120}
121
Kent Hagerman4f355f52020-03-30 16:01:33 -0400122type mockKV struct {
123 kvstore.Client // pretend we implement everything
124
125 mutex sync.RWMutex
126 data map[string]interface{}
127}
128
129func (kv *mockKV) List(_ context.Context, key string) (map[string]*kvstore.KVPair, error) {
130 kv.mutex.RLock()
131 defer kv.mutex.RUnlock()
132
133 ret := make(map[string]*kvstore.KVPair, len(kv.data))
134 for k, v := range kv.data {
135 if strings.HasPrefix(k, key) {
136 ret[k] = &kvstore.KVPair{Key: k, Value: v}
137 }
138 }
139 return ret, nil
140}
141func (kv *mockKV) Get(_ context.Context, key string) (*kvstore.KVPair, error) {
142 kv.mutex.RLock()
143 defer kv.mutex.RUnlock()
144
145 if val, have := kv.data[key]; have {
146 return &kvstore.KVPair{Key: key, Value: val}, nil
147 }
148 return nil, nil
149}
150func (kv *mockKV) Put(_ context.Context, key string, value interface{}) error {
151 kv.mutex.Lock()
152 defer kv.mutex.Unlock()
153
154 kv.data[key] = value
155 return nil
156}
157func (kv *mockKV) Delete(_ context.Context, key string) error {
158 kv.mutex.Lock()
159 defer kv.mutex.Unlock()
160
161 delete(kv.data, key)
162 return nil
163}
164
165var mockBackend = &db.Backend{Client: &mockKV{data: make(map[string]interface{})}}
166
sbarbari17d7e222019-11-05 10:02:29 -0500167func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
168 devIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530169 TestProxyDeviceID = "0001" + hex.EncodeToString(devIDBin)[:12]
170 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500171
Kent Hagerman4f355f52020-03-30 16:01:33 -0400172 if err := TestProxyRootDevice.AddWithID(context.Background(), "devices", TestProxyDeviceID, TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530173 BenchmarkProxyLogger.Errorf("Failed to add test proxy device due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400174 t.Errorf("failed to add device: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530175 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400176 t.Logf("Added device : %+v", TestProxyDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500177
178 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400179 d := &voltha.Device{}
180 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530181 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530182 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400183 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500184 t.Error("Failed to find added device")
185 } else {
186 djson, _ := json.Marshal(d)
187 t.Logf("Found device: %s", string(djson))
188 }
189}
190
191func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530192 TestProxyDevice.Id = TestProxyDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500193
Kent Hagerman4f355f52020-03-30 16:01:33 -0400194 if err := TestProxyRootDevice.add(context.Background(), "devices", TestProxyDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530195 BenchmarkProxyLogger.Errorf("Failed to add device to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530196 assert.NotNil(t, err)
197 }
sbarbari17d7e222019-11-05 10:02:29 -0500198
Kent Hagerman4f355f52020-03-30 16:01:33 -0400199 d := &voltha.Device{}
200 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d); err != nil {
201 BenchmarkProxyLogger.Errorf("Failed get device info from test proxy due to error: %v", err)
202 assert.NotNil(t, err)
203 } else if !have {
204 t.Error("Failed to find added device")
205 } else {
206 if d.String() != TestProxyDevice.String() {
207 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, d)
208 }
209 djson, _ := json.Marshal(d)
210 t.Logf("Found device: %s", string(djson))
sbarbari17d7e222019-11-05 10:02:29 -0500211 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400212
sbarbari17d7e222019-11-05 10:02:29 -0500213}
214
215func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530216 TestProxyAdapterID = "test-adapter"
217 TestProxyAdapter.Id = TestProxyAdapterID
sbarbari17d7e222019-11-05 10:02:29 -0500218
219 // Add the adapter
Kent Hagerman4f355f52020-03-30 16:01:33 -0400220 if err := TestProxyRootAdapter.AddWithID(context.Background(), "adapters", TestProxyAdapterID, TestProxyAdapter); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530221 BenchmarkProxyLogger.Errorf("Failed to add adapter to test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530222 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500223 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400224 t.Logf("Added adapter : %+v", TestProxyAdapter.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500225 }
226
sbarbari17d7e222019-11-05 10:02:29 -0500227 // Verify that the added device can now be retrieved
Kent Hagerman4f355f52020-03-30 16:01:33 -0400228 d := &voltha.Device{}
229 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530230 BenchmarkProxyLogger.Errorf("Failed to retrieve 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 {
sbarbari17d7e222019-11-05 10:02:29 -0500233 t.Error("Failed to find added adapter")
234 } else {
235 djson, _ := json.Marshal(d)
236 t.Logf("Found adapter: %s", string(djson))
237 }
238
sbarbari17d7e222019-11-05 10:02:29 -0500239}
240
241func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400242 var devices []*voltha.Device
243 if err := TestProxyRootDevice.List(context.Background(), "devices", &devices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530244 BenchmarkProxyLogger.Errorf("Failed to get all devices info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530245 assert.NotNil(t, err)
246 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400247 if len(devices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500248 t.Error("there are no available devices to retrieve")
249 } else {
250 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400251 TestProxyTargetDeviceID = devices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500252 t.Logf("retrieved all devices: %+v", devices)
253 }
254}
255
256func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400257 d := &voltha.Device{}
258 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530259 BenchmarkProxyLogger.Errorf("Failed to get single device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530260 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400261 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530262 t.Errorf("Failed to find device : %s", TestProxyTargetDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500263 } else {
264 djson, _ := json.Marshal(d)
265 t.Logf("Found device: %s", string(djson))
266 }
267}
268
269func TestProxy_1_3_1_Update_Device(t *testing.T) {
270 var fwVersion int
271
Kent Hagerman4f355f52020-03-30 16:01:33 -0400272 retrieved := &voltha.Device{}
273 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530274 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530275 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400276 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500277 t.Error("Failed to get device")
278 } else {
279 t.Logf("Found raw device (root proxy): %+v", retrieved)
280
Kent Hagerman4f355f52020-03-30 16:01:33 -0400281 if retrieved.FirmwareVersion == "n/a" {
sbarbari17d7e222019-11-05 10:02:29 -0500282 fwVersion = 0
283 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400284 fwVersion, _ = strconv.Atoi(retrieved.FirmwareVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500285 fwVersion++
286 }
287
Kent Hagerman4f355f52020-03-30 16:01:33 -0400288 retrieved.FirmwareVersion = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500289
Kent Hagerman4f355f52020-03-30 16:01:33 -0400290 if err := TestProxyRootDevice.Update(context.Background(), "devices/"+TestProxyTargetDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530291 BenchmarkProxyLogger.Errorf("Failed to update device info test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530292 assert.NotNil(t, err)
293 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400294 afterUpdate := &voltha.Device{}
295 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, afterUpdate); err != nil {
296 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
297 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500298 t.Error("Failed to update device")
299 } else {
300 t.Logf("Updated device : %+v", afterUpdate)
301 }
302
Kent Hagerman4f355f52020-03-30 16:01:33 -0400303 d := &voltha.Device{}
304 if have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyTargetDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530305 BenchmarkProxyLogger.Errorf("Failed to get device info from test proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530306 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400307 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500308 t.Error("Failed to find updated device (root proxy)")
309 } else {
310 djson, _ := json.Marshal(d)
311 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
312 }
313 }
314}
315
sbarbari17d7e222019-11-05 10:02:29 -0500316func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
sbarbari17d7e222019-11-05 10:02:29 -0500317
Kent Hagerman4f355f52020-03-30 16:01:33 -0400318 adaptersProxy := NewProxy(mockBackend, "/adapters")
319
320 retrieved := &voltha.Adapter{}
321 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530322 BenchmarkProxyLogger.Errorf("Failed to retrieve adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530323 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400324 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500325 t.Error("Failed to get adapter")
326 } else {
327 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
328
Kent Hagerman4f355f52020-03-30 16:01:33 -0400329 retrieved.Version = "test-adapter-version-2"
sbarbari17d7e222019-11-05 10:02:29 -0500330
Kent Hagerman4f355f52020-03-30 16:01:33 -0400331 if err := adaptersProxy.Update(context.Background(), TestProxyAdapterID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530332 BenchmarkProxyLogger.Errorf("Failed to update adapter info in adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530333 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500334 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400335 t.Logf("Updated adapter : %s", retrieved.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500336 }
337
Kent Hagerman4f355f52020-03-30 16:01:33 -0400338 d := &voltha.Adapter{}
339 if have, err := TestProxyRootAdapter.Get(context.Background(), "adapters/"+TestProxyAdapterID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530340 BenchmarkProxyLogger.Errorf("Failed to get updated adapter info from adapters proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530341 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400342 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500343 t.Error("Failed to find updated adapter (root proxy)")
344 } else {
345 djson, _ := json.Marshal(d)
346 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
347 }
348 }
349}
350
351func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400352 if err := TestProxyRootDevice.Remove(context.Background(), "devices/"+TestProxyDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530353 BenchmarkProxyLogger.Errorf("Failed to remove device from devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400354 t.Errorf("failed to remove device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500355 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400356 t.Logf("Removed device : %+v", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500357 }
358
Kent Hagerman4f355f52020-03-30 16:01:33 -0400359 d := &voltha.Device{}
360 have, err := TestProxyRootDevice.Get(context.Background(), "devices/"+TestProxyDeviceID, d)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530361 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530362 BenchmarkProxyLogger.Errorf("Failed to get device info from devices proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530363 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400364 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500365 djson, _ := json.Marshal(d)
366 t.Errorf("Device was not removed - %s", djson)
367 } else {
npujar9a30c702019-11-14 17:06:39 +0530368 t.Logf("Device was removed: %s", TestProxyDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500369 }
370}
371
372func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
373
374 ldIDBin, _ := uuid.New().MarshalBinary()
npujar9a30c702019-11-14 17:06:39 +0530375 TestProxyLogicalDeviceID = "0001" + hex.EncodeToString(ldIDBin)[:12]
376 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500377
Kent Hagerman4f355f52020-03-30 16:01:33 -0400378 if err := TestProxyRootLogicalDevice.AddWithID(context.Background(), "logical_devices", TestProxyLogicalDeviceID, TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530379 BenchmarkProxyLogger.Errorf("Failed to add new logical device into proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530380 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500381 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400382 t.Logf("Added logical device : %s", TestProxyLogicalDevice.Id)
sbarbari17d7e222019-11-05 10:02:29 -0500383 }
384
Kent Hagerman4f355f52020-03-30 16:01:33 -0400385 ld := &voltha.LogicalDevice{}
386 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530387 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 +0530388 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400389 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500390 t.Error("Failed to find added logical device")
391 } else {
392 ldJSON, _ := json.Marshal(ld)
393 t.Logf("Found logical device: %s", string(ldJSON))
394 }
sbarbari17d7e222019-11-05 10:02:29 -0500395}
396
397func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +0530398 TestProxyLogicalDevice.Id = TestProxyLogicalDeviceID
sbarbari17d7e222019-11-05 10:02:29 -0500399
Kent Hagerman4f355f52020-03-30 16:01:33 -0400400 if err := TestProxyRootLogicalDevice.add(context.Background(), "logical_devices", TestProxyLogicalDevice); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530401 BenchmarkProxyLogger.Errorf("Failed to add logical device due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530402 assert.NotNil(t, err)
403 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400404
405 device := &voltha.LogicalDevice{}
406 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices", device); err != nil {
407 BenchmarkProxyLogger.Errorf("Failed to get logical device info from logical device proxy due to error: %v", err)
408 assert.NotNil(t, err)
409 } else if !have {
410 t.Error("Failed to find added logical device")
411 } else {
412 if device.String() != TestProxyLogicalDevice.String() {
413 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxyLogicalDevice, device)
414 }
sbarbari17d7e222019-11-05 10:02:29 -0500415 }
416}
417
418func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400419 var logicalDevices []*voltha.LogicalDevice
420 if err := TestProxyRootLogicalDevice.List(context.Background(), "logical_devices", &logicalDevices); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530421 BenchmarkProxyLogger.Errorf("Failed to get all logical devices from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530422 assert.NotNil(t, err)
423 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400424 if len(logicalDevices) == 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500425 t.Error("there are no available logical devices to retrieve")
426 } else {
427 // Save the target device id for later tests
Kent Hagerman4f355f52020-03-30 16:01:33 -0400428 TestProxyTargetLogicalDeviceID = logicalDevices[0].Id
sbarbari17d7e222019-11-05 10:02:29 -0500429 t.Logf("retrieved all logical devices: %+v", logicalDevices)
430 }
431}
432
433func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400434 ld := &voltha.LogicalDevice{}
435 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, ld); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530436 BenchmarkProxyLogger.Errorf("Failed to get single logical device from proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530437 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400438 } else if !have {
npujar9a30c702019-11-14 17:06:39 +0530439 t.Errorf("Failed to find logical device : %s", TestProxyTargetLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500440 } else {
441 ldJSON, _ := json.Marshal(ld)
442 t.Logf("Found logical device: %s", string(ldJSON))
443 }
444
445}
446
447func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
448 var fwVersion int
sbarbari17d7e222019-11-05 10:02:29 -0500449
Kent Hagerman4f355f52020-03-30 16:01:33 -0400450 retrieved := &voltha.LogicalDevice{}
451 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530452 BenchmarkProxyLogger.Errorf("Failed to get logical devices due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530453 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400454 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500455 t.Error("Failed to get logical device")
456 } else {
457 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
458
Kent Hagerman4f355f52020-03-30 16:01:33 -0400459 if retrieved.RootDeviceId == "" {
sbarbari17d7e222019-11-05 10:02:29 -0500460 fwVersion = 0
461 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400462 fwVersion, _ = strconv.Atoi(retrieved.RootDeviceId)
sbarbari17d7e222019-11-05 10:02:29 -0500463 fwVersion++
464 }
465
Kent Hagerman4f355f52020-03-30 16:01:33 -0400466 retrieved.RootDeviceId = strconv.Itoa(fwVersion)
sbarbari17d7e222019-11-05 10:02:29 -0500467
Kent Hagerman4f355f52020-03-30 16:01:33 -0400468 if err := TestProxyRootLogicalDevice.Update(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, retrieved); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530469 BenchmarkProxyLogger.Errorf("Faield to update logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530470 assert.NotNil(t, err)
sbarbari17d7e222019-11-05 10:02:29 -0500471 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400472 t.Log("Updated logical device")
sbarbari17d7e222019-11-05 10:02:29 -0500473 }
474
Kent Hagerman4f355f52020-03-30 16:01:33 -0400475 d := &voltha.LogicalDevice{}
476 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyTargetLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530477 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530478 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400479 } else if !have {
sbarbari17d7e222019-11-05 10:02:29 -0500480 t.Error("Failed to find updated logical device (root proxy)")
481 } else {
482 djson, _ := json.Marshal(d)
sbarbari17d7e222019-11-05 10:02:29 -0500483 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
484 }
485 }
486}
487
sbarbari17d7e222019-11-05 10:02:29 -0500488func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400489 if err := TestProxyRootLogicalDevice.Remove(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530490 BenchmarkProxyLogger.Errorf("Failed to remove device from logical devices proxy due to error: %v", err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400491 t.Errorf("Failed to remove logical device: %s", err)
sbarbari17d7e222019-11-05 10:02:29 -0500492 } else {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400493 t.Logf("Removed device : %+v", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500494 }
495
Kent Hagerman4f355f52020-03-30 16:01:33 -0400496 d := &voltha.LogicalDevice{}
497 if have, err := TestProxyRootLogicalDevice.Get(context.Background(), "logical_devices/"+TestProxyLogicalDeviceID, d); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530498 BenchmarkProxyLogger.Errorf("Failed to get logical device info due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530499 assert.NotNil(t, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400500 } else if have {
sbarbari17d7e222019-11-05 10:02:29 -0500501 djson, _ := json.Marshal(d)
502 t.Errorf("Device was not removed - %s", djson)
503 } else {
npujar9a30c702019-11-14 17:06:39 +0530504 t.Logf("Device was removed: %s", TestProxyLogicalDeviceID)
sbarbari17d7e222019-11-05 10:02:29 -0500505 }
506}