blob: 0c373ea01a19065e47cc6a5d39cbafe4ca2858e5 [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) {
khenaidoo0db4c812020-05-27 15:27:30 -0400601 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
602 if len(devices.Items) == 0 {
603 // Nothing to do
604 return
605 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400606 //Get an OLT device
607 oltDevice, err := nb.getADevice(true, nbi)
608 assert.Nil(t, err)
609 assert.NotNil(t, oltDevice)
610
611 // Delete the oltDevice
612 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
613 assert.Nil(t, err)
614
615 // Wait for all devices to be deleted
616 vFunction := func(devices *voltha.Devices) bool {
617 return devices != nil && len(devices.Items) == 0
618 }
619 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
620 assert.Nil(t, err)
621
622 // Wait for absence of logical device
623 vlFunction := func(lds *voltha.LogicalDevices) bool {
624 return lds != nil && len(lds.Items) == 0
625 }
626
627 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
628 assert.Nil(t, err)
629}
630
Kent Hagerman2b216042020-04-03 18:28:56 -0400631func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500632 //Create the device with valid data
633 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
634 assert.Nil(t, err)
635 assert.NotNil(t, oltDevice)
636
637 //Get an OLT device
638 oltDevice, err = nb.getADevice(true, nbi)
639 assert.Nil(t, err)
640 assert.NotNil(t, oltDevice)
641
642 // Enable the oltDevice
643 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
644 assert.Nil(t, err)
645
646 // Wait for the logical device to be in the ready state
647 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
648 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
649 }
650 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
651 assert.Nil(t, err)
652
653 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400654 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500655 assert.Nil(t, err)
656
657 // Wait for the all onu devices to be enabled
658 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
659 return device.AdminState == voltha.AdminState_ENABLED
660 }
661 for _, onu := range onuDevices.Items {
662 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
663 assert.Nil(t, err)
664 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500665 // Wait for each onu device to get deleted
666 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
667 return device == nil
668 }
669
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500670 // Delete the onuDevice
671 for _, onu := range onuDevices.Items {
672 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
673 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500674 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
675 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500676 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500677
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500678 // Disable the oltDevice
679 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
680 assert.Nil(t, err)
681
682 // Wait for the olt device to be disabled
683 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
684 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
685 }
686 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
687 assert.Nil(t, err)
688
689 // Delete the oltDevice
690 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
691 assert.Nil(t, err)
692
693 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
694 return devices != nil && len(devices.Items) == 0
695 }
696 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
697 assert.Nil(t, err)
698}
Kent Hagerman2b216042020-04-03 18:28:56 -0400699func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500700 //Get an OLT device
701 var cp *voltha.Port
702 oltDevice, err := nb.getADevice(true, nbi)
703 assert.Nil(t, err)
704 assert.NotNil(t, oltDevice)
705
706 for _, cp = range oltDevice.Ports {
707 if cp.Type == voltha.Port_PON_OLT {
708 break
709 }
710
711 }
712 assert.NotNil(t, cp)
713 cp.DeviceId = oltDevice.Id
714
715 // Disable the NW Port of oltDevice
716 _, err = nbi.DisablePort(getContext(), cp)
717 assert.Nil(t, err)
718 // Wait for the olt device Port to be disabled
719 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
720 for _, port := range device.Ports {
721 if port.PortNo == cp.PortNo {
722 return port.AdminState == voltha.AdminState_DISABLED
723 }
724 }
725 return false
726 }
727 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
728 assert.Nil(t, err)
729 // Wait for the logical device to satisfy the expected condition
730 var vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500731 if ld == nil {
732 return false
733 }
kesavandbc2d1622020-01-21 00:42:01 -0500734 for _, lp := range ld.Ports {
735 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
736 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
737 return false
738 }
739 }
740 return true
741 }
742 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
743 assert.Nil(t, err)
744
745 // Enable the NW Port of oltDevice
746 _, err = nbi.EnablePort(getContext(), cp)
747 assert.Nil(t, err)
748
749 // Wait for the olt device Port to be enabled
750 vdFunction = func(device *voltha.Device) bool {
751 for _, port := range device.Ports {
752 if port.PortNo == cp.PortNo {
753 return port.AdminState == voltha.AdminState_ENABLED
754 }
755 }
756 return false
757 }
758 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
759 assert.Nil(t, err)
760 // Wait for the logical device to satisfy the expected condition
761 vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500762 if ld == nil {
763 return false
764 }
kesavandbc2d1622020-01-21 00:42:01 -0500765 for _, lp := range ld.Ports {
766 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
767 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
768 return false
769 }
770 }
771 return true
772 }
773 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
774 assert.Nil(t, err)
775
776 // Disable a non-PON port
777 for _, cp = range oltDevice.Ports {
778 if cp.Type != voltha.Port_PON_OLT {
779 break
780 }
781
782 }
783 assert.NotNil(t, cp)
784 cp.DeviceId = oltDevice.Id
785
786 // Disable the NW Port of oltDevice
787 _, err = nbi.DisablePort(getContext(), cp)
788 assert.NotNil(t, err)
789
790}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500791
Kent Hagerman2b216042020-04-03 18:28:56 -0400792func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700793 //Get an OLT device
794 oltDevice, err := nb.getADevice(true, nbi)
795 assert.Nil(t, err)
796 assert.NotNil(t, oltDevice)
797 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
798 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
799
800 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400801 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700802 assert.Nil(t, err)
803 assert.NotNil(t, onuDevices)
804 assert.Greater(t, len(onuDevices.Items), 0)
805
806 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
807 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
808 assert.Nil(t, err)
809
810 var vlFunction0 = func(d *voltha.Device) bool {
811 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
812 }
813
814 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
815 assert.Nil(t, err)
816
817 // Wait for the logical device to satisfy the expected condition
818 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
819 return ld == nil
820 }
821
822 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
823 assert.Nil(t, err)
824
825 // Wait for the device to satisfy the expected condition (device does not have flows)
826 var vlFunction2 = func(d *voltha.Device) bool {
827 var deviceFlows *ofp.Flows
828 var err error
829 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
830 return false
831 }
832 return len(deviceFlows.Items) == 0
833 }
834
835 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
836 assert.Nil(t, err)
837
838 // Wait for the device to satisfy the expected condition (there are no child devices)
839 var vlFunction3 = func(d *voltha.Device) bool {
840 var devices *voltha.Devices
841 var err error
842 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
843 return false
844 }
845 for _, device := range devices.Items {
846 if device.ParentId == d.Id {
847 // We have a child device still left
848 return false
849 }
850 }
851 return true
852 }
853
854 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
855 assert.Nil(t, err)
856
857 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
858 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400859 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700860 assert.Nil(t, err)
861
862 // Verify the device connection and operation states
863 oltDevice, err = nb.getADevice(true, nbi)
864 assert.Nil(t, err)
865 assert.NotNil(t, oltDevice)
866 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
867 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
868
869 // Wait for the logical device to satisfy the expected condition
870 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
871 return ld != nil
872 }
873 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
874 assert.Nil(t, err)
875
876 // Verify that logical device is created again
877 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
878 assert.Nil(t, err)
879 assert.NotNil(t, logicalDevices)
880 assert.Equal(t, 1, len(logicalDevices.Items))
881
882 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400883 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700884 assert.Nil(t, err)
885 assert.NotNil(t, onuDevices)
886 assert.Equal(t, 0, len(onuDevices.Items))
887}
888
Kent Hagerman2b216042020-04-03 18:28:56 -0400889func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700890 // -----------------------------------------------------------------------
891 // SubTest 1: Omci test action should fail due to nonexistent device id
892
893 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
894 _, err := nbi.StartOmciTestAction(getContext(), request)
895 assert.NotNil(t, err)
896 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
897
898 // -----------------------------------------------------------------------
899 // SubTest 2: Error should be returned for device with no adapter registered
900
901 // Create a device that has no adapter registered
902 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
903 assert.Nil(t, err)
904 assert.NotNil(t, deviceNoAdapter)
905
906 // Omci test action should fail due to nonexistent adapter
907 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
908 _, err = nbi.StartOmciTestAction(getContext(), request)
909 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400910 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700911
912 //Remove the device
913 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
914 assert.Nil(t, err)
915
916 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
917 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
918 return devices != nil && len(devices.Items) == 0
919 }
920 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
921 assert.Nil(t, err)
922
923 // -----------------------------------------------------------------------
924 // SubTest 3: Omci test action should succeed on valid ONU
925
926 // Create the device with valid data
927 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
928 assert.Nil(t, err)
929 assert.NotNil(t, oltDevice)
930
931 // Verify oltDevice exist in the core
932 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
933 assert.Nil(t, err)
934 assert.Equal(t, 1, len(devices.Items))
935 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
936
937 // Enable the oltDevice
938 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
939 assert.Nil(t, err)
940
941 // Wait for the logical device to be in the ready state
942 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
943 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
944 }
945 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
946 assert.Nil(t, err)
947
948 // Wait for the olt device to be enabled
949 vdFunction := func(device *voltha.Device) bool {
950 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
951 }
952 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
953 assert.Nil(t, err)
954
Kent Hagerman2b216042020-04-03 18:28:56 -0400955 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700956 assert.Nil(t, err)
957 assert.Greater(t, len(onuDevices.Items), 0)
958
959 onuDevice := onuDevices.Items[0]
960
961 // Omci test action should succeed
962 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
963 resp, err := nbi.StartOmciTestAction(getContext(), request)
964 assert.Nil(t, err)
965 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
966
967 //Remove the device
968 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
969 assert.Nil(t, err)
970 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
971 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
972 assert.Nil(t, err)
973}
974
khenaidoo67b22152020-03-02 16:01:25 -0500975func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
976 matchFields := make([]*ofp.OfpOxmField, 0)
977 for _, val := range fa.MatchFields {
978 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
979 }
980 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
981}
982
983func createMetadata(cTag int, techProfile int, port int) uint64 {
984 md := 0
985 md = (md | (cTag & 0xFFFF)) << 16
986 md = (md | (techProfile & 0xFFFF)) << 32
987 return uint64(md | (port & 0xFFFFFFFF))
988}
989
khenaidoo8b4abbf2020-04-24 17:04:30 -0400990func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500991 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400992 if flowAddFail {
993 expectedNumFlows = 0
994 }
995 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500996 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700997 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
998 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500999 }
1000 // No timeout implies a success
1001 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1002 assert.Nil(t, err)
1003}
1004
Kent Hagerman2b216042020-04-03 18:28:56 -04001005func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDevice *voltha.LogicalDevice, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -05001006 // Send flows for the parent device
1007 var nniPorts []*voltha.LogicalPort
1008 var uniPorts []*voltha.LogicalPort
1009 for _, p := range logicalDevice.Ports {
1010 if p.RootPort {
1011 nniPorts = append(nniPorts, p)
1012 } else {
1013 uniPorts = append(uniPorts, p)
1014 }
1015 }
1016 assert.Equal(t, 1, len(nniPorts))
1017 //assert.Greater(t, len(uniPorts), 1 )
1018 nniPort := nniPorts[0].OfpPort.PortNo
1019 maxInt32 := uint64(0xFFFFFFFF)
1020 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1021 var fa *flows.FlowArgs
1022 fa = &flows.FlowArgs{
1023 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1024 MatchFields: []*ofp.OfpOxmOfbField{
1025 flows.InPort(nniPort),
1026 flows.EthType(35020),
1027 },
1028 Actions: []*ofp.OfpAction{
1029 flows.Output(controllerPortMask),
1030 },
1031 }
1032 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1033 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1034 assert.Nil(t, err)
1035
1036 fa = &flows.FlowArgs{
1037 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1038 MatchFields: []*ofp.OfpOxmOfbField{
1039 flows.InPort(nniPort),
1040 flows.EthType(2048),
1041 flows.IpProto(17),
1042 flows.UdpSrc(67),
1043 flows.UdpDst(68),
1044 },
1045 Actions: []*ofp.OfpAction{
1046 flows.Output(controllerPortMask),
1047 },
1048 }
1049 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1050 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1051 assert.Nil(t, err)
1052
1053 fa = &flows.FlowArgs{
1054 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1055 MatchFields: []*ofp.OfpOxmOfbField{
1056 flows.InPort(nniPort),
1057 flows.EthType(34525),
1058 flows.IpProto(17),
1059 flows.UdpSrc(546),
1060 flows.UdpDst(547),
1061 },
1062 Actions: []*ofp.OfpAction{
1063 flows.Output(controllerPortMask),
1064 },
1065 }
1066 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1067 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1068 assert.Nil(t, err)
1069
1070 return len(nniPorts), len(uniPorts)
1071}
1072
Kent Hagerman2b216042020-04-03 18:28:56 -04001073func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001074 maxInt32 := uint64(0xFFFFFFFF)
1075 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1076 fa := &flows.FlowArgs{
1077 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},
1078 MatchFields: []*ofp.OfpOxmOfbField{
1079 flows.InPort(port.PortNo),
1080 flows.EthType(34958),
1081 flows.VlanVid(8187),
1082 },
1083 Actions: []*ofp.OfpAction{
1084 flows.Output(controllerPortMask),
1085 },
1086 }
1087 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001088 maxTries := 3
1089 var err error
1090 for {
1091 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1092 if maxTries < 3 {
1093 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1094 }
1095 break
1096 }
1097 t.Log("Sending EAPOL flows fail:", err)
1098 time.Sleep(50 * time.Millisecond)
1099 maxTries--
1100 if maxTries == 0 {
1101 break
1102 }
1103 }
khenaidoo67b22152020-03-02 16:01:25 -05001104 assert.Nil(t, err)
1105}
1106
khenaidoo0db4c812020-05-27 15:27:30 -04001107func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup, flowAddFail bool, flowDeleteFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -05001108 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001109
1110 // Clear any existing flows on the adapters
1111 nb.oltAdapter.ClearFlows()
1112 nb.onuAdapter.ClearFlows()
1113
khenaidoo8b4abbf2020-04-24 17:04:30 -04001114 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001115 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1116 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001117
khenaidoo67b22152020-03-02 16:01:25 -05001118 // Wait until a logical device is ready
1119 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1120 if lds == nil || len(lds.Items) != 1 {
1121 return false
1122 }
1123 // Ensure there are both NNI ports and at least one UNI port on the logical device
1124 ld := lds.Items[0]
1125 nniPort := false
1126 uniPort := false
1127 for _, p := range ld.Ports {
1128 nniPort = nniPort || p.RootPort == true
1129 uniPort = uniPort || p.RootPort == false
1130 if nniPort && uniPort {
1131 return true
1132 }
1133 }
1134 return false
1135 }
1136 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1137 assert.Nil(t, err)
1138
1139 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1140 assert.Nil(t, err)
1141 assert.NotNil(t, logicalDevices)
1142 assert.Equal(t, 1, len(logicalDevices.Items))
1143
1144 logicalDevice := logicalDevices.Items[0]
1145 meterID := rand.Uint32()
1146
1147 // Add a meter to the logical device
1148 meterMod := &ofp.OfpMeterMod{
1149 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1150 Flags: rand.Uint32(),
1151 MeterId: meterID,
1152 Bands: []*ofp.OfpMeterBandHeader{
1153 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1154 Rate: rand.Uint32(),
1155 BurstSize: rand.Uint32(),
1156 Data: nil,
1157 },
1158 },
1159 }
1160 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDevice.Id, MeterMod: meterMod})
1161 assert.Nil(t, err)
1162
1163 // Send initial set of Trap flows
1164 startingVlan := 4091
1165 nb.sendTrapFlows(t, nbi, logicalDevice, uint64(meterID), startingVlan)
1166
1167 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001168 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001169 processedNniLogicalPorts := 0
1170 processedUniLogicalPorts := 0
1171
Kent Hagerman45a13e42020-04-13 12:23:50 -04001172 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001173 startingVlan++
1174 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1175 ps := portStatus.PortStatus
1176 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001177 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001178 processedUniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001179 nb.sendEAPFlows(t, nbi, logicalDevice.Id, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001180 } else {
1181 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001182 }
1183 }
1184 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001185
1186 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001187 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001188 break
1189 }
1190 }
1191 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001192 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001193
khenaidoo8b4abbf2020-04-24 17:04:30 -04001194 // Wait until all flows have been sent to the OLT adapters (or all failed)
1195 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1196 if flowAddFail {
1197 expectedFlowCount = 0
1198 }
khenaidoo67b22152020-03-02 16:01:25 -05001199 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001200 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001201 }
1202 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1203 assert.Nil(t, err)
1204
khenaidoo8b4abbf2020-04-24 17:04:30 -04001205 // Wait until all flows have been sent to the ONU adapters (or all failed)
1206 expectedFlowCount = numUNIPorts
1207 if flowAddFail {
1208 expectedFlowCount = 0
1209 }
khenaidoo67b22152020-03-02 16:01:25 -05001210 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001211 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001212 }
1213 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1214 assert.Nil(t, err)
1215}
1216
khenaidoo8b4abbf2020-04-24 17:04:30 -04001217func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1218
1219 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1220 var wg sync.WaitGroup
1221 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001222 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001223
1224 // Create the device with valid data
1225 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1226 assert.Nil(t, err)
1227 assert.NotNil(t, oltDevice)
1228
1229 // Verify oltDevice exist in the core
1230 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1231 assert.Nil(t, err)
1232 assert.Equal(t, 1, len(devices.Items))
1233 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1234
1235 // Enable the oltDevice
1236 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1237 assert.Nil(t, err)
1238
1239 // Wait for the logical device to be in the ready state
1240 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
1241 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
1242 }
1243 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
1244 assert.Nil(t, err)
1245
1246 // Verify that the devices have been setup correctly
1247 nb.verifyDevices(t, nbi)
1248
1249 // Get latest oltDevice data
1250 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1251 assert.Nil(t, err)
1252
1253 // Verify that the logical device has been setup correctly
1254 nb.verifyLogicalDevices(t, oltDevice, nbi)
1255
1256 // Wait until all flows has been sent to the devices successfully
1257 wg.Wait()
1258}
1259
Matteo Scandolod525ae32020-04-02 17:27:29 -07001260func TestSuiteNbiApiHandler(t *testing.T) {
Kent Hagerman2b216042020-04-03 18:28:56 -04001261 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001262 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001263 logger.Fatalf("could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001264 }
1265 defer f.Close()
1266 runtime.SetBlockProfileRate(1)
1267 runtime.SetMutexProfileFraction(-1)
1268 if err := pprof.StartCPUProfile(f); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001269 logger.Fatalf("could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001270 }
1271 defer pprof.StopCPUProfile()
1272
khenaidoo442e7c72020-03-10 16:13:48 -04001273 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1274
khenaidoob64fc8a2019-11-27 15:08:19 -05001275 nb := newNBTest()
1276 assert.NotNil(t, nb)
1277
1278 defer nb.stopAll()
1279
1280 // Start the Core
1281 nb.startCore(false)
1282
1283 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001284 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001285
1286 // 1. Basic test with no data in Core
1287 nb.testCoreWithoutData(t, nbi)
1288
1289 // Create/register the adapters
Thomas Lee Se5a44012019-11-07 20:32:24 +05301290 nb.createAndregisterAdapters(t)
khenaidoob64fc8a2019-11-27 15:08:19 -05001291
1292 // 2. Test adapter registration
1293 nb.testAdapterRegistration(t, nbi)
1294
khenaidoo8b4abbf2020-04-24 17:04:30 -04001295 numberOfTestRuns := 2
1296 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001297 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001298 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001299
khenaidoo93d5a3d2020-01-15 12:37:05 -05001300 // 4. Test Enable a device
1301 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001302
khenaidoo0db4c812020-05-27 15:27:30 -04001303 //// 5. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001304 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001305
kesavandbc2d1622020-01-21 00:42:01 -05001306 // 6. Test disable and Enable pon port of OLT device
1307 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001308
Girish Gowdra408cd962020-03-11 14:31:31 -07001309 // 7.Test Device unreachable when OLT is enabled
1310 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1311
1312 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001313 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001314
Girish Gowdra408cd962020-03-11 14:31:31 -07001315 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001316 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001317
1318 // 10. Test omci test
1319 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001320
khenaidoo0db4c812020-05-27 15:27:30 -04001321 // 11. Remove all devices from tests above
1322 nb.deleteAllDevices(t, nbi)
1323
khenaidoo8b4abbf2020-04-24 17:04:30 -04001324 // 11. Test flow add failure
1325 nb.testFlowAddFailure(t, nbi)
1326
1327 // 12. Clean up
1328 nb.deleteAllDevices(t, nbi)
1329 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001330}