Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/hex" |
| 21 | "encoding/json" |
| 22 | "github.com/golang/protobuf/proto" |
| 23 | "github.com/google/uuid" |
| 24 | "github.com/opencord/voltha-protos/go/common" |
| 25 | "github.com/opencord/voltha-protos/go/openflow_13" |
| 26 | "github.com/opencord/voltha-protos/go/voltha" |
| 27 | "math/rand" |
| 28 | "reflect" |
| 29 | "strconv" |
| 30 | "testing" |
| 31 | "time" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | TestProxy_Root *root |
| 36 | TestProxy_Root_LogicalDevice *Proxy |
| 37 | TestProxy_Root_Device *Proxy |
| 38 | TestProxy_Root_Adapter *Proxy |
| 39 | TestProxy_DeviceId string |
| 40 | TestProxy_AdapterId string |
| 41 | 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 |
| 50 | TestProxy_Adapter *voltha.Adapter |
| 51 | ) |
| 52 | |
| 53 | func init() { |
| 54 | //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) |
| 57 | 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) |
| 60 | |
| 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 | } |
| 102 | |
| 103 | TestProxy_Adapter = &voltha.Adapter{ |
| 104 | Id: TestProxy_AdapterId, |
| 105 | Vendor: "test-adapter-vendor", |
| 106 | Version: "test-adapter-version", |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestProxy_1_1_1_Add_NewDevice(t *testing.T) { |
| 111 | devIDBin, _ := uuid.New().MarshalBinary() |
| 112 | TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12] |
| 113 | TestProxy_Device.Id = TestProxy_DeviceId |
| 114 | |
| 115 | preAddExecuted := make(chan struct{}) |
| 116 | postAddExecuted := make(chan struct{}) |
| 117 | preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted |
| 118 | |
| 119 | devicesProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/devices", false) |
| 120 | devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes") |
| 121 | devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes") |
| 122 | |
| 123 | // Register ADD instructions callbacks |
| 124 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr) |
| 125 | TestProxy_Root_Device.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr) |
| 126 | |
| 127 | if added := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, ""); added == nil { |
| 128 | t.Error("Failed to add device") |
| 129 | } else { |
| 130 | t.Logf("Added device : %+v", added) |
| 131 | } |
| 132 | |
| 133 | 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 | |
| 140 | // Verify that the added device can now be retrieved |
| 141 | if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 142 | t.Error("Failed to find added device") |
| 143 | } else { |
| 144 | djson, _ := json.Marshal(d) |
| 145 | t.Logf("Found device: %s", string(djson)) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) { |
| 150 | TestProxy_Device.Id = TestProxy_DeviceId |
| 151 | |
| 152 | added := TestProxy_Root_Device.Add(context.Background(), "/devices", TestProxy_Device, "") |
| 153 | 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) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func 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 | |
| 172 | func TestProxy_1_1_3_Add_NewAdapter(t *testing.T) { |
| 173 | TestProxy_AdapterId = "test-adapter" |
| 174 | TestProxy_Adapter.Id = TestProxy_AdapterId |
| 175 | preAddExecuted := make(chan struct{}) |
| 176 | postAddExecuted := make(chan struct{}) |
| 177 | preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted |
| 178 | |
| 179 | // Register ADD instructions callbacks |
| 180 | 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) |
| 182 | |
| 183 | // Add the adapter |
| 184 | if added := TestProxy_Root_Adapter.Add(context.Background(), "/adapters", TestProxy_Adapter, ""); added == nil { |
| 185 | t.Error("Failed to add adapter") |
| 186 | } else { |
| 187 | t.Logf("Added adapter : %+v", added) |
| 188 | } |
| 189 | |
| 190 | verifyGotResponse(postAddExecuted) |
| 191 | |
| 192 | // Verify that the added device can now be retrieved |
| 193 | if d := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 194 | t.Error("Failed to find added adapter") |
| 195 | } else { |
| 196 | djson, _ := json.Marshal(d) |
| 197 | t.Logf("Found adapter: %s", string(djson)) |
| 198 | } |
| 199 | |
| 200 | if !verifyGotResponse(preAddExecuted) { |
| 201 | t.Error("PRE_ADD callback was not executed") |
| 202 | } |
| 203 | if !verifyGotResponse(postAddExecuted) { |
| 204 | t.Error("POST_ADD callback was not executed") |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestProxy_1_2_1_Get_AllDevices(t *testing.T) { |
| 209 | devices := TestProxy_Root_Device.Get(context.Background(), "/devices", 1, false, "") |
| 210 | |
| 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 |
| 215 | TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id |
| 216 | t.Logf("retrieved all devices: %+v", devices) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) { |
| 221 | if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 222 | t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId) |
| 223 | } else { |
| 224 | djson, _ := json.Marshal(d) |
| 225 | t.Logf("Found device: %s", string(djson)) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestProxy_1_3_1_Update_Device(t *testing.T) { |
| 230 | var fwVersion int |
| 231 | |
| 232 | preUpdateExecuted := make(chan struct{}) |
| 233 | postUpdateExecuted := make(chan struct{}) |
| 234 | preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted |
| 235 | |
| 236 | if retrieved := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil { |
| 237 | t.Error("Failed to get device") |
| 238 | } else { |
| 239 | t.Logf("Found raw device (root proxy): %+v", retrieved) |
| 240 | |
| 241 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 242 | fwVersion = 0 |
| 243 | } else { |
| 244 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 245 | fwVersion++ |
| 246 | } |
| 247 | |
| 248 | retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion) |
| 249 | |
| 250 | TestProxy_Root_Device.RegisterCallback( |
| 251 | PRE_UPDATE, |
| 252 | commonChanCallback, |
| 253 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr, |
| 254 | ) |
| 255 | TestProxy_Root_Device.RegisterCallback( |
| 256 | POST_UPDATE, |
| 257 | commonChanCallback, |
| 258 | "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr, |
| 259 | ) |
| 260 | |
| 261 | if afterUpdate := TestProxy_Root_Device.Update(context.Background(), "/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil { |
| 262 | t.Error("Failed to update device") |
| 263 | } else { |
| 264 | t.Logf("Updated device : %+v", afterUpdate) |
| 265 | } |
| 266 | |
| 267 | 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 | |
| 274 | if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
| 275 | t.Error("Failed to find updated device (root proxy)") |
| 276 | } else { |
| 277 | djson, _ := json.Marshal(d) |
| 278 | t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d) |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) { |
| 284 | // Get a device proxy and update a specific port |
| 285 | devFlowsProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", false) |
| 286 | flows := devFlowsProxy.Get(context.Background(), "/", 0, false, "") |
| 287 | flows.(*openflow_13.Flows).Items[0].TableId = 2244 |
| 288 | |
| 289 | preUpdateExecuted := make(chan struct{}) |
| 290 | postUpdateExecuted := make(chan struct{}) |
| 291 | preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted |
| 292 | |
| 293 | devFlowsProxy.RegisterCallback( |
| 294 | PRE_UPDATE, |
| 295 | commonChanCallback, |
| 296 | "PRE_UPDATE instructions (flows proxy)", &preUpdateExecutedPtr, |
| 297 | ) |
| 298 | devFlowsProxy.RegisterCallback( |
| 299 | POST_UPDATE, |
| 300 | commonChanCallback, |
| 301 | "POST_UPDATE instructions (flows proxy)", &postUpdateExecutedPtr, |
| 302 | ) |
| 303 | |
| 304 | kvFlows := devFlowsProxy.Get(context.Background(), "/", 0, false, "") |
| 305 | |
| 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 | |
| 310 | if updated := devFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
| 311 | t.Error("Failed to update flow") |
| 312 | } else { |
| 313 | t.Logf("Updated flows : %+v", updated) |
| 314 | } |
| 315 | |
| 316 | 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 | |
| 323 | if d := devFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil { |
| 324 | 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 | |
| 330 | if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() { |
| 331 | 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 | } |
| 336 | } |
| 337 | |
| 338 | func TestProxy_1_3_3_Update_Adapter(t *testing.T) { |
| 339 | preUpdateExecuted := make(chan struct{}) |
| 340 | postUpdateExecuted := make(chan struct{}) |
| 341 | preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted |
| 342 | |
| 343 | adaptersProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/adapters", false) |
| 344 | |
| 345 | if retrieved := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, ""); retrieved == nil { |
| 346 | 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, |
| 354 | commonChanCallback, |
| 355 | "PRE_UPDATE instructions for adapters", &preUpdateExecutedPtr, |
| 356 | ) |
| 357 | adaptersProxy.RegisterCallback( |
| 358 | POST_UPDATE, |
| 359 | commonChanCallback, |
| 360 | "POST_UPDATE instructions for adapters", &postUpdateExecutedPtr, |
| 361 | ) |
| 362 | |
| 363 | if afterUpdate := adaptersProxy.Update(context.Background(), "/"+TestProxy_AdapterId, retrieved, false, ""); afterUpdate == nil { |
| 364 | t.Error("Failed to update adapter") |
| 365 | } else { |
| 366 | t.Logf("Updated adapter : %+v", afterUpdate) |
| 367 | } |
| 368 | |
| 369 | 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 | |
| 376 | if d := TestProxy_Root_Adapter.Get(context.Background(), "/adapters/"+TestProxy_AdapterId, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
| 377 | 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 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | func TestProxy_1_4_1_Remove_Device(t *testing.T) { |
| 386 | preRemoveExecuted := make(chan struct{}) |
| 387 | postRemoveExecuted := make(chan struct{}) |
| 388 | preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted |
| 389 | |
| 390 | TestProxy_Root_Device.RegisterCallback( |
| 391 | PRE_REMOVE, |
| 392 | commonChanCallback, |
| 393 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr, |
| 394 | ) |
| 395 | TestProxy_Root_Device.RegisterCallback( |
| 396 | POST_REMOVE, |
| 397 | commonChanCallback, |
| 398 | "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr, |
| 399 | ) |
| 400 | |
| 401 | if removed := TestProxy_Root_Device.Remove(context.Background(), "/devices/"+TestProxy_DeviceId, ""); removed == nil { |
| 402 | t.Error("Failed to remove device") |
| 403 | } else { |
| 404 | t.Logf("Removed device : %+v", removed) |
| 405 | } |
| 406 | |
| 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 | |
| 414 | if d := TestProxy_Root_Device.Get(context.Background(), "/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
| 415 | djson, _ := json.Marshal(d) |
| 416 | t.Errorf("Device was not removed - %s", djson) |
| 417 | } else { |
| 418 | t.Logf("Device was removed: %s", TestProxy_DeviceId) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) { |
| 423 | |
| 424 | ldIDBin, _ := uuid.New().MarshalBinary() |
| 425 | TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12] |
| 426 | TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId |
| 427 | |
| 428 | preAddExecuted := make(chan struct{}) |
| 429 | postAddExecuted := make(chan struct{}) |
| 430 | preAddExecutedPtr, postAddExecutedPtr := preAddExecuted, postAddExecuted |
| 431 | |
| 432 | // Register |
| 433 | TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonChanCallback, "PRE_ADD instructions", &preAddExecutedPtr) |
| 434 | TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonChanCallback, "POST_ADD instructions", &postAddExecutedPtr) |
| 435 | |
| 436 | if added := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, ""); added == nil { |
| 437 | t.Error("Failed to add logical device") |
| 438 | } else { |
| 439 | t.Logf("Added logical device : %+v", added) |
| 440 | } |
| 441 | |
| 442 | verifyGotResponse(postAddExecuted) |
| 443 | |
| 444 | if ld := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
| 445 | 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 | |
| 451 | if !verifyGotResponse(preAddExecuted) { |
| 452 | t.Error("PRE_ADD callback was not executed") |
| 453 | } |
| 454 | if !verifyGotResponse(postAddExecuted) { |
| 455 | t.Error("POST_ADD callback was not executed") |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) { |
| 460 | TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId |
| 461 | |
| 462 | added := TestProxy_Root_LogicalDevice.Add(context.Background(), "/logical_devices", TestProxy_LogicalDevice, "") |
| 463 | 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 | } |
| 466 | } |
| 467 | |
| 468 | func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) { |
| 469 | logicalDevices := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices", 1, false, "") |
| 470 | |
| 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 |
| 475 | TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id |
| 476 | t.Logf("retrieved all logical devices: %+v", logicalDevices) |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) { |
| 481 | if ld := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
| 482 | t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId) |
| 483 | } else { |
| 484 | ldJSON, _ := json.Marshal(ld) |
| 485 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | |
| 490 | func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) { |
| 491 | var fwVersion int |
| 492 | preUpdateExecuted := make(chan struct{}) |
| 493 | postUpdateExecuted := make(chan struct{}) |
| 494 | preUpdateExecutedPtr, postUpdateExecutedPtr := preUpdateExecuted, postUpdateExecuted |
| 495 | |
| 496 | if retrieved := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil { |
| 497 | 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 | |
| 508 | TestProxy_Root_LogicalDevice.RegisterCallback( |
| 509 | PRE_UPDATE, |
| 510 | commonChanCallback, |
| 511 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecutedPtr, |
| 512 | ) |
| 513 | TestProxy_Root_LogicalDevice.RegisterCallback( |
| 514 | POST_UPDATE, |
| 515 | commonChanCallback, |
| 516 | "POST_UPDATE instructions (root proxy)", &postUpdateExecutedPtr, |
| 517 | ) |
| 518 | |
| 519 | retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion) |
| 520 | |
| 521 | if afterUpdate := TestProxy_Root_LogicalDevice.Update(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false, |
| 522 | ""); afterUpdate == nil { |
| 523 | t.Error("Failed to update logical device") |
| 524 | } else { |
| 525 | t.Logf("Updated logical device : %+v", afterUpdate) |
| 526 | } |
| 527 | |
| 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 | |
| 535 | if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
| 536 | 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 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) { |
| 546 | // Get a device proxy and update a specific port |
| 547 | ldFlowsProxy := TestProxy_Root.node.CreateProxy(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false) |
| 548 | flows := ldFlowsProxy.Get(context.Background(), "/", 0, false, "") |
| 549 | 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 | |
| 561 | kvFlows := ldFlowsProxy.Get(context.Background(), "/", 0, false, "") |
| 562 | |
| 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 | |
| 567 | if updated := ldFlowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
| 568 | t.Error("Failed to update logical device flows") |
| 569 | } else { |
| 570 | t.Logf("Updated logical device flows : %+v", updated) |
| 571 | } |
| 572 | |
| 573 | if d := ldFlowsProxy.Get(context.Background(), "/", 0, false, ""); d == nil { |
| 574 | 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 | |
| 580 | if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false, |
| 581 | ""); !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 | |
| 589 | func TestProxy_2_4_1_Remove_Device(t *testing.T) { |
| 590 | preRemoveExecuted := make(chan struct{}) |
| 591 | postRemoveExecuted := make(chan struct{}) |
| 592 | preRemoveExecutedPtr, postRemoveExecutedPtr := preRemoveExecuted, postRemoveExecuted |
| 593 | |
| 594 | TestProxy_Root_LogicalDevice.RegisterCallback( |
| 595 | PRE_REMOVE, |
| 596 | commonChanCallback, |
| 597 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecutedPtr, |
| 598 | ) |
| 599 | TestProxy_Root_LogicalDevice.RegisterCallback( |
| 600 | POST_REMOVE, |
| 601 | commonChanCallback, |
| 602 | "POST_REMOVE instructions (root proxy)", &postRemoveExecutedPtr, |
| 603 | ) |
| 604 | |
| 605 | if removed := TestProxy_Root_LogicalDevice.Remove(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil { |
| 606 | t.Error("Failed to remove logical device") |
| 607 | } else { |
| 608 | t.Logf("Removed device : %+v", removed) |
| 609 | } |
| 610 | |
| 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 | |
| 618 | if d := TestProxy_Root_LogicalDevice.Get(context.Background(), "/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
| 619 | djson, _ := json.Marshal(d) |
| 620 | t.Errorf("Device was not removed - %s", djson) |
| 621 | } else { |
| 622 | t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId) |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // ----------------------------- |
| 627 | // Callback tests |
| 628 | // ----------------------------- |
| 629 | |
| 630 | func TestProxy_Callbacks_1_Register(t *testing.T) { |
| 631 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
| 632 | |
| 633 | m := make(map[string]string) |
| 634 | m["name"] = "fghij" |
| 635 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
| 636 | |
| 637 | d := &voltha.Device{Id: "12345"} |
| 638 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
| 639 | } |
| 640 | |
| 641 | func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
| 642 | TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil) |
| 643 | } |
| 644 | |
| 645 | func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
| 646 | TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil) |
| 647 | } |
| 648 | |
| 649 | func 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) |
| 653 | } |
| 654 | |
| 655 | //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") |
| 657 | //} |
| 658 | // |
| 659 | //func TestProxy_Callbacks_6_Execute(t *testing.T) { |
| 660 | // TestProxy_Root_Device.Root.ExecuteCallbacks() |
| 661 | //} |