khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "strings" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 26 | "github.com/golang/protobuf/ptypes/empty" |
| 27 | "github.com/opencord/voltha-go/rw_core/config" |
| 28 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 31 | lm "github.com/opencord/voltha-lib-go/v3/pkg/mocks" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 33 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 34 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 35 | "github.com/phayes/freeport" |
| 36 | "github.com/stretchr/testify/assert" |
| 37 | "google.golang.org/grpc/codes" |
| 38 | "google.golang.org/grpc/status" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | type NBTest struct { |
| 42 | etcdServer *lm.EtcdServer |
| 43 | core *Core |
| 44 | kClient kafka.Client |
| 45 | kvClientPort int |
| 46 | numONUPerOLT int |
| 47 | oltAdapterName string |
| 48 | onuAdapterName string |
| 49 | coreInstanceID string |
| 50 | defaultTimeout time.Duration |
| 51 | maxTimeout time.Duration |
| 52 | } |
| 53 | |
| 54 | func newNBTest() *NBTest { |
| 55 | test := &NBTest{} |
| 56 | // Start the embedded etcd server |
| 57 | var err error |
| 58 | test.etcdServer, test.kvClientPort, err = startEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error") |
| 59 | if err != nil { |
| 60 | log.Fatal(err) |
| 61 | } |
| 62 | // Create the kafka client |
| 63 | test.kClient = lm.NewKafkaClient() |
| 64 | test.oltAdapterName = "olt_adapter_mock" |
| 65 | test.onuAdapterName = "onu_adapter_mock" |
| 66 | test.coreInstanceID = "rw-nbi-test" |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 67 | test.defaultTimeout = 10 * time.Second |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 68 | test.maxTimeout = 20 * time.Second |
| 69 | return test |
| 70 | } |
| 71 | |
| 72 | func (nb *NBTest) startCore(inCompeteMode bool) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 73 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 74 | defer cancel() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 75 | cfg := config.NewRWCoreFlags() |
| 76 | cfg.CorePairTopic = "rw_core" |
| 77 | cfg.DefaultRequestTimeout = nb.defaultTimeout.Nanoseconds() / 1000000 //TODO: change when Core changes to Duration |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 78 | cfg.DefaultCoreTimeout = nb.defaultTimeout.Nanoseconds() / 1000000 |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 79 | cfg.KVStorePort = nb.kvClientPort |
| 80 | cfg.InCompetingMode = inCompeteMode |
| 81 | grpcPort, err := freeport.GetFreePort() |
| 82 | if err != nil { |
| 83 | log.Fatal("Cannot get a freeport for grpc") |
| 84 | } |
| 85 | cfg.GrpcPort = grpcPort |
| 86 | cfg.GrpcHost = "127.0.0.1" |
| 87 | setCoreCompeteMode(inCompeteMode) |
| 88 | client := setupKVClient(cfg, nb.coreInstanceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 89 | nb.core = NewCore(ctx, nb.coreInstanceID, cfg, client, nb.kClient) |
| 90 | err = nb.core.Start(context.Background()) |
| 91 | if err != nil { |
| 92 | log.Fatal("Cannot start core") |
| 93 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 96 | func (nb *NBTest) createAndregisterAdapters(t *testing.T) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 97 | // Setup the mock OLT adapter |
| 98 | oltAdapter, err := createMockAdapter(OltAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.oltAdapterName) |
| 99 | if err != nil { |
| 100 | log.Fatalw("setting-mock-olt-adapter-failed", log.Fields{"error": err}) |
| 101 | } |
| 102 | if adapter, ok := (oltAdapter).(*cm.OLTAdapter); ok { |
| 103 | nb.numONUPerOLT = adapter.GetNumONUPerOLT() |
| 104 | } |
| 105 | // Register the adapter |
| 106 | registrationData := &voltha.Adapter{ |
| 107 | Id: nb.oltAdapterName, |
| 108 | Vendor: "Voltha-olt", |
| 109 | Version: version.VersionInfo.Version, |
| 110 | } |
| 111 | types := []*voltha.DeviceType{{Id: nb.oltAdapterName, Adapter: nb.oltAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 112 | deviceTypes := &voltha.DeviceTypes{Items: types} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 113 | if _, err := nb.core.adapterMgr.registerAdapter(registrationData, deviceTypes); err != nil { |
| 114 | log.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 115 | assert.NotNil(t, err) |
| 116 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 117 | |
| 118 | // Setup the mock ONU adapter |
| 119 | if _, err := createMockAdapter(OnuAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.onuAdapterName); err != nil { |
| 120 | log.Fatalw("setting-mock-onu-adapter-failed", log.Fields{"error": err}) |
| 121 | } |
| 122 | // Register the adapter |
| 123 | registrationData = &voltha.Adapter{ |
| 124 | Id: nb.onuAdapterName, |
| 125 | Vendor: "Voltha-onu", |
| 126 | Version: version.VersionInfo.Version, |
| 127 | } |
| 128 | types = []*voltha.DeviceType{{Id: nb.onuAdapterName, Adapter: nb.onuAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 129 | deviceTypes = &voltha.DeviceTypes{Items: types} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 130 | if _, err := nb.core.adapterMgr.registerAdapter(registrationData, deviceTypes); err != nil { |
| 131 | log.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 132 | assert.NotNil(t, err) |
| 133 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func (nb *NBTest) stopAll() { |
| 137 | if nb.kClient != nil { |
| 138 | nb.kClient.Stop() |
| 139 | } |
| 140 | if nb.core != nil { |
| 141 | nb.core.Stop(context.Background()) |
| 142 | } |
| 143 | if nb.etcdServer != nil { |
| 144 | stopEmbeddedEtcdServer(nb.etcdServer) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *APIHandler) { |
| 149 | // Get the latest set of logical devices |
| 150 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 151 | assert.Nil(t, err) |
| 152 | assert.NotNil(t, logicalDevices) |
| 153 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 154 | |
| 155 | ld := logicalDevices.Items[0] |
| 156 | assert.NotEqual(t, "", ld.Id) |
| 157 | assert.NotEqual(t, uint64(0), ld.DatapathId) |
| 158 | assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc) |
| 159 | assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc) |
| 160 | assert.NotEqual(t, "", ld.RootDeviceId) |
| 161 | assert.NotEqual(t, "", ld.Desc.SerialNum) |
| 162 | assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers) |
| 163 | assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables) |
| 164 | assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities) |
| 165 | assert.Equal(t, 1+nb.numONUPerOLT, len(ld.Ports)) |
| 166 | assert.Equal(t, oltDevice.ParentId, ld.Id) |
| 167 | //Expected port no |
| 168 | expectedPortNo := make(map[uint32]bool) |
| 169 | expectedPortNo[uint32(2)] = false |
| 170 | for i := 0; i < nb.numONUPerOLT; i++ { |
| 171 | expectedPortNo[uint32(i+100)] = false |
| 172 | } |
| 173 | for _, p := range ld.Ports { |
| 174 | assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo) |
| 175 | assert.Equal(t, uint32(4), p.OfpPort.State) |
| 176 | expectedPortNo[p.OfpPort.PortNo] = true |
| 177 | if strings.HasPrefix(p.Id, "nni") { |
| 178 | assert.Equal(t, true, p.RootPort) |
| 179 | //assert.Equal(t, uint32(2), p.OfpPort.PortNo) |
| 180 | assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo)) |
| 181 | } else { |
| 182 | assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo)) |
| 183 | assert.Equal(t, false, p.RootPort) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func (nb *NBTest) verifyDevices(t *testing.T, nbi *APIHandler) { |
| 189 | // Get the latest set of devices |
| 190 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 191 | assert.Nil(t, err) |
| 192 | assert.NotNil(t, devices) |
| 193 | |
| 194 | // Wait until devices are in the correct states |
| 195 | var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 196 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 197 | } |
| 198 | for _, d := range devices.Items { |
| 199 | err = waitUntilDeviceReadiness(d.Id, nb.maxTimeout, vFunction, nbi) |
| 200 | assert.Nil(t, err) |
| 201 | assert.NotNil(t, d) |
| 202 | } |
| 203 | // Get the latest device updates as they may have changed since last list devices |
| 204 | updatedDevices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 205 | assert.Nil(t, err) |
| 206 | assert.NotNil(t, devices) |
| 207 | for _, d := range updatedDevices.Items { |
| 208 | assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState) |
| 209 | assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus) |
| 210 | assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus) |
| 211 | assert.Equal(t, d.Type, d.Adapter) |
| 212 | assert.NotEqual(t, "", d.MacAddress) |
| 213 | assert.NotEqual(t, "", d.SerialNumber) |
| 214 | |
| 215 | if d.Type == "olt_adapter_mock" { |
| 216 | assert.Equal(t, true, d.Root) |
| 217 | assert.NotEqual(t, "", d.Id) |
| 218 | assert.NotEqual(t, "", d.ParentId) |
| 219 | assert.Nil(t, d.ProxyAddress) |
| 220 | } else if d.Type == "onu_adapter_mock" { |
| 221 | assert.Equal(t, false, d.Root) |
| 222 | assert.NotEqual(t, uint32(0), d.Vlan) |
| 223 | assert.NotEqual(t, "", d.Id) |
| 224 | assert.NotEqual(t, "", d.ParentId) |
| 225 | assert.NotEqual(t, "", d.ProxyAddress.DeviceId) |
| 226 | assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType) |
| 227 | } else { |
| 228 | assert.Error(t, errors.New("invalid-device-type")) |
| 229 | } |
| 230 | assert.Equal(t, 2, len(d.Ports)) |
| 231 | for _, p := range d.Ports { |
| 232 | assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState) |
| 233 | assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus) |
| 234 | if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI { |
| 235 | assert.Equal(t, 0, len(p.Peers)) |
| 236 | } else if p.Type == voltha.Port_PON_OLT { |
| 237 | assert.Equal(t, nb.numONUPerOLT, len(p.Peers)) |
| 238 | assert.Equal(t, uint32(1), p.PortNo) |
| 239 | } else if p.Type == voltha.Port_PON_ONU { |
| 240 | assert.Equal(t, 1, len(p.Peers)) |
| 241 | assert.Equal(t, uint32(1), p.PortNo) |
| 242 | } else { |
| 243 | assert.Error(t, errors.New("invalid-port")) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | func (nb *NBTest) getADevice(rootDevice bool, nbi *APIHandler) (*voltha.Device, error) { |
| 250 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 251 | if err != nil { |
| 252 | return nil, err |
| 253 | } |
| 254 | for _, d := range devices.Items { |
| 255 | if d.Root == rootDevice { |
| 256 | return d, nil |
| 257 | } |
| 258 | } |
| 259 | return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice) |
| 260 | } |
| 261 | |
| 262 | func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *APIHandler) { |
| 263 | lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 264 | assert.Nil(t, err) |
| 265 | assert.NotNil(t, lds) |
| 266 | assert.Equal(t, 0, len(lds.Items)) |
| 267 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 268 | assert.Nil(t, err) |
| 269 | assert.NotNil(t, devices) |
| 270 | assert.Equal(t, 0, len(devices.Items)) |
| 271 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
| 272 | assert.Nil(t, err) |
| 273 | assert.NotNil(t, adapters) |
| 274 | assert.Equal(t, 0, len(adapters.Items)) |
| 275 | } |
| 276 | |
| 277 | func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *APIHandler) { |
| 278 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
| 279 | assert.Nil(t, err) |
| 280 | assert.NotNil(t, adapters) |
| 281 | assert.Equal(t, 2, len(adapters.Items)) |
| 282 | for _, a := range adapters.Items { |
| 283 | switch a.Id { |
| 284 | case nb.oltAdapterName: |
| 285 | assert.Equal(t, "Voltha-olt", a.Vendor) |
| 286 | case nb.onuAdapterName: |
| 287 | assert.Equal(t, "Voltha-onu", a.Vendor) |
| 288 | default: |
| 289 | log.Fatal("unregistered-adapter", a.Id) |
| 290 | } |
| 291 | } |
| 292 | deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{}) |
| 293 | assert.Nil(t, err) |
| 294 | assert.NotNil(t, deviceTypes) |
| 295 | assert.Equal(t, 2, len(deviceTypes.Items)) |
| 296 | for _, dt := range deviceTypes.Items { |
| 297 | switch dt.Id { |
| 298 | case nb.oltAdapterName: |
| 299 | assert.Equal(t, nb.oltAdapterName, dt.Adapter) |
| 300 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 301 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 302 | case nb.onuAdapterName: |
| 303 | assert.Equal(t, nb.onuAdapterName, dt.Adapter) |
| 304 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 305 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 306 | default: |
| 307 | log.Fatal("invalid-device-type", dt.Id) |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | func (nb *NBTest) testCreateDevice(t *testing.T, nbi *APIHandler) { |
| 313 | // Create a valid device |
| 314 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 315 | assert.Nil(t, err) |
| 316 | assert.NotNil(t, oltDevice) |
| 317 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 318 | assert.Nil(t, err) |
| 319 | assert.NotNil(t, device) |
| 320 | assert.Equal(t, oltDevice.String(), device.String()) |
| 321 | |
| 322 | // Try to create the same device |
| 323 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 324 | assert.NotNil(t, err) |
| 325 | assert.Equal(t, "Device is already pre-provisioned", err.Error()) |
| 326 | |
| 327 | // Try to create a device with invalid data |
| 328 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName}) |
| 329 | assert.NotNil(t, err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 330 | assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error()) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 331 | |
| 332 | // Ensure we only have 1 device in the Core |
| 333 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 334 | assert.Nil(t, err) |
| 335 | assert.NotNil(t, devices) |
| 336 | assert.Equal(t, 1, len(devices.Items)) |
| 337 | assert.Equal(t, oltDevice.String(), devices.Items[0].String()) |
| 338 | |
| 339 | //Remove the device |
| 340 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 341 | assert.Nil(t, err) |
| 342 | |
| 343 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 344 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 345 | return devices != nil && len(devices.Items) == 0 |
| 346 | } |
| 347 | err = waitUntilConditionForDevices(5*time.Second, nbi, vFunction) |
| 348 | assert.Nil(t, err) |
| 349 | } |
| 350 | |
| 351 | func (nb *NBTest) testEnableDevice(t *testing.T, nbi *APIHandler) { |
| 352 | // Create a device that has no adapter registered |
| 353 | oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"}) |
| 354 | assert.Nil(t, err) |
| 355 | assert.NotNil(t, oltDeviceNoAdapter) |
| 356 | |
| 357 | // Try to enable the oltDevice and check the error message |
| 358 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 359 | assert.NotNil(t, err) |
| 360 | assert.Equal(t, "Adapter-not-registered-for-device-type noAdapterRegistered", err.Error()) |
| 361 | |
| 362 | //Remove the device |
| 363 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 364 | assert.Nil(t, err) |
| 365 | |
| 366 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 367 | var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 368 | return devices != nil && len(devices.Items) == 0 |
| 369 | } |
| 370 | err = waitUntilConditionForDevices(5*time.Second, nbi, vdFunction) |
| 371 | assert.Nil(t, err) |
| 372 | |
| 373 | // Create the device with valid data |
| 374 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 375 | assert.Nil(t, err) |
| 376 | assert.NotNil(t, oltDevice) |
| 377 | |
| 378 | // Verify oltDevice exist in the core |
| 379 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 380 | assert.Nil(t, err) |
| 381 | assert.Equal(t, 1, len(devices.Items)) |
| 382 | assert.Equal(t, oltDevice.Id, devices.Items[0].Id) |
| 383 | |
| 384 | // Enable the oltDevice |
| 385 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 386 | assert.Nil(t, err) |
| 387 | |
| 388 | // Wait for the logical device to be in the ready state |
| 389 | var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool { |
| 390 | return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1 |
| 391 | } |
| 392 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
| 393 | assert.Nil(t, err) |
| 394 | |
| 395 | // Verify that the devices have been setup correctly |
| 396 | nb.verifyDevices(t, nbi) |
| 397 | |
| 398 | // Get latest oltDevice data |
| 399 | oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 400 | assert.Nil(t, err) |
| 401 | |
| 402 | // Verify that the logical device has been setup correctly |
| 403 | nb.verifyLogicalDevices(t, oltDevice, nbi) |
| 404 | } |
| 405 | |
| 406 | func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *APIHandler) { |
| 407 | //Get an OLT device |
| 408 | oltDevice, err := nb.getADevice(true, nbi) |
| 409 | assert.Nil(t, err) |
| 410 | assert.NotNil(t, oltDevice) |
| 411 | |
| 412 | // Disable the oltDevice |
| 413 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 414 | assert.Nil(t, err) |
| 415 | |
| 416 | // Wait for the old device to be disabled |
| 417 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 418 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 419 | } |
| 420 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 421 | assert.Nil(t, err) |
| 422 | |
| 423 | // Verify that all onu devices are disabled as well |
| 424 | onuDevices, err := nb.core.deviceMgr.getAllChildDevices(oltDevice.Id) |
| 425 | assert.Nil(t, err) |
| 426 | for _, onu := range onuDevices.Items { |
| 427 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 428 | assert.Nil(t, err) |
| 429 | } |
| 430 | |
| 431 | // Wait for the logical device to satisfy the expected condition |
| 432 | var vlFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool { |
| 433 | for _, lp := range ld.Ports { |
| 434 | if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 435 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) { |
| 436 | return false |
| 437 | } |
| 438 | } |
| 439 | return true |
| 440 | } |
| 441 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 442 | assert.Nil(t, err) |
| 443 | |
| 444 | // Reenable the oltDevice |
| 445 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 446 | assert.Nil(t, err) |
| 447 | |
| 448 | // Wait for the old device to be enabled |
| 449 | vdFunction = func(device *voltha.Device) bool { |
| 450 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 451 | } |
| 452 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 453 | assert.Nil(t, err) |
| 454 | |
| 455 | // Verify that all onu devices are enabled as well |
| 456 | onuDevices, err = nb.core.deviceMgr.getAllChildDevices(oltDevice.Id) |
| 457 | assert.Nil(t, err) |
| 458 | for _, onu := range onuDevices.Items { |
| 459 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 460 | assert.Nil(t, err) |
| 461 | } |
| 462 | |
| 463 | // Wait for the logical device to satisfy the expected condition |
| 464 | vlFunction = func(ld *voltha.LogicalDevice) bool { |
| 465 | for _, lp := range ld.Ports { |
| 466 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 467 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 468 | return false |
| 469 | } |
| 470 | } |
| 471 | return true |
| 472 | } |
| 473 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 474 | assert.Nil(t, err) |
| 475 | } |
| 476 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 477 | func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *APIHandler) { |
| 478 | //Get an OLT device |
| 479 | oltDevice, err := nb.getADevice(true, nbi) |
| 480 | assert.Nil(t, err) |
| 481 | assert.NotNil(t, oltDevice) |
| 482 | |
| 483 | // Disable the oltDevice |
| 484 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 485 | assert.Nil(t, err) |
| 486 | |
| 487 | // Wait for the olt device to be disabled |
| 488 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 489 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 490 | } |
| 491 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 492 | assert.Nil(t, err) |
| 493 | |
| 494 | // Verify that all onu devices are disabled as well |
| 495 | onuDevices, err := nb.core.deviceMgr.getAllChildDevices(oltDevice.Id) |
| 496 | assert.Nil(t, err) |
| 497 | for _, onu := range onuDevices.Items { |
| 498 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 499 | assert.Nil(t, err) |
| 500 | } |
| 501 | |
| 502 | // Delete the oltDevice |
| 503 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 504 | assert.Nil(t, err) |
| 505 | |
| 506 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 507 | return devices != nil && len(devices.Items) == 0 |
| 508 | } |
| 509 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 510 | assert.Nil(t, err) |
| 511 | |
| 512 | // Wait for absence of logical device |
| 513 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 514 | return lds != nil && len(lds.Items) == 0 |
| 515 | } |
| 516 | |
| 517 | err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 518 | assert.Nil(t, err) |
| 519 | } |
| 520 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 521 | func TestSuite1(t *testing.T) { |
| 522 | nb := newNBTest() |
| 523 | assert.NotNil(t, nb) |
| 524 | |
| 525 | defer nb.stopAll() |
| 526 | |
| 527 | // Start the Core |
| 528 | nb.startCore(false) |
| 529 | |
| 530 | // Set the grpc API interface - no grpc server is running in unit test |
| 531 | nbi := NewAPIHandler(nb.core) |
| 532 | |
| 533 | // 1. Basic test with no data in Core |
| 534 | nb.testCoreWithoutData(t, nbi) |
| 535 | |
| 536 | // Create/register the adapters |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 537 | nb.createAndregisterAdapters(t) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 538 | |
| 539 | // 2. Test adapter registration |
| 540 | nb.testAdapterRegistration(t, nbi) |
| 541 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 542 | numberOfDeviceTestRuns := 2 |
| 543 | for i := 1; i <= numberOfDeviceTestRuns; i++ { |
| 544 | // 3. Test create device |
| 545 | nb.testCreateDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 546 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 547 | // 4. Test Enable a device |
| 548 | nb.testEnableDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 549 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 550 | // 5. Test disable and ReEnable a root device |
| 551 | nb.testDisableAndReEnableRootDevice(t, nbi) |
| 552 | |
| 553 | // 6. Test disable and delete all devices |
| 554 | nb.testDisableAndDeleteAllDevice(t, nbi) |
| 555 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 556 | |
| 557 | //x. TODO - More tests to come |
| 558 | } |