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" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "github.com/google/uuid" |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/protos/openflow_13" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/voltha" |
| 24 | "math/rand" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "reflect" |
| 26 | "strconv" |
| 27 | "testing" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 30 | var ( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 31 | ldevProxy *Proxy |
| 32 | devProxy *Proxy |
| 33 | flowProxy *Proxy |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 36 | func init() { |
| 37 | ldevProxy = modelTestConfig.Root.node.CreateProxy("/", false) |
| 38 | devProxy = modelTestConfig.Root.node.CreateProxy("/", false) |
| 39 | } |
| 40 | |
| 41 | func Test_Proxy_1_1_1_Add_NewDevice(t *testing.T) { |
| 42 | devIDBin, _ := uuid.New().MarshalBinary() |
| 43 | devID = "0001" + hex.EncodeToString(devIDBin)[:12] |
| 44 | device.Id = devID |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 45 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 46 | preAddExecuted := false |
| 47 | postAddExecuted := false |
| 48 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | // Register ADD instructions callbacks |
| 50 | devProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 51 | devProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 52 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | // Add the device |
| 54 | if added := devProxy.Add("/devices", device, ""); added == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 55 | t.Error("Failed to add device") |
| 56 | } else { |
| 57 | t.Logf("Added device : %+v", added) |
| 58 | } |
| 59 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | // Verify that the added device can now be retrieved |
| 61 | if d := devProxy.Get("/devices/"+devID, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 62 | t.Error("Failed to find added device") |
| 63 | } else { |
| 64 | djson, _ := json.Marshal(d) |
| 65 | t.Logf("Found device: %s", string(djson)) |
| 66 | } |
| 67 | |
| 68 | if !preAddExecuted { |
| 69 | t.Error("PRE_ADD callback was not executed") |
| 70 | } |
| 71 | if !postAddExecuted { |
| 72 | t.Error("POST_ADD callback was not executed") |
| 73 | } |
| 74 | } |
| 75 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 76 | func Test_Proxy_1_1_2_Add_ExistingDevice(t *testing.T) { |
| 77 | device.Id = devID |
| 78 | |
| 79 | if added := devProxy.Add("/devices", device, ""); added == nil { |
| 80 | t.Logf("Successfully detected that the device already exists: %s", devID) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 81 | } else { |
| 82 | t.Errorf("A new device should not have been created : %+v", added) |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | func Test_Proxy_1_2_1_Get_AllDevices(t *testing.T) { |
| 88 | devices := devProxy.Get("/devices", 1, false, "") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 89 | |
| 90 | if len(devices.([]interface{})) == 0 { |
| 91 | t.Error("there are no available devices to retrieve") |
| 92 | } else { |
| 93 | // Save the target device id for later tests |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 94 | targetDevID = devices.([]interface{})[0].(*voltha.Device).Id |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 95 | t.Logf("retrieved all devices: %+v", devices) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 99 | func Test_Proxy_1_2_2_Get_SingleDevice(t *testing.T) { |
| 100 | if d := devProxy.Get("/devices/"+targetDevID, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 101 | t.Errorf("Failed to find device : %s", targetDevID) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 102 | } else { |
| 103 | djson, _ := json.Marshal(d) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 104 | t.Logf("Found device: %s", string(djson)) |
| 105 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 106 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 109 | func Test_Proxy_1_3_1_Update_Device(t *testing.T) { |
| 110 | var fwVersion int |
| 111 | preUpdateExecuted := false |
| 112 | postUpdateExecuted := false |
| 113 | |
| 114 | if retrieved := devProxy.Get("/devices/"+targetDevID, 1, false, ""); retrieved == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 115 | t.Error("Failed to get device") |
| 116 | } else { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 117 | t.Logf("Found raw device (root proxy): %+v", retrieved) |
| 118 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 119 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 120 | fwVersion = 0 |
| 121 | } else { |
| 122 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 123 | fwVersion++ |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 126 | retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 127 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 128 | devProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 129 | PRE_UPDATE, |
| 130 | commonCallback, |
| 131 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 132 | ) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 133 | devProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 134 | POST_UPDATE, |
| 135 | commonCallback, |
| 136 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 137 | ) |
| 138 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 139 | if afterUpdate := devProxy.Update("/devices/"+targetDevID, retrieved, false, ""); afterUpdate == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 140 | t.Error("Failed to update device") |
| 141 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 142 | t.Logf("Updated device : %+v", afterUpdate) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 143 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 144 | |
| 145 | if d := devProxy.Get("/devices/"+targetDevID, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 146 | t.Error("Failed to find updated device (root proxy)") |
| 147 | } else { |
| 148 | djson, _ := json.Marshal(d) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 149 | t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if !preUpdateExecuted { |
| 153 | t.Error("PRE_UPDATE callback was not executed") |
| 154 | } |
| 155 | if !postUpdateExecuted { |
| 156 | t.Error("POST_UPDATE callback was not executed") |
| 157 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 158 | } |
| 159 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 160 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 161 | func Test_Proxy_1_3_2_Update_DeviceFlows(t *testing.T) { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 162 | // Get a device proxy and update a specific port |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 163 | flowProxy = modelTestConfig.Root.node.CreateProxy("/devices/"+devID+"/flows", false) |
| 164 | flows := flowProxy.Get("/", 0, false, "") |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 165 | flows.(*openflow_13.Flows).Items[0].TableId = 2244 |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 166 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 167 | preUpdateExecuted := false |
| 168 | postUpdateExecuted := false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 169 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 170 | flowProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 171 | PRE_UPDATE, |
| 172 | commonCallback, |
| 173 | "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted, |
| 174 | ) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 175 | flowProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 176 | POST_UPDATE, |
| 177 | commonCallback, |
| 178 | "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted, |
| 179 | ) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 180 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 181 | kvFlows := flowProxy.Get("/", 0, false, "") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 182 | |
| 183 | if reflect.DeepEqual(flows, kvFlows) { |
| 184 | t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 185 | } |
| 186 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | if updated := flowProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 188 | t.Error("Failed to update flow") |
| 189 | } else { |
| 190 | t.Logf("Updated flows : %+v", updated) |
| 191 | } |
| 192 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 193 | if d := flowProxy.Get("/", 0, false, ""); d == nil { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 194 | t.Error("Failed to find updated flows (flows proxy)") |
| 195 | } else { |
| 196 | djson, _ := json.Marshal(d) |
| 197 | t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 198 | } |
| 199 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 200 | if d := devProxy.Get("/devices/"+devID+"/flows", 1, false, ""); !reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 201 | t.Error("Failed to find updated flows (root proxy)") |
| 202 | } else { |
| 203 | djson, _ := json.Marshal(d) |
| 204 | t.Logf("Found flows (root proxy): %s", string(djson)) |
| 205 | } |
| 206 | |
| 207 | if !preUpdateExecuted { |
| 208 | t.Error("PRE_UPDATE callback was not executed") |
| 209 | } |
| 210 | if !postUpdateExecuted { |
| 211 | t.Error("POST_UPDATE callback was not executed") |
| 212 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 215 | func Test_Proxy_1_4_1_Remove_Device(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 216 | preRemoveExecuted := false |
| 217 | postRemoveExecuted := false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 218 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 219 | modelTestConfig.RootProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 220 | PRE_REMOVE, |
| 221 | commonCallback, |
| 222 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 223 | ) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 224 | modelTestConfig.RootProxy.RegisterCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 225 | POST_REMOVE, |
| 226 | commonCallback, |
| 227 | "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 228 | ) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 229 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 230 | if removed := modelTestConfig.RootProxy.Remove("/devices/"+devID, ""); removed == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 231 | t.Error("Failed to remove device") |
| 232 | } else { |
| 233 | t.Logf("Removed device : %+v", removed) |
| 234 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 235 | if d := modelTestConfig.RootProxy.Get("/devices/"+devID, 0, false, ""); reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 236 | djson, _ := json.Marshal(d) |
| 237 | t.Errorf("Device was not removed - %s", djson) |
| 238 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 239 | t.Logf("Device was removed: %s", devID) |
| 240 | } |
| 241 | |
| 242 | if !preRemoveExecuted { |
| 243 | t.Error("PRE_UPDATE callback was not executed") |
| 244 | } |
| 245 | if !postRemoveExecuted { |
| 246 | t.Error("POST_UPDATE callback was not executed") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func Test_Proxy_2_1_1_Add_NewLogicalDevice(t *testing.T) { |
| 251 | |
| 252 | ldIDBin, _ := uuid.New().MarshalBinary() |
| 253 | ldevID = "0001" + hex.EncodeToString(ldIDBin)[:12] |
| 254 | logicalDevice.Id = ldevID |
| 255 | |
| 256 | preAddExecuted := false |
| 257 | postAddExecuted := false |
| 258 | |
| 259 | // Register |
| 260 | ldevProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 261 | ldevProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
| 262 | |
| 263 | if added := ldevProxy.Add("/logical_devices", logicalDevice, ""); added == nil { |
| 264 | t.Error("Failed to add logical device") |
| 265 | } else { |
| 266 | t.Logf("Added logical device : %+v", added) |
| 267 | } |
| 268 | |
| 269 | if ld := ldevProxy.Get("/logical_devices/"+ldevID, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
| 270 | t.Error("Failed to find added logical device") |
| 271 | } else { |
| 272 | ldJSON, _ := json.Marshal(ld) |
| 273 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 274 | } |
| 275 | |
| 276 | if !preAddExecuted { |
| 277 | t.Error("PRE_ADD callback was not executed") |
| 278 | } |
| 279 | if !postAddExecuted { |
| 280 | t.Error("POST_ADD callback was not executed") |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func Test_Proxy_2_1_2_Add_ExistingLogicalDevice(t *testing.T) { |
| 285 | logicalDevice.Id = ldevID |
| 286 | if added := ldevProxy.Add("/logical_devices", logicalDevice, ""); added == nil { |
| 287 | t.Logf("Successfully detected that the logical device already exists: %s", ldevID) |
| 288 | } else { |
| 289 | t.Errorf("A new logical device should not have been created : %+v", added) |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | |
| 294 | func Test_Proxy_2_2_1_Get_AllLogicalDevices(t *testing.T) { |
| 295 | logicalDevices := ldevProxy.Get("/logical_devices", 1, false, "") |
| 296 | |
| 297 | if len(logicalDevices.([]interface{})) == 0 { |
| 298 | t.Error("there are no available logical devices to retrieve") |
| 299 | } else { |
| 300 | // Save the target device id for later tests |
| 301 | targetLogDevID = logicalDevices.([]interface{})[0].(*voltha.LogicalDevice).Id |
| 302 | t.Logf("retrieved all logical devices: %+v", logicalDevices) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func Test_Proxy_2_2_2_Get_SingleLogicalDevice(t *testing.T) { |
| 307 | if ld := ldevProxy.Get("/logical_devices/"+targetLogDevID, 0, false, ""); !reflect.ValueOf(ld).IsValid() { |
| 308 | t.Errorf("Failed to find logical device : %s", targetLogDevID) |
| 309 | } else { |
| 310 | ldJSON, _ := json.Marshal(ld) |
| 311 | t.Logf("Found logical device: %s", string(ldJSON)) |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | func Test_Proxy_2_3_1_Update_LogicalDevice(t *testing.T) { |
| 317 | var fwVersion int |
| 318 | preUpdateExecuted := false |
| 319 | postUpdateExecuted := false |
| 320 | |
| 321 | if retrieved := ldevProxy.Get("/logical_devices/"+targetLogDevID, 1, false, ""); retrieved == nil { |
| 322 | t.Error("Failed to get logical device") |
| 323 | } else { |
| 324 | t.Logf("Found raw logical device (root proxy): %+v", retrieved) |
| 325 | |
| 326 | if retrieved.(*voltha.LogicalDevice).RootDeviceId == "" { |
| 327 | fwVersion = 0 |
| 328 | } else { |
| 329 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.LogicalDevice).RootDeviceId) |
| 330 | fwVersion++ |
| 331 | } |
| 332 | |
| 333 | ldevProxy.RegisterCallback( |
| 334 | PRE_UPDATE, |
| 335 | commonCallback, |
| 336 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 337 | ) |
| 338 | ldevProxy.RegisterCallback( |
| 339 | POST_UPDATE, |
| 340 | commonCallback, |
| 341 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 342 | ) |
| 343 | |
| 344 | retrieved.(*voltha.LogicalDevice).RootDeviceId = strconv.Itoa(fwVersion) |
| 345 | |
| 346 | if afterUpdate := ldevProxy.Update("/logical_devices/"+targetLogDevID, retrieved, false, |
| 347 | ""); afterUpdate == nil { |
| 348 | t.Error("Failed to update logical device") |
| 349 | } else { |
| 350 | t.Logf("Updated logical device : %+v", afterUpdate) |
| 351 | } |
| 352 | if d := ldevProxy.Get("/logical_devices/"+targetLogDevID, 1, false, ""); !reflect.ValueOf(d).IsValid() { |
| 353 | t.Error("Failed to find updated logical device (root proxy)") |
| 354 | } else { |
| 355 | djson, _ := json.Marshal(d) |
| 356 | |
| 357 | t.Logf("Found logical device (root proxy): %s raw: %+v", string(djson), d) |
| 358 | } |
| 359 | |
| 360 | if !preUpdateExecuted { |
| 361 | t.Error("PRE_UPDATE callback was not executed") |
| 362 | } |
| 363 | if !postUpdateExecuted { |
| 364 | t.Error("POST_UPDATE callback was not executed") |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | func Test_Proxy_2_3_2_Update_LogicalDeviceFlows(t *testing.T) { |
| 370 | // Get a device proxy and update a specific port |
| 371 | ldFlowsProxy := modelTestConfig.Root.node.CreateProxy("/logical_devices/"+ldevID+"/flows", false) |
| 372 | flows := ldFlowsProxy.Get("/", 0, false, "") |
| 373 | flows.(*openflow_13.Flows).Items[0].TableId = rand.Uint32() |
| 374 | t.Logf("before updated flows: %+v", flows) |
| 375 | |
| 376 | ldFlowsProxy.RegisterCallback( |
| 377 | PRE_UPDATE, |
| 378 | commonCallback2, |
| 379 | ) |
| 380 | ldFlowsProxy.RegisterCallback( |
| 381 | POST_UPDATE, |
| 382 | commonCallback2, |
| 383 | ) |
| 384 | |
| 385 | kvFlows := ldFlowsProxy.Get("/", 0, false, "") |
| 386 | |
| 387 | if reflect.DeepEqual(flows, kvFlows) { |
| 388 | t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 389 | } |
| 390 | |
| 391 | if updated := ldFlowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil { |
| 392 | t.Error("Failed to update logical device flows") |
| 393 | } else { |
| 394 | t.Logf("Updated logical device flows : %+v", updated) |
| 395 | } |
| 396 | |
| 397 | if d := ldFlowsProxy.Get("/", 0, false, ""); d == nil { |
| 398 | t.Error("Failed to find updated logical device flows (flows proxy)") |
| 399 | } else { |
| 400 | djson, _ := json.Marshal(d) |
| 401 | t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 402 | } |
| 403 | |
| 404 | if d := modelTestConfig.RootProxy.Get("/logical_devices/"+ldevID+"/flows", 0, false, |
| 405 | ""); !reflect.ValueOf(d).IsValid() { |
| 406 | t.Error("Failed to find updated logical device flows (root proxy)") |
| 407 | } else { |
| 408 | djson, _ := json.Marshal(d) |
| 409 | t.Logf("Found logical device flows (root proxy): %s", string(djson)) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | func Test_Proxy_2_4_1_Remove_Device(t *testing.T) { |
| 414 | preRemoveExecuted := false |
| 415 | postRemoveExecuted := false |
| 416 | |
| 417 | ldevProxy.RegisterCallback( |
| 418 | PRE_REMOVE, |
| 419 | commonCallback, |
| 420 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 421 | ) |
| 422 | ldevProxy.RegisterCallback( |
| 423 | POST_REMOVE, |
| 424 | commonCallback, |
| 425 | "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 426 | ) |
| 427 | |
| 428 | if removed := ldevProxy.Remove("/logical_devices/"+ldevID, ""); removed == nil { |
| 429 | t.Error("Failed to remove logical device") |
| 430 | } else { |
| 431 | t.Logf("Removed device : %+v", removed) |
| 432 | } |
| 433 | if d := ldevProxy.Get("/logical_devices/"+ldevID, 0, false, ""); reflect.ValueOf(d).IsValid() { |
| 434 | djson, _ := json.Marshal(d) |
| 435 | t.Errorf("Device was not removed - %s", djson) |
| 436 | } else { |
| 437 | t.Logf("Device was removed: %s", ldevID) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 438 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 439 | |
| 440 | if !preRemoveExecuted { |
| 441 | t.Error("PRE_UPDATE callback was not executed") |
| 442 | } |
| 443 | if !postRemoveExecuted { |
| 444 | t.Error("POST_UPDATE callback was not executed") |
| 445 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 446 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 447 | |
| 448 | // ----------------------------- |
| 449 | // Callback tests |
| 450 | // ----------------------------- |
| 451 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 452 | func Test_Proxy_Callbacks_1_Register(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 453 | modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 454 | |
| 455 | m := make(map[string]string) |
| 456 | m["name"] = "fghij" |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 457 | modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 458 | |
| 459 | d := &voltha.Device{Id: "12345"} |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 460 | modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 464 | modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, false, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 465 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 466 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 467 | func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 468 | modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, true, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | func Test_Proxy_Callbacks_4_Unregister(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 472 | modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, firstCallback) |
| 473 | modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, secondCallback) |
| 474 | modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, thirdCallback) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 475 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 476 | |
| 477 | //func Test_Proxy_Callbacks_5_Add(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 478 | // modelTestConfig.RootProxy.Root.AddCallback(modelTestConfig.RootProxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 479 | //} |
| 480 | // |
| 481 | //func Test_Proxy_Callbacks_6_Execute(t *testing.T) { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 482 | // modelTestConfig.RootProxy.Root.ExecuteCallbacks() |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 483 | //} |