blob: 1824d3f04ec5f0c9950bdd5929d68abbeef7bc01 [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
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 "github.com/opencord/voltha-go/protos/voltha"
20 "testing"
21 "github.com/opencord/voltha-go/common/log"
22 "strconv"
23 "reflect"
24 "github.com/google/uuid"
25 "encoding/hex"
26 "encoding/json"
27)
28
29type proxyTest struct {
30 Root *Root
31 Backend *Backend
32 Proxy *Proxy
33 DbPrefix string
34 DbType string
35 DbHost string
36 DbPort int
37 DbTimeout int
38}
39
40var (
41 pt = &proxyTest{
42 DbPrefix: "service/voltha/data/core/0001",
43 DbType: "etcd",
44 //DbHost: "10.102.58.0",
45 DbHost: "localhost",
46 DbPort: 2379,
47 DbTimeout: 5,
48 }
49 devId string
50 targetDeviceId string
51)
52
53func init() {
54 if _, err := log.SetLogger(log.CONSOLE, 0, log.Fields{"instanceId": "proxy_test"}); err != nil {
55 log.With(log.Fields{"error": err}).Fatal("cannot setup logging")
56 }
57 defer log.CleanUp()
58
59}
60func Test_Proxy_0_GetRootProxy(t *testing.T) {
61 pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
62
63 msgClass := &voltha.Voltha{}
64 root := NewRoot(msgClass, pt.Backend, nil)
65 pt.Root = root.Load(msgClass)
66
67 GetProfiling().Report()
68
69 pt.Proxy = pt.Root.Node.GetProxy("/", false)
70}
71
72func Test_Proxy_1_GetDevices(t *testing.T) {
73 devices := pt.Proxy.Get("/devices", 1, false, "")
74
75 if len(devices.([]interface{})) == 0 {
76 t.Error("there are no available devices to retrieve")
77 } else {
78 // Save the target device id for later tests
79 targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
80 t.Logf("retrieved devices: %+v", devices)
81 }
82}
83
84func Test_Proxy_2_GetDevice(t *testing.T) {
85 basePath := "/devices/" + targetDeviceId
86 device1 := pt.Proxy.Get(basePath + "/ports", 1, false, "")
87 t.Logf("retrieved device with ports: %+v", device1)
88
89 device2 := pt.Proxy.Get(basePath, 0, false, "")
90
91 t.Logf("retrieved device: %+v", device2)
92}
93
94//func Test_Proxy_3_AddDevice(t *testing.T) {
95// //ports := []*voltha.Port{
96// // {
97// // PortNo: 123,
98// // Label: "test-port-0",
99// // Type: voltha.Port_PON_OLT,
100// // AdminState: common.AdminState_ENABLED,
101// // OperStatus: common.OperStatus_ACTIVE,
102// // DeviceId: "etcd_port-0-device-id",
103// // Peers: []*voltha.Port_PeerPort{},
104// // },
105// //}
106// devIdBin, _ := uuid.New().MarshalBinary()
107// devId := hex.EncodeToString(devIdBin)[:12]
108//
109// device := &voltha.Device{
110// Id: devId,
111// Type: "simulated_olt",
112// //Root: true,
113// //ParentId: "",
114// //ParentPortNo: 0,
115// //Vendor: "voltha-test",
116// //Model: "latest-voltha-simulated-olt",
117// //HardwareVersion: "1.0.0",
118// //FirmwareVersion: "1.0.0",
119// //Images: &voltha.Images{},
120// //SerialNumber: "abcdef-123456",
121// //VendorId: "DEADBEEF-INC",
122// //Adapter: "simulated_olt",
123// //Vlan: 1234,
124// Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
125// //ExtraArgs: "",
126// //ProxyAddress: &voltha.Device_ProxyAddress{},
127// AdminState: voltha.AdminState_PREPROVISIONED,
128// //OperStatus: common.OperStatus_ACTIVE,
129// //Reason: "",
130// //ConnectStatus: common.ConnectStatus_REACHABLE,
131// //Custom: &any.Any{},
132// //Ports: ports,
133// //Flows: &openflow_13.Flows{},
134// //FlowGroups: &openflow_13.FlowGroups{},
135// //PmConfigs: &voltha.PmConfigs{},
136// //ImageDownloads: []*voltha.ImageDownload{},
137// }
138//
139// //if retrieved := pt.Proxy.Get("/devices/00019b09a90bbe17", 0, false, ""); retrieved == nil {
140// // t.Error("Failed to get device")
141// //} else {
142// // devIdBin, _ := uuid.New().MarshalBinary()
143// // devId = "0001" + hex.EncodeToString(devIdBin)[:12]
144// // newDevice := Clone(de\).(*voltha.Device)
145// // newDevice.Id = devId
146//
147// if added := pt.Proxy.Add("/devices", device, ""); added == nil {
148// t.Error("Failed to add device")
149// } else {
150// t.Logf("Added device : %+v", added)
151// }
152// //}
153//
154//}
155func Test_Proxy_3_AddDevice(t *testing.T) {
156 devIdBin, _ := uuid.New().MarshalBinary()
157 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
158
159 device := &voltha.Device{
160 Id: devId,
161 Type: "simulated_olt",
162 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
163 AdminState: voltha.AdminState_PREPROVISIONED,
164 }
165
166 if added := pt.Proxy.Add("/devices", device, ""); added == nil {
167 t.Error("Failed to add device")
168 } else {
169 t.Logf("Added device : %+v", added)
170 }
171}
172
173func Test_Proxy_4_CheckAddedDevice(t *testing.T) {
174 if d := pt.Proxy.Get("/devices/" + devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
175 t.Error("Failed to find added device")
176 } else {
177 djson, _ := json.Marshal(d)
178
179 t.Logf("Found device: count: %s", djson)
180 }
181}
182
183func Test_Proxy_5_UpdateDevice(t *testing.T) {
184 if retrieved := pt.Proxy.Get("/devices/" + targetDeviceId, 1, false, ""); retrieved == nil {
185 t.Error("Failed to get device")
186 } else {
187 var fwVersion int
188 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
189 fwVersion = 0
190 } else {
191 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
192 fwVersion += 1
193 }
194
195 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
196 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
197 t.Logf("Before update : %+v", cloned)
198
199 if afterUpdate := pt.Proxy.Update("/devices/" + targetDeviceId, &cloned, false, ""); afterUpdate == nil {
200 t.Error("Failed to update device")
201 } else {
202 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
203 }
204 }
205}
206
207func Test_Proxy_6_CheckUpdatedDevice(t *testing.T) {
208 device := pt.Proxy.Get("/devices/" + targetDeviceId, 0, false, "")
209
210 t.Logf("content of updated device: %+v", device)
211}
212
213func Test_Proxy_7_RemoveDevice(t *testing.T) {
214 if removed := pt.Proxy.Remove("/devices/" + devId, ""); removed == nil {
215 t.Error("Failed to remove device")
216 } else {
217 t.Logf("Removed device : %+v", removed)
218 }
219}
220
221func Test_Proxy_8_CheckRemovedDevice(t *testing.T) {
222 if d := pt.Proxy.Get("/devices/" + devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
223 djson, _ := json.Marshal(d)
224 t.Errorf("Device was not removed - %s", djson)
225 } else {
226 t.Logf("Device was removed: %s", devId)
227 }
228}