Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [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 ( |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 19 | "encoding/hex" |
| 20 | "encoding/json" |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 21 | "github.com/golang/protobuf/proto" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "github.com/google/uuid" |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/common" |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/protos/openflow_13" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 26 | "math/rand" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | "reflect" |
| 28 | "strconv" |
| 29 | "testing" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 32 | var ( |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 33 | TestProxy_Root *root |
| 34 | TestProxy_Root_LogicalDevice *Proxy |
| 35 | TestProxy_Root_Device *Proxy |
| 36 | TestProxy_DeviceId string |
| 37 | TestProxy_LogicalDeviceId string |
| 38 | TestProxy_TargetDeviceId string |
| 39 | TestProxy_TargetLogicalDeviceId string |
| 40 | TestProxy_LogicalPorts []*voltha.LogicalPort |
| 41 | TestProxy_Ports []*voltha.Port |
| 42 | TestProxy_Stats *openflow_13.OfpFlowStats |
| 43 | TestProxy_Flows *openflow_13.Flows |
| 44 | TestProxy_Device *voltha.Device |
| 45 | TestProxy_LogicalDevice *voltha.LogicalDevice |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 46 | ) |
| 47 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 48 | func init() { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 49 | //log.AddPackage(log.JSON, log.InfoLevel, log.Fields{"instanceId": "DB_MODEL"}) |
| 50 | //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"}) |
| 51 | TestProxy_Root = NewRoot(&voltha.Voltha{}, nil) |
| 52 | TestProxy_Root_LogicalDevice = TestProxy_Root.CreateProxy("/", false) |
| 53 | TestProxy_Root_Device = TestProxy_Root.CreateProxy("/", false) |
| 54 | |
| 55 | TestProxy_LogicalPorts = []*voltha.LogicalPort{ |
| 56 | { |
| 57 | Id: "123", |
| 58 | DeviceId: "logicalport-0-device-id", |
| 59 | DevicePortNo: 123, |
| 60 | RootPort: false, |
| 61 | }, |
| 62 | } |
| 63 | TestProxy_Ports = []*voltha.Port{ |
| 64 | { |
| 65 | PortNo: 123, |
| 66 | Label: "test-port-0", |
| 67 | Type: voltha.Port_PON_OLT, |
| 68 | AdminState: common.AdminState_ENABLED, |
| 69 | OperStatus: common.OperStatus_ACTIVE, |
| 70 | DeviceId: "etcd_port-0-device-id", |
| 71 | Peers: []*voltha.Port_PeerPort{}, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | TestProxy_Stats = &openflow_13.OfpFlowStats{ |
| 76 | Id: 1111, |
| 77 | } |
| 78 | TestProxy_Flows = &openflow_13.Flows{ |
| 79 | Items: []*openflow_13.OfpFlowStats{TestProxy_Stats}, |
| 80 | } |
| 81 | TestProxy_Device = &voltha.Device{ |
| 82 | Id: TestProxy_DeviceId, |
| 83 | Type: "simulated_olt", |
| 84 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 85 | AdminState: voltha.AdminState_PREPROVISIONED, |
| 86 | Flows: TestProxy_Flows, |
| 87 | Ports: TestProxy_Ports, |
| 88 | } |
| 89 | |
| 90 | TestProxy_LogicalDevice = &voltha.LogicalDevice{ |
| 91 | Id: TestProxy_DeviceId, |
| 92 | DatapathId: 0, |
| 93 | Ports: TestProxy_LogicalPorts, |
| 94 | Flows: TestProxy_Flows, |
| 95 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 98 | func TestProxy_1_1_1_Add_NewDevice(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 99 | devIDBin, _ := uuid.New().MarshalBinary() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 100 | TestProxy_DeviceId = "0001" + hex.EncodeToString(devIDBin)[:12] |
| 101 | TestProxy_Device.Id = TestProxy_DeviceId |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 102 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 103 | preAddExecuted := false |
| 104 | postAddExecuted := false |
| 105 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 106 | devicesProxy := TestProxy_Root.node.CreateProxy("/devices", false) |
| 107 | devicesProxy.RegisterCallback(PRE_ADD, commonCallback2, "PRE_ADD Device container changes") |
| 108 | devicesProxy.RegisterCallback(POST_ADD, commonCallback2, "POST_ADD Device container changes") |
| 109 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 110 | // Register ADD instructions callbacks |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 111 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 112 | TestProxy_Root_Device.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 113 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | // Add the device |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 115 | if added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, ""); added == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 116 | t.Error("Failed to add device") |
| 117 | } else { |
| 118 | t.Logf("Added device : %+v", added) |
| 119 | } |
| 120 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 121 | // Verify that the added device can now be retrieved |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 122 | if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 123 | t.Error("Failed to find added device") |
| 124 | } else { |
| 125 | djson, _ := json.Marshal(d) |
| 126 | t.Logf("Found device: %s", string(djson)) |
| 127 | } |
| 128 | |
| 129 | if !preAddExecuted { |
| 130 | t.Error("PRE_ADD callback was not executed") |
| 131 | } |
| 132 | if !postAddExecuted { |
| 133 | t.Error("POST_ADD callback was not executed") |
| 134 | } |
| 135 | } |
| 136 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 137 | func TestProxy_1_1_2_Add_ExistingDevice(t *testing.T) { |
| 138 | TestProxy_Device.Id = TestProxy_DeviceId |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 139 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 140 | added := TestProxy_Root_Device.Add("/devices", TestProxy_Device, ""); |
| 141 | if added.(proto.Message).String() != reflect.ValueOf(TestProxy_Device).Interface().(proto.Message).String() { |
| 142 | t.Errorf("Devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 143 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 146 | func TestProxy_1_2_1_Get_AllDevices(t *testing.T) { |
| 147 | devices := TestProxy_Root_Device.Get("/devices", 1, false, "") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 148 | |
| 149 | if len(devices.([]interface{})) == 0 { |
| 150 | t.Error("there are no available devices to retrieve") |
| 151 | } else { |
| 152 | // Save the target device id for later tests |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 153 | TestProxy_TargetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 154 | t.Logf("retrieved all devices: %+v", devices) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 158 | func TestProxy_1_2_2_Get_SingleDevice(t *testing.T) { |
| 159 | if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 160 | t.Errorf("Failed to find device : %s", TestProxy_TargetDeviceId) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 161 | } else { |
| 162 | djson, _ := json.Marshal(d) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 163 | t.Logf("Found device: %s", string(djson)) |
| 164 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 167 | func TestProxy_1_3_1_Update_Device(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 168 | var fwVersion int |
| 169 | preUpdateExecuted := false |
| 170 | postUpdateExecuted := false |
| 171 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 172 | if retrieved := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); retrieved == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 173 | t.Error("Failed to get device") |
| 174 | } else { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 175 | t.Logf("Found raw device (root proxy): %+v", retrieved) |
| 176 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 177 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 178 | fwVersion = 0 |
| 179 | } else { |
| 180 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 181 | fwVersion++ |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 184 | retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 185 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 186 | TestProxy_Root_Device.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 187 | PRE_UPDATE, |
| 188 | commonCallback, |
| 189 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 190 | ) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 191 | TestProxy_Root_Device.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 192 | POST_UPDATE, |
| 193 | commonCallback, |
| 194 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 195 | ) |
| 196 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 197 | if afterUpdate := TestProxy_Root_Device.Update("/devices/"+TestProxy_TargetDeviceId, retrieved, false, ""); afterUpdate == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 198 | t.Error("Failed to update device") |
| 199 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 200 | t.Logf("Updated device : %+v", afterUpdate) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 201 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 203 | if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_TargetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 204 | t.Error("Failed to find updated device (root proxy)") |
| 205 | } else { |
| 206 | djson, _ := json.Marshal(d) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 207 | t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if !preUpdateExecuted { |
| 211 | t.Error("PRE_UPDATE callback was not executed") |
| 212 | } |
| 213 | if !postUpdateExecuted { |
| 214 | t.Error("POST_UPDATE callback was not executed") |
| 215 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 216 | } |
| 217 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 218 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 219 | func TestProxy_1_3_2_Update_DeviceFlows(t *testing.T) { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 220 | // Get a device proxy and update a specific port |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 221 | devFlowsProxy := TestProxy_Root.node.CreateProxy("/devices/"+TestProxy_DeviceId+"/flows", false) |
| 222 | flows := devFlowsProxy.Get("/", 0, false, "") |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 223 | flows.(*openflow_13.Flows).Items[0].TableId = 2244 |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 224 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 225 | preUpdateExecuted := false |
| 226 | postUpdateExecuted := false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 227 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 228 | devFlowsProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 229 | PRE_UPDATE, |
| 230 | commonCallback, |
| 231 | "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted, |
| 232 | ) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 233 | devFlowsProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 234 | POST_UPDATE, |
| 235 | commonCallback, |
| 236 | "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted, |
| 237 | ) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 238 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 239 | kvFlows := devFlowsProxy.Get("/", 0, false, "") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 240 | |
| 241 | if reflect.DeepEqual(flows, kvFlows) { |
| 242 | t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 243 | } |
| 244 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 245 | if updated := devFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 246 | t.Error("Failed to update flow") |
| 247 | } else { |
| 248 | t.Logf("Updated flows : %+v", updated) |
| 249 | } |
| 250 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 251 | if d := devFlowsProxy.Get("/", 0, false, ""); d == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 252 | t.Error("Failed to find updated flows (flows proxy)") |
| 253 | } else { |
| 254 | djson, _ := json.Marshal(d) |
| 255 | t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 256 | } |
| 257 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 258 | if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 259 | t.Error("Failed to find updated flows (root proxy)") |
| 260 | } else { |
| 261 | djson, _ := json.Marshal(d) |
| 262 | t.Logf("Found flows (root proxy): %s", string(djson)) |
| 263 | } |
| 264 | |
| 265 | if !preUpdateExecuted { |
| 266 | t.Error("PRE_UPDATE callback was not executed") |
| 267 | } |
| 268 | if !postUpdateExecuted { |
| 269 | t.Error("POST_UPDATE callback was not executed") |
| 270 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 273 | func TestProxy_1_4_1_Remove_Device(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 274 | preRemoveExecuted := false |
| 275 | postRemoveExecuted := false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 276 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 277 | TestProxy_Root_Device.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 278 | PRE_REMOVE, |
| 279 | commonCallback, |
| 280 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 281 | ) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 282 | TestProxy_Root_Device.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 283 | POST_REMOVE, |
| 284 | commonCallback, |
| 285 | "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 286 | ) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 287 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 288 | if removed := TestProxy_Root_Device.Remove("/devices/"+TestProxy_DeviceId, ""); removed == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 289 | t.Error("Failed to remove device") |
| 290 | } else { |
| 291 | t.Logf("Removed device : %+v", removed) |
| 292 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 293 | if d := TestProxy_Root_Device.Get("/devices/"+TestProxy_DeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 294 | djson, _ := json.Marshal(d) |
| 295 | t.Errorf("Device was not removed - %s", djson) |
| 296 | } else { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 297 | t.Logf("Device was removed: %s", TestProxy_DeviceId) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if !preRemoveExecuted { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 301 | t.Error("PRE_REMOVE callback was not executed") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 302 | } |
| 303 | if !postRemoveExecuted { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 304 | t.Error("POST_REMOVE callback was not executed") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 308 | func TestProxy_2_1_1_Add_NewLogicalDevice(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 309 | |
| 310 | ldIDBin, _ := uuid.New().MarshalBinary() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 311 | TestProxy_LogicalDeviceId = "0001" + hex.EncodeToString(ldIDBin)[:12] |
| 312 | TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 313 | |
| 314 | preAddExecuted := false |
| 315 | postAddExecuted := false |
| 316 | |
| 317 | // Register |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 318 | TestProxy_Root_LogicalDevice.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 319 | TestProxy_Root_LogicalDevice.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 320 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 321 | if added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, ""); added == nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 322 | t.Error("Failed to add logical device") |
| 323 | } else { |
| 324 | t.Logf("Added logical device : %+v", added) |
| 325 | } |
| 326 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 327 | if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 328 | t.Error("Failed to find added logical device") |
| 329 | } else { |
| 330 | ldJSON, _ := json.Marshal(ld) |
| 331 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 332 | } |
| 333 | |
| 334 | if !preAddExecuted { |
| 335 | t.Error("PRE_ADD callback was not executed") |
| 336 | } |
| 337 | if !postAddExecuted { |
| 338 | t.Error("POST_ADD callback was not executed") |
| 339 | } |
| 340 | } |
| 341 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 342 | func TestProxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) { |
| 343 | TestProxy_LogicalDevice.Id = TestProxy_LogicalDeviceId |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 344 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 345 | added := TestProxy_Root_LogicalDevice.Add("/logical_devices", TestProxy_LogicalDevice, ""); |
| 346 | if added.(proto.Message).String() != reflect.ValueOf(TestProxy_LogicalDevice).Interface().(proto.Message).String() { |
| 347 | t.Errorf("Logical devices don't match - existing: %+v returned: %+v", TestProxy_LogicalDevice, added) |
| 348 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 351 | func TestProxy_2_2_1_Get_AllLogicalDevices(t *testing.T) { |
| 352 | logicalDevices := TestProxy_Root_LogicalDevice.Get("/logical_devices", 1, false, "") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 353 | |
| 354 | if len(logicalDevices.([]interface{})) == 0 { |
| 355 | t.Error("there are no available logical devices to retrieve") |
| 356 | } else { |
| 357 | // Save the target device id for later tests |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 358 | TestProxy_TargetLogicalDeviceId = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 359 | t.Logf("retrieved all logical devices: %+v", logicalDevices) |
| 360 | } |
| 361 | } |
| 362 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 363 | func TestProxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) { |
| 364 | if ld := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
| 365 | t.Errorf("Failed to find logical device : %s", TestProxy_TargetLogicalDeviceId) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 366 | } else { |
| 367 | ldJSON, _ := json.Marshal(ld) |
| 368 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 369 | } |
| 370 | |
| 371 | } |
| 372 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 373 | func TestProxy_2_3_1_Update_LogicalDevice(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 374 | var fwVersion int |
| 375 | preUpdateExecuted := false |
| 376 | postUpdateExecuted := false |
| 377 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 378 | if retrieved := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); retrieved == nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 379 | t.Error("Failed to get logical device") |
| 380 | } else { |
| 381 | t.Logf("Found raw logical device (root proxy): %+v", retrieved) |
| 382 | |
| 383 | if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" { |
| 384 | fwVersion = 0 |
| 385 | } else { |
| 386 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId) |
| 387 | fwVersion++ |
| 388 | } |
| 389 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 390 | TestProxy_Root_LogicalDevice.RegisterCallback( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 391 | PRE_UPDATE, |
| 392 | commonCallback, |
| 393 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 394 | ) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 395 | TestProxy_Root_LogicalDevice.RegisterCallback( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 396 | POST_UPDATE, |
| 397 | commonCallback, |
| 398 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 399 | ) |
| 400 | |
| 401 | retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion) |
| 402 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 403 | if afterUpdate := TestProxy_Root_LogicalDevice.Update("/logical_devices/"+TestProxy_TargetLogicalDeviceId, retrieved, false, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 404 | ""); afterUpdate == nil { |
| 405 | t.Error("Failed to update logical device") |
| 406 | } else { |
| 407 | t.Logf("Updated logical device : %+v", afterUpdate) |
| 408 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 409 | if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_TargetLogicalDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 410 | t.Error("Failed to find updated logical device (root proxy)") |
| 411 | } else { |
| 412 | djson, _ := json.Marshal(d) |
| 413 | |
| 414 | t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d) |
| 415 | } |
| 416 | |
| 417 | if !preUpdateExecuted { |
| 418 | t.Error("PRE_UPDATE callback was not executed") |
| 419 | } |
| 420 | if !postUpdateExecuted { |
| 421 | t.Error("POST_UPDATE callback was not executed") |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 426 | func TestProxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 427 | // Get a device proxy and update a specific port |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 428 | ldFlowsProxy := TestProxy_Root.node.CreateProxy("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", false) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 429 | flows := ldFlowsProxy.Get("/", 0, false, "") |
| 430 | flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32() |
| 431 | t.Logf("before updated flows: %+v", flows) |
| 432 | |
| 433 | ldFlowsProxy.RegisterCallback( |
| 434 | PRE_UPDATE, |
| 435 | commonCallback2, |
| 436 | ) |
| 437 | ldFlowsProxy.RegisterCallback( |
| 438 | POST_UPDATE, |
| 439 | commonCallback2, |
| 440 | ) |
| 441 | |
| 442 | kvFlows := ldFlowsProxy.Get("/", 0, false, "") |
| 443 | |
| 444 | if reflect.DeepEqual(flows, kvFlows) { |
| 445 | t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 446 | } |
| 447 | |
| 448 | if updated := ldFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
| 449 | t.Error("Failed to update logical device flows") |
| 450 | } else { |
| 451 | t.Logf("Updated logical device flows : %+v", updated) |
| 452 | } |
| 453 | |
| 454 | if d := ldFlowsProxy.Get("/", 0, false, ""); d == nil { |
| 455 | t.Error("Failed to find updated logical device flows (flows proxy)") |
| 456 | } else { |
| 457 | djson, _ := json.Marshal(d) |
| 458 | t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 459 | } |
| 460 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 461 | if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId+"/flows", 0, false, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 462 | ""); !reflect.ValueOf(d).IsValid() { |
| 463 | t.Error("Failed to find updated logical device flows (root proxy)") |
| 464 | } else { |
| 465 | djson, _ := json.Marshal(d) |
| 466 | t.Logf("Found logical device flows (root proxy): %s", string(djson)) |
| 467 | } |
| 468 | } |
| 469 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 470 | func TestProxy_2_4_1_Remove_Device(t *testing.T) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 471 | preRemoveExecuted := false |
| 472 | postRemoveExecuted := false |
| 473 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 474 | TestProxy_Root_LogicalDevice.RegisterCallback( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 475 | PRE_REMOVE, |
| 476 | commonCallback, |
| 477 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 478 | ) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 479 | TestProxy_Root_LogicalDevice.RegisterCallback( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 480 | POST_REMOVE, |
| 481 | commonCallback, |
| 482 | "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 483 | ) |
| 484 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 485 | if removed := TestProxy_Root_LogicalDevice.Remove("/logical_devices/"+TestProxy_LogicalDeviceId, ""); removed == nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 486 | t.Error("Failed to remove logical device") |
| 487 | } else { |
| 488 | t.Logf("Removed device : %+v", removed) |
| 489 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 490 | if d := TestProxy_Root_LogicalDevice.Get("/logical_devices/"+TestProxy_LogicalDeviceId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 491 | djson, _ := json.Marshal(d) |
| 492 | t.Errorf("Device was not removed - %s", djson) |
| 493 | } else { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 494 | t.Logf("Device was removed: %s", TestProxy_LogicalDeviceId) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 495 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 496 | |
| 497 | if !preRemoveExecuted { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 498 | t.Error("PRE_REMOVE callback was not executed") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 499 | } |
| 500 | if !postRemoveExecuted { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 501 | t.Error("POST_REMOVE callback was not executed") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 502 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 503 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 504 | |
| 505 | // ----------------------------- |
| 506 | // Callback tests |
| 507 | // ----------------------------- |
| 508 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 509 | func TestProxy_Callbacks_1_Register(t *testing.T) { |
| 510 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 511 | |
| 512 | m := make(map[string]string) |
| 513 | m["name"] = "fghij" |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 514 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 515 | |
| 516 | d := &voltha.Device{Id: "12345"} |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 517 | TestProxy_Root_Device.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 520 | func TestProxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
| 521 | TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, false, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 522 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 523 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 524 | func TestProxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
| 525 | TestProxy_Root_Device.InvokeCallbacks(PRE_ADD, true, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 528 | func TestProxy_Callbacks_4_Unregister(t *testing.T) { |
| 529 | TestProxy_Root_Device.UnregisterCallback(PRE_ADD, firstCallback) |
| 530 | TestProxy_Root_Device.UnregisterCallback(PRE_ADD, secondCallback) |
| 531 | TestProxy_Root_Device.UnregisterCallback(PRE_ADD, thirdCallback) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 532 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 533 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 534 | //func TestProxy_Callbacks_5_Add(t *testing.T) { |
| 535 | // TestProxy_Root_Device.Root.AddCallback(TestProxy_Root_Device.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 536 | //} |
| 537 | // |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 538 | //func TestProxy_Callbacks_6_Execute(t *testing.T) { |
| 539 | // TestProxy_Root_Device.Root.ExecuteCallbacks() |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 540 | //} |