blob: ea15bac1e6da32fd9604aa4157036dce96596671 [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 Barbarie1039ec42019-02-04 10:43:16 -050021 "github.com/golang/protobuf/proto"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/google/uuid"
Stephane Barbarie1039ec42019-02-04 10:43:16 -050023 "github.com/opencord/voltha-go/protos/common"
Stephane Barbariea188d942018-10-16 16:43:04 -040024 "github.com/opencord/voltha-go/protos/openflow_13"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050025 "github.com/opencord/voltha-go/protos/voltha"
26 "math/rand"
khenaidoob9203542018-09-17 22:56:37 -040027 "reflect"
28 "strconv"
29 "testing"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040030)
31
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050033 TestProxy_Root *root
34 TestProxy_Root_LogicalDevice *Proxy
35 TestProxy_Root_Device *Proxy
36 TestProxy_DeviceId string
37 TestProxy_LogicalDeviceId string
38 TestProxy_TargetDeviceId string
39 TestProxy_TargetLogicalDeviceId string
40 TestProxy_LogicalPorts []*voltha.LogicalPort
41 TestProxy_Ports []*voltha.Port
42 TestProxy_Stats *openflow_13.OfpFlowStats
43 TestProxy_Flows *openflow_13.Flows
44 TestProxy_Device *voltha.Device
45 TestProxy_LogicalDevice *voltha.LogicalDevice
Stephane Barbarieec0919b2018-09-05 14:14:29 -040046)
47
Stephane Barbariedc5022d2018-11-19 15:21:44 -050048func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050049 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
50 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
51 TestProxy_Root = NewRoot(&voltha.Voltha{}, nil)
52 TestProxy_Root_LogicalDevice = TestProxy_Root.CreateProxy("/", false)
53 TestProxy_Root_Device = TestProxy_Root.CreateProxy("/", false)
54
55 TestProxy_LogicalPorts = []*voltha.LogicalPort{
56 {
57 Id: "123",
58 DeviceId: "logicalport-0-device-id",
59 DevicePortNo: 123,
60 RootPort: false,
61 },
62 }
63 TestProxy_Ports = []*voltha.Port{
64 {
65 PortNo: 123,
66 Label: "test-port-0",
67 Type: voltha.Port_PON_OLT,
68 AdminState: common.AdminState_ENABLED,
69 OperStatus: common.OperStatus_ACTIVE,
70 DeviceId: "etcd_port-0-device-id",
71 Peers: []*voltha.Port_PeerPort{},
72 },
73 }
74
75 TestProxy_Stats = &openflow_13.OfpFlowStats{
76 Id: 1111,
77 }
78 TestProxy_Flows = &openflow_13.Flows{
79 Items: []*openflow_13.OfpFlowStats{TestProxy_Stats},
80 }
81 TestProxy_Device = &voltha.Device{
82 Id: TestProxy_DeviceId,
83 Type: "simulated_olt",
84 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
85 AdminState: voltha.AdminState_PREPROVISIONED,
86 Flows: TestProxy_Flows,
87 Ports: TestProxy_Ports,
88 }
89
90 TestProxy_LogicalDevice = &voltha.LogicalDevice{
91 Id: TestProxy_DeviceId,
92 DatapathId: 0,
93 Ports: TestProxy_LogicalPorts,
94 Flows: TestProxy_Flows,
95 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -050096}
97
Stephane Barbarie1039ec42019-02-04 10:43:16 -050098func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050099 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500100 TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
101 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbarie126101e2018-10-11 16:18:48 -0400102
Stephane Barbariea188d942018-10-16 16:43:04 -0400103 preAddExecuted := false
104 postAddExecuted := false
105
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500106 devicesProxy := TestProxy_Root.node.CreateProxy("/devices", false)
107 devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes")
108 devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes")
109
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500110 // Register ADD instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500111 TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
112 TestProxy_Root_Device.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400113
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500114 // Add the device
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500115 if added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400116 t.Error("Failed to add device")
117 } else {
118 t.Logf("Added device : %+v", added)
119 }
120
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500121 // Verify that the added device can now be retrieved
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500122 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400123 t.Error("Failed to find added device")
124 } else {
125 djson, _ := json.Marshal(d)
126 t.Logf("Found device: %s", string(djson))
127 }
128
129 if !preAddExecuted {
130 t.Error("PRE_ADD callback was not executed")
131 }
132 if !postAddExecuted {
133 t.Error("POST_ADD callback was not executed")
134 }
135}
136
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500137func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
138 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500139
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500140 added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, "");
141 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() {
142 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400143 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400144}
145
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500146func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
147 devices := TestProxy_Root_Device.Get("/devices", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400148
149 if len(devices.([]interface{})) == 0 {
150 t.Error("there are no available devices to retrieve")
151 } else {
152 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500153 TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -0400154 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400155 }
156}
157
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500158func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
159 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
160 t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400161 } else {
162 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400163 t.Logf("Found device: %s", string(djson))
164 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400165}
166
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500167func TestProxy_1_3_1_Update_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500168 var fwVersion int
169 preUpdateExecuted := false
170 postUpdateExecuted := false
171
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500172 if retrieved := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400173 t.Error("Failed to get device")
174 } else {
Stephane Barbariea188d942018-10-16 16:43:04 -0400175 t.Logf("Found raw device (root proxy): %+v", retrieved)
176
Stephane Barbarie126101e2018-10-11 16:18:48 -0400177 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
178 fwVersion = 0
179 } else {
180 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500181 fwVersion++
Stephane Barbarie126101e2018-10-11 16:18:48 -0400182 }
183
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500184 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400185
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500186 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400187 PRE_UPDATE,
188 commonCallback,
189 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
190 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500191 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400192 POST_UPDATE,
193 commonCallback,
194 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
195 )
196
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500197 if afterUpdate := TestProxy_Root_Device.Update("/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400198 t.Error("Failed to update device")
199 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500200 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400201 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500202
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500203 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400204 t.Error("Failed to find updated device (root proxy)")
205 } else {
206 djson, _ := json.Marshal(d)
Stephane Barbariea188d942018-10-16 16:43:04 -0400207 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400208 }
209
210 if !preUpdateExecuted {
211 t.Error("PRE_UPDATE callback was not executed")
212 }
213 if !postUpdateExecuted {
214 t.Error("POST_UPDATE callback was not executed")
215 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400216 }
217}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400218
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500219func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400220 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500221 devFlowsProxy := TestProxy_Root.node.CreateProxy("/devices/"+TestProxy_DeviceId+"/flows", false)
222 flows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbariea188d942018-10-16 16:43:04 -0400223 flows.(*openflow_13.Flows).Items[0].TableId = 2244
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400224
Stephane Barbariea188d942018-10-16 16:43:04 -0400225 preUpdateExecuted := false
226 postUpdateExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400227
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500228 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400229 PRE_UPDATE,
230 commonCallback,
231 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted,
232 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500233 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400234 POST_UPDATE,
235 commonCallback,
236 "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted,
237 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400238
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500239 kvFlows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400240
241 if reflect.DeepEqual(flows, kvFlows) {
242 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
243 }
244
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500245 if updated := devFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400246 t.Error("Failed to update flow")
247 } else {
248 t.Logf("Updated flows : %+v", updated)
249 }
250
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500251 if d := devFlowsProxy.Get("/", 0, false, ""); d == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400252 t.Error("Failed to find updated flows (flows proxy)")
253 } else {
254 djson, _ := json.Marshal(d)
255 t.Logf("Found flows (flows proxy): %s", string(djson))
256 }
257
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500258 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400259 t.Error("Failed to find updated flows (root proxy)")
260 } else {
261 djson, _ := json.Marshal(d)
262 t.Logf("Found flows (root proxy): %s", string(djson))
263 }
264
265 if !preUpdateExecuted {
266 t.Error("PRE_UPDATE callback was not executed")
267 }
268 if !postUpdateExecuted {
269 t.Error("POST_UPDATE callback was not executed")
270 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400271}
272
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500273func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Stephane Barbariea188d942018-10-16 16:43:04 -0400274 preRemoveExecuted := false
275 postRemoveExecuted := false
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400276
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500277 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400278 PRE_REMOVE,
279 commonCallback,
280 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
281 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500282 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400283 POST_REMOVE,
284 commonCallback,
285 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
286 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400287
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500288 if removed := TestProxy_Root_Device.Remove("/devices/"+TestProxy_DeviceId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400289 t.Error("Failed to remove device")
290 } else {
291 t.Logf("Removed device : %+v", removed)
292 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500293 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400294 djson, _ := json.Marshal(d)
295 t.Errorf("Device was not removed - %s", djson)
296 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500297 t.Logf("Device was removed: %s", TestProxy_DeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500298 }
299
300 if !preRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500301 t.Error("PRE_REMOVE callback was not executed")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500302 }
303 if !postRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500304 t.Error("POST_REMOVE callback was not executed")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500305 }
306}
307
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500308func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500309
310 ldIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500311 TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12]
312 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500313
314 preAddExecuted := false
315 postAddExecuted := false
316
317 // Register
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500318 TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
319 TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500320
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500321 if added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, ""); added == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500322 t.Error("Failed to add logical device")
323 } else {
324 t.Logf("Added logical device : %+v", added)
325 }
326
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500327 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500328 t.Error("Failed to find added logical device")
329 } else {
330 ldJSON, _ := json.Marshal(ld)
331 t.Logf("Found logical device: %s", string(ldJSON))
332 }
333
334 if !preAddExecuted {
335 t.Error("PRE_ADD callback was not executed")
336 }
337 if !postAddExecuted {
338 t.Error("POST_ADD callback was not executed")
339 }
340}
341
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500342func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
343 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500344
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500345 added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, "");
346 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() {
347 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
348 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500349}
350
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500351func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
352 logicalDevices := TestProxy_Root_LogicalDevice.Get("/logical_devices", 1, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500353
354 if len(logicalDevices.([]interface{})) == 0 {
355 t.Error("there are no available logical devices to retrieve")
356 } else {
357 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500358 TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500359 t.Logf("retrieved all logical devices: %+v", logicalDevices)
360 }
361}
362
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500363func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
364 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
365 t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500366 } else {
367 ldJSON, _ := json.Marshal(ld)
368 t.Logf("Found logical device: %s", string(ldJSON))
369 }
370
371}
372
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500373func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500374 var fwVersion int
375 preUpdateExecuted := false
376 postUpdateExecuted := false
377
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500378 if retrieved := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500379 t.Error("Failed to get logical device")
380 } else {
381 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
382
383 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
384 fwVersion = 0
385 } else {
386 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
387 fwVersion++
388 }
389
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500390 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500391 PRE_UPDATE,
392 commonCallback,
393 "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
394 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500395 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500396 POST_UPDATE,
397 commonCallback,
398 "POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
399 )
400
401 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
402
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500403 if afterUpdate := TestProxy_Root_LogicalDevice.Update("/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500404 ""); afterUpdate == nil {
405 t.Error("Failed to update logical device")
406 } else {
407 t.Logf("Updated logical device : %+v", afterUpdate)
408 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500409 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500410 t.Error("Failed to find updated logical device (root proxy)")
411 } else {
412 djson, _ := json.Marshal(d)
413
414 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
415 }
416
417 if !preUpdateExecuted {
418 t.Error("PRE_UPDATE callback was not executed")
419 }
420 if !postUpdateExecuted {
421 t.Error("POST_UPDATE callback was not executed")
422 }
423 }
424}
425
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500426func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500427 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500428 ldFlowsProxy := TestProxy_Root.node.CreateProxy("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500429 flows := ldFlowsProxy.Get("/", 0, false, "")
430 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
431 t.Logf("before updated flows: %+v", flows)
432
433 ldFlowsProxy.RegisterCallback(
434 PRE_UPDATE,
435 commonCallback2,
436 )
437 ldFlowsProxy.RegisterCallback(
438 POST_UPDATE,
439 commonCallback2,
440 )
441
442 kvFlows := ldFlowsProxy.Get("/", 0, false, "")
443
444 if reflect.DeepEqual(flows, kvFlows) {
445 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
446 }
447
448 if updated := ldFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
449 t.Error("Failed to update logical device flows")
450 } else {
451 t.Logf("Updated logical device flows : %+v", updated)
452 }
453
454 if d := ldFlowsProxy.Get("/", 0, false, ""); d == nil {
455 t.Error("Failed to find updated logical device flows (flows proxy)")
456 } else {
457 djson, _ := json.Marshal(d)
458 t.Logf("Found flows (flows proxy): %s", string(djson))
459 }
460
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500461 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500462 ""); !reflect.ValueOf(d).IsValid() {
463 t.Error("Failed to find updated logical device flows (root proxy)")
464 } else {
465 djson, _ := json.Marshal(d)
466 t.Logf("Found logical device flows (root proxy): %s", string(djson))
467 }
468}
469
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500470func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500471 preRemoveExecuted := false
472 postRemoveExecuted := false
473
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500474 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500475 PRE_REMOVE,
476 commonCallback,
477 "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
478 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500479 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500480 POST_REMOVE,
481 commonCallback,
482 "POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
483 )
484
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500485 if removed := TestProxy_Root_LogicalDevice.Remove("/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500486 t.Error("Failed to remove logical device")
487 } else {
488 t.Logf("Removed device : %+v", removed)
489 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500490 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500491 djson, _ := json.Marshal(d)
492 t.Errorf("Device was not removed - %s", djson)
493 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500494 t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400495 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400496
497 if !preRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500498 t.Error("PRE_REMOVE callback was not executed")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400499 }
500 if !postRemoveExecuted {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500501 t.Error("POST_REMOVE callback was not executed")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400502 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400503}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400504
505// -----------------------------
506// Callback tests
507// -----------------------------
508
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500509func TestProxy_Callbacks_1_Register(t *testing.T) {
510 TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400511
512 m := make(map[string]string)
513 m["name"] = "fghij"
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500514 TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400515
516 d := &voltha.Device{Id: "12345"}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500517 TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400518}
519
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500520func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
521 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400522}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400523
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500524func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
525 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400526}
527
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500528func TestProxy_Callbacks_4_Unregister(t *testing.T) {
529 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback)
530 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback)
531 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400532}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400533
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500534//func TestProxy_Callbacks_5_Add(t *testing.T) {
535// TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400536//}
537//
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500538//func TestProxy_Callbacks_6_Execute(t *testing.T) {
539// TestProxy_Root_Device.Root.ExecuteCallbacks()
Stephane Barbarie126101e2018-10-11 16:18:48 -0400540//}