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