blob: 434df90688f8769ae73812f0f39fe52a280f9a1c [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"
Neha Sharmad1387da2020-05-07 20:07:28 +000027 "strconv"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080028 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050029 "sync"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "testing"
31 "time"
32
khenaidoob64fc8a2019-11-27 15:08:19 -050033 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040034 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050035 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040036 "github.com/opencord/voltha-go/rw_core/core/adapter"
37 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050038 cm "github.com/opencord/voltha-go/rw_core/mocks"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070039 tst "github.com/opencord/voltha-go/rw_core/test"
Kent Hagerman2b216042020-04-03 18:28:56 -040040 "github.com/opencord/voltha-lib-go/v3/pkg/db"
41 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080042 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
Matteo Scandolod525ae32020-04-02 17:27:29 -070043 mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
44 mock_kafka "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080045 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
53type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070054 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040055 deviceMgr *device.Manager
56 logicalDeviceMgr *device.LogicalManager
57 adapterMgr *adapter.Manager
58 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050059 kClient kafka.Client
60 kvClientPort int
61 numONUPerOLT int
62 startingUNIPortNo int
63 oltAdapter *cm.OLTAdapter
64 onuAdapter *cm.ONUAdapter
65 oltAdapterName string
66 onuAdapterName string
67 coreInstanceID string
68 defaultTimeout time.Duration
69 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050070}
71
Rohan Agrawal31f21802020-06-12 05:38:46 +000072func newNBTest(ctx context.Context) *NBTest {
khenaidoob64fc8a2019-11-27 15:08:19 -050073 test := &NBTest{}
74 // Start the embedded etcd server
75 var err error
Rohan Agrawal31f21802020-06-12 05:38:46 +000076 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050077 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000078 logger.Fatal(ctx, err)
khenaidoob64fc8a2019-11-27 15:08:19 -050079 }
80 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070081 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050082 test.oltAdapterName = "olt_adapter_mock"
83 test.onuAdapterName = "onu_adapter_mock"
84 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050085 test.defaultTimeout = 10 * time.Second
86 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050087 return test
88}
89
90func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053091 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
92 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050093 cfg := config.NewRWCoreFlags()
serkant.uluderya8ff291d2020-05-20 00:58:00 -070094 cfg.CoreTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040095 cfg.DefaultRequestTimeout = nb.defaultTimeout
96 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +000097 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -050098 grpcPort, err := freeport.GetFreePort()
99 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000100 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500101 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000102 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500103 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000104 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400105 backend := &db.Backend{
106 Client: client,
107 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000108 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400109 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700110 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000112 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400113 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700114 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400115
116 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400117 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700119 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400120 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400121
Rohan Agrawal31f21802020-06-12 05:38:46 +0000122 if err := nb.kmp.Start(ctx); err != nil {
123 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400124 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400125 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000126 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
127 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400128 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500129}
130
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500132 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000133 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500134 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400135 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500137 }
138 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500140 }
141}
142
Kent Hagerman2b216042020-04-03 18:28:56 -0400143func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500144 // Get the latest set of logical devices
145 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
146 assert.Nil(t, err)
147 assert.NotNil(t, logicalDevices)
148 assert.Equal(t, 1, len(logicalDevices.Items))
149
150 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400151 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
152 assert.Nil(t, err)
153
khenaidoob64fc8a2019-11-27 15:08:19 -0500154 assert.NotEqual(t, "", ld.Id)
155 assert.NotEqual(t, uint64(0), ld.DatapathId)
156 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
157 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
158 assert.NotEqual(t, "", ld.RootDeviceId)
159 assert.NotEqual(t, "", ld.Desc.SerialNum)
160 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
161 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
162 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400163 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500164 assert.Equal(t, oltDevice.ParentId, ld.Id)
165 //Expected port no
166 expectedPortNo := make(map[uint32]bool)
167 expectedPortNo[uint32(2)] = false
168 for i := 0; i < nb.numONUPerOLT; i++ {
169 expectedPortNo[uint32(i+100)] = false
170 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400171 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500172 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
173 assert.Equal(t, uint32(4), p.OfpPort.State)
174 expectedPortNo[p.OfpPort.PortNo] = true
175 if strings.HasPrefix(p.Id, "nni") {
176 assert.Equal(t, true, p.RootPort)
177 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
178 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
179 } else {
180 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
181 assert.Equal(t, false, p.RootPort)
182 }
183 }
184}
185
Kent Hagerman2b216042020-04-03 18:28:56 -0400186func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500187 // Get the latest set of devices
188 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
189 assert.Nil(t, err)
190 assert.NotNil(t, devices)
191
khenaidoo67b22152020-03-02 16:01:25 -0500192 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500193 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
194 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
195 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500196
khenaidoo67b22152020-03-02 16:01:25 -0500197 var wg sync.WaitGroup
198 for _, device := range devices.Items {
199 wg.Add(1)
200 go func(wg *sync.WaitGroup, device *voltha.Device) {
201 // Wait until the device is in the right state
202 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
203 assert.Nil(t, err)
204
205 // Now, verify the details of the device. First get the latest update
206 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
207 assert.Nil(t, err)
208 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
209 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
210 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
211 assert.Equal(t, d.Type, d.Adapter)
212 assert.NotEqual(t, "", d.MacAddress)
213 assert.NotEqual(t, "", d.SerialNumber)
214
215 if d.Type == "olt_adapter_mock" {
216 assert.Equal(t, true, d.Root)
217 assert.NotEqual(t, "", d.Id)
218 assert.NotEqual(t, "", d.ParentId)
219 assert.Nil(t, d.ProxyAddress)
220 } else if d.Type == "onu_adapter_mock" {
221 assert.Equal(t, false, d.Root)
222 assert.NotEqual(t, uint32(0), d.Vlan)
223 assert.NotEqual(t, "", d.Id)
224 assert.NotEqual(t, "", d.ParentId)
225 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
226 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500227 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500228 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500229 }
khenaidoo67b22152020-03-02 16:01:25 -0500230 assert.Equal(t, 2, len(d.Ports))
231 for _, p := range d.Ports {
232 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
233 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
234 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
235 assert.Equal(t, 0, len(p.Peers))
236 } else if p.Type == voltha.Port_PON_OLT {
237 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
238 assert.Equal(t, uint32(1), p.PortNo)
239 } else if p.Type == voltha.Port_PON_ONU {
240 assert.Equal(t, 1, len(p.Peers))
241 assert.Equal(t, uint32(1), p.PortNo)
242 } else {
243 assert.Error(t, errors.New("invalid-port"))
244 }
245 }
246 wg.Done()
247 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500248 }
khenaidoo67b22152020-03-02 16:01:25 -0500249 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500250}
251
Kent Hagerman2b216042020-04-03 18:28:56 -0400252func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500253 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
254 if err != nil {
255 return nil, err
256 }
257 for _, d := range devices.Items {
258 if d.Root == rootDevice {
259 return d, nil
260 }
261 }
262 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
263}
264
Kent Hagerman2b216042020-04-03 18:28:56 -0400265func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500266 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
267 assert.Nil(t, err)
268 assert.NotNil(t, lds)
269 assert.Equal(t, 0, len(lds.Items))
270 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
271 assert.Nil(t, err)
272 assert.NotNil(t, devices)
273 assert.Equal(t, 0, len(devices.Items))
274 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400275 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500276 assert.Nil(t, err)
277 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500278}
279
Kent Hagerman2b216042020-04-03 18:28:56 -0400280func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000281 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500282 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
283 assert.Nil(t, err)
284 assert.NotNil(t, adapters)
285 assert.Equal(t, 2, len(adapters.Items))
286 for _, a := range adapters.Items {
287 switch a.Id {
288 case nb.oltAdapterName:
289 assert.Equal(t, "Voltha-olt", a.Vendor)
290 case nb.onuAdapterName:
291 assert.Equal(t, "Voltha-onu", a.Vendor)
292 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000293 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500294 }
295 }
296 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
297 assert.Nil(t, err)
298 assert.NotNil(t, deviceTypes)
299 assert.Equal(t, 2, len(deviceTypes.Items))
300 for _, dt := range deviceTypes.Items {
301 switch dt.Id {
302 case nb.oltAdapterName:
303 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
304 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
305 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
306 case nb.onuAdapterName:
307 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
308 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
309 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
310 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000311 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500312 }
313 }
314}
315
Kent Hagerman2b216042020-04-03 18:28:56 -0400316func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500317 // Create a valid device
318 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
319 assert.Nil(t, err)
320 assert.NotNil(t, oltDevice)
321 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
322 assert.Nil(t, err)
323 assert.NotNil(t, device)
324 assert.Equal(t, oltDevice.String(), device.String())
325
326 // Try to create the same device
327 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
328 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400329 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500330
331 // Try to create a device with invalid data
332 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
333 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530334 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500335
336 // Ensure we only have 1 device in the Core
337 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
338 assert.Nil(t, err)
339 assert.NotNil(t, devices)
340 assert.Equal(t, 1, len(devices.Items))
341 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
342
343 //Remove the device
344 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
345 assert.Nil(t, err)
346
347 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
348 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
349 return devices != nil && len(devices.Items) == 0
350 }
khenaidoo442e7c72020-03-10 16:13:48 -0400351 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500352 assert.Nil(t, err)
353}
354
Kent Hagerman2b216042020-04-03 18:28:56 -0400355func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500356 // Create a device that has no adapter registered
357 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
358 assert.Nil(t, err)
359 assert.NotNil(t, oltDeviceNoAdapter)
360
361 // Try to enable the oltDevice and check the error message
362 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
363 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400364 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500365
366 //Remove the device
367 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
368 assert.Nil(t, err)
369
370 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
371 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
372 return devices != nil && len(devices.Items) == 0
373 }
khenaidoo442e7c72020-03-10 16:13:48 -0400374 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500375 assert.Nil(t, err)
376
khenaidoo67b22152020-03-02 16:01:25 -0500377 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
378 var wg sync.WaitGroup
379 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400380 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500381
khenaidoob64fc8a2019-11-27 15:08:19 -0500382 // Create the device with valid data
383 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
384 assert.Nil(t, err)
385 assert.NotNil(t, oltDevice)
386
387 // Verify oltDevice exist in the core
388 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
389 assert.Nil(t, err)
390 assert.Equal(t, 1, len(devices.Items))
391 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
392
393 // Enable the oltDevice
394 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
395 assert.Nil(t, err)
396
397 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400398 var vldFunction = func(ports []*voltha.LogicalPort) bool {
399 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500400 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400401 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500402 assert.Nil(t, err)
403
404 // Verify that the devices have been setup correctly
405 nb.verifyDevices(t, nbi)
406
407 // Get latest oltDevice data
408 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
409 assert.Nil(t, err)
410
411 // Verify that the logical device has been setup correctly
412 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500413
414 // Wait until all flows has been sent to the devices successfully
415 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500416}
417
Kent Hagerman2b216042020-04-03 18:28:56 -0400418func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500419 //Get an OLT device
420 oltDevice, err := nb.getADevice(true, nbi)
421 assert.Nil(t, err)
422 assert.NotNil(t, oltDevice)
423
424 // Disable the oltDevice
425 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
426 assert.Nil(t, err)
427
428 // Wait for the old device to be disabled
429 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
430 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
431 }
432 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
433 assert.Nil(t, err)
434
435 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400436 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500437 assert.Nil(t, err)
438 for _, onu := range onuDevices.Items {
439 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
440 assert.Nil(t, err)
441 }
442
443 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400444 var vlFunction = func(ports []*voltha.LogicalPort) bool {
445 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500446 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
447 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
448 return false
449 }
450 }
451 return true
452 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400453 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500454 assert.Nil(t, err)
455
456 // Reenable the oltDevice
457 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
458 assert.Nil(t, err)
459
460 // Wait for the old device to be enabled
461 vdFunction = func(device *voltha.Device) bool {
462 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
463 }
464 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
465 assert.Nil(t, err)
466
467 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400468 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500469 assert.Nil(t, err)
470 for _, onu := range onuDevices.Items {
471 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
472 assert.Nil(t, err)
473 }
474
475 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400476 vlFunction = func(ports []*voltha.LogicalPort) bool {
477 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500478 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
479 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
480 return false
481 }
482 }
483 return true
484 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400485 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500486 assert.Nil(t, err)
487}
488
Kent Hagerman2b216042020-04-03 18:28:56 -0400489func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500490 //Get an OLT device
491 oltDevice, err := nb.getADevice(true, nbi)
492 assert.Nil(t, err)
493 assert.NotNil(t, oltDevice)
494
495 // Disable the oltDevice
496 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
497 assert.Nil(t, err)
498
499 // Wait for the olt device to be disabled
500 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
501 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
502 }
503 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
504 assert.Nil(t, err)
505
506 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400507 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500508 assert.Nil(t, err)
509 for _, onu := range onuDevices.Items {
510 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
511 assert.Nil(t, err)
512 }
513
514 // Delete the oltDevice
515 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
516 assert.Nil(t, err)
517
518 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
519 return devices != nil && len(devices.Items) == 0
520 }
521 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
522 assert.Nil(t, err)
523
524 // Wait for absence of logical device
525 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
526 return lds != nil && len(lds.Items) == 0
527 }
528
529 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
530 assert.Nil(t, err)
531}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400532
533func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400534 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
535 if len(devices.Items) == 0 {
536 // Nothing to do
537 return
538 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400539 //Get an OLT device
540 oltDevice, err := nb.getADevice(true, nbi)
541 assert.Nil(t, err)
542 assert.NotNil(t, oltDevice)
543
544 // Delete the oltDevice
545 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
546 assert.Nil(t, err)
547
548 // Wait for all devices to be deleted
549 vFunction := func(devices *voltha.Devices) bool {
550 return devices != nil && len(devices.Items) == 0
551 }
552 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
553 assert.Nil(t, err)
554
555 // Wait for absence of logical device
556 vlFunction := func(lds *voltha.LogicalDevices) bool {
557 return lds != nil && len(lds.Items) == 0
558 }
559
560 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
561 assert.Nil(t, err)
562}
563
Kent Hagerman2b216042020-04-03 18:28:56 -0400564func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500565 //Create the device with valid data
566 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
567 assert.Nil(t, err)
568 assert.NotNil(t, oltDevice)
569
570 //Get an OLT device
571 oltDevice, err = nb.getADevice(true, nbi)
572 assert.Nil(t, err)
573 assert.NotNil(t, oltDevice)
574
575 // Enable the oltDevice
576 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
577 assert.Nil(t, err)
578
579 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400580 var vldFunction = func(ports []*voltha.LogicalPort) bool {
581 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500582 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400583 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500584 assert.Nil(t, err)
585
586 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400587 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500588 assert.Nil(t, err)
589
590 // Wait for the all onu devices to be enabled
591 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
592 return device.AdminState == voltha.AdminState_ENABLED
593 }
594 for _, onu := range onuDevices.Items {
595 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
596 assert.Nil(t, err)
597 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500598 // Wait for each onu device to get deleted
599 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
600 return device == nil
601 }
602
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500603 // Delete the onuDevice
604 for _, onu := range onuDevices.Items {
605 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
606 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500607 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
608 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500609 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500610
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500611 // Disable the oltDevice
612 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
613 assert.Nil(t, err)
614
615 // Wait for the olt device to be disabled
616 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
617 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
618 }
619 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
620 assert.Nil(t, err)
621
622 // Delete the oltDevice
623 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
624 assert.Nil(t, err)
625
626 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
627 return devices != nil && len(devices.Items) == 0
628 }
629 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
630 assert.Nil(t, err)
631}
Kent Hagerman2b216042020-04-03 18:28:56 -0400632func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500633 //Get an OLT device
634 var cp *voltha.Port
635 oltDevice, err := nb.getADevice(true, nbi)
636 assert.Nil(t, err)
637 assert.NotNil(t, oltDevice)
638
639 for _, cp = range oltDevice.Ports {
640 if cp.Type == voltha.Port_PON_OLT {
641 break
642 }
643
644 }
645 assert.NotNil(t, cp)
646 cp.DeviceId = oltDevice.Id
647
648 // Disable the NW Port of oltDevice
649 _, err = nbi.DisablePort(getContext(), cp)
650 assert.Nil(t, err)
651 // Wait for the olt device Port to be disabled
652 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
653 for _, port := range device.Ports {
654 if port.PortNo == cp.PortNo {
655 return port.AdminState == voltha.AdminState_DISABLED
656 }
657 }
658 return false
659 }
660 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
661 assert.Nil(t, err)
662 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400663 var vlFunction = func(ports []*voltha.LogicalPort) bool {
664 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500665 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
666 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
667 return false
668 }
669 }
670 return true
671 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400672 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500673 assert.Nil(t, err)
674
675 // Enable the NW Port of oltDevice
676 _, err = nbi.EnablePort(getContext(), cp)
677 assert.Nil(t, err)
678
679 // Wait for the olt device Port to be enabled
680 vdFunction = func(device *voltha.Device) bool {
681 for _, port := range device.Ports {
682 if port.PortNo == cp.PortNo {
683 return port.AdminState == voltha.AdminState_ENABLED
684 }
685 }
686 return false
687 }
688 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
689 assert.Nil(t, err)
690 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400691 vlFunction = func(ports []*voltha.LogicalPort) bool {
692 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500693 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
694 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
695 return false
696 }
697 }
698 return true
699 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400700 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500701 assert.Nil(t, err)
702
703 // Disable a non-PON port
704 for _, cp = range oltDevice.Ports {
705 if cp.Type != voltha.Port_PON_OLT {
706 break
707 }
708
709 }
710 assert.NotNil(t, cp)
711 cp.DeviceId = oltDevice.Id
712
713 // Disable the NW Port of oltDevice
714 _, err = nbi.DisablePort(getContext(), cp)
715 assert.NotNil(t, err)
716
717}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500718
Kent Hagerman2b216042020-04-03 18:28:56 -0400719func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700720 //Get an OLT device
721 oltDevice, err := nb.getADevice(true, nbi)
722 assert.Nil(t, err)
723 assert.NotNil(t, oltDevice)
724 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
725 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
726
727 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400728 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700729 assert.Nil(t, err)
730 assert.NotNil(t, onuDevices)
731 assert.Greater(t, len(onuDevices.Items), 0)
732
733 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
734 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
735 assert.Nil(t, err)
736
737 var vlFunction0 = func(d *voltha.Device) bool {
738 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
739 }
740
741 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
742 assert.Nil(t, err)
743
744 // Wait for the logical device to satisfy the expected condition
745 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
746 return ld == nil
747 }
748
749 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
750 assert.Nil(t, err)
751
752 // Wait for the device to satisfy the expected condition (device does not have flows)
753 var vlFunction2 = func(d *voltha.Device) bool {
754 var deviceFlows *ofp.Flows
755 var err error
756 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
757 return false
758 }
759 return len(deviceFlows.Items) == 0
760 }
761
762 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
763 assert.Nil(t, err)
764
765 // Wait for the device to satisfy the expected condition (there are no child devices)
766 var vlFunction3 = func(d *voltha.Device) bool {
767 var devices *voltha.Devices
768 var err error
769 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
770 return false
771 }
772 for _, device := range devices.Items {
773 if device.ParentId == d.Id {
774 // We have a child device still left
775 return false
776 }
777 }
778 return true
779 }
780
781 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
782 assert.Nil(t, err)
783
784 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
785 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400786 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700787 assert.Nil(t, err)
788
789 // Verify the device connection and operation states
790 oltDevice, err = nb.getADevice(true, nbi)
791 assert.Nil(t, err)
792 assert.NotNil(t, oltDevice)
793 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
794 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
795
796 // Wait for the logical device to satisfy the expected condition
797 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
798 return ld != nil
799 }
800 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
801 assert.Nil(t, err)
802
803 // Verify that logical device is created again
804 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
805 assert.Nil(t, err)
806 assert.NotNil(t, logicalDevices)
807 assert.Equal(t, 1, len(logicalDevices.Items))
808
809 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400810 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700811 assert.Nil(t, err)
812 assert.NotNil(t, onuDevices)
813 assert.Equal(t, 0, len(onuDevices.Items))
814}
815
Kent Hagerman2b216042020-04-03 18:28:56 -0400816func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700817 // -----------------------------------------------------------------------
818 // SubTest 1: Omci test action should fail due to nonexistent device id
819
820 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
821 _, err := nbi.StartOmciTestAction(getContext(), request)
822 assert.NotNil(t, err)
823 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
824
825 // -----------------------------------------------------------------------
826 // SubTest 2: Error should be returned for device with no adapter registered
827
828 // Create a device that has no adapter registered
829 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
830 assert.Nil(t, err)
831 assert.NotNil(t, deviceNoAdapter)
832
833 // Omci test action should fail due to nonexistent adapter
834 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
835 _, err = nbi.StartOmciTestAction(getContext(), request)
836 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400837 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700838
839 //Remove the device
840 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
841 assert.Nil(t, err)
842
843 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
844 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
845 return devices != nil && len(devices.Items) == 0
846 }
847 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
848 assert.Nil(t, err)
849
850 // -----------------------------------------------------------------------
851 // SubTest 3: Omci test action should succeed on valid ONU
852
853 // Create the device with valid data
854 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
855 assert.Nil(t, err)
856 assert.NotNil(t, oltDevice)
857
858 // Verify oltDevice exist in the core
859 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
860 assert.Nil(t, err)
861 assert.Equal(t, 1, len(devices.Items))
862 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
863
864 // Enable the oltDevice
865 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
866 assert.Nil(t, err)
867
868 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400869 var vldFunction = func(ports []*voltha.LogicalPort) bool {
870 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -0700871 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400872 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -0700873 assert.Nil(t, err)
874
875 // Wait for the olt device to be enabled
876 vdFunction := func(device *voltha.Device) bool {
877 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
878 }
879 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
880 assert.Nil(t, err)
881
Kent Hagerman2b216042020-04-03 18:28:56 -0400882 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700883 assert.Nil(t, err)
884 assert.Greater(t, len(onuDevices.Items), 0)
885
886 onuDevice := onuDevices.Items[0]
887
888 // Omci test action should succeed
889 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
890 resp, err := nbi.StartOmciTestAction(getContext(), request)
891 assert.Nil(t, err)
892 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
893
894 //Remove the device
895 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
896 assert.Nil(t, err)
897 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
898 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
899 assert.Nil(t, err)
900}
901
khenaidoo67b22152020-03-02 16:01:25 -0500902func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
903 matchFields := make([]*ofp.OfpOxmField, 0)
904 for _, val := range fa.MatchFields {
905 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
906 }
907 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
908}
909
910func createMetadata(cTag int, techProfile int, port int) uint64 {
911 md := 0
912 md = (md | (cTag & 0xFFFF)) << 16
913 md = (md | (techProfile & 0xFFFF)) << 32
914 return uint64(md | (port & 0xFFFFFFFF))
915}
916
khenaidoo8b4abbf2020-04-24 17:04:30 -0400917func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500918 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400919 if flowAddFail {
920 expectedNumFlows = 0
921 }
922 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500923 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700924 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
925 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500926 }
927 // No timeout implies a success
928 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
929 assert.Nil(t, err)
930}
931
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400932func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, ports []*voltha.LogicalPort, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -0500933 // Send flows for the parent device
934 var nniPorts []*voltha.LogicalPort
935 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400936 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -0500937 if p.RootPort {
938 nniPorts = append(nniPorts, p)
939 } else {
940 uniPorts = append(uniPorts, p)
941 }
942 }
943 assert.Equal(t, 1, len(nniPorts))
944 //assert.Greater(t, len(uniPorts), 1 )
945 nniPort := nniPorts[0].OfpPort.PortNo
946 maxInt32 := uint64(0xFFFFFFFF)
947 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
948 var fa *flows.FlowArgs
949 fa = &flows.FlowArgs{
950 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
951 MatchFields: []*ofp.OfpOxmOfbField{
952 flows.InPort(nniPort),
953 flows.EthType(35020),
954 },
955 Actions: []*ofp.OfpAction{
956 flows.Output(controllerPortMask),
957 },
958 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400959 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500960 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
961 assert.Nil(t, err)
962
963 fa = &flows.FlowArgs{
964 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
965 MatchFields: []*ofp.OfpOxmOfbField{
966 flows.InPort(nniPort),
967 flows.EthType(2048),
968 flows.IpProto(17),
969 flows.UdpSrc(67),
970 flows.UdpDst(68),
971 },
972 Actions: []*ofp.OfpAction{
973 flows.Output(controllerPortMask),
974 },
975 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400976 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500977 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
978 assert.Nil(t, err)
979
980 fa = &flows.FlowArgs{
981 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
982 MatchFields: []*ofp.OfpOxmOfbField{
983 flows.InPort(nniPort),
984 flows.EthType(34525),
985 flows.IpProto(17),
986 flows.UdpSrc(546),
987 flows.UdpDst(547),
988 },
989 Actions: []*ofp.OfpAction{
990 flows.Output(controllerPortMask),
991 },
992 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400993 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500994 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
995 assert.Nil(t, err)
996
997 return len(nniPorts), len(uniPorts)
998}
999
Kent Hagerman2b216042020-04-03 18:28:56 -04001000func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001001 maxInt32 := uint64(0xFFFFFFFF)
1002 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1003 fa := &flows.FlowArgs{
1004 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},
1005 MatchFields: []*ofp.OfpOxmOfbField{
1006 flows.InPort(port.PortNo),
1007 flows.EthType(34958),
1008 flows.VlanVid(8187),
1009 },
1010 Actions: []*ofp.OfpAction{
1011 flows.Output(controllerPortMask),
1012 },
1013 }
1014 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001015 maxTries := 3
1016 var err error
1017 for {
1018 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1019 if maxTries < 3 {
1020 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1021 }
1022 break
1023 }
1024 t.Log("Sending EAPOL flows fail:", err)
1025 time.Sleep(50 * time.Millisecond)
1026 maxTries--
1027 if maxTries == 0 {
1028 break
1029 }
1030 }
khenaidoo67b22152020-03-02 16:01:25 -05001031 assert.Nil(t, err)
1032}
1033
khenaidoo0db4c812020-05-27 15:27:30 -04001034func (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 -05001035 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001036
1037 // Clear any existing flows on the adapters
1038 nb.oltAdapter.ClearFlows()
1039 nb.onuAdapter.ClearFlows()
1040
khenaidoo8b4abbf2020-04-24 17:04:30 -04001041 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001042 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1043 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001044
khenaidoo67b22152020-03-02 16:01:25 -05001045 // Wait until a logical device is ready
1046 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1047 if lds == nil || len(lds.Items) != 1 {
1048 return false
1049 }
1050 // Ensure there are both NNI ports and at least one UNI port on the logical device
1051 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001052 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1053 if err != nil {
1054 return false
1055 }
khenaidoo67b22152020-03-02 16:01:25 -05001056 nniPort := false
1057 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001058 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001059 nniPort = nniPort || p.RootPort == true
1060 uniPort = uniPort || p.RootPort == false
1061 if nniPort && uniPort {
1062 return true
1063 }
1064 }
1065 return false
1066 }
1067 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1068 assert.Nil(t, err)
1069
1070 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1071 assert.Nil(t, err)
1072 assert.NotNil(t, logicalDevices)
1073 assert.Equal(t, 1, len(logicalDevices.Items))
1074
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001075 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001076 meterID := rand.Uint32()
1077
1078 // Add a meter to the logical device
1079 meterMod := &ofp.OfpMeterMod{
1080 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1081 Flags: rand.Uint32(),
1082 MeterId: meterID,
1083 Bands: []*ofp.OfpMeterBandHeader{
1084 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1085 Rate: rand.Uint32(),
1086 BurstSize: rand.Uint32(),
1087 Data: nil,
1088 },
1089 },
1090 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001091 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1092 assert.Nil(t, err)
1093
1094 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001095 assert.Nil(t, err)
1096
1097 // Send initial set of Trap flows
1098 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001099 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001100
1101 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001102 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001103 processedNniLogicalPorts := 0
1104 processedUniLogicalPorts := 0
1105
Kent Hagerman45a13e42020-04-13 12:23:50 -04001106 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001107 startingVlan++
1108 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1109 ps := portStatus.PortStatus
1110 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001111 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001112 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001113 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001114 } else {
1115 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001116 }
1117 }
1118 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001119
1120 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001121 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001122 break
1123 }
1124 }
1125 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001126 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001127
khenaidoo8b4abbf2020-04-24 17:04:30 -04001128 // Wait until all flows have been sent to the OLT adapters (or all failed)
1129 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1130 if flowAddFail {
1131 expectedFlowCount = 0
1132 }
khenaidoo67b22152020-03-02 16:01:25 -05001133 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001134 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001135 }
1136 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1137 assert.Nil(t, err)
1138
khenaidoo8b4abbf2020-04-24 17:04:30 -04001139 // Wait until all flows have been sent to the ONU adapters (or all failed)
1140 expectedFlowCount = numUNIPorts
1141 if flowAddFail {
1142 expectedFlowCount = 0
1143 }
khenaidoo67b22152020-03-02 16:01:25 -05001144 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001145 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001146 }
1147 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1148 assert.Nil(t, err)
1149}
1150
khenaidoo8b4abbf2020-04-24 17:04:30 -04001151func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1152
1153 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1154 var wg sync.WaitGroup
1155 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001156 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001157
1158 // Create the device with valid data
1159 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1160 assert.Nil(t, err)
1161 assert.NotNil(t, oltDevice)
1162
1163 // Verify oltDevice exist in the core
1164 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1165 assert.Nil(t, err)
1166 assert.Equal(t, 1, len(devices.Items))
1167 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1168
1169 // Enable the oltDevice
1170 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1171 assert.Nil(t, err)
1172
1173 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001174 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1175 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001176 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001177 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001178 assert.Nil(t, err)
1179
1180 // Verify that the devices have been setup correctly
1181 nb.verifyDevices(t, nbi)
1182
1183 // Get latest oltDevice data
1184 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1185 assert.Nil(t, err)
1186
1187 // Verify that the logical device has been setup correctly
1188 nb.verifyLogicalDevices(t, oltDevice, nbi)
1189
1190 // Wait until all flows has been sent to the devices successfully
1191 wg.Wait()
1192}
1193
Matteo Scandolod525ae32020-04-02 17:27:29 -07001194func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001195 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001196 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001197 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001198 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001199 }
1200 defer f.Close()
1201 runtime.SetBlockProfileRate(1)
1202 runtime.SetMutexProfileFraction(-1)
1203 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001204 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001205 }
1206 defer pprof.StopCPUProfile()
1207
khenaidoo442e7c72020-03-10 16:13:48 -04001208 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1209
Rohan Agrawal31f21802020-06-12 05:38:46 +00001210 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001211 assert.NotNil(t, nb)
1212
Rohan Agrawal31f21802020-06-12 05:38:46 +00001213 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001214
1215 // Start the Core
1216 nb.startCore(false)
1217
1218 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001219 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001220
1221 // 1. Basic test with no data in Core
1222 nb.testCoreWithoutData(t, nbi)
1223
1224 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001225 nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(ctx, t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr)
Mahir Gunyel03de0d32020-06-03 01:36:59 -07001226 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1227 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001228
1229 // 2. Test adapter registration
1230 nb.testAdapterRegistration(t, nbi)
1231
khenaidoo8b4abbf2020-04-24 17:04:30 -04001232 numberOfTestRuns := 2
1233 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001234 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001235 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001236
khenaidoo93d5a3d2020-01-15 12:37:05 -05001237 // 4. Test Enable a device
1238 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001239
khenaidoo0db4c812020-05-27 15:27:30 -04001240 //// 5. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001241 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001242
kesavandbc2d1622020-01-21 00:42:01 -05001243 // 6. Test disable and Enable pon port of OLT device
1244 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001245
Girish Gowdra408cd962020-03-11 14:31:31 -07001246 // 7.Test Device unreachable when OLT is enabled
1247 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1248
1249 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001250 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001251
Girish Gowdra408cd962020-03-11 14:31:31 -07001252 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001253 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001254
1255 // 10. Test omci test
1256 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001257
khenaidoo0db4c812020-05-27 15:27:30 -04001258 // 11. Remove all devices from tests above
1259 nb.deleteAllDevices(t, nbi)
1260
khenaidoo8b4abbf2020-04-24 17:04:30 -04001261 // 11. Test flow add failure
1262 nb.testFlowAddFailure(t, nbi)
1263
1264 // 12. Clean up
1265 nb.deleteAllDevices(t, nbi)
1266 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001267}