blob: 6226caaa581a170549711895eb73e1558ccf4866 [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"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/google/uuid"
22 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040023 "github.com/opencord/voltha-go/protos/common"
Stephane Barbarie126101e2018-10-11 16:18:48 -040024 "github.com/opencord/voltha-go/protos/openflow_13"
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 Barbarie126101e2018-10-11 16:18:48 -040051 ports = []*voltha.Port{
52 {
53 PortNo: 123,
54 Label: "test-port-0",
55 Type: voltha.Port_PON_OLT,
56 AdminState: common.AdminState_ENABLED,
57 OperStatus: common.OperStatus_ACTIVE,
58 DeviceId: "etcd_port-0-device-id",
59 Peers: []*voltha.Port_PeerPort{},
60 },
61 }
62
63 stats = &openflow_13.OfpFlowStats{
64 Id: 1111,
65 }
66 flows = &openflow_13.Flows{
67 Items: []*openflow_13.OfpFlowStats{stats},
68 }
69 device = &voltha.Device{
70 Id: devId,
71 Type: "simulated_olt",
72 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
73 AdminState: voltha.AdminState_PREPROVISIONED,
74 Flows: flows,
75 Ports: ports,
76 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -040077 devId string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040078 targetDeviceId string
Stephane Barbarie126101e2018-10-11 16:18:48 -040079
80 preAddExecuted = false
81 postAddExecuted = false
82 preUpdateExecuted = false
83 postUpdateExecuted = false
84 preRemoveExecuted = false
85 postRemoveExecuted = false
Stephane Barbarieec0919b2018-09-05 14:14:29 -040086)
87
88func init() {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040089 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoo2c6f1672018-09-20 23:14:41 -040090 log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
91
Stephane Barbarieec0919b2018-09-05 14:14:29 -040092 defer log.CleanUp()
93
Stephane Barbarieec0919b2018-09-05 14:14:29 -040094 pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
95
96 msgClass := &voltha.Voltha{}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040097 root := NewRoot(msgClass, pt.Backend)
Stephane Barbarieec0919b2018-09-05 14:14:29 -040098 pt.Root = root.Load(msgClass)
99
100 GetProfiling().Report()
101
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400102 pt.Proxy = pt.Root.GetProxy("/", false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400103}
104
Stephane Barbarie126101e2018-10-11 16:18:48 -0400105func commonCallback(args ...interface{}) interface{} {
106 log.Infof("Running common callback - arg count: %s", len(args))
107
108 for i := 0; i < len(args); i++ {
109 log.Infof("ARG %d : %+v", i, args[i])
110 }
111 execStatus := args[1].(*bool)
112
113 // Inform the caller that the callback was executed
114 *execStatus = true
115
116 return nil
117}
118
119func Test_Proxy_1_1_Add_NewDevice(t *testing.T) {
120 devIdBin, _ := uuid.New().MarshalBinary()
121 devId = "0001" + hex.EncodeToString(devIdBin)[:12]
122
123 pt.Proxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
124 pt.Proxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
125
126 device.Id = devId
127 if added := pt.Proxy.Add("/devices", device, ""); added == nil {
128 t.Error("Failed to add device")
129 } else {
130 t.Logf("Added device : %+v", added)
131 }
132
133 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
134 t.Error("Failed to find added device")
135 } else {
136 djson, _ := json.Marshal(d)
137 t.Logf("Found device: %s", string(djson))
138 }
139
140 if !preAddExecuted {
141 t.Error("PRE_ADD callback was not executed")
142 }
143 if !postAddExecuted {
144 t.Error("POST_ADD callback was not executed")
145 }
146}
147
148func Test_Proxy_1_2_Add_ExistingDevice(t *testing.T) {
149 device.Id = devId
150 if added := pt.Proxy.Add("/devices", device, ""); added == nil {
151 t.Logf("Successfully detected that the device already exists: %s", devId)
152 } else {
153 t.Errorf("A new device should not have been created : %+v", added)
154 }
155
156}
157
158func Test_Proxy_2_1_Get_AllDevices(t *testing.T) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400159 devices := pt.Proxy.Get("/devices", 1, false, "")
160
161 if len(devices.([]interface{})) == 0 {
162 t.Error("there are no available devices to retrieve")
163 } else {
164 // Save the target device id for later tests
165 targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -0400166 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400167 }
168}
169
Stephane Barbarie126101e2018-10-11 16:18:48 -0400170func Test_Proxy_2_2_Get_SingleDevice(t *testing.T) {
171 if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
172 t.Errorf("Failed to find device : %s", targetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400173 } else {
174 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400175 t.Logf("Found device: %s", string(djson))
176 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400177
Stephane Barbarie126101e2018-10-11 16:18:48 -0400178}
179
180func Test_Proxy_3_1_Update_Device_WithRootProxy(t *testing.T) {
181 if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
182 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 preUpdateExecuted = false
193 postUpdateExecuted = false
194
195 pt.Proxy.RegisterCallback(
196 PRE_UPDATE,
197 commonCallback,
198 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
199 )
200 pt.Proxy.RegisterCallback(
201 POST_UPDATE,
202 commonCallback,
203 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
204 )
205
206 //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device)
207 //cloned.FirmwareVersion = strconv.Itoa(fwVersion)
208 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
209 //t.Logf("Before update : %+v", cloned)
210
211 if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, retrieved, false, ""); afterUpdate == nil {
212 t.Error("Failed to update device")
213 } else {
214 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
215 }
216 if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
217 t.Error("Failed to find updated device (root proxy)")
218 } else {
219 djson, _ := json.Marshal(d)
220 t.Logf("Found device (root proxy): %s", string(djson))
221 }
222
223 if !preUpdateExecuted {
224 t.Error("PRE_UPDATE callback was not executed")
225 }
226 if !postUpdateExecuted {
227 t.Error("POST_UPDATE callback was not executed")
228 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400229 }
230}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400231
232func Test_Proxy_3_2_Update_Flow_WithSubProxy(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400233 // Get a device proxy and update a specific port
Stephane Barbarie126101e2018-10-11 16:18:48 -0400234 devflowsProxy := pt.Root.GetProxy("/devices/"+devId+"/flows", false)
235 flows := devflowsProxy.Get("/", 0, false, "")
236 //flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2222
237 flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244
238 //flows.(*openflow_13.Flows).Items[0].TableId = 2244
239 t.Logf("before updated flows: %+v", flows)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400240
Stephane Barbarie126101e2018-10-11 16:18:48 -0400241 //devPortsProxy := pt.Root.node.GetProxy("/devices/"+devId+"/ports", false)
242 //port123 := devPortsProxy.Get("/123", 0, false, "")
243 //t.Logf("got ports: %+v", port123)
244 //port123.(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400245
Stephane Barbarie126101e2018-10-11 16:18:48 -0400246 preUpdateExecuted = false
247 postUpdateExecuted = false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400248
Stephane Barbarie126101e2018-10-11 16:18:48 -0400249 devflowsProxy.RegisterCallback(
250 PRE_UPDATE,
251 commonCallback,
252 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted,
253 )
254 devflowsProxy.RegisterCallback(
255 POST_UPDATE,
256 commonCallback,
257 "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted,
258 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400259
Stephane Barbarie126101e2018-10-11 16:18:48 -0400260 kvFlows := devflowsProxy.Get("/", 0, false, "")
261
262 if reflect.DeepEqual(flows, kvFlows) {
263 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
264 }
265
266 if updated := devflowsProxy.Update("/", flows.([]interface{})[0], false, ""); updated == nil {
267 t.Error("Failed to update flow")
268 } else {
269 t.Logf("Updated flows : %+v", updated)
270 }
271
272 if d := devflowsProxy.Get("/", 0, false, ""); d == nil {
273 t.Error("Failed to find updated flows (flows proxy)")
274 } else {
275 djson, _ := json.Marshal(d)
276 t.Logf("Found flows (flows proxy): %s", string(djson))
277 }
278
279 if d := pt.Proxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
280 t.Error("Failed to find updated flows (root proxy)")
281 } else {
282 djson, _ := json.Marshal(d)
283 t.Logf("Found flows (root proxy): %s", string(djson))
284 }
285
286 if !preUpdateExecuted {
287 t.Error("PRE_UPDATE callback was not executed")
288 }
289 if !postUpdateExecuted {
290 t.Error("POST_UPDATE callback was not executed")
291 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400292 //
293 // Get a device proxy and update all its ports
294 //
295
296 //devProxy := pt.Root.GetProxy("/devices/"+devId, false)
297 //ports := devProxy.Get("/ports", 0, false, "")
298 //t.Logf("got ports: %+v", ports)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400299 //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400300 //
301 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
302 //
303 //devProxy.Update("/ports", ports, false, "")
304 //updated := devProxy.Get("/ports", 0, false, "")
305 //t.Logf("got updated ports: %+v", updated)
306
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400307 //
308 // Get a device proxy, retrieve all the ports and update a specific one
309 //
310
311 //devProxy := pt.Root.GetProxy("/devices/"+devId, false)
312 //ports := devProxy.Get("/ports", 0, false, "")
313 //t.Logf("got ports: %+v", ports)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400314 //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400315 //
316 //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
317 //
318 //devProxy.Update("/ports/123", ports.([]interface{})[0], false, "")
319 //updated := devProxy.Get("/ports", 0, false, "")
320 //t.Logf("got updated ports: %+v", updated)
321}
322
Stephane Barbarie126101e2018-10-11 16:18:48 -0400323func Test_Proxy_4_1_Remove_Device(t *testing.T) {
324 preRemoveExecuted = false
325 postRemoveExecuted = false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400326
Stephane Barbarie126101e2018-10-11 16:18:48 -0400327 pt.Proxy.RegisterCallback(
328 PRE_REMOVE,
329 commonCallback,
330 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
331 )
332 pt.Proxy.RegisterCallback(
333 POST_REMOVE,
334 commonCallback,
335 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
336 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400337
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400338 if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400339 t.Error("Failed to remove device")
340 } else {
341 t.Logf("Removed device : %+v", removed)
342 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400343 if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400344 djson, _ := json.Marshal(d)
345 t.Errorf("Device was not removed - %s", djson)
346 } else {
347 t.Logf("Device was removed: %s", devId)
348 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400349
350 if !preRemoveExecuted {
351 t.Error("PRE_UPDATE callback was not executed")
352 }
353 if !postRemoveExecuted {
354 t.Error("POST_UPDATE callback was not executed")
355 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400356}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400357
358// -----------------------------
359// Callback tests
360// -----------------------------
361
362func firstCallback(args ...interface{}) interface{} {
363 name := args[0]
364 id := args[1]
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400365 log.Infof("Running first callback - name: %s, id: %s\n", name, id)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400366 return nil
367}
368func secondCallback(args ...interface{}) interface{} {
369 name := args[0].(map[string]string)
370 id := args[1]
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400371 log.Infof("Running second callback - name: %s, id: %f\n", name["name"], id)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400372 // FIXME: the panic call seem to interfere with the logging mechanism
373 //panic("Generating a panic in second callback")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400374 return nil
375}
376func thirdCallback(args ...interface{}) interface{} {
377 name := args[0]
378 id := args[1].(*voltha.Device)
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400379 log.Infof("Running third callback - name: %+v, id: %s\n", name, id.Id)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400380 return nil
381}
382
383func Test_Proxy_Callbacks_1_Register(t *testing.T) {
384 pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
385
386 m := make(map[string]string)
387 m["name"] = "fghij"
388 pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
389
390 d := &voltha.Device{Id: "12345"}
391 pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
392}
393
394func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400395 pt.Proxy.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400396}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400397
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400398func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400399 pt.Proxy.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400400}
401
402func Test_Proxy_Callbacks_4_Unregister(t *testing.T) {
403 pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback)
404 pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback)
405 pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback)
406}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400407
408//func Test_Proxy_Callbacks_5_Add(t *testing.T) {
409// pt.Proxy.Root.AddCallback(pt.Proxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
410//}
411//
412//func Test_Proxy_Callbacks_6_Execute(t *testing.T) {
413// pt.Proxy.Root.ExecuteCallbacks()
414//}