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" |
| 22 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/common" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 24 | "github.com/opencord/voltha-go/protos/openflow_13" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 26 | "reflect" |
| 27 | "strconv" |
| 28 | "testing" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type proxyTest struct { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 32 | Root *root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | Backend *Backend |
| 34 | Proxy *Proxy |
| 35 | DbPrefix string |
| 36 | DbType string |
| 37 | DbHost string |
| 38 | DbPort int |
| 39 | DbTimeout int |
| 40 | } |
| 41 | |
| 42 | var ( |
| 43 | pt = &proxyTest{ |
| 44 | DbPrefix: "service/voltha/data/core/0001", |
| 45 | DbType: "etcd", |
| 46 | //DbHost: "10.102.58.0", |
| 47 | DbHost: "localhost", |
| 48 | DbPort: 2379, |
| 49 | DbTimeout: 5, |
| 50 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 51 | ports = []*voltha.Port{ |
| 52 | { |
| 53 | PortNo: 123, |
| 54 | Label: "test-port-0", |
| 55 | Type: voltha.Port_PON_OLT, |
| 56 | AdminState: common.AdminState_ENABLED, |
| 57 | OperStatus: common.OperStatus_ACTIVE, |
| 58 | DeviceId: "etcd_port-0-device-id", |
| 59 | Peers: []*voltha.Port_PeerPort{}, |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | stats = &openflow_13.OfpFlowStats{ |
| 64 | Id: 1111, |
| 65 | } |
| 66 | flows = &openflow_13.Flows{ |
| 67 | Items: []*openflow_13.OfpFlowStats{stats}, |
| 68 | } |
| 69 | device = &voltha.Device{ |
| 70 | Id: devId, |
| 71 | Type: "simulated_olt", |
| 72 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 73 | AdminState: voltha.AdminState_PREPROVISIONED, |
| 74 | Flows: flows, |
| 75 | Ports: ports, |
| 76 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 77 | devId string |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 78 | targetDeviceId string |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 79 | |
| 80 | preAddExecuted = false |
| 81 | postAddExecuted = false |
| 82 | preUpdateExecuted = false |
| 83 | postUpdateExecuted = false |
| 84 | preRemoveExecuted = false |
| 85 | postRemoveExecuted = false |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 86 | ) |
| 87 | |
| 88 | func init() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 89 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 90 | log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"}) |
| 91 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 92 | defer log.CleanUp() |
| 93 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 94 | pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix) |
| 95 | |
| 96 | msgClass := &voltha.Voltha{} |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 97 | root := NewRoot(msgClass, pt.Backend) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 98 | pt.Root = root.Load(msgClass) |
| 99 | |
| 100 | GetProfiling().Report() |
| 101 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 102 | pt.Proxy = pt.Root.GetProxy("/", false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 105 | func commonCallback(args ...interface{}) interface{} { |
| 106 | log.Infof("Running common callback - arg count: %s", len(args)) |
| 107 | |
| 108 | for i := 0; i < len(args); i++ { |
| 109 | log.Infof("ARG %d : %+v", i, args[i]) |
| 110 | } |
| 111 | execStatus := args[1].(*bool) |
| 112 | |
| 113 | // Inform the caller that the callback was executed |
| 114 | *execStatus = true |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func Test_Proxy_1_1_Add_NewDevice(t *testing.T) { |
| 120 | devIdBin, _ := uuid.New().MarshalBinary() |
| 121 | devId = "0001" + hex.EncodeToString(devIdBin)[:12] |
| 122 | |
| 123 | pt.Proxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted) |
| 124 | pt.Proxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted) |
| 125 | |
| 126 | device.Id = devId |
| 127 | if added := pt.Proxy.Add("/devices", device, ""); added == nil { |
| 128 | t.Error("Failed to add device") |
| 129 | } else { |
| 130 | t.Logf("Added device : %+v", added) |
| 131 | } |
| 132 | |
| 133 | if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 134 | t.Error("Failed to find added device") |
| 135 | } else { |
| 136 | djson, _ := json.Marshal(d) |
| 137 | t.Logf("Found device: %s", string(djson)) |
| 138 | } |
| 139 | |
| 140 | if !preAddExecuted { |
| 141 | t.Error("PRE_ADD callback was not executed") |
| 142 | } |
| 143 | if !postAddExecuted { |
| 144 | t.Error("POST_ADD callback was not executed") |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func Test_Proxy_1_2_Add_ExistingDevice(t *testing.T) { |
| 149 | device.Id = devId |
| 150 | if added := pt.Proxy.Add("/devices", device, ""); added == nil { |
| 151 | t.Logf("Successfully detected that the device already exists: %s", devId) |
| 152 | } else { |
| 153 | t.Errorf("A new device should not have been created : %+v", added) |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | func Test_Proxy_2_1_Get_AllDevices(t *testing.T) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 159 | devices := pt.Proxy.Get("/devices", 1, false, "") |
| 160 | |
| 161 | if len(devices.([]interface{})) == 0 { |
| 162 | t.Error("there are no available devices to retrieve") |
| 163 | } else { |
| 164 | // Save the target device id for later tests |
| 165 | targetDeviceId = devices.([]interface{})[0].(*voltha.Device).Id |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 166 | t.Logf("retrieved all devices: %+v", devices) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 170 | func Test_Proxy_2_2_Get_SingleDevice(t *testing.T) { |
| 171 | if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 172 | t.Errorf("Failed to find device : %s", targetDeviceId) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 173 | } else { |
| 174 | djson, _ := json.Marshal(d) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 175 | t.Logf("Found device: %s", string(djson)) |
| 176 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 177 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 178 | } |
| 179 | |
| 180 | func Test_Proxy_3_1_Update_Device_WithRootProxy(t *testing.T) { |
| 181 | if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil { |
| 182 | t.Error("Failed to get device") |
| 183 | } else { |
| 184 | var fwVersion int |
| 185 | if retrieved.(*voltha.Device).FirmwareVersion == "n/a" { |
| 186 | fwVersion = 0 |
| 187 | } else { |
| 188 | fwVersion, _ = strconv.Atoi(retrieved.(*voltha.Device).FirmwareVersion) |
| 189 | fwVersion += 1 |
| 190 | } |
| 191 | |
| 192 | preUpdateExecuted = false |
| 193 | postUpdateExecuted = false |
| 194 | |
| 195 | pt.Proxy.RegisterCallback( |
| 196 | PRE_UPDATE, |
| 197 | commonCallback, |
| 198 | "PRE_UPDATE instructions (root proxy)", &preUpdateExecuted, |
| 199 | ) |
| 200 | pt.Proxy.RegisterCallback( |
| 201 | POST_UPDATE, |
| 202 | commonCallback, |
| 203 | "POST_UPDATE instructions (root proxy)", &postUpdateExecuted, |
| 204 | ) |
| 205 | |
| 206 | //cloned := reflect.ValueOf(retrieved).Elem().Interface().(voltha.Device) |
| 207 | //cloned.FirmwareVersion = strconv.Itoa(fwVersion) |
| 208 | retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion) |
| 209 | //t.Logf("Before update : %+v", cloned) |
| 210 | |
| 211 | if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, retrieved, false, ""); afterUpdate == nil { |
| 212 | t.Error("Failed to update device") |
| 213 | } else { |
| 214 | t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData()) |
| 215 | } |
| 216 | if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 217 | t.Error("Failed to find updated device (root proxy)") |
| 218 | } else { |
| 219 | djson, _ := json.Marshal(d) |
| 220 | t.Logf("Found device (root proxy): %s", string(djson)) |
| 221 | } |
| 222 | |
| 223 | if !preUpdateExecuted { |
| 224 | t.Error("PRE_UPDATE callback was not executed") |
| 225 | } |
| 226 | if !postUpdateExecuted { |
| 227 | t.Error("POST_UPDATE callback was not executed") |
| 228 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 229 | } |
| 230 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 231 | |
| 232 | func Test_Proxy_3_2_Update_Flow_WithSubProxy(t *testing.T) { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 233 | // Get a device proxy and update a specific port |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 234 | devflowsProxy := pt.Root.GetProxy("/devices/"+devId+"/flows", false) |
| 235 | flows := devflowsProxy.Get("/", 0, false, "") |
| 236 | //flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2222 |
| 237 | flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244 |
| 238 | //flows.(*openflow_13.Flows).Items[0].TableId = 2244 |
| 239 | t.Logf("before updated flows: %+v", flows) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 240 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 241 | //devPortsProxy := pt.Root.node.GetProxy("/devices/"+devId+"/ports", false) |
| 242 | //port123 := devPortsProxy.Get("/123", 0, false, "") |
| 243 | //t.Logf("got ports: %+v", port123) |
| 244 | //port123.(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 245 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 246 | preUpdateExecuted = false |
| 247 | postUpdateExecuted = false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 248 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 249 | devflowsProxy.RegisterCallback( |
| 250 | PRE_UPDATE, |
| 251 | commonCallback, |
| 252 | "PRE_UPDATE instructions (flows proxy)", &preUpdateExecuted, |
| 253 | ) |
| 254 | devflowsProxy.RegisterCallback( |
| 255 | POST_UPDATE, |
| 256 | commonCallback, |
| 257 | "POST_UPDATE instructions (flows proxy)", &postUpdateExecuted, |
| 258 | ) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 259 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 260 | kvFlows := devflowsProxy.Get("/", 0, false, "") |
| 261 | |
| 262 | if reflect.DeepEqual(flows, kvFlows) { |
| 263 | t.Errorf("Local changes have changed the KV store contents - local:%+v, kv: %+v", flows, kvFlows) |
| 264 | } |
| 265 | |
| 266 | if updated := devflowsProxy.Update("/", flows.([]interface{})[0], false, ""); updated == nil { |
| 267 | t.Error("Failed to update flow") |
| 268 | } else { |
| 269 | t.Logf("Updated flows : %+v", updated) |
| 270 | } |
| 271 | |
| 272 | if d := devflowsProxy.Get("/", 0, false, ""); d == nil { |
| 273 | t.Error("Failed to find updated flows (flows proxy)") |
| 274 | } else { |
| 275 | djson, _ := json.Marshal(d) |
| 276 | t.Logf("Found flows (flows proxy): %s", string(djson)) |
| 277 | } |
| 278 | |
| 279 | if d := pt.Proxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() { |
| 280 | t.Error("Failed to find updated flows (root proxy)") |
| 281 | } else { |
| 282 | djson, _ := json.Marshal(d) |
| 283 | t.Logf("Found flows (root proxy): %s", string(djson)) |
| 284 | } |
| 285 | |
| 286 | if !preUpdateExecuted { |
| 287 | t.Error("PRE_UPDATE callback was not executed") |
| 288 | } |
| 289 | if !postUpdateExecuted { |
| 290 | t.Error("POST_UPDATE callback was not executed") |
| 291 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 292 | // |
| 293 | // Get a device proxy and update all its ports |
| 294 | // |
| 295 | |
| 296 | //devProxy := pt.Root.GetProxy("/devices/"+devId, false) |
| 297 | //ports := devProxy.Get("/ports", 0, false, "") |
| 298 | //t.Logf("got ports: %+v", ports) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 299 | //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 300 | // |
| 301 | //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED |
| 302 | // |
| 303 | //devProxy.Update("/ports", ports, false, "") |
| 304 | //updated := devProxy.Get("/ports", 0, false, "") |
| 305 | //t.Logf("got updated ports: %+v", updated) |
| 306 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 307 | // |
| 308 | // Get a device proxy, retrieve all the ports and update a specific one |
| 309 | // |
| 310 | |
| 311 | //devProxy := pt.Root.GetProxy("/devices/"+devId, false) |
| 312 | //ports := devProxy.Get("/ports", 0, false, "") |
| 313 | //t.Logf("got ports: %+v", ports) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 314 | //devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 315 | // |
| 316 | //ports.([]interface{})[0].(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED |
| 317 | // |
| 318 | //devProxy.Update("/ports/123", ports.([]interface{})[0], false, "") |
| 319 | //updated := devProxy.Get("/ports", 0, false, "") |
| 320 | //t.Logf("got updated ports: %+v", updated) |
| 321 | } |
| 322 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 323 | func Test_Proxy_4_1_Remove_Device(t *testing.T) { |
| 324 | preRemoveExecuted = false |
| 325 | postRemoveExecuted = false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 326 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 327 | pt.Proxy.RegisterCallback( |
| 328 | PRE_REMOVE, |
| 329 | commonCallback, |
| 330 | "PRE_REMOVE instructions (root proxy)", &preRemoveExecuted, |
| 331 | ) |
| 332 | pt.Proxy.RegisterCallback( |
| 333 | POST_REMOVE, |
| 334 | commonCallback, |
| 335 | "POST_REMOVE instructions (root proxy)", &postRemoveExecuted, |
| 336 | ) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 337 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 338 | if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 339 | t.Error("Failed to remove device") |
| 340 | } else { |
| 341 | t.Logf("Removed device : %+v", removed) |
| 342 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 343 | if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 344 | djson, _ := json.Marshal(d) |
| 345 | t.Errorf("Device was not removed - %s", djson) |
| 346 | } else { |
| 347 | t.Logf("Device was removed: %s", devId) |
| 348 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 349 | |
| 350 | if !preRemoveExecuted { |
| 351 | t.Error("PRE_UPDATE callback was not executed") |
| 352 | } |
| 353 | if !postRemoveExecuted { |
| 354 | t.Error("POST_UPDATE callback was not executed") |
| 355 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 356 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 357 | |
| 358 | // ----------------------------- |
| 359 | // Callback tests |
| 360 | // ----------------------------- |
| 361 | |
| 362 | func firstCallback(args ...interface{}) interface{} { |
| 363 | name := args[0] |
| 364 | id := args[1] |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 365 | log.Infof("Running first callback - name: %s, id: %s\n", name, id) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 366 | return nil |
| 367 | } |
| 368 | func secondCallback(args ...interface{}) interface{} { |
| 369 | name := args[0].(map[string]string) |
| 370 | id := args[1] |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 371 | log.Infof("Running second callback - name: %s, id: %f\n", name["name"], id) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 372 | // FIXME: the panic call seem to interfere with the logging mechanism |
| 373 | //panic("Generating a panic in second callback") |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 374 | return nil |
| 375 | } |
| 376 | func thirdCallback(args ...interface{}) interface{} { |
| 377 | name := args[0] |
| 378 | id := args[1].(*voltha.Device) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 379 | log.Infof("Running third callback - name: %+v, id: %s\n", name, id.Id) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 380 | return nil |
| 381 | } |
| 382 | |
| 383 | func Test_Proxy_Callbacks_1_Register(t *testing.T) { |
| 384 | pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345") |
| 385 | |
| 386 | m := make(map[string]string) |
| 387 | m["name"] = "fghij" |
| 388 | pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345) |
| 389 | |
| 390 | d := &voltha.Device{Id: "12345"} |
| 391 | pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d) |
| 392 | } |
| 393 | |
| 394 | func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 395 | pt.Proxy.InvokeCallbacks(PRE_ADD, false, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 396 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 397 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 398 | func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 399 | pt.Proxy.InvokeCallbacks(PRE_ADD, true, nil) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | func Test_Proxy_Callbacks_4_Unregister(t *testing.T) { |
| 403 | pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback) |
| 404 | pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback) |
| 405 | pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback) |
| 406 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame^] | 407 | |
| 408 | //func Test_Proxy_Callbacks_5_Add(t *testing.T) { |
| 409 | // pt.Proxy.Root.AddCallback(pt.Proxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data") |
| 410 | //} |
| 411 | // |
| 412 | //func Test_Proxy_Callbacks_6_Execute(t *testing.T) { |
| 413 | // pt.Proxy.Root.ExecuteCallbacks() |
| 414 | //} |