blob: 3f65997c8da6cb718cd0af4b4cc4ef12f5c9f379 [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 Barbarieef6650d2019-07-18 12:15:09 -040019 "context"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040020 "encoding/hex"
21 "encoding/json"
Stephane Barbarie1039ec42019-02-04 10:43:16 -050022 "github.com/golang/protobuf/proto"
khenaidoob9203542018-09-17 22:56:37 -040023 "github.com/google/uuid"
William Kurkiandaa6bb22019-03-07 12:26:28 -050024 "github.com/opencord/voltha-protos/go/common"
25 "github.com/opencord/voltha-protos/go/openflow_13"
26 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027 "math/rand"
khenaidoob9203542018-09-17 22:56:37 -040028 "reflect"
29 "strconv"
30 "testing"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040031 "time"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032)
33
Stephane Barbarieec0919b2018-09-05 14:14:29 -040034var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050035 TestProxy_Root *root
36 TestProxy_Root_LogicalDevice *Proxy
37 TestProxy_Root_Device *Proxy
Stephane Barbarieaa467942019-02-06 14:09:44 -050038 TestProxy_Root_Adapter *Proxy
Stephane Barbarie1039ec42019-02-04 10:43:16 -050039 TestProxy_DeviceId string
Stephane Barbarieaa467942019-02-06 14:09:44 -050040 TestProxy_AdapterId string
Stephane Barbarie1039ec42019-02-04 10:43:16 -050041 TestProxy_LogicalDeviceId string
42 TestProxy_TargetDeviceId string
43 TestProxy_TargetLogicalDeviceId string
44 TestProxy_LogicalPorts []*voltha.LogicalPort
45 TestProxy_Ports []*voltha.Port
46 TestProxy_Stats *openflow_13.OfpFlowStats
47 TestProxy_Flows *openflow_13.Flows
48 TestProxy_Device *voltha.Device
49 TestProxy_LogicalDevice *voltha.LogicalDevice
Stephane Barbarieaa467942019-02-06 14:09:44 -050050 TestProxy_Adapter *voltha.Adapter
Stephane Barbarieec0919b2018-09-05 14:14:29 -040051)
52
Stephane Barbariedc5022d2018-11-19 15:21:44 -050053func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050054 //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"})
55 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
56 TestProxy_Root = NewRoot(&voltha.Voltha{}, nil)
Stephane Barbarieef6650d2019-07-18 12:15:09 -040057 TestProxy_Root_LogicalDevice = TestProxy_Root.CreateProxy(context.Background(), "/", false)
58 TestProxy_Root_Device = TestProxy_Root.CreateProxy(context.Background(), "/", false)
59 TestProxy_Root_Adapter = TestProxy_Root.CreateProxy(context.Background(), "/", false)
Stephane Barbarie1039ec42019-02-04 10:43:16 -050060
61 TestProxy_LogicalPorts = []*voltha.LogicalPort{
62 {
63 Id: "123",
64 DeviceId: "logicalport-0-device-id",
65 DevicePortNo: 123,
66 RootPort: false,
67 },
68 }
69 TestProxy_Ports = []*voltha.Port{
70 {
71 PortNo: 123,
72 Label: "test-port-0",
73 Type: voltha.Port_PON_OLT,
74 AdminState: common.AdminState_ENABLED,
75 OperStatus: common.OperStatus_ACTIVE,
76 DeviceId: "etcd_port-0-device-id",
77 Peers: []*voltha.Port_PeerPort{},
78 },
79 }
80
81 TestProxy_Stats = &openflow_13.OfpFlowStats{
82 Id: 1111,
83 }
84 TestProxy_Flows = &openflow_13.Flows{
85 Items: []*openflow_13.OfpFlowStats{TestProxy_Stats},
86 }
87 TestProxy_Device = &voltha.Device{
88 Id: TestProxy_DeviceId,
89 Type: "simulated_olt",
90 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
91 AdminState: voltha.AdminState_PREPROVISIONED,
92 Flows: TestProxy_Flows,
93 Ports: TestProxy_Ports,
94 }
95
96 TestProxy_LogicalDevice = &voltha.LogicalDevice{
97 Id: TestProxy_DeviceId,
98 DatapathId: 0,
99 Ports: TestProxy_LogicalPorts,
100 Flows: TestProxy_Flows,
101 }
Stephane Barbarieaa467942019-02-06 14:09:44 -0500102
103 TestProxy_Adapter = &voltha.Adapter{
104 Id: TestProxy_AdapterId,
105 Vendor: "test-adapter-vendor",
106 Version: "test-adapter-version",
107 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500108}
109
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500110func TestProxy_1_1_1_Add_NewDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500111 devIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500112 TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12]
113 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbarie126101e2018-10-11 16:18:48 -0400114
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400115 preAddExecuted := make(chan struct{})
116 postAddExecuted := make(chan struct{})
117 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbariea188d942018-10-16 16:43:04 -0400118
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400119 devicesProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/devices", false)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500120 devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes")
121 devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes")
122
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500123 // Register ADD instructions callbacks
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400124 TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
125 TestProxy_Root_Device.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400126
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400127 if added := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, ""); added == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400128 t.Error("Failed to add device")
129 } else {
130 t.Logf("Added device : %+v", added)
131 }
132
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400133 if !verifyGotResponse(preAddExecuted) {
134 t.Error("PRE_ADD callback was not executed")
135 }
136 if !verifyGotResponse(postAddExecuted) {
137 t.Error("POST_ADD callback was not executed")
138 }
139
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500140 // Verify that the added device can now be retrieved
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400141 if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400142 t.Error("Failed to find added device")
143 } else {
144 djson, _ := json.Marshal(d)
145 t.Logf("Found device: %s", string(djson))
146 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400147}
148
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500149func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) {
150 TestProxy_Device.Id = TestProxy_DeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500151
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400152 added := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, "")
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500153 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() {
154 t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400155 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400156}
157
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400158func verifyGotResponse(callbackIndicator <-chan struct{}) bool {
159 timeout := time.After(1 * time.Second)
160 // Wait until the channel closes, or we time out
161 select {
162 case <-callbackIndicator:
163 // Received response successfully
164 return true
165
166 case <-timeout:
167 // Got a timeout! fail with a timeout error
168 return false
169 }
170}
171
Stephane Barbarieaa467942019-02-06 14:09:44 -0500172func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) {
173 TestProxy_AdapterId = "test-adapter"
174 TestProxy_Adapter.Id = TestProxy_AdapterId
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400175 preAddExecuted := make(chan struct{})
176 postAddExecuted := make(chan struct{})
177 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbarieaa467942019-02-06 14:09:44 -0500178
179 // Register ADD instructions callbacks
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400180 TestProxy_Root_Adapter.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions for adapters", &preAddExecutedPtr)
181 TestProxy_Root_Adapter.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions for adapters", &postAddExecutedPtr)
Stephane Barbarieaa467942019-02-06 14:09:44 -0500182
183 // Add the adapter
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400184 if added := TestProxy_Root_Adapter.Add(context.Background(), "/adapters", TestProxy_Adapter, ""); added == nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500185 t.Error("Failed to add adapter")
186 } else {
187 t.Logf("Added adapter : %+v", added)
188 }
189
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400190 verifyGotResponse(postAddExecuted)
191
Stephane Barbarieaa467942019-02-06 14:09:44 -0500192 // Verify that the added device can now be retrieved
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400193 if d := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500194 t.Error("Failed to find added adapter")
195 } else {
196 djson, _ := json.Marshal(d)
197 t.Logf("Found adapter: %s", string(djson))
198 }
199
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400200 if !verifyGotResponse(preAddExecuted) {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500201 t.Error("PRE_ADD callback was not executed")
202 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400203 if !verifyGotResponse(postAddExecuted) {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500204 t.Error("POST_ADD callback was not executed")
205 }
206}
207
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500208func TestProxy_1_2_1_Get_AllDevices(t *testing.T) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400209 devices := TestProxy_Root_Device.Get(context.Background(), "/devices", 1, false, "")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400210
211 if len(devices.([]interface{})) == 0 {
212 t.Error("there are no available devices to retrieve")
213 } else {
214 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500215 TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id
Stephane Barbarie126101e2018-10-11 16:18:48 -0400216 t.Logf("retrieved all devices: %+v", devices)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400217 }
218}
219
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500220func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400221 if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500222 t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400223 } else {
224 djson, _ := json.Marshal(d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400225 t.Logf("Found device: %s", string(djson))
226 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400227}
228
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500229func TestProxy_1_3_1_Update_Device(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500230 var fwVersion int
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400231
232 preUpdateExecuted := make(chan struct{})
233 postUpdateExecuted := make(chan struct{})
234 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500235
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400236 if retrieved := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400237 t.Error("Failed to get device")
238 } else {
Stephane Barbariea188d942018-10-16 16:43:04 -0400239 t.Logf("Found raw device (root proxy): %+v", retrieved)
240
Stephane Barbarie126101e2018-10-11 16:18:48 -0400241 if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
242 fwVersion = 0
243 } else {
244 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500245 fwVersion++
Stephane Barbarie126101e2018-10-11 16:18:48 -0400246 }
247
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248 retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400249
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500250 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400251 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400252 commonChanCallback,
253 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400254 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500255 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400256 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400257 commonChanCallback,
258 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400259 )
260
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400261 if afterUpdate := TestProxy_Root_Device.Update(context.Background(), "/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400262 t.Error("Failed to update device")
263 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500264 t.Logf("Updated device : %+v", afterUpdate)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400265 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500266
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400267 if !verifyGotResponse(preUpdateExecuted) {
268 t.Error("PRE_UPDATE callback was not executed")
269 }
270 if !verifyGotResponse(postUpdateExecuted) {
271 t.Error("POST_UPDATE callback was not executed")
272 }
273
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400274 if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400275 t.Error("Failed to find updated device (root proxy)")
276 } else {
277 djson, _ := json.Marshal(d)
Stephane Barbariea188d942018-10-16 16:43:04 -0400278 t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400279 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400280 }
281}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400282
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500283func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400284 // Get a device proxy and update a specific port
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400285 devFlowsProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", false)
286 flows := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
Stephane Barbariea188d942018-10-16 16:43:04 -0400287 flows.(*openflow_13.Flows).Items[0].TableId = 2244
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400288
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400289 preUpdateExecuted := make(chan struct{})
290 postUpdateExecuted := make(chan struct{})
291 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400292
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500293 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400294 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400295 commonChanCallback,
296 "PRE_UPDATE instructions (flows proxy)", &preUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400297 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500298 devFlowsProxy.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400299 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400300 commonChanCallback,
301 "POST_UPDATE instructions (flows proxy)", &postUpdateExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400302 )
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400303
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400304 kvFlows := devFlowsProxy.Get(context.Background(), "/", 0, false, "")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400305
306 if reflect.DeepEqual(flows, kvFlows) {
307 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
308 }
309
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400310 if updated := devFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400311 t.Error("Failed to update flow")
312 } else {
313 t.Logf("Updated flows : %+v", updated)
314 }
315
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400316 if !verifyGotResponse(preUpdateExecuted) {
317 t.Error("PRE_UPDATE callback was not executed")
318 }
319 if !verifyGotResponse(postUpdateExecuted) {
320 t.Error("POST_UPDATE callback was not executed")
321 }
322
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400323 if d := devFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400324 t.Error("Failed to find updated flows (flows proxy)")
325 } else {
326 djson, _ := json.Marshal(d)
327 t.Logf("Found flows (flows proxy): %s", string(djson))
328 }
329
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400330 if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400331 t.Error("Failed to find updated flows (root proxy)")
332 } else {
333 djson, _ := json.Marshal(d)
334 t.Logf("Found flows (root proxy): %s", string(djson))
335 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400336}
337
Stephane Barbarieaa467942019-02-06 14:09:44 -0500338func TestProxy_1_3_3_Update_Adapter(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400339 preUpdateExecuted := make(chan struct{})
340 postUpdateExecuted := make(chan struct{})
341 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbarieaa467942019-02-06 14:09:44 -0500342
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400343 adaptersProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/adapters", false)
Stephane Barbarieaa467942019-02-06 14:09:44 -0500344
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400345 if retrieved := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, ""); retrieved == nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500346 t.Error("Failed to get adapter")
347 } else {
348 t.Logf("Found raw adapter (root proxy): %+v", retrieved)
349
350 retrieved.(*voltha.Adapter).Version = "test-adapter-version-2"
351
352 adaptersProxy.RegisterCallback(
353 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400354 commonChanCallback,
355 "PRE_UPDATE instructions for adapters", &preUpdateExecutedPtr,
Stephane Barbarieaa467942019-02-06 14:09:44 -0500356 )
357 adaptersProxy.RegisterCallback(
358 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400359 commonChanCallback,
360 "POST_UPDATE instructions for adapters", &postUpdateExecutedPtr,
Stephane Barbarieaa467942019-02-06 14:09:44 -0500361 )
362
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400363 if afterUpdate := adaptersProxy.Update(context.Background(), "/"+TestProxy_AdapterId, retrieved, false, ""); afterUpdate == nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500364 t.Error("Failed to update adapter")
365 } else {
366 t.Logf("Updated adapter : %+v", afterUpdate)
367 }
368
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400369 if !verifyGotResponse(preUpdateExecuted) {
370 t.Error("PRE_UPDATE callback for adapter was not executed")
371 }
372 if !verifyGotResponse(postUpdateExecuted) {
373 t.Error("POST_UPDATE callback for adapter was not executed")
374 }
375
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400376 if d := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500377 t.Error("Failed to find updated adapter (root proxy)")
378 } else {
379 djson, _ := json.Marshal(d)
380 t.Logf("Found adapter (root proxy): %s raw: %+v", string(djson), d)
381 }
Stephane Barbarieaa467942019-02-06 14:09:44 -0500382 }
383}
384
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500385func TestProxy_1_4_1_Remove_Device(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400386 preRemoveExecuted := make(chan struct{})
387 postRemoveExecuted := make(chan struct{})
388 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400389
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500390 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400391 PRE_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400392 commonChanCallback,
393 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400394 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500395 TestProxy_Root_Device.RegisterCallback(
Stephane Barbarie126101e2018-10-11 16:18:48 -0400396 POST_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400397 commonChanCallback,
398 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400399 )
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400400
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400401 if removed := TestProxy_Root_Device.Remove(context.Background(), "/devices/"+TestProxy_DeviceId, ""); removed == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400402 t.Error("Failed to remove device")
403 } else {
404 t.Logf("Removed device : %+v", removed)
405 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400406
407 if !verifyGotResponse(preRemoveExecuted) {
408 t.Error("PRE_REMOVE callback was not executed")
409 }
410 if !verifyGotResponse(postRemoveExecuted) {
411 t.Error("POST_REMOVE callback was not executed")
412 }
413
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400414 if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400415 djson, _ := json.Marshal(d)
416 t.Errorf("Device was not removed - %s", djson)
417 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500418 t.Logf("Device was removed: %s", TestProxy_DeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500419 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500420}
421
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500422func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500423
424 ldIDBin, _ := uuid.New().MarshalBinary()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500425 TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12]
426 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500427
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400428 preAddExecuted := make(chan struct{})
429 postAddExecuted := make(chan struct{})
430 preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500431
432 // Register
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400433 TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr)
434 TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500435
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400436 if added := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, ""); added == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500437 t.Error("Failed to add logical device")
438 } else {
439 t.Logf("Added logical device : %+v", added)
440 }
441
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400442 verifyGotResponse(postAddExecuted)
443
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400444 if ld := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500445 t.Error("Failed to find added logical device")
446 } else {
447 ldJSON, _ := json.Marshal(ld)
448 t.Logf("Found logical device: %s", string(ldJSON))
449 }
450
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400451 if !verifyGotResponse(preAddExecuted) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500452 t.Error("PRE_ADD callback was not executed")
453 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400454 if !verifyGotResponse(postAddExecuted) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500455 t.Error("POST_ADD callback was not executed")
456 }
457}
458
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500459func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) {
460 TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500461
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400462 added := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, "")
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500463 if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() {
464 t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added)
465 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500466}
467
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500468func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400469 logicalDevices := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices", 1, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500470
471 if len(logicalDevices.([]interface{})) == 0 {
472 t.Error("there are no available logical devices to retrieve")
473 } else {
474 // Save the target device id for later tests
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500475 TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500476 t.Logf("retrieved all logical devices: %+v", logicalDevices)
477 }
478}
479
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500480func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400481 if ld := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500482 t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500483 } else {
484 ldJSON, _ := json.Marshal(ld)
485 t.Logf("Found logical device: %s", string(ldJSON))
486 }
487
488}
489
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500490func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500491 var fwVersion int
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400492 preUpdateExecuted := make(chan struct{})
493 postUpdateExecuted := make(chan struct{})
494 preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500495
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400496 if retrieved := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500497 t.Error("Failed to get logical device")
498 } else {
499 t.Logf("Found raw logical device (root proxy): %+v", retrieved)
500
501 if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" {
502 fwVersion = 0
503 } else {
504 fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId)
505 fwVersion++
506 }
507
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500508 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500509 PRE_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400510 commonChanCallback,
511 "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500512 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500513 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500514 POST_UPDATE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400515 commonChanCallback,
516 "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500517 )
518
519 retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion)
520
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400521 if afterUpdate := TestProxy_Root_LogicalDevice.Update(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500522 ""); afterUpdate == nil {
523 t.Error("Failed to update logical device")
524 } else {
525 t.Logf("Updated logical device : %+v", afterUpdate)
526 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400527
528 if !verifyGotResponse(preUpdateExecuted) {
529 t.Error("PRE_UPDATE callback was not executed")
530 }
531 if !verifyGotResponse(postUpdateExecuted) {
532 t.Error("POST_UPDATE callback was not executed")
533 }
534
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400535 if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500536 t.Error("Failed to find updated logical device (root proxy)")
537 } else {
538 djson, _ := json.Marshal(d)
539
540 t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d)
541 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500542 }
543}
544
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500545func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500546 // Get a device proxy and update a specific port
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400547 ldFlowsProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false)
548 flows := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500549 flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32()
550 t.Logf("before updated flows: %+v", flows)
551
552 ldFlowsProxy.RegisterCallback(
553 PRE_UPDATE,
554 commonCallback2,
555 )
556 ldFlowsProxy.RegisterCallback(
557 POST_UPDATE,
558 commonCallback2,
559 )
560
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400561 kvFlows := ldFlowsProxy.Get(context.Background(), "/", 0, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500562
563 if reflect.DeepEqual(flows, kvFlows) {
564 t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows)
565 }
566
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400567 if updated := ldFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500568 t.Error("Failed to update logical device flows")
569 } else {
570 t.Logf("Updated logical device flows : %+v", updated)
571 }
572
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400573 if d := ldFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500574 t.Error("Failed to find updated logical device flows (flows proxy)")
575 } else {
576 djson, _ := json.Marshal(d)
577 t.Logf("Found flows (flows proxy): %s", string(djson))
578 }
579
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400580 if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500581 ""); !reflect.ValueOf(d).IsValid() {
582 t.Error("Failed to find updated logical device flows (root proxy)")
583 } else {
584 djson, _ := json.Marshal(d)
585 t.Logf("Found logical device flows (root proxy): %s", string(djson))
586 }
587}
588
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500589func TestProxy_2_4_1_Remove_Device(t *testing.T) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400590 preRemoveExecuted := make(chan struct{})
591 postRemoveExecuted := make(chan struct{})
592 preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500593
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500594 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500595 PRE_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400596 commonChanCallback,
597 "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500598 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500599 TestProxy_Root_LogicalDevice.RegisterCallback(
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500600 POST_REMOVE,
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400601 commonChanCallback,
602 "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500603 )
604
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400605 if removed := TestProxy_Root_LogicalDevice.Remove(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500606 t.Error("Failed to remove logical device")
607 } else {
608 t.Logf("Removed device : %+v", removed)
609 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400610
611 if !verifyGotResponse(preRemoveExecuted) {
612 t.Error("PRE_REMOVE callback was not executed")
613 }
614 if !verifyGotResponse(postRemoveExecuted) {
615 t.Error("POST_REMOVE callback was not executed")
616 }
617
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400618 if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500619 djson, _ := json.Marshal(d)
620 t.Errorf("Device was not removed - %s", djson)
621 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500622 t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400623 }
624}
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400625
626// -----------------------------
627// Callback tests
628// -----------------------------
629
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500630func TestProxy_Callbacks_1_Register(t *testing.T) {
631 TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400632
633 m := make(map[string]string)
634 m["name"] = "fghij"
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500635 TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400636
637 d := &voltha.Device{Id: "12345"}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500638 TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400639}
640
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500641func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
642 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400643}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400644
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500645func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
646 TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400647}
648
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500649func TestProxy_Callbacks_4_Unregister(t *testing.T) {
650 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback)
651 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback)
652 TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400653}
Stephane Barbarie126101e2018-10-11 16:18:48 -0400654
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500655//func TestProxy_Callbacks_5_Add(t *testing.T) {
656// TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
Stephane Barbarie126101e2018-10-11 16:18:48 -0400657//}
658//
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500659//func TestProxy_Callbacks_6_Execute(t *testing.T) {
660// TestProxy_Root_Device.Root.ExecuteCallbacks()
Stephane Barbarie126101e2018-10-11 16:18:48 -0400661//}