blob: f583b993c09c303a62facb183b4359082e788e72 [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"
William Kurkiandaa6bb22019-03-07 12:26:28 -050023 "github.com/opencord/voltha-protos/go/common"
24 "github.com/opencord/voltha-protos/go/openflow_13"
25 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026 "math/rand"
khenaidoob9203542018-09-17 22:56:37 -040027 "reflect"
28 "strconv"
29 "testing"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040030 "time"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040031)
32
Stephane Barbarieec0919b2018-09-05 14:14:29 -040033var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050034 TestProxy_Root *root
35 TestProxy_Root_LogicalDevice *Proxy
36 TestProxy_Root_Device *Proxy
Stephane Barbarieaa467942019-02-06 14:09:44 -050037 TestProxy_Root_Adapter *Proxy
Stephane Barbarie1039ec42019-02-04 10:43:16 -050038 TestProxy_DeviceId string
Stephane Barbarieaa467942019-02-06 14:09:44 -050039 TestProxy_AdapterId string
Stephane Barbarie1039ec42019-02-04 10:43:16 -050040 TestProxy_LogicalDeviceId string
41 TestProxy_TargetDeviceId string
42 TestProxy_TargetLogicalDeviceId string
43 TestProxy_LogicalPorts []*voltha.LogicalPort
44 TestProxy_Ports []*voltha.Port
45 TestProxy_Stats *openflow_13.OfpFlowStats
46 TestProxy_Flows *openflow_13.Flows
47 TestProxy_Device *voltha.Device
48 TestProxy_LogicalDevice *voltha.LogicalDevice
Stephane Barbarieaa467942019-02-06 14:09:44 -050049 TestProxy_Adapter *voltha.Adapter
Stephane Barbarieec0919b2018-09-05 14:14:29 -040050)
51
Stephane Barbariedc5022d2018-11-19 15:21:44 -050052func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050053 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
54 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
55 TestProxy_Root = NewRoot(&voltha.Voltha{}, nil)
56 TestProxy_Root_LogicalDevice = TestProxy_Root.CreateProxy("/", false)
57 TestProxy_Root_Device = TestProxy_Root.CreateProxy("/", false)
Stephane Barbarieaa467942019-02-06 14:09:44 -050058 TestProxy_Root_Adapter = TestProxy_Root.CreateProxy("/", false)
Stephane Barbarie1039ec42019-02-04 10:43:16 -050059
60 TestProxy_LogicalPorts = []*voltha.LogicalPort{
61 {
62 Id: "123",
63 DeviceId: "logicalport-0-device-id",
64 DevicePortNo: 123,
65 RootPort: false,
66 },
67 }
68 TestProxy_Ports = []*voltha.Port{
69 {
70 PortNo: 123,
71 Label: "test-port-0",
72 Type: voltha.Port_PON_OLT,
73 AdminState: common.AdminState_ENABLED,
74 OperStatus: common.OperStatus_ACTIVE,
75 DeviceId: "etcd_port-0-device-id",
76 Peers: []*voltha.Port_PeerPort{},
77 },
78 }
79
80 TestProxy_Stats = &openflow_13.OfpFlowStats{
81 Id: 1111,
82 }
83 TestProxy_Flows = &openflow_13.Flows{
84 Items: []*openflow_13.OfpFlowStats{TestProxy_Stats},
85 }
86 TestProxy_Device = &voltha.Device{
87 Id: TestProxy_DeviceId,
88 Type: "simulated_olt",
89 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
90 AdminState: voltha.AdminState_PREPROVISIONED,
91 Flows: TestProxy_Flows,
92 Ports: TestProxy_Ports,
93 }
94
95 TestProxy_LogicalDevice = &voltha.LogicalDevice{
96 Id: TestProxy_DeviceId,
97 DatapathId: 0,
98 Ports: TestProxy_LogicalPorts,
99 Flows: TestProxy_Flows,
100 }
Stephane Barbarieaa467942019-02-06 14:09:44 -0500101
102 TestProxy_Adapter = &voltha.Adapter{
103 Id: TestProxy_AdapterId,
104 Vendor: "test-adapter-vendor",
105 Version: "test-adapter-version",
106 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500107}
108
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500109func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500110 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500111 TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
112 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbarie126101e2018-10-11 16:18:48 -0400113
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400114 preAddExecuted := make(chan struct{})
115 postAddExecuted := make(chan struct{})
116 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbariea188d942018-10-16 16:43:04 -0400117
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500118 devicesProxy := TestProxy_Root.node.CreateProxy("/devices", false)
119 devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes")
120 devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes")
121
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500122 // Register ADD instructions callbacks
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400123 TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
124 TestProxy_Root_Device.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400125
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500126 if added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400127 t.Error("Failed to add device")
128 } else {
129 t.Logf("Added device : %+v", added)
130 }
131
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400132 if !verifyGotResponse(preAddExecuted) {
133 t.Error("PRE_ADD callback was not executed")
134 }
135 if !verifyGotResponse(postAddExecuted) {
136 t.Error("POST_ADD callback was not executed")
137 }
138
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500139 // Verify that the added device can now be retrieved
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500140 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400141 t.Error("Failed to find added device")
142 } else {
143 djson, _ := json.Marshal(d)
144 t.Logf("Found device: %s", string(djson))
145 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400146}
147
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500148func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
149 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500150
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400151 added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, "")
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500152 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() {
153 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400154 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400155}
156
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400157func verifyGotResponse(callbackIndicator <-chan struct{}) bool {
158 timeout := time.After(1 * time.Second)
159 // Wait until the channel closes, or we time out
160 select {
161 case <-callbackIndicator:
162 // Received response successfully
163 return true
164
165 case <-timeout:
166 // Got a timeout! fail with a timeout error
167 return false
168 }
169}
170
Stephane Barbarieaa467942019-02-06 14:09:44 -0500171func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
172 TestProxy_AdapterId = "test-adapter"
173 TestProxy_Adapter.Id = TestProxy_AdapterId
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400174 preAddExecuted := make(chan struct{})
175 postAddExecuted := make(chan struct{})
176 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbarieaa467942019-02-06 14:09:44 -0500177
178 // Register ADD instructions callbacks
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400179 TestProxy_Root_Adapter.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions for adapters", &preAddExecutedPtr)
180 TestProxy_Root_Adapter.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions for adapters", &postAddExecutedPtr)
Stephane Barbarieaa467942019-02-06 14:09:44 -0500181
182 // Add the adapter
183 if added := TestProxy_Root_Adapter.Add("/adapters", TestProxy_Adapter, ""); added == nil {
184 t.Error("Failed to add adapter")
185 } else {
186 t.Logf("Added adapter : %+v", added)
187 }
188
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400189 verifyGotResponse(postAddExecuted)
190
Stephane Barbarieaa467942019-02-06 14:09:44 -0500191 // Verify that the added device can now be retrieved
192 if d := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
193 t.Error("Failed to find added adapter")
194 } else {
195 djson, _ := json.Marshal(d)
196 t.Logf("Found adapter: %s", string(djson))
197 }
198
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400199 if !verifyGotResponse(preAddExecuted) {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500200 t.Error("PRE_ADD callback was not executed")
201 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400202 if !verifyGotResponse(postAddExecuted) {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500203 t.Error("POST_ADD callback was not executed")
204 }
205}
206
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500207func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
208 devices := TestProxy_Root_Device.Get("/devices", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400209
210 if len(devices.([]interface{})) == 0 {
211 t.Error("there are no available devices to retrieve")
212 } else {
213 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500214 TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -0400215 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400216 }
217}
218
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500219func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
220 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
221 t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400222 } else {
223 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400224 t.Logf("Found device: %s", string(djson))
225 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400226}
227
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500228func TestProxy_1_3_1_Update_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500229 var fwVersion int
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400230
231 preUpdateExecuted := make(chan struct{})
232 postUpdateExecuted := make(chan struct{})
233 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500234
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500235 if retrieved := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400236 t.Error("Failed to get device")
237 } else {
Stephane Barbariea188d942018-10-16 16:43:04 -0400238 t.Logf("Found raw device (root proxy): %+v", retrieved)
239
Stephane Barbarie126101e2018-10-11 16:18:48 -0400240 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
241 fwVersion = 0
242 } else {
243 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500244 fwVersion++
Stephane Barbarie126101e2018-10-11 16:18:48 -0400245 }
246
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500247 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400248
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500249 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400250 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400251 commonChanCallback,
252 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400253 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500254 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400255 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400256 commonChanCallback,
257 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400258 )
259
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500260 if afterUpdate := TestProxy_Root_Device.Update("/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400261 t.Error("Failed to update device")
262 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500263 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400264 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500265
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400266 if !verifyGotResponse(preUpdateExecuted) {
267 t.Error("PRE_UPDATE callback was not executed")
268 }
269 if !verifyGotResponse(postUpdateExecuted) {
270 t.Error("POST_UPDATE callback was not executed")
271 }
272
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500273 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400274 t.Error("Failed to find updated device (root proxy)")
275 } else {
276 djson, _ := json.Marshal(d)
Stephane Barbariea188d942018-10-16 16:43:04 -0400277 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400278 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400279 }
280}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400281
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500282func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400283 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500284 devFlowsProxy := TestProxy_Root.node.CreateProxy("/devices/"+TestProxy_DeviceId+"/flows", false)
285 flows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbariea188d942018-10-16 16:43:04 -0400286 flows.(*openflow_13.Flows).Items[0].TableId = 2244
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400287
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400288 preUpdateExecuted := make(chan struct{})
289 postUpdateExecuted := make(chan struct{})
290 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400291
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500292 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400293 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400294 commonChanCallback,
295 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400296 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500297 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400298 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400299 commonChanCallback,
300 "POST_UPDATE instructions (flows proxy)", &postUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400301 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400302
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500303 kvFlows := devFlowsProxy.Get("/", 0, false, "")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400304
305 if reflect.DeepEqual(flows, kvFlows) {
306 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
307 }
308
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500309 if updated := devFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400310 t.Error("Failed to update flow")
311 } else {
312 t.Logf("Updated flows : %+v", updated)
313 }
314
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400315 if !verifyGotResponse(preUpdateExecuted) {
316 t.Error("PRE_UPDATE callback was not executed")
317 }
318 if !verifyGotResponse(postUpdateExecuted) {
319 t.Error("POST_UPDATE callback was not executed")
320 }
321
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500322 if d := devFlowsProxy.Get("/", 0, false, ""); d == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400323 t.Error("Failed to find updated flows (flows proxy)")
324 } else {
325 djson, _ := json.Marshal(d)
326 t.Logf("Found flows (flows proxy): %s", string(djson))
327 }
328
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500329 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400330 t.Error("Failed to find updated flows (root proxy)")
331 } else {
332 djson, _ := json.Marshal(d)
333 t.Logf("Found flows (root proxy): %s", string(djson))
334 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400335}
336
Stephane Barbarieaa467942019-02-06 14:09:44 -0500337func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400338 preUpdateExecuted := make(chan struct{})
339 postUpdateExecuted := make(chan struct{})
340 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbarieaa467942019-02-06 14:09:44 -0500341
342 adaptersProxy := TestProxy_Root.node.CreateProxy("/adapters", false)
343
344 if retrieved := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 1, false, ""); retrieved == nil {
345 t.Error("Failed to get adapter")
346 } else {
347 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
348
349 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
350
351 adaptersProxy.RegisterCallback(
352 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400353 commonChanCallback,
354 "PRE_UPDATE instructions for adapters", &preUpdateExecutedPtr,
Stephane Barbarieaa467942019-02-06 14:09:44 -0500355 )
356 adaptersProxy.RegisterCallback(
357 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400358 commonChanCallback,
359 "POST_UPDATE instructions for adapters", &postUpdateExecutedPtr,
Stephane Barbarieaa467942019-02-06 14:09:44 -0500360 )
361
362 if afterUpdate := adaptersProxy.Update("/"+TestProxy_AdapterId, retrieved, false, ""); afterUpdate == nil {
363 t.Error("Failed to update adapter")
364 } else {
365 t.Logf("Updated adapter : %+v", afterUpdate)
366 }
367
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400368 if !verifyGotResponse(preUpdateExecuted) {
369 t.Error("PRE_UPDATE callback for adapter was not executed")
370 }
371 if !verifyGotResponse(postUpdateExecuted) {
372 t.Error("POST_UPDATE callback for adapter was not executed")
373 }
374
Stephane Barbarieaa467942019-02-06 14:09:44 -0500375 if d := TestProxy_Root_Adapter.Get("/adapters/"+TestProxy_AdapterId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
376 t.Error("Failed to find updated adapter (root proxy)")
377 } else {
378 djson, _ := json.Marshal(d)
379 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
380 }
Stephane Barbarieaa467942019-02-06 14:09:44 -0500381 }
382}
383
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500384func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400385 preRemoveExecuted := make(chan struct{})
386 postRemoveExecuted := make(chan struct{})
387 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400388
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500389 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400390 PRE_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400391 commonChanCallback,
392 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400393 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500394 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400395 POST_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400396 commonChanCallback,
397 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400398 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400399
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500400 if removed := TestProxy_Root_Device.Remove("/devices/"+TestProxy_DeviceId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400401 t.Error("Failed to remove device")
402 } else {
403 t.Logf("Removed device : %+v", removed)
404 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400405
406 if !verifyGotResponse(preRemoveExecuted) {
407 t.Error("PRE_REMOVE callback was not executed")
408 }
409 if !verifyGotResponse(postRemoveExecuted) {
410 t.Error("POST_REMOVE callback was not executed")
411 }
412
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500413 if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400414 djson, _ := json.Marshal(d)
415 t.Errorf("Device was not removed - %s", djson)
416 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500417 t.Logf("Device was removed: %s", TestProxy_DeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500418 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500419}
420
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500421func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500422
423 ldIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500424 TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12]
425 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500426
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400427 preAddExecuted := make(chan struct{})
428 postAddExecuted := make(chan struct{})
429 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500430
431 // Register
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400432 TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
433 TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500434
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500435 if added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, ""); added == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500436 t.Error("Failed to add logical device")
437 } else {
438 t.Logf("Added logical device : %+v", added)
439 }
440
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400441 verifyGotResponse(postAddExecuted)
442
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500443 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500444 t.Error("Failed to find added logical device")
445 } else {
446 ldJSON, _ := json.Marshal(ld)
447 t.Logf("Found logical device: %s", string(ldJSON))
448 }
449
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400450 if !verifyGotResponse(preAddExecuted) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500451 t.Error("PRE_ADD callback was not executed")
452 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400453 if !verifyGotResponse(postAddExecuted) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500454 t.Error("POST_ADD callback was not executed")
455 }
456}
457
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500458func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
459 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500460
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400461 added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, "")
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500462 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() {
463 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
464 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500465}
466
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500467func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
468 logicalDevices := TestProxy_Root_LogicalDevice.Get("/logical_devices", 1, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500469
470 if len(logicalDevices.([]interface{})) == 0 {
471 t.Error("there are no available logical devices to retrieve")
472 } else {
473 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500474 TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500475 t.Logf("retrieved all logical devices: %+v", logicalDevices)
476 }
477}
478
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500479func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
480 if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
481 t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500482 } else {
483 ldJSON, _ := json.Marshal(ld)
484 t.Logf("Found logical device: %s", string(ldJSON))
485 }
486
487}
488
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500489func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500490 var fwVersion int
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400491 preUpdateExecuted := make(chan struct{})
492 postUpdateExecuted := make(chan struct{})
493 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500494
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500495 if retrieved := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500496 t.Error("Failed to get logical device")
497 } else {
498 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
499
500 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
501 fwVersion = 0
502 } else {
503 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
504 fwVersion++
505 }
506
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500507 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500508 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400509 commonChanCallback,
510 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500511 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500512 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500513 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400514 commonChanCallback,
515 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500516 )
517
518 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
519
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500520 if afterUpdate := TestProxy_Root_LogicalDevice.Update("/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500521 ""); afterUpdate == nil {
522 t.Error("Failed to update logical device")
523 } else {
524 t.Logf("Updated logical device : %+v", afterUpdate)
525 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400526
527 if !verifyGotResponse(preUpdateExecuted) {
528 t.Error("PRE_UPDATE callback was not executed")
529 }
530 if !verifyGotResponse(postUpdateExecuted) {
531 t.Error("POST_UPDATE callback was not executed")
532 }
533
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500534 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500535 t.Error("Failed to find updated logical device (root proxy)")
536 } else {
537 djson, _ := json.Marshal(d)
538
539 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
540 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500541 }
542}
543
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500544func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500545 // Get a device proxy and update a specific port
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500546 ldFlowsProxy := TestProxy_Root.node.CreateProxy("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500547 flows := ldFlowsProxy.Get("/", 0, false, "")
548 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
549 t.Logf("before updated flows: %+v", flows)
550
551 ldFlowsProxy.RegisterCallback(
552 PRE_UPDATE,
553 commonCallback2,
554 )
555 ldFlowsProxy.RegisterCallback(
556 POST_UPDATE,
557 commonCallback2,
558 )
559
560 kvFlows := ldFlowsProxy.Get("/", 0, false, "")
561
562 if reflect.DeepEqual(flows, kvFlows) {
563 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
564 }
565
566 if updated := ldFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
567 t.Error("Failed to update logical device flows")
568 } else {
569 t.Logf("Updated logical device flows : %+v", updated)
570 }
571
572 if d := ldFlowsProxy.Get("/", 0, false, ""); d == nil {
573 t.Error("Failed to find updated logical device flows (flows proxy)")
574 } else {
575 djson, _ := json.Marshal(d)
576 t.Logf("Found flows (flows proxy): %s", string(djson))
577 }
578
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500579 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500580 ""); !reflect.ValueOf(d).IsValid() {
581 t.Error("Failed to find updated logical device flows (root proxy)")
582 } else {
583 djson, _ := json.Marshal(d)
584 t.Logf("Found logical device flows (root proxy): %s", string(djson))
585 }
586}
587
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500588func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400589 preRemoveExecuted := make(chan struct{})
590 postRemoveExecuted := make(chan struct{})
591 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500592
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500593 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500594 PRE_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400595 commonChanCallback,
596 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500597 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500598 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500599 POST_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400600 commonChanCallback,
601 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500602 )
603
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500604 if removed := TestProxy_Root_LogicalDevice.Remove("/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500605 t.Error("Failed to remove logical device")
606 } else {
607 t.Logf("Removed device : %+v", removed)
608 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400609
610 if !verifyGotResponse(preRemoveExecuted) {
611 t.Error("PRE_REMOVE callback was not executed")
612 }
613 if !verifyGotResponse(postRemoveExecuted) {
614 t.Error("POST_REMOVE callback was not executed")
615 }
616
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500617 if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500618 djson, _ := json.Marshal(d)
619 t.Errorf("Device was not removed - %s", djson)
620 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500621 t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400622 }
623}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400624
625// -----------------------------
626// Callback tests
627// -----------------------------
628
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500629func TestProxy_Callbacks_1_Register(t *testing.T) {
630 TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400631
632 m := make(map[string]string)
633 m["name"] = "fghij"
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500634 TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400635
636 d := &voltha.Device{Id: "12345"}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500637 TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400638}
639
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500640func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
641 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400642}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400643
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500644func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
645 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400646}
647
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500648func TestProxy_Callbacks_4_Unregister(t *testing.T) {
649 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback)
650 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback)
651 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400652}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400653
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500654//func TestProxy_Callbacks_5_Add(t *testing.T) {
655// TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400656//}
657//
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500658//func TestProxy_Callbacks_6_Execute(t *testing.T) {
659// TestProxy_Root_Device.Root.ExecuteCallbacks()
Stephane Barbarie126101e2018-10-11 16:18:48 -0400660//}