blob: 3ebe06a478636c44abdaa5aa6c2a825396fdf142 [file] [log] [blame]
Stephane Barbariea188d942018-10-16 16:43:04 -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 "encoding/hex"
20 "encoding/json"
21 "github.com/google/uuid"
22 "github.com/opencord/voltha-go/protos/voltha"
23 "reflect"
24 "strconv"
25 "testing"
26)
27
28/*
29
301. Add device
312. Do parallel updates of that device
323. Do parallel gets of that device
334. Remove device
34
35 */
36
37var (
38 //pctTargetDeviceId string
39 target *voltha.Device
40)
41
42func Test_ConcurrentProxy_1_1_Add_NewDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043 devIDBin, _ := uuid.New().MarshalBinary()
44 devID = "0001" + hex.EncodeToString(devIDBin)[:12]
Stephane Barbariea188d942018-10-16 16:43:04 -040045
46 preAddExecuted := false
47 postAddExecuted := false
48
49 modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
50 modelTestConfig.RootProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
51
Stephane Barbariedc5022d2018-11-19 15:21:44 -050052 device.Id = devID
Stephane Barbariea188d942018-10-16 16:43:04 -040053 if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
54 t.Error("Failed to add device")
55 } else {
56 t.Logf("Added device : %+v", added)
57 }
58
Stephane Barbariedc5022d2018-11-19 15:21:44 -050059 if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariea188d942018-10-16 16:43:04 -040060 t.Error("Failed to find added device")
61 } else {
62 djson, _ := json.Marshal(d)
63 t.Logf("Found device: %s", string(djson))
64 }
65
66 //if !preAddExecuted {
67 // t.Error("PRE_ADD callback was not executed")
68 //}
69 //if !postAddExecuted {
70 // t.Error("POST_ADD callback was not executed")
71 //}
72}
73
74func Test_ConcurrentProxy_1_Add_ExistingDevice(t *testing.T) {
75 t.Parallel()
76
Stephane Barbariedc5022d2018-11-19 15:21:44 -050077 device.Id = devID
Stephane Barbariea188d942018-10-16 16:43:04 -040078 if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050079 t.Logf("Successfully detected that the device already exists: %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -040080 } else {
81 t.Errorf("A new device should not have been created : %+v", added)
82 }
83
84}
85
86func Test_ConcurrentProxy_Get_AllDevices(t *testing.T) {
87 t.Parallel()
88
89 devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
90
91 if len(devices.([]interface{})) == 0 {
92 t.Error("there are no available devices to retrieve")
93 } else {
94 t.Logf("retrieved all devices: %+v", devices)
95 }
96}
97
98func Test_ConcurrentProxy_Get_Update_DeviceAdminState(t *testing.T) {
99 t.Parallel()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500100 if retrieved := modelTestConfig.RootProxy.Get("/devices/"+devID, 1, false, ""); retrieved == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400101 t.Error("Failed to get device")
102 } else {
103 retrieved.(*voltha.Device).AdminState = voltha.AdminState_DISABLED
104
105 preUpdateExecuted := false
106 postUpdateExecuted := false
107
108 modelTestConfig.RootProxy.RegisterCallback(
109 PRE_UPDATE,
110 commonCallback,
111 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
112 )
113 modelTestConfig.RootProxy.RegisterCallback(
114 POST_UPDATE,
115 commonCallback,
116 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
117 )
118
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500119 if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+devID, retrieved, false, ""); afterUpdate == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400120 t.Error("Failed to update device")
121 } else {
122 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
123 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500124 if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariea188d942018-10-16 16:43:04 -0400125 t.Error("Failed to find updated device (root proxy)")
126 } else {
127 djson, _ := json.Marshal(d)
128 t.Logf("Found device (root proxy): %s", string(djson))
129 }
130
131 //if !preUpdateExecuted {
132 // t.Error("PRE_UPDATE callback was not executed")
133 //}
134 //if !postUpdateExecuted {
135 // t.Error("POST_UPDATE callback was not executed")
136 //}
137 }
138}
139
140func Test_ConcurrentProxy_Get_SingleDevice(t *testing.T) {
141 //t.Parallel()
142
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500143 if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); !reflect.ValueOf(d).IsValid() {
144 t.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400145 } else {
146 djson, _ := json.Marshal(d)
147 t.Logf("Found device: %s", string(djson))
148 }
149
150}
151
152func Test_ConcurrentProxy_Get_SingleDeviceFlows(t *testing.T) {
153 t.Parallel()
154
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500155 if d := modelTestConfig.RootProxy.Get("/devices/"+devID+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
156 t.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400157 } else {
158 djson, _ := json.Marshal(d)
159 t.Logf("Found device: %s", string(djson))
160 }
161
162}
163
164func Test_ConcurrentProxy_Get_SingleDevicePorts(t *testing.T) {
165 t.Parallel()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500166 if d := modelTestConfig.RootProxy.Get("/devices/"+devID+"/ports", 0, false, ""); !reflect.ValueOf(d).IsValid() {
167 t.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400168 } else {
169 djson, _ := json.Marshal(d)
170 t.Logf("Found device: %s", string(djson))
171 }
172
173}
174
175func Test_ConcurrentProxy_Get_Update_DeviceFirmware(t *testing.T) {
176 t.Parallel()
177
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500178 if retrieved := modelTestConfig.RootProxy.Get("/devices/"+devID, 1, false, ""); retrieved == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400179 t.Error("Failed to get device")
180 } else {
181 var fwVersion int
182 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
183 fwVersion = 0
184 } else {
185 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500186 fwVersion++
Stephane Barbariea188d942018-10-16 16:43:04 -0400187 }
188
189 preUpdateExecuted := false
190 postUpdateExecuted := false
191
192 modelTestConfig.RootProxy.RegisterCallback(
193 PRE_UPDATE,
194 commonCallback,
195 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
196 )
197 modelTestConfig.RootProxy.RegisterCallback(
198 POST_UPDATE,
199 commonCallback,
200 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
201 )
202
203 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
204
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500205 if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+devID, retrieved, false, ""); afterUpdate == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400206 t.Error("Failed to update device")
207 } else {
208 t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
209 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210 if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariea188d942018-10-16 16:43:04 -0400211 t.Error("Failed to find updated device (root proxy)")
212 } else {
213 djson, _ := json.Marshal(d)
214 t.Logf("Found device (root proxy): %s", string(djson))
215 }
216
217 //if !preUpdateExecuted {
218 // t.Error("PRE_UPDATE callback was not executed")
219 //}
220 //if !postUpdateExecuted {
221 // t.Error("POST_UPDATE callback was not executed")
222 //}
223 }
224}
225
226//func Test_ConcurrentProxy_Get_Update_DeviceFlows(t *testing.T) {
227// t.Parallel()
228//
229// // Get a device proxy and update a specific port
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500230// //devflowsProxy := modelTestConfig.Root.CreateProxy("/devices/"+devID+"/flows", false)
Stephane Barbariea188d942018-10-16 16:43:04 -0400231// flows := modelTestConfig.RootProxy.Get("/", 0, false, "")
232// flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244
233//
234// preUpdateExecuted := false
235// postUpdateExecuted := false
236//
237// modelTestConfig.RootProxy.RegisterCallback(
238// PRE_UPDATE,
239// commonCallback,
240// "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted,
241// )
242// modelTestConfig.RootProxy.RegisterCallback(
243// POST_UPDATE,
244// commonCallback,
245// "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted,
246// )
247//
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248// kvFlows := modelTestConfig.RootProxy.Get("/devices/"+devID+"/flows", 0, false, "")
Stephane Barbariea188d942018-10-16 16:43:04 -0400249//
250// if reflect.DeepEqual(flows, kvFlows) {
251// t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
252// }
253//
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500254// if updated := modelTestConfig.RootProxy.Update("/devices/"+devID+"/flows", flows.([]interface{})[0], false, ""); updated == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400255// t.Error("Failed to update flow")
256// } else {
257// t.Logf("Updated flows : %+v", updated)
258// }
259//
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500260// if d := modelTestConfig.RootProxy.Get("/devices/"+devID+"/flows", 0, false, ""); d == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400261// t.Error("Failed to find updated flows (flows proxy)")
262// } else {
263// djson, _ := json.Marshal(d)
264// t.Logf("Found flows (flows proxy): %s", string(djson))
265// }
266//
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500267// if d := modelTestConfig.RootProxy.Get("/devices/"+devID+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariea188d942018-10-16 16:43:04 -0400268// t.Error("Failed to find updated flows (root proxy)")
269// } else {
270// djson, _ := json.Marshal(d)
271// t.Logf("Found flows (root proxy): %s", string(djson))
272// }
273//
274// //if !preUpdateExecuted {
275// // t.Error("PRE_UPDATE callback was not executed")
276// //}
277// //if !postUpdateExecuted {
278// // t.Error("POST_UPDATE callback was not executed")
279// //}
280//}
281
282//func Test_ConcurrentProxy_4_1_Remove_Device(t *testing.T) {
283// preRemoveExecuted := false
284// postRemoveExecuted := false
285//
286// modelTestConfig.RootProxy.RegisterCallback(
287// PRE_REMOVE,
288// commonCallback,
289// "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
290// )
291// modelTestConfig.RootProxy.RegisterCallback(
292// POST_REMOVE,
293// commonCallback,
294// "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
295// )
296//
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500297// if removed := modelTestConfig.RootProxy.Remove("/devices/"+devID, ""); removed == nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400298// t.Error("Failed to remove device")
299// } else {
300// t.Logf("Removed device : %+v", removed)
301// }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500302// if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbariea188d942018-10-16 16:43:04 -0400303// djson, _ := json.Marshal(d)
304// t.Errorf("Device was not removed - %s", djson)
305// } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500306// t.Logf("Device was removed: %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400307// }
308//
309// if !preRemoveExecuted {
310// t.Error("PRE_UPDATE callback was not executed")
311// }
312// if !postRemoveExecuted {
313// t.Error("POST_UPDATE callback was not executed")
314// }
315//}
316
317//func Benchmark_ConcurrentProxy_UpdateFirmware(b *testing.B) {
318// //var target *voltha.Device
319// if target == nil {
320// devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
321// if len(devices.([]interface{})) == 0 {
322// b.Error("there are no available devices to retrieve")
323// } else {
324// // Save the target device id for later tests
325// target = devices.([]interface{})[0].(*voltha.Device)
326// //b.Logf("retrieved all devices: %+v", devices)
327// }
328// }
329//
330// for n := 0; n < b.N; n++ {
331// var fwVersion int
332//
333// if target.FirmwareVersion == "n/a" {
334// fwVersion = 0
335// } else {
336// fwVersion, _ = strconv.Atoi(target.FirmwareVersion)
337// fwVersion += 1
338// }
339//
340// target.FirmwareVersion = strconv.Itoa(fwVersion)
341//
342// if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+target.Id, target, false,
343// ""); afterUpdate == nil {
344// b.Error("Failed to update device")
345// } else {
346// if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500347// b.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400348// } else {
349// //djson, _ := json.Marshal(d)
350// //b.Logf("Checking updated device device: %s", string(djson))
351// }
352// }
353// }
354//
355//}
356//
357//func Benchmark_ConcurrentProxy_GetDevice(b *testing.B) {
358// if target == nil {
359// //var target *voltha.Device
360// devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
361// if len(devices.([]interface{})) == 0 {
362// b.Error("there are no available devices to retrieve")
363// } else {
364// // Save the target device id for later tests
365// target = devices.([]interface{})[0].(*voltha.Device)
366// //b.Logf("retrieved all devices: %+v", devices)
367// }
368// }
369//
370// for n := 0; n < b.N; n++ {
371// if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500372// b.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400373// } else {
374// //djson, _ := json.Marshal(d)
375// //b.Logf("Found device: %s", string(djson))
376// }
377// }
378//
379//}
380
381func Benchmark_ConcurrentProxy_UpdateFirmware(b *testing.B) {
382 //GetProfiling().Reset()
383 defer GetProfiling().Report()
384 //b.SetParallelism(100)
385 if target == nil {
386 devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
387 if len(devices.([]interface{})) == 0 {
388 b.Error("there are no available devices to retrieve")
389 } else {
390 // Save the target device id for later tests
391 target = devices.([]interface{})[0].(*voltha.Device)
392 //b.Logf("retrieved all devices: %+v", devices)
393 }
394 }
395
396 b.RunParallel(func(pb *testing.PB) {
397 for pb.Next() {
398 var fwVersion int
399
400 if target.FirmwareVersion == "n/a" {
401 fwVersion = 0
402 } else {
403 fwVersion, _ = strconv.Atoi(target.FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500404 fwVersion++
Stephane Barbariea188d942018-10-16 16:43:04 -0400405 }
406
407 target.FirmwareVersion = strconv.Itoa(fwVersion)
408
409 if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+target.Id, target, false,
410 ""); afterUpdate == nil {
411 b.Error("Failed to update device")
412 } else {
413 if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500414 b.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400415 } else if d.(*voltha.Device).FirmwareVersion != target.FirmwareVersion {
416 b.Errorf("Firmware was not uptaded - expected: %s, actual: %s",
417 target.FirmwareVersion,
418 d.(*voltha.Device).FirmwareVersion,
419 )
420 } else {
421 b.Logf("Firmware is now : %s", d.(*voltha.Device).FirmwareVersion)
422 //djson, _ := json.Marshal(d)
423 //b.Logf("Checking updated device device: %s", string(djson))
424 }
425 }
426 }
427 })
428}
429
430func Benchmark_ConcurrentProxy_GetDevice(b *testing.B) {
431 //GetProfiling().Reset()
432 defer GetProfiling().Report()
433 //b.SetParallelism(5)
434 if target == nil {
435 //var target *voltha.Device
436 devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
437 if len(devices.([]interface{})) == 0 {
438 b.Error("there are no available devices to retrieve")
439 } else {
440 // Save the target device id for later tests
441 target = devices.([]interface{})[0].(*voltha.Device)
442 //b.Logf("retrieved all devices: %+v", devices)
443 }
444 }
445
446 b.RunParallel(func(pb *testing.PB) {
447 for pb.Next() {
448 if d := modelTestConfig.RootProxy.Get("/devices/"+target.Id, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500449 b.Errorf("Failed to find device : %s", devID)
Stephane Barbariea188d942018-10-16 16:43:04 -0400450 } else {
451 //djson, _ := json.Marshal(d)
452 //b.Logf("Found device: %s", string(djson))
453 }
454 }
455 })
456}