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