blob: e7b644cc51bc1be588f2714bba6fdf1c9e7d1616 [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 (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040019 "encoding/hex"
20 "encoding/json"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040021 "fmt"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/google/uuid"
23 "github.com/opencord/voltha-go/common/log"
24 "github.com/opencord/voltha-go/protos/voltha"
25 "reflect"
26 "strconv"
27 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040028)
29
30type proxyTest struct {
31 Root *Root
32 Backend *Backend
33 Proxy *Proxy
34 DbPrefix string
35 DbType string
36 DbHost string
37 DbPort int
38 DbTimeout int
39}
40
41var (
42 pt = &proxyTest{
43 DbPrefix: "service/voltha/data/core/0001",
44 DbType: "etcd",
45 //DbHost: "10.102.58.0",
46 DbHost: "localhost",
47 DbPort: 2379,
48 DbTimeout: 5,
49 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -040050 devId string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040051 targetDeviceId string
52)
53
54func init() {
khenaidoo2c6f1672018-09-20 23:14:41 -040055
56 log.AddPackage(log.JSON, log.ErrorLevel, nil)
57 log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
58
Stephane Barbarieec0919b2018-09-05 14:14:29 -040059 defer log.CleanUp()
60
Stephane Barbarieec0919b2018-09-05 14:14:29 -040061 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
Stephane Barbarie694e2b92018-09-07 12:17:36 -040072//func Test_Proxy_0_GetRootProxy(t *testing.T) {
73// pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
74//
75// msgClass := &voltha.Voltha{}
76// root := NewRoot(msgClass, pt.Backend, nil)
77// pt.Root = root.Load(msgClass)
78//
79// GetProfiling().Report()
80//
81// pt.Proxy = pt.Root.Node.GetProxy("/", false)
82//}
83
Stephane Barbarieec0919b2018-09-05 14:14:29 -040084func Test_Proxy_1_GetDevices(t *testing.T) {
85 devices := pt.Proxy.Get("/devices", 1, false, "")
86
87 if len(devices.([]interface{})) == 0 {
88 t.Error("there are no available devices to retrieve")
89 } else {
90 // Save the target device id for later tests
91 targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
92 t.Logf("retrieved devices: %+v", devices)
93 }
94}
95
96func Test_Proxy_2_GetDevice(t *testing.T) {
97 basePath := "/devices/" + targetDeviceId
Stephane Barbarie694e2b92018-09-07 12:17:36 -040098 device1 := pt.Proxy.Get(basePath+"/ports", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -040099 t.Logf("retrieved device with ports: %+v", device1)
100
101 device2 := pt.Proxy.Get(basePath, 0, false, "")
102
103 t.Logf("retrieved device: %+v", device2)
104}
105
106//func Test_Proxy_3_AddDevice(t *testing.T) {
107// //ports := []*voltha.Port{
108// // {
109// // PortNo: 123,
110// // Label: "test-port-0",
111// // Type: voltha.Port_PON_OLT,
112// // AdminState: common.AdminState_ENABLED,
113// // OperStatus: common.OperStatus_ACTIVE,
114// // DeviceId: "etcd_port-0-device-id",
115// // Peers: []*voltha.Port_PeerPort{},
116// // },
117// //}
118// devIdBin, _ := uuid.New().MarshalBinary()
119// devId := hex.EncodeToString(devIdBin)[:12]
120//
121// device := &voltha.Device{
122// Id: devId,
123// Type: "simulated_olt",
124// //Root: true,
125// //ParentId: "",
126// //ParentPortNo: 0,
127// //Vendor: "voltha-test",
128// //Model: "latest-voltha-simulated-olt",
129// //HardwareVersion: "1.0.0",
130// //FirmwareVersion: "1.0.0",
131// //Images: &voltha.Images{},
132// //SerialNumber: "abcdef-123456",
133// //VendorId: "DEADBEEF-INC",
134// //Adapter: "simulated_olt",
135// //Vlan: 1234,
136// Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
137// //ExtraArgs: "",
138// //ProxyAddress: &voltha.Device_ProxyAddress{},
139// AdminState: voltha.AdminState_PREPROVISIONED,
140// //OperStatus: common.OperStatus_ACTIVE,
141// //Reason: "",
142// //ConnectStatus: common.ConnectStatus_REACHABLE,
143// //Custom: &any.Any{},
144// //Ports: ports,
145// //Flows: &openflow_13.Flows{},
146// //FlowGroups: &openflow_13.FlowGroups{},
147// //PmConfigs: &voltha.PmConfigs{},
148// //ImageDownloads: []*voltha.ImageDownload{},
149// }
150//
151// //if retrieved := pt.Proxy.Get("/devices/00019b09a90bbe17", 0, false, ""); retrieved == nil {
152// // t.Error("Failed to get device")
153// //} else {
154// // devIdBin, _ := uuid.New().MarshalBinary()
155// // devId = "0001" + hex.EncodeToString(devIdBin)[:12]
156// // newDevice := Clone(de\).(*voltha.Device)
157// // newDevice.Id = devId
158//
159// if added := pt.Proxy.Add("/devices", device, ""); added == nil {
160// t.Error("Failed to add device")
161// } else {
162// t.Logf("Added device : %+v", added)
163// }
164// //}
165//
166//}
167func Test_Proxy_3_AddDevice(t *testing.T) {
168 devIdBin, _ := uuid.New().MarshalBinary()
169 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
170
171 device := &voltha.Device{
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400172 Id: devId,
173 Type: "simulated_olt",
174 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
175 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400176 }
177
178 if added := pt.Proxy.Add("/devices", device, ""); added == nil {
179 t.Error("Failed to add device")
180 } else {
181 t.Logf("Added device : %+v", added)
182 }
183}
184
185func Test_Proxy_4_CheckAddedDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400186 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400187 t.Error("Failed to find added device")
188 } else {
189 djson, _ := json.Marshal(d)
190
191 t.Logf("Found device: count: %s", djson)
192 }
193}
194
195func Test_Proxy_5_UpdateDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400196 if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400197 t.Error("Failed to get device")
198 } else {
199 var fwVersion int
200 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
201 fwVersion = 0
202 } else {
203 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
204 fwVersion += 1
205 }
206
207 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
208 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
209 t.Logf("Before update : %+v", cloned)
210
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400211 if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400212 t.Error("Failed to update device")
213 } else {
214 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
215 }
216 }
217}
218
219func Test_Proxy_6_CheckUpdatedDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400220 device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400221
222 t.Logf("content of updated device: %+v", device)
223}
224
225func Test_Proxy_7_RemoveDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400226 if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400227 t.Error("Failed to remove device")
228 } else {
229 t.Logf("Removed device : %+v", removed)
230 }
231}
232
233func Test_Proxy_8_CheckRemovedDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400234 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400235 djson, _ := json.Marshal(d)
236 t.Errorf("Device was not removed - %s", djson)
237 } else {
238 t.Logf("Device was removed: %s", devId)
239 }
240}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400241
242// -----------------------------
243// Callback tests
244// -----------------------------
245
246func firstCallback(args ...interface{}) interface{} {
247 name := args[0]
248 id := args[1]
249 fmt.Printf("Running first callback - name: %s, id: %s\n", name, id)
250 return nil
251}
252func secondCallback(args ...interface{}) interface{} {
253 name := args[0].(map[string]string)
254 id := args[1]
255 fmt.Printf("Running second callback - name: %s, id: %f\n", name["name"], id)
256 panic("Generating a panic in second callback")
257 return nil
258}
259func thirdCallback(args ...interface{}) interface{} {
260 name := args[0]
261 id := args[1].(*voltha.Device)
262 fmt.Printf("Running third callback - name: %+v, id: %s\n", name, id.Id)
263 return nil
264}
265
266func Test_Proxy_Callbacks_1_Register(t *testing.T) {
267 pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
268
269 m := make(map[string]string)
270 m["name"] = "fghij"
271 pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
272
273 d := &voltha.Device{Id: "12345"}
274 pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
275}
276
277func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
278 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true)
279}
280func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
281 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false)
282}
283
284func Test_Proxy_Callbacks_4_Unregister(t *testing.T) {
285 pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback)
286 pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback)
287 pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback)
288}