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