blob: fa1f6575cd66367a96c79f99d4bf07db033efbb0 [file] [log] [blame]
khenaidoob64fc8a2019-11-27 15:08:19 -05001/*
Kent Hagerman45a13e42020-04-13 12:23:50 -04002 * 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.
khenaidoob64fc8a2019-11-27 15:08:19 -050015 */
Kent Hagerman45a13e42020-04-13 12:23:50 -040016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package api
khenaidoob64fc8a2019-11-27 15:08:19 -050018
19import (
20 "context"
21 "errors"
22 "fmt"
khenaidoo67b22152020-03-02 16:01:25 -050023 "math/rand"
24 "os"
25 "runtime"
26 "runtime/pprof"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050028 "sync"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080029 "testing"
30 "time"
31
khenaidoob64fc8a2019-11-27 15:08:19 -050032 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040033 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050034 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040035 "github.com/opencord/voltha-go/rw_core/core/adapter"
36 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050037 cm "github.com/opencord/voltha-go/rw_core/mocks"
Kent Hagerman2b216042020-04-03 18:28:56 -040038 "github.com/opencord/voltha-lib-go/v3/pkg/db"
39 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080040 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
41 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Matteo Scandolod525ae32020-04-02 17:27:29 -070042 mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
43 mock_kafka "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080044 "github.com/opencord/voltha-lib-go/v3/pkg/version"
45 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
46 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050047 "github.com/phayes/freeport"
48 "github.com/stretchr/testify/assert"
49 "google.golang.org/grpc/codes"
50 "google.golang.org/grpc/status"
khenaidoob64fc8a2019-11-27 15:08:19 -050051)
52
Kent Hagerman2b216042020-04-03 18:28:56 -040053const (
54 coreName = "rw_core"
55)
56
khenaidoob64fc8a2019-11-27 15:08:19 -050057type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070058 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040059 deviceMgr *device.Manager
60 logicalDeviceMgr *device.LogicalManager
61 adapterMgr *adapter.Manager
62 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050063 kClient kafka.Client
64 kvClientPort int
65 numONUPerOLT int
66 startingUNIPortNo int
67 oltAdapter *cm.OLTAdapter
68 onuAdapter *cm.ONUAdapter
69 oltAdapterName string
70 onuAdapterName string
71 coreInstanceID string
72 defaultTimeout time.Duration
73 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050074}
75
76func newNBTest() *NBTest {
77 test := &NBTest{}
78 // Start the embedded etcd server
79 var err error
80 test.etcdServer, test.kvClientPort, err = startEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
81 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000082 logger.Fatal(err)
khenaidoob64fc8a2019-11-27 15:08:19 -050083 }
84 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070085 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050086 test.oltAdapterName = "olt_adapter_mock"
87 test.onuAdapterName = "onu_adapter_mock"
88 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050089 test.defaultTimeout = 10 * time.Second
90 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050091 return test
92}
93
94func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053095 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
96 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050097 cfg := config.NewRWCoreFlags()
98 cfg.CorePairTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040099 cfg.DefaultRequestTimeout = nb.defaultTimeout
100 cfg.DefaultCoreTimeout = nb.defaultTimeout
khenaidoob64fc8a2019-11-27 15:08:19 -0500101 cfg.KVStorePort = nb.kvClientPort
102 cfg.InCompetingMode = inCompeteMode
103 grpcPort, err := freeport.GetFreePort()
104 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000105 logger.Fatal("Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500106 }
107 cfg.GrpcPort = grpcPort
108 cfg.GrpcHost = "127.0.0.1"
109 setCoreCompeteMode(inCompeteMode)
110 client := setupKVClient(cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 backend := &db.Backend{
112 Client: client,
113 StoreType: cfg.KVStoreType,
114 Host: cfg.KVStoreHost,
115 Port: cfg.KVStorePort,
116 Timeout: cfg.KVStoreTimeout,
117 LivenessChannelInterval: cfg.LiveProbeInterval / 2,
118 PathPrefix: cfg.KVStoreDataPrefix}
119 nb.kmp = kafka.NewInterContainerProxy(
120 kafka.InterContainerHost(cfg.KafkaAdapterHost),
121 kafka.InterContainerPort(cfg.KafkaAdapterPort),
122 kafka.MsgClient(nb.kClient),
123 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}),
124 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: cfg.AffinityRouterTopic}))
125
126 endpointMgr := kafka.NewEndpointManager(backend)
127 proxy := model.NewProxy(backend, "/")
128 nb.adapterMgr = adapter.NewAdapterManager(proxy, nb.coreInstanceID, nb.kClient)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400129 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CorePairTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400130 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400131
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400132 if err := nb.kmp.Start(); err != nil {
Kent Hagerman2b216042020-04-03 18:28:56 -0400133 logger.Fatalf("Cannot start InterContainerProxy: %s", err)
134 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400135 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Kent Hagerman2b216042020-04-03 18:28:56 -0400136 if err := nb.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
137 logger.Fatalf("Cannot add request handler: %s", err)
138 }
139 if err := nb.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: cfg.CorePairTopic}, kafka.OffsetNewest); err != nil {
140 logger.Fatalf("Cannot add default request handler: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530141 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500142}
143
Thomas Lee Se5a44012019-11-07 20:32:24 +0530144func (nb *NBTest) createAndregisterAdapters(t *testing.T) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500145 // Setup the mock OLT adapter
146 oltAdapter, err := createMockAdapter(OltAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.oltAdapterName)
147 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000148 logger.Fatalw("setting-mock-olt-adapter-failed", log.Fields{"error": err})
khenaidoob64fc8a2019-11-27 15:08:19 -0500149 }
khenaidoo67b22152020-03-02 16:01:25 -0500150 nb.oltAdapter = (oltAdapter).(*cm.OLTAdapter)
151 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
152 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
153
khenaidoob64fc8a2019-11-27 15:08:19 -0500154 // Register the adapter
155 registrationData := &voltha.Adapter{
Matteo Scandolod525ae32020-04-02 17:27:29 -0700156 Id: nb.oltAdapterName,
157 Vendor: "Voltha-olt",
158 Version: version.VersionInfo.Version,
159 Type: nb.oltAdapterName,
160 CurrentReplica: 1,
161 TotalReplicas: 1,
162 Endpoint: nb.oltAdapterName,
khenaidoob64fc8a2019-11-27 15:08:19 -0500163 }
164 types := []*voltha.DeviceType{{Id: nb.oltAdapterName, Adapter: nb.oltAdapterName, AcceptsAddRemoveFlowUpdates: true}}
165 deviceTypes := &voltha.DeviceTypes{Items: types}
Kent Hagerman2b216042020-04-03 18:28:56 -0400166 if _, err := nb.adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000167 logger.Errorw("failed-to-register-adapter", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530168 assert.NotNil(t, err)
169 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500170
171 // Setup the mock ONU adapter
khenaidoo67b22152020-03-02 16:01:25 -0500172 onuAdapter, err := createMockAdapter(OnuAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.onuAdapterName)
173 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000174 logger.Fatalw("setting-mock-onu-adapter-failed", log.Fields{"error": err})
khenaidoob64fc8a2019-11-27 15:08:19 -0500175 }
khenaidoo67b22152020-03-02 16:01:25 -0500176 nb.onuAdapter = (onuAdapter).(*cm.ONUAdapter)
177
khenaidoob64fc8a2019-11-27 15:08:19 -0500178 // Register the adapter
179 registrationData = &voltha.Adapter{
Matteo Scandolod525ae32020-04-02 17:27:29 -0700180 Id: nb.onuAdapterName,
181 Vendor: "Voltha-onu",
182 Version: version.VersionInfo.Version,
183 Type: nb.onuAdapterName,
184 CurrentReplica: 1,
185 TotalReplicas: 1,
186 Endpoint: nb.onuAdapterName,
khenaidoob64fc8a2019-11-27 15:08:19 -0500187 }
188 types = []*voltha.DeviceType{{Id: nb.onuAdapterName, Adapter: nb.onuAdapterName, AcceptsAddRemoveFlowUpdates: true}}
189 deviceTypes = &voltha.DeviceTypes{Items: types}
Kent Hagerman2b216042020-04-03 18:28:56 -0400190 if _, err := nb.adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000191 logger.Errorw("failed-to-register-adapter", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530192 assert.NotNil(t, err)
193 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500194}
195
196func (nb *NBTest) stopAll() {
197 if nb.kClient != nil {
198 nb.kClient.Stop()
199 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400200 if nb.kmp != nil {
201 nb.kmp.Stop()
khenaidoob64fc8a2019-11-27 15:08:19 -0500202 }
203 if nb.etcdServer != nil {
204 stopEmbeddedEtcdServer(nb.etcdServer)
205 }
206}
207
Kent Hagerman2b216042020-04-03 18:28:56 -0400208func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500209 // Get the latest set of logical devices
210 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
211 assert.Nil(t, err)
212 assert.NotNil(t, logicalDevices)
213 assert.Equal(t, 1, len(logicalDevices.Items))
214
215 ld := logicalDevices.Items[0]
216 assert.NotEqual(t, "", ld.Id)
217 assert.NotEqual(t, uint64(0), ld.DatapathId)
218 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
219 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
220 assert.NotEqual(t, "", ld.RootDeviceId)
221 assert.NotEqual(t, "", ld.Desc.SerialNum)
222 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
223 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
224 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
225 assert.Equal(t, 1+nb.numONUPerOLT, len(ld.Ports))
226 assert.Equal(t, oltDevice.ParentId, ld.Id)
227 //Expected port no
228 expectedPortNo := make(map[uint32]bool)
229 expectedPortNo[uint32(2)] = false
230 for i := 0; i < nb.numONUPerOLT; i++ {
231 expectedPortNo[uint32(i+100)] = false
232 }
233 for _, p := range ld.Ports {
234 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
235 assert.Equal(t, uint32(4), p.OfpPort.State)
236 expectedPortNo[p.OfpPort.PortNo] = true
237 if strings.HasPrefix(p.Id, "nni") {
238 assert.Equal(t, true, p.RootPort)
239 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
240 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
241 } else {
242 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
243 assert.Equal(t, false, p.RootPort)
244 }
245 }
246}
247
Kent Hagerman2b216042020-04-03 18:28:56 -0400248func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500249 // Get the latest set of devices
250 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
251 assert.Nil(t, err)
252 assert.NotNil(t, devices)
253
khenaidoo67b22152020-03-02 16:01:25 -0500254 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500255 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
256 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
257 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500258
khenaidoo67b22152020-03-02 16:01:25 -0500259 var wg sync.WaitGroup
260 for _, device := range devices.Items {
261 wg.Add(1)
262 go func(wg *sync.WaitGroup, device *voltha.Device) {
263 // Wait until the device is in the right state
264 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
265 assert.Nil(t, err)
266
267 // Now, verify the details of the device. First get the latest update
268 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
269 assert.Nil(t, err)
270 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
271 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
272 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
273 assert.Equal(t, d.Type, d.Adapter)
274 assert.NotEqual(t, "", d.MacAddress)
275 assert.NotEqual(t, "", d.SerialNumber)
276
277 if d.Type == "olt_adapter_mock" {
278 assert.Equal(t, true, d.Root)
279 assert.NotEqual(t, "", d.Id)
280 assert.NotEqual(t, "", d.ParentId)
281 assert.Nil(t, d.ProxyAddress)
282 } else if d.Type == "onu_adapter_mock" {
283 assert.Equal(t, false, d.Root)
284 assert.NotEqual(t, uint32(0), d.Vlan)
285 assert.NotEqual(t, "", d.Id)
286 assert.NotEqual(t, "", d.ParentId)
287 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
288 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500289 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500290 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500291 }
khenaidoo67b22152020-03-02 16:01:25 -0500292 assert.Equal(t, 2, len(d.Ports))
293 for _, p := range d.Ports {
294 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
295 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
296 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
297 assert.Equal(t, 0, len(p.Peers))
298 } else if p.Type == voltha.Port_PON_OLT {
299 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
300 assert.Equal(t, uint32(1), p.PortNo)
301 } else if p.Type == voltha.Port_PON_ONU {
302 assert.Equal(t, 1, len(p.Peers))
303 assert.Equal(t, uint32(1), p.PortNo)
304 } else {
305 assert.Error(t, errors.New("invalid-port"))
306 }
307 }
308 wg.Done()
309 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500310 }
khenaidoo67b22152020-03-02 16:01:25 -0500311 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500312}
313
Kent Hagerman2b216042020-04-03 18:28:56 -0400314func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500315 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
316 if err != nil {
317 return nil, err
318 }
319 for _, d := range devices.Items {
320 if d.Root == rootDevice {
321 return d, nil
322 }
323 }
324 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
325}
326
Kent Hagerman2b216042020-04-03 18:28:56 -0400327func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500328 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
329 assert.Nil(t, err)
330 assert.NotNil(t, lds)
331 assert.Equal(t, 0, len(lds.Items))
332 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
333 assert.Nil(t, err)
334 assert.NotNil(t, devices)
335 assert.Equal(t, 0, len(devices.Items))
336 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400337 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500338 assert.Nil(t, err)
339 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500340}
341
Kent Hagerman2b216042020-04-03 18:28:56 -0400342func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500343 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
344 assert.Nil(t, err)
345 assert.NotNil(t, adapters)
346 assert.Equal(t, 2, len(adapters.Items))
347 for _, a := range adapters.Items {
348 switch a.Id {
349 case nb.oltAdapterName:
350 assert.Equal(t, "Voltha-olt", a.Vendor)
351 case nb.onuAdapterName:
352 assert.Equal(t, "Voltha-onu", a.Vendor)
353 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000354 logger.Fatal("unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500355 }
356 }
357 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
358 assert.Nil(t, err)
359 assert.NotNil(t, deviceTypes)
360 assert.Equal(t, 2, len(deviceTypes.Items))
361 for _, dt := range deviceTypes.Items {
362 switch dt.Id {
363 case nb.oltAdapterName:
364 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
365 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
366 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
367 case nb.onuAdapterName:
368 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
369 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
370 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
371 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000372 logger.Fatal("invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500373 }
374 }
375}
376
Kent Hagerman2b216042020-04-03 18:28:56 -0400377func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500378 // Create a valid device
379 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
380 assert.Nil(t, err)
381 assert.NotNil(t, oltDevice)
382 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
383 assert.Nil(t, err)
384 assert.NotNil(t, device)
385 assert.Equal(t, oltDevice.String(), device.String())
386
387 // Try to create the same device
388 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
389 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400390 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500391
392 // Try to create a device with invalid data
393 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
394 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530395 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500396
397 // Ensure we only have 1 device in the Core
398 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
399 assert.Nil(t, err)
400 assert.NotNil(t, devices)
401 assert.Equal(t, 1, len(devices.Items))
402 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
403
404 //Remove the device
405 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
406 assert.Nil(t, err)
407
408 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
409 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
410 return devices != nil && len(devices.Items) == 0
411 }
khenaidoo442e7c72020-03-10 16:13:48 -0400412 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500413 assert.Nil(t, err)
414}
415
Kent Hagerman2b216042020-04-03 18:28:56 -0400416func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500417 // Create a device that has no adapter registered
418 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
419 assert.Nil(t, err)
420 assert.NotNil(t, oltDeviceNoAdapter)
421
422 // Try to enable the oltDevice and check the error message
423 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
424 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400425 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500426
427 //Remove the device
428 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
429 assert.Nil(t, err)
430
431 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
432 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
433 return devices != nil && len(devices.Items) == 0
434 }
khenaidoo442e7c72020-03-10 16:13:48 -0400435 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500436 assert.Nil(t, err)
437
khenaidoo67b22152020-03-02 16:01:25 -0500438 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
439 var wg sync.WaitGroup
440 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400441 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500442
khenaidoob64fc8a2019-11-27 15:08:19 -0500443 // Create the device with valid data
444 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
445 assert.Nil(t, err)
446 assert.NotNil(t, oltDevice)
447
448 // Verify oltDevice exist in the core
449 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
450 assert.Nil(t, err)
451 assert.Equal(t, 1, len(devices.Items))
452 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
453
454 // Enable the oltDevice
455 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
456 assert.Nil(t, err)
457
458 // Wait for the logical device to be in the ready state
459 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
460 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
461 }
462 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
463 assert.Nil(t, err)
464
465 // Verify that the devices have been setup correctly
466 nb.verifyDevices(t, nbi)
467
468 // Get latest oltDevice data
469 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
470 assert.Nil(t, err)
471
472 // Verify that the logical device has been setup correctly
473 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500474
475 // Wait until all flows has been sent to the devices successfully
476 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500477}
478
Kent Hagerman2b216042020-04-03 18:28:56 -0400479func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500480 //Get an OLT device
481 oltDevice, err := nb.getADevice(true, nbi)
482 assert.Nil(t, err)
483 assert.NotNil(t, oltDevice)
484
485 // Disable the oltDevice
486 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
487 assert.Nil(t, err)
488
489 // Wait for the old device to be disabled
490 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
491 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
492 }
493 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
494 assert.Nil(t, err)
495
496 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400497 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500498 assert.Nil(t, err)
499 for _, onu := range onuDevices.Items {
500 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
501 assert.Nil(t, err)
502 }
503
504 // Wait for the logical device to satisfy the expected condition
505 var vlFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500506 if ld == nil {
507 return false
508 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500509 for _, lp := range ld.Ports {
510 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
511 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
512 return false
513 }
514 }
515 return true
516 }
517 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
518 assert.Nil(t, err)
519
520 // Reenable the oltDevice
521 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
522 assert.Nil(t, err)
523
524 // Wait for the old device to be enabled
525 vdFunction = func(device *voltha.Device) bool {
526 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
527 }
528 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
529 assert.Nil(t, err)
530
531 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400532 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500533 assert.Nil(t, err)
534 for _, onu := range onuDevices.Items {
535 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
536 assert.Nil(t, err)
537 }
538
539 // Wait for the logical device to satisfy the expected condition
540 vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500541 if ld == nil {
542 return false
543 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500544 for _, lp := range ld.Ports {
545 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
546 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
547 return false
548 }
549 }
550 return true
551 }
552 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
553 assert.Nil(t, err)
554}
555
Kent Hagerman2b216042020-04-03 18:28:56 -0400556func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500557 //Get an OLT device
558 oltDevice, err := nb.getADevice(true, nbi)
559 assert.Nil(t, err)
560 assert.NotNil(t, oltDevice)
561
562 // Disable the oltDevice
563 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
564 assert.Nil(t, err)
565
566 // Wait for the olt device to be disabled
567 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
568 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
569 }
570 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
571 assert.Nil(t, err)
572
573 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400574 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500575 assert.Nil(t, err)
576 for _, onu := range onuDevices.Items {
577 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
578 assert.Nil(t, err)
579 }
580
581 // Delete the oltDevice
582 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
583 assert.Nil(t, err)
584
585 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
586 return devices != nil && len(devices.Items) == 0
587 }
588 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
589 assert.Nil(t, err)
590
591 // Wait for absence of logical device
592 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
593 return lds != nil && len(lds.Items) == 0
594 }
595
596 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
597 assert.Nil(t, err)
598}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400599
600func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
601 //Get an OLT device
602 oltDevice, err := nb.getADevice(true, nbi)
603 assert.Nil(t, err)
604 assert.NotNil(t, oltDevice)
605
606 // Delete the oltDevice
607 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
608 assert.Nil(t, err)
609
610 // Wait for all devices to be deleted
611 vFunction := func(devices *voltha.Devices) bool {
612 return devices != nil && len(devices.Items) == 0
613 }
614 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
615 assert.Nil(t, err)
616
617 // Wait for absence of logical device
618 vlFunction := func(lds *voltha.LogicalDevices) bool {
619 return lds != nil && len(lds.Items) == 0
620 }
621
622 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
623 assert.Nil(t, err)
624}
625
Kent Hagerman2b216042020-04-03 18:28:56 -0400626func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500627 //Create the device with valid data
628 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
629 assert.Nil(t, err)
630 assert.NotNil(t, oltDevice)
631
632 //Get an OLT device
633 oltDevice, err = nb.getADevice(true, nbi)
634 assert.Nil(t, err)
635 assert.NotNil(t, oltDevice)
636
637 // Enable the oltDevice
638 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
639 assert.Nil(t, err)
640
641 // Wait for the logical device to be in the ready state
642 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
643 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
644 }
645 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
646 assert.Nil(t, err)
647
648 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400649 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500650 assert.Nil(t, err)
651
652 // Wait for the all onu devices to be enabled
653 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
654 return device.AdminState == voltha.AdminState_ENABLED
655 }
656 for _, onu := range onuDevices.Items {
657 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
658 assert.Nil(t, err)
659 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500660 // Wait for each onu device to get deleted
661 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
662 return device == nil
663 }
664
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500665 // Delete the onuDevice
666 for _, onu := range onuDevices.Items {
667 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
668 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500669 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
670 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500671 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500672
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500673 // Disable the oltDevice
674 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
675 assert.Nil(t, err)
676
677 // Wait for the olt device to be disabled
678 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
679 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
680 }
681 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
682 assert.Nil(t, err)
683
684 // Delete the oltDevice
685 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
686 assert.Nil(t, err)
687
688 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
689 return devices != nil && len(devices.Items) == 0
690 }
691 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
692 assert.Nil(t, err)
693}
Kent Hagerman2b216042020-04-03 18:28:56 -0400694func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500695 //Get an OLT device
696 var cp *voltha.Port
697 oltDevice, err := nb.getADevice(true, nbi)
698 assert.Nil(t, err)
699 assert.NotNil(t, oltDevice)
700
701 for _, cp = range oltDevice.Ports {
702 if cp.Type == voltha.Port_PON_OLT {
703 break
704 }
705
706 }
707 assert.NotNil(t, cp)
708 cp.DeviceId = oltDevice.Id
709
710 // Disable the NW Port of oltDevice
711 _, err = nbi.DisablePort(getContext(), cp)
712 assert.Nil(t, err)
713 // Wait for the olt device Port to be disabled
714 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
715 for _, port := range device.Ports {
716 if port.PortNo == cp.PortNo {
717 return port.AdminState == voltha.AdminState_DISABLED
718 }
719 }
720 return false
721 }
722 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
723 assert.Nil(t, err)
724 // Wait for the logical device to satisfy the expected condition
725 var vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500726 if ld == nil {
727 return false
728 }
kesavandbc2d1622020-01-21 00:42:01 -0500729 for _, lp := range ld.Ports {
730 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
731 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
732 return false
733 }
734 }
735 return true
736 }
737 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
738 assert.Nil(t, err)
739
740 // Enable the NW Port of oltDevice
741 _, err = nbi.EnablePort(getContext(), cp)
742 assert.Nil(t, err)
743
744 // Wait for the olt device Port to be enabled
745 vdFunction = func(device *voltha.Device) bool {
746 for _, port := range device.Ports {
747 if port.PortNo == cp.PortNo {
748 return port.AdminState == voltha.AdminState_ENABLED
749 }
750 }
751 return false
752 }
753 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
754 assert.Nil(t, err)
755 // Wait for the logical device to satisfy the expected condition
756 vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500757 if ld == nil {
758 return false
759 }
kesavandbc2d1622020-01-21 00:42:01 -0500760 for _, lp := range ld.Ports {
761 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
762 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
763 return false
764 }
765 }
766 return true
767 }
768 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
769 assert.Nil(t, err)
770
771 // Disable a non-PON port
772 for _, cp = range oltDevice.Ports {
773 if cp.Type != voltha.Port_PON_OLT {
774 break
775 }
776
777 }
778 assert.NotNil(t, cp)
779 cp.DeviceId = oltDevice.Id
780
781 // Disable the NW Port of oltDevice
782 _, err = nbi.DisablePort(getContext(), cp)
783 assert.NotNil(t, err)
784
785}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500786
Kent Hagerman2b216042020-04-03 18:28:56 -0400787func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700788 //Get an OLT device
789 oltDevice, err := nb.getADevice(true, nbi)
790 assert.Nil(t, err)
791 assert.NotNil(t, oltDevice)
792 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
793 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
794
795 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400796 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700797 assert.Nil(t, err)
798 assert.NotNil(t, onuDevices)
799 assert.Greater(t, len(onuDevices.Items), 0)
800
801 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
802 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
803 assert.Nil(t, err)
804
805 var vlFunction0 = func(d *voltha.Device) bool {
806 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
807 }
808
809 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
810 assert.Nil(t, err)
811
812 // Wait for the logical device to satisfy the expected condition
813 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
814 return ld == nil
815 }
816
817 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
818 assert.Nil(t, err)
819
820 // Wait for the device to satisfy the expected condition (device does not have flows)
821 var vlFunction2 = func(d *voltha.Device) bool {
822 var deviceFlows *ofp.Flows
823 var err error
824 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
825 return false
826 }
827 return len(deviceFlows.Items) == 0
828 }
829
830 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
831 assert.Nil(t, err)
832
833 // Wait for the device to satisfy the expected condition (there are no child devices)
834 var vlFunction3 = func(d *voltha.Device) bool {
835 var devices *voltha.Devices
836 var err error
837 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
838 return false
839 }
840 for _, device := range devices.Items {
841 if device.ParentId == d.Id {
842 // We have a child device still left
843 return false
844 }
845 }
846 return true
847 }
848
849 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
850 assert.Nil(t, err)
851
852 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
853 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400854 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700855 assert.Nil(t, err)
856
857 // Verify the device connection and operation states
858 oltDevice, err = nb.getADevice(true, nbi)
859 assert.Nil(t, err)
860 assert.NotNil(t, oltDevice)
861 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
862 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
863
864 // Wait for the logical device to satisfy the expected condition
865 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
866 return ld != nil
867 }
868 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
869 assert.Nil(t, err)
870
871 // Verify that logical device is created again
872 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
873 assert.Nil(t, err)
874 assert.NotNil(t, logicalDevices)
875 assert.Equal(t, 1, len(logicalDevices.Items))
876
877 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400878 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700879 assert.Nil(t, err)
880 assert.NotNil(t, onuDevices)
881 assert.Equal(t, 0, len(onuDevices.Items))
882}
883
Kent Hagerman2b216042020-04-03 18:28:56 -0400884func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700885 // -----------------------------------------------------------------------
886 // SubTest 1: Omci test action should fail due to nonexistent device id
887
888 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
889 _, err := nbi.StartOmciTestAction(getContext(), request)
890 assert.NotNil(t, err)
891 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
892
893 // -----------------------------------------------------------------------
894 // SubTest 2: Error should be returned for device with no adapter registered
895
896 // Create a device that has no adapter registered
897 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
898 assert.Nil(t, err)
899 assert.NotNil(t, deviceNoAdapter)
900
901 // Omci test action should fail due to nonexistent adapter
902 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
903 _, err = nbi.StartOmciTestAction(getContext(), request)
904 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400905 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700906
907 //Remove the device
908 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
909 assert.Nil(t, err)
910
911 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
912 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
913 return devices != nil && len(devices.Items) == 0
914 }
915 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
916 assert.Nil(t, err)
917
918 // -----------------------------------------------------------------------
919 // SubTest 3: Omci test action should succeed on valid ONU
920
921 // Create the device with valid data
922 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
923 assert.Nil(t, err)
924 assert.NotNil(t, oltDevice)
925
926 // Verify oltDevice exist in the core
927 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
928 assert.Nil(t, err)
929 assert.Equal(t, 1, len(devices.Items))
930 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
931
932 // Enable the oltDevice
933 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
934 assert.Nil(t, err)
935
936 // Wait for the logical device to be in the ready state
937 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
938 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
939 }
940 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
941 assert.Nil(t, err)
942
943 // Wait for the olt device to be enabled
944 vdFunction := func(device *voltha.Device) bool {
945 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
946 }
947 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
948 assert.Nil(t, err)
949
Kent Hagerman2b216042020-04-03 18:28:56 -0400950 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700951 assert.Nil(t, err)
952 assert.Greater(t, len(onuDevices.Items), 0)
953
954 onuDevice := onuDevices.Items[0]
955
956 // Omci test action should succeed
957 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
958 resp, err := nbi.StartOmciTestAction(getContext(), request)
959 assert.Nil(t, err)
960 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
961
962 //Remove the device
963 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
964 assert.Nil(t, err)
965 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
966 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
967 assert.Nil(t, err)
968}
969
khenaidoo67b22152020-03-02 16:01:25 -0500970func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
971 matchFields := make([]*ofp.OfpOxmField, 0)
972 for _, val := range fa.MatchFields {
973 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
974 }
975 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
976}
977
978func createMetadata(cTag int, techProfile int, port int) uint64 {
979 md := 0
980 md = (md | (cTag & 0xFFFF)) << 16
981 md = (md | (techProfile & 0xFFFF)) << 32
982 return uint64(md | (port & 0xFFFFFFFF))
983}
984
khenaidoo8b4abbf2020-04-24 17:04:30 -0400985func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500986 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400987 if flowAddFail {
988 expectedNumFlows = 0
989 }
990 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500991 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700992 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
993 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500994 }
995 // No timeout implies a success
996 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
997 assert.Nil(t, err)
998}
999
Kent Hagerman2b216042020-04-03 18:28:56 -04001000func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDevice *voltha.LogicalDevice, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -05001001 // Send flows for the parent device
1002 var nniPorts []*voltha.LogicalPort
1003 var uniPorts []*voltha.LogicalPort
1004 for _, p := range logicalDevice.Ports {
1005 if p.RootPort {
1006 nniPorts = append(nniPorts, p)
1007 } else {
1008 uniPorts = append(uniPorts, p)
1009 }
1010 }
1011 assert.Equal(t, 1, len(nniPorts))
1012 //assert.Greater(t, len(uniPorts), 1 )
1013 nniPort := nniPorts[0].OfpPort.PortNo
1014 maxInt32 := uint64(0xFFFFFFFF)
1015 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1016 var fa *flows.FlowArgs
1017 fa = &flows.FlowArgs{
1018 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1019 MatchFields: []*ofp.OfpOxmOfbField{
1020 flows.InPort(nniPort),
1021 flows.EthType(35020),
1022 },
1023 Actions: []*ofp.OfpAction{
1024 flows.Output(controllerPortMask),
1025 },
1026 }
1027 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1028 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1029 assert.Nil(t, err)
1030
1031 fa = &flows.FlowArgs{
1032 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1033 MatchFields: []*ofp.OfpOxmOfbField{
1034 flows.InPort(nniPort),
1035 flows.EthType(2048),
1036 flows.IpProto(17),
1037 flows.UdpSrc(67),
1038 flows.UdpDst(68),
1039 },
1040 Actions: []*ofp.OfpAction{
1041 flows.Output(controllerPortMask),
1042 },
1043 }
1044 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1045 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1046 assert.Nil(t, err)
1047
1048 fa = &flows.FlowArgs{
1049 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1050 MatchFields: []*ofp.OfpOxmOfbField{
1051 flows.InPort(nniPort),
1052 flows.EthType(34525),
1053 flows.IpProto(17),
1054 flows.UdpSrc(546),
1055 flows.UdpDst(547),
1056 },
1057 Actions: []*ofp.OfpAction{
1058 flows.Output(controllerPortMask),
1059 },
1060 }
1061 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1062 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1063 assert.Nil(t, err)
1064
1065 return len(nniPorts), len(uniPorts)
1066}
1067
Kent Hagerman2b216042020-04-03 18:28:56 -04001068func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001069 maxInt32 := uint64(0xFFFFFFFF)
1070 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1071 fa := &flows.FlowArgs{
1072 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1, "write_metadata": createMetadata(vlan, 64, 0), "meter_id": meterID},
1073 MatchFields: []*ofp.OfpOxmOfbField{
1074 flows.InPort(port.PortNo),
1075 flows.EthType(34958),
1076 flows.VlanVid(8187),
1077 },
1078 Actions: []*ofp.OfpAction{
1079 flows.Output(controllerPortMask),
1080 },
1081 }
1082 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1083 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP)
1084 assert.Nil(t, err)
1085}
1086
khenaidoo8b4abbf2020-04-24 17:04:30 -04001087func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup, flowAddFail bool, flowDelete bool) {
khenaidoo67b22152020-03-02 16:01:25 -05001088 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001089
1090 // Clear any existing flows on the adapters
1091 nb.oltAdapter.ClearFlows()
1092 nb.onuAdapter.ClearFlows()
1093
khenaidoo8b4abbf2020-04-24 17:04:30 -04001094 // Set the adapter actions on flow addition/deletion
1095 nb.oltAdapter.SetFlowAction(flowAddFail, flowDelete)
1096 nb.onuAdapter.SetFlowAction(flowAddFail, flowDelete)
1097
khenaidoo67b22152020-03-02 16:01:25 -05001098 // Wait until a logical device is ready
1099 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1100 if lds == nil || len(lds.Items) != 1 {
1101 return false
1102 }
1103 // Ensure there are both NNI ports and at least one UNI port on the logical device
1104 ld := lds.Items[0]
1105 nniPort := false
1106 uniPort := false
1107 for _, p := range ld.Ports {
1108 nniPort = nniPort || p.RootPort == true
1109 uniPort = uniPort || p.RootPort == false
1110 if nniPort && uniPort {
1111 return true
1112 }
1113 }
1114 return false
1115 }
1116 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1117 assert.Nil(t, err)
1118
1119 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1120 assert.Nil(t, err)
1121 assert.NotNil(t, logicalDevices)
1122 assert.Equal(t, 1, len(logicalDevices.Items))
1123
1124 logicalDevice := logicalDevices.Items[0]
1125 meterID := rand.Uint32()
1126
1127 // Add a meter to the logical device
1128 meterMod := &ofp.OfpMeterMod{
1129 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1130 Flags: rand.Uint32(),
1131 MeterId: meterID,
1132 Bands: []*ofp.OfpMeterBandHeader{
1133 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1134 Rate: rand.Uint32(),
1135 BurstSize: rand.Uint32(),
1136 Data: nil,
1137 },
1138 },
1139 }
1140 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDevice.Id, MeterMod: meterMod})
1141 assert.Nil(t, err)
1142
1143 // Send initial set of Trap flows
1144 startingVlan := 4091
1145 nb.sendTrapFlows(t, nbi, logicalDevice, uint64(meterID), startingVlan)
1146
1147 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001148 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001149 processedNniLogicalPorts := 0
1150 processedUniLogicalPorts := 0
1151
Kent Hagerman45a13e42020-04-13 12:23:50 -04001152 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001153 startingVlan++
1154 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1155 ps := portStatus.PortStatus
1156 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001157 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001158 processedUniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001159 nb.sendEAPFlows(t, nbi, logicalDevice.Id, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001160 } else {
1161 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001162 }
1163 }
1164 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001165
1166 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001167 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001168 break
1169 }
1170 }
1171 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001172 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001173
khenaidoo8b4abbf2020-04-24 17:04:30 -04001174 // Wait until all flows have been sent to the OLT adapters (or all failed)
1175 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1176 if flowAddFail {
1177 expectedFlowCount = 0
1178 }
khenaidoo67b22152020-03-02 16:01:25 -05001179 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001180 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001181 }
1182 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1183 assert.Nil(t, err)
1184
khenaidoo8b4abbf2020-04-24 17:04:30 -04001185 // Wait until all flows have been sent to the ONU adapters (or all failed)
1186 expectedFlowCount = numUNIPorts
1187 if flowAddFail {
1188 expectedFlowCount = 0
1189 }
khenaidoo67b22152020-03-02 16:01:25 -05001190 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001191 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001192 }
1193 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1194 assert.Nil(t, err)
1195}
1196
khenaidoo8b4abbf2020-04-24 17:04:30 -04001197func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1198
1199 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1200 var wg sync.WaitGroup
1201 wg.Add(1)
1202 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, true)
1203
1204 // Create the device with valid data
1205 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1206 assert.Nil(t, err)
1207 assert.NotNil(t, oltDevice)
1208
1209 // Verify oltDevice exist in the core
1210 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1211 assert.Nil(t, err)
1212 assert.Equal(t, 1, len(devices.Items))
1213 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1214
1215 // Enable the oltDevice
1216 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1217 assert.Nil(t, err)
1218
1219 // Wait for the logical device to be in the ready state
1220 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
1221 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
1222 }
1223 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
1224 assert.Nil(t, err)
1225
1226 // Verify that the devices have been setup correctly
1227 nb.verifyDevices(t, nbi)
1228
1229 // Get latest oltDevice data
1230 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1231 assert.Nil(t, err)
1232
1233 // Verify that the logical device has been setup correctly
1234 nb.verifyLogicalDevices(t, oltDevice, nbi)
1235
1236 // Wait until all flows has been sent to the devices successfully
1237 wg.Wait()
1238}
1239
Matteo Scandolod525ae32020-04-02 17:27:29 -07001240func TestSuiteNbiApiHandler(t *testing.T) {
Kent Hagerman2b216042020-04-03 18:28:56 -04001241 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001242 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001243 logger.Fatalf("could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001244 }
1245 defer f.Close()
1246 runtime.SetBlockProfileRate(1)
1247 runtime.SetMutexProfileFraction(-1)
1248 if err := pprof.StartCPUProfile(f); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001249 logger.Fatalf("could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001250 }
1251 defer pprof.StopCPUProfile()
1252
khenaidoo442e7c72020-03-10 16:13:48 -04001253 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1254
khenaidoob64fc8a2019-11-27 15:08:19 -05001255 nb := newNBTest()
1256 assert.NotNil(t, nb)
1257
1258 defer nb.stopAll()
1259
1260 // Start the Core
1261 nb.startCore(false)
1262
1263 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001264 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001265
1266 // 1. Basic test with no data in Core
1267 nb.testCoreWithoutData(t, nbi)
1268
1269 // Create/register the adapters
Thomas Lee Se5a44012019-11-07 20:32:24 +05301270 nb.createAndregisterAdapters(t)
khenaidoob64fc8a2019-11-27 15:08:19 -05001271
1272 // 2. Test adapter registration
1273 nb.testAdapterRegistration(t, nbi)
1274
khenaidoo8b4abbf2020-04-24 17:04:30 -04001275 numberOfTestRuns := 2
1276 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001277 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001278 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001279
khenaidoo93d5a3d2020-01-15 12:37:05 -05001280 // 4. Test Enable a device
1281 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001282
khenaidoo93d5a3d2020-01-15 12:37:05 -05001283 // 5. Test disable and ReEnable a root device
1284 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001285
kesavandbc2d1622020-01-21 00:42:01 -05001286 // 6. Test disable and Enable pon port of OLT device
1287 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001288
Girish Gowdra408cd962020-03-11 14:31:31 -07001289 // 7.Test Device unreachable when OLT is enabled
1290 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1291
1292 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001293 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001294
Girish Gowdra408cd962020-03-11 14:31:31 -07001295 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001296 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001297
1298 // 10. Test omci test
1299 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001300
khenaidoo8b4abbf2020-04-24 17:04:30 -04001301 // 11. Test flow add failure
1302 nb.testFlowAddFailure(t, nbi)
1303
1304 // 12. Clean up
1305 nb.deleteAllDevices(t, nbi)
1306 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001307}