blob: a96aa28a14a53c2d0885666f3c9855b69253fb6b [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"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040024 "github.com/opencord/voltha-go/protos/common"
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/opencord/voltha-go/protos/voltha"
26 "reflect"
27 "strconv"
28 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040029)
30
31type proxyTest struct {
Stephane Barbarie06c4a742018-10-01 11:09:32 -040032 Root *root
Stephane Barbarieec0919b2018-09-05 14:14:29 -040033 Backend *Backend
34 Proxy *Proxy
35 DbPrefix string
36 DbType string
37 DbHost string
38 DbPort int
39 DbTimeout int
40}
41
42var (
43 pt = &proxyTest{
44 DbPrefix: "service/voltha/data/core/0001",
45 DbType: "etcd",
46 //DbHost: "10.102.58.0",
47 DbHost: "localhost",
48 DbPort: 2379,
49 DbTimeout: 5,
50 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -040051 devId string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040052 targetDeviceId string
53)
54
55func init() {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040056 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoo2c6f1672018-09-20 23:14:41 -040057 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{}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040064 root := NewRoot(msgClass, pt.Backend)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040065 pt.Root = root.Load(msgClass)
66
67 GetProfiling().Report()
68
Stephane Barbarie06c4a742018-10-01 11:09:32 -040069 pt.Proxy = pt.Root.GetProxy("/", false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040070}
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
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040084func Test_Proxy_2_AddDevice(t *testing.T) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040085 devIdBin, _ := uuid.New().MarshalBinary()
86 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
87
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040088 ports := []*voltha.Port{
89 {
90 PortNo: 123,
91 Label: "test-port-0",
92 Type: voltha.Port_PON_OLT,
93 AdminState: common.AdminState_ENABLED,
94 OperStatus: common.OperStatus_ACTIVE,
95 DeviceId: "etcd_port-0-device-id",
96 Peers: []*voltha.Port_PeerPort{},
97 },
98 }
99
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400100 device := &voltha.Device{
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400101 Id: devId,
102 Type: "simulated_olt",
103 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
104 AdminState: voltha.AdminState_PREPROVISIONED,
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400105 Ports: ports,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400106 }
107
108 if added := pt.Proxy.Add("/devices", device, ""); added == nil {
109 t.Error("Failed to add device")
110 } else {
111 t.Logf("Added device : %+v", added)
112 }
113}
114
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400115func Test_Proxy_3_GetDevice_PostAdd(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400116 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400117 t.Error("Failed to find added device")
118 } else {
119 djson, _ := json.Marshal(d)
120
121 t.Logf("Found device: count: %s", djson)
122 }
123}
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400124func Test_Proxy_3_1_RegisterProxy(t *testing.T) {
125 // Get a device proxy and update a specific port
126 devProxy := pt.Root.GetProxy("/devices/"+devId, false)
127 port123 := devProxy.Get("/ports/123", 0, false, "")
128 t.Logf("got ports: %+v", port123)
129
130 devProxy.RegisterCallback(POST_UPDATE, deviceCallback, nil)
131
132 port123.(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
133
134 devProxy.Update("/ports/123", port123, false, "")
135 updated := devProxy.Get("/ports", 0, false, "")
136 t.Logf("got updated ports: %+v", updated)
137
138 //
139 // Get a device proxy and update all its ports
140 //
141
142 //devProxy := pt.Root.GetProxy("/devices/"+devId, false)
143 //ports := devProxy.Get("/ports", 0, false, "")
144 //t.Logf("got ports: %+v", ports)
145 //devProxy.RegisterCallback(POST_UPDATE, deviceCallback, nil)
146 //
147 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
148 //
149 //devProxy.Update("/ports", ports, false, "")
150 //updated := devProxy.Get("/ports", 0, false, "")
151 //t.Logf("got updated ports: %+v", updated)
152
153
154 //
155 // Get a device proxy, retrieve all the ports and update a specific one
156 //
157
158 //devProxy := pt.Root.GetProxy("/devices/"+devId, false)
159 //ports := devProxy.Get("/ports", 0, false, "")
160 //t.Logf("got ports: %+v", ports)
161 //devProxy.RegisterCallback(POST_UPDATE, deviceCallback, nil)
162 //
163 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
164 //
165 //devProxy.Update("/ports/123", ports.([]interface{})[0], false, "")
166 //updated := devProxy.Get("/ports", 0, false, "")
167 //t.Logf("got updated ports: %+v", updated)
168}
169
170func Test_Proxy_3_2_GetDevice_PostRegister(t *testing.T) {
171 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
172 t.Error("Failed to find updated registered device")
173 } else {
174 djson, _ := json.Marshal(d)
175
176 t.Logf("Found device: count: %s", djson)
177 }
178}
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400179
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400180func Test_Proxy_4_UpdateDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400181 if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400182 t.Error("Failed to get device")
183 } else {
184 var fwVersion int
185 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
186 fwVersion = 0
187 } else {
188 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
189 fwVersion += 1
190 }
191
192 cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
193 cloned.FirmwareVersion = strconv.Itoa(fwVersion)
194 t.Logf("Before update : %+v", cloned)
195
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400196 if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, &cloned, false, ""); afterUpdate == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400197 t.Error("Failed to update device")
198 } else {
199 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
200 }
201 }
202}
203
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400204func Test_Proxy_5_GetDevice_PostUpdate(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400205 device := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400206
207 t.Logf("content of updated device: %+v", device)
208}
209
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400210func Test_Proxy_6_RemoveDevice(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400211 if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400212 t.Error("Failed to remove device")
213 } else {
214 t.Logf("Removed device : %+v", removed)
215 }
216}
217
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400218func Test_Proxy_7_GetDevice_PostRemove(t *testing.T) {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400219 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400220 djson, _ := json.Marshal(d)
221 t.Errorf("Device was not removed - %s", djson)
222 } else {
223 t.Logf("Device was removed: %s", devId)
224 }
225}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400226
227// -----------------------------
228// Callback tests
229// -----------------------------
230
231func firstCallback(args ...interface{}) interface{} {
232 name := args[0]
233 id := args[1]
234 fmt.Printf("Running first callback - name: %s, id: %s\n", name, id)
235 return nil
236}
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400237func deviceCallback(args ...interface{}) interface{} {
238 fmt.Printf("Running device callback\n")
239 return nil
240}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400241func secondCallback(args ...interface{}) interface{} {
242 name := args[0].(map[string]string)
243 id := args[1]
244 fmt.Printf("Running second callback - name: %s, id: %f\n", name["name"], id)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400245 // FIXME: the panic call seem to interfere with the logging mechanism
246 //panic("Generating a panic in second callback")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400247 return nil
248}
249func thirdCallback(args ...interface{}) interface{} {
250 name := args[0]
251 id := args[1].(*voltha.Device)
252 fmt.Printf("Running third callback - name: %+v, id: %s\n", name, id.Id)
253 return nil
254}
255
256func Test_Proxy_Callbacks_1_Register(t *testing.T) {
257 pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
258
259 m := make(map[string]string)
260 m["name"] = "fghij"
261 pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
262
263 d := &voltha.Device{Id: "12345"}
264 pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
265}
266
267func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
268 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, true)
269}
270func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
271 pt.Proxy.InvokeCallbacks(PRE_ADD, nil, false)
272}
273
274func Test_Proxy_Callbacks_4_Unregister(t *testing.T) {
275 pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback)
276 pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback)
277 pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback)
278}