blob: 743608354b9f7e8b8b4157261f460a6563650f32 [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"
Maninderdfadc982020-10-28 14:04:33 +053040 "github.com/opencord/voltha-lib-go/v4/pkg/db"
41 "github.com/opencord/voltha-lib-go/v4/pkg/flows"
42 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
43 mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd"
44 mock_kafka "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka"
45 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
46 "github.com/opencord/voltha-protos/v4/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
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030053const numTrapOnNNIFlows = 4
54
khenaidoob64fc8a2019-11-27 15:08:19 -050055type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070056 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040057 deviceMgr *device.Manager
58 logicalDeviceMgr *device.LogicalManager
59 adapterMgr *adapter.Manager
60 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050061 kClient kafka.Client
62 kvClientPort int
63 numONUPerOLT int
64 startingUNIPortNo int
65 oltAdapter *cm.OLTAdapter
66 onuAdapter *cm.ONUAdapter
67 oltAdapterName string
68 onuAdapterName string
69 coreInstanceID string
70 defaultTimeout time.Duration
71 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050072}
73
Rohan Agrawal31f21802020-06-12 05:38:46 +000074func newNBTest(ctx context.Context) *NBTest {
khenaidoob64fc8a2019-11-27 15:08:19 -050075 test := &NBTest{}
76 // Start the embedded etcd server
77 var err error
Rohan Agrawal31f21802020-06-12 05:38:46 +000078 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050079 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000080 logger.Fatal(ctx, err)
khenaidoob64fc8a2019-11-27 15:08:19 -050081 }
82 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070083 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050084 test.oltAdapterName = "olt_adapter_mock"
85 test.onuAdapterName = "onu_adapter_mock"
86 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050087 test.defaultTimeout = 10 * time.Second
88 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050089 return test
90}
91
92func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053093 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
94 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050095 cfg := config.NewRWCoreFlags()
serkant.uluderya8ff291d2020-05-20 00:58:00 -070096 cfg.CoreTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040097 cfg.DefaultRequestTimeout = nb.defaultTimeout
98 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +000099 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500100 grpcPort, err := freeport.GetFreePort()
101 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000102 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500103 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000104 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500105 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000106 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400107 backend := &db.Backend{
108 Client: client,
109 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000110 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700112 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400113 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000114 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400115 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700116 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400117
118 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400119 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000120 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700121 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 -0400122 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400123
Rohan Agrawal31f21802020-06-12 05:38:46 +0000124 if err := nb.kmp.Start(ctx); err != nil {
125 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400126 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400127 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
129 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400130 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500131}
132
Rohan Agrawal31f21802020-06-12 05:38:46 +0000133func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500134 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000135 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500136 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400137 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000138 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500139 }
140 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000141 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500142 }
143}
144
Kent Hagerman2b216042020-04-03 18:28:56 -0400145func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500146 // Get the latest set of logical devices
147 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
148 assert.Nil(t, err)
149 assert.NotNil(t, logicalDevices)
150 assert.Equal(t, 1, len(logicalDevices.Items))
151
152 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400153 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
154 assert.Nil(t, err)
155
khenaidoob64fc8a2019-11-27 15:08:19 -0500156 assert.NotEqual(t, "", ld.Id)
157 assert.NotEqual(t, uint64(0), ld.DatapathId)
158 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
159 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
160 assert.NotEqual(t, "", ld.RootDeviceId)
161 assert.NotEqual(t, "", ld.Desc.SerialNum)
162 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
163 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
164 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400165 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500166 assert.Equal(t, oltDevice.ParentId, ld.Id)
167 //Expected port no
168 expectedPortNo := make(map[uint32]bool)
169 expectedPortNo[uint32(2)] = false
170 for i := 0; i < nb.numONUPerOLT; i++ {
171 expectedPortNo[uint32(i+100)] = false
172 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400173 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500174 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
175 assert.Equal(t, uint32(4), p.OfpPort.State)
176 expectedPortNo[p.OfpPort.PortNo] = true
177 if strings.HasPrefix(p.Id, "nni") {
178 assert.Equal(t, true, p.RootPort)
179 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
180 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
181 } else {
182 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
183 assert.Equal(t, false, p.RootPort)
184 }
185 }
186}
187
Kent Hagerman2b216042020-04-03 18:28:56 -0400188func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500189 // Get the latest set of devices
190 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
191 assert.Nil(t, err)
192 assert.NotNil(t, devices)
193
khenaidoo67b22152020-03-02 16:01:25 -0500194 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500195 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
196 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
197 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500198
khenaidoo67b22152020-03-02 16:01:25 -0500199 var wg sync.WaitGroup
200 for _, device := range devices.Items {
201 wg.Add(1)
202 go func(wg *sync.WaitGroup, device *voltha.Device) {
203 // Wait until the device is in the right state
204 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
205 assert.Nil(t, err)
206
207 // Now, verify the details of the device. First get the latest update
208 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
209 assert.Nil(t, err)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400210 dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id})
211 assert.Nil(t, err)
khenaidoo67b22152020-03-02 16:01:25 -0500212 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
213 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
214 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
215 assert.Equal(t, d.Type, d.Adapter)
216 assert.NotEqual(t, "", d.MacAddress)
217 assert.NotEqual(t, "", d.SerialNumber)
218
219 if d.Type == "olt_adapter_mock" {
220 assert.Equal(t, true, d.Root)
221 assert.NotEqual(t, "", d.Id)
222 assert.NotEqual(t, "", d.ParentId)
223 assert.Nil(t, d.ProxyAddress)
224 } else if d.Type == "onu_adapter_mock" {
225 assert.Equal(t, false, d.Root)
226 assert.NotEqual(t, uint32(0), d.Vlan)
227 assert.NotEqual(t, "", d.Id)
228 assert.NotEqual(t, "", d.ParentId)
229 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
230 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500231 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500232 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500233 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400234 assert.Equal(t, 2, len(dPorts.Items))
235 for _, p := range dPorts.Items {
khenaidoo67b22152020-03-02 16:01:25 -0500236 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
237 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
238 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
239 assert.Equal(t, 0, len(p.Peers))
240 } else if p.Type == voltha.Port_PON_OLT {
241 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
242 assert.Equal(t, uint32(1), p.PortNo)
243 } else if p.Type == voltha.Port_PON_ONU {
244 assert.Equal(t, 1, len(p.Peers))
245 assert.Equal(t, uint32(1), p.PortNo)
246 } else {
247 assert.Error(t, errors.New("invalid-port"))
248 }
249 }
250 wg.Done()
251 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500252 }
khenaidoo67b22152020-03-02 16:01:25 -0500253 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500254}
255
Kent Hagerman2b216042020-04-03 18:28:56 -0400256func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500257 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
258 if err != nil {
259 return nil, err
260 }
261 for _, d := range devices.Items {
262 if d.Root == rootDevice {
263 return d, nil
264 }
265 }
266 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
267}
268
Kent Hagerman2b216042020-04-03 18:28:56 -0400269func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500270 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
271 assert.Nil(t, err)
272 assert.NotNil(t, lds)
273 assert.Equal(t, 0, len(lds.Items))
274 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
275 assert.Nil(t, err)
276 assert.NotNil(t, devices)
277 assert.Equal(t, 0, len(devices.Items))
278 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400279 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500280 assert.Nil(t, err)
281 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500282}
283
Kent Hagerman2b216042020-04-03 18:28:56 -0400284func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000285 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500286 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
287 assert.Nil(t, err)
288 assert.NotNil(t, adapters)
289 assert.Equal(t, 2, len(adapters.Items))
290 for _, a := range adapters.Items {
291 switch a.Id {
292 case nb.oltAdapterName:
293 assert.Equal(t, "Voltha-olt", a.Vendor)
294 case nb.onuAdapterName:
295 assert.Equal(t, "Voltha-onu", a.Vendor)
296 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000297 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500298 }
299 }
300 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
301 assert.Nil(t, err)
302 assert.NotNil(t, deviceTypes)
303 assert.Equal(t, 2, len(deviceTypes.Items))
304 for _, dt := range deviceTypes.Items {
305 switch dt.Id {
306 case nb.oltAdapterName:
307 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
308 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
309 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
310 case nb.onuAdapterName:
311 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
312 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
313 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
314 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000315 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500316 }
317 }
318}
319
Kent Hagerman2b216042020-04-03 18:28:56 -0400320func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500321 // Create a valid device
322 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
323 assert.Nil(t, err)
324 assert.NotNil(t, oltDevice)
325 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
326 assert.Nil(t, err)
327 assert.NotNil(t, device)
328 assert.Equal(t, oltDevice.String(), device.String())
329
330 // Try to create the same device
331 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
332 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400333 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500334
335 // Try to create a device with invalid data
336 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
337 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530338 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500339
340 // Ensure we only have 1 device in the Core
341 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
342 assert.Nil(t, err)
343 assert.NotNil(t, devices)
344 assert.Equal(t, 1, len(devices.Items))
345 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
346
347 //Remove the device
348 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
349 assert.Nil(t, err)
350
351 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
352 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
353 return devices != nil && len(devices.Items) == 0
354 }
khenaidoo442e7c72020-03-10 16:13:48 -0400355 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500356 assert.Nil(t, err)
357}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530358func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) {
359 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
360 var wg sync.WaitGroup
361 wg.Add(1)
362 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
363
364 // Enable the oltDevice
365 _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
366 assert.Nil(t, err)
367
368 // Wait for the logical device to be in the ready state
369 var vldFunction = func(ports []*voltha.LogicalPort) bool {
370 return len(ports) == nb.numONUPerOLT+1
371 }
372 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
373 assert.Nil(t, err)
374
375 // Verify that the devices have been setup correctly
376 nb.verifyDevices(t, nbi)
377
378 // Get latest oltDevice data
379 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
380 assert.Nil(t, err)
381
382 // Verify that the logical device has been setup correctly
383 nb.verifyLogicalDevices(t, oltDevice, nbi)
384
385 // Wait until all flows has been sent to the devices successfully
386 wg.Wait()
387
388}
389func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
390 // Create a valid device
391 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
392 assert.Nil(t, err)
393 assert.NotNil(t, oltDevice)
394 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
395 assert.Nil(t, err)
396 assert.NotNil(t, device)
397 assert.Equal(t, oltDevice.String(), device.String())
398
399 // Ensure we only have 1 device in the Core
400 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
401 assert.Nil(t, err)
402 assert.NotNil(t, devices)
403 assert.Equal(t, 1, len(devices.Items))
404 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
405
406 //Remove the device forcefully
407 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
408 assert.Nil(t, err)
409
410 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
411 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
412 return devices != nil && len(devices.Items) == 0
413 }
414 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
415 assert.Nil(t, err)
416}
417
418func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
419 // Create a valid device
420 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
421 assert.Nil(t, err)
422 assert.NotNil(t, oltDevice)
423 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
424 assert.Nil(t, err)
425 assert.NotNil(t, device)
426 assert.Equal(t, oltDevice.String(), device.String())
427
428 nb.enableDevice(t, nbi, oltDevice)
429
430 //Remove the device forcefully
431 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
432 assert.Nil(t, err)
433
434 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
435 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
436 return devices != nil && len(devices.Items) == 0
437 }
438 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
439 assert.Nil(t, err)
440}
441
442func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
443 // Create a valid device
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 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
448 assert.Nil(t, err)
449 assert.NotNil(t, device)
450 assert.Equal(t, oltDevice.String(), device.String())
451
452 // Ensure we only have 1 device in the Core
453 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
454 assert.Nil(t, err)
455 assert.NotNil(t, devices)
456 assert.Equal(t, 1, len(devices.Items))
457 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
458
459 //Remove the device forcefully
460 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
461 assert.Nil(t, err)
462
463 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
464 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
465 return devices != nil && len(devices.Items) == 0
466 }
467 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
468 assert.Nil(t, err)
469}
470
471func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
472 // Create a valid device
473 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
474 assert.Nil(t, err)
475 assert.NotNil(t, oltDevice)
476 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
477 assert.Nil(t, err)
478 assert.NotNil(t, device)
479 assert.Equal(t, oltDevice.String(), device.String())
480
481 nb.enableDevice(t, nbi, oltDevice)
482
483 //Remove the device
484 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
485 assert.Nil(t, err)
486
487 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
488 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
489 return devices != nil && len(devices.Items) == 0
490 }
491 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
492 assert.Nil(t, err)
493}
494
495func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
496 // Create a valid device
497 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
498 assert.Nil(t, err)
499 assert.NotNil(t, oltDevice)
500 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
501 assert.Nil(t, err)
502 assert.NotNil(t, device)
503 assert.Equal(t, oltDevice.String(), device.String())
504
505 nb.enableDevice(t, nbi, oltDevice)
506 nb.oltAdapter.SetDeleteAction(true)
507 //Remove the device
508 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
509 assert.Nil(t, err)
510
511 //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout
512 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
513 return devices != nil && len(devices.Items) == 0
514 }
515 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
516 assert.Nil(t, err)
517
518}
519
520func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
521 // Create a valid device
522 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
523 assert.Nil(t, err)
524 assert.NotNil(t, oltDevice)
525 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
526 assert.Nil(t, err)
527 assert.NotNil(t, device)
528 assert.Equal(t, oltDevice.String(), device.String())
529
530 nb.enableDevice(t, nbi, oltDevice)
531
532 nb.oltAdapter.SetDeleteAction(true)
533 //Remove the device
534 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
535 assert.Nil(t, err)
536
537 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
538 var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
539 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1)
540 }
541 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
542 assert.Nil(t, err)
543
544 nb.oltAdapter.SetDeleteAction(false)
545
546 // Now Force Delete this device
547 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
548 assert.Nil(t, err)
549
550 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
551 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
552 return devices != nil && len(devices.Items) == 0
553 }
554 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
555 assert.Nil(t, err)
556
557}
khenaidoob64fc8a2019-11-27 15:08:19 -0500558
Kent Hagerman2b216042020-04-03 18:28:56 -0400559func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500560 // Create a device that has no adapter registered
561 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
562 assert.Nil(t, err)
563 assert.NotNil(t, oltDeviceNoAdapter)
564
565 // Try to enable the oltDevice and check the error message
566 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
567 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400568 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500569
570 //Remove the device
571 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
572 assert.Nil(t, err)
573
574 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
575 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
576 return devices != nil && len(devices.Items) == 0
577 }
khenaidoo442e7c72020-03-10 16:13:48 -0400578 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500579 assert.Nil(t, err)
580
khenaidoo67b22152020-03-02 16:01:25 -0500581 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
582 var wg sync.WaitGroup
583 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400584 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500585
khenaidoob64fc8a2019-11-27 15:08:19 -0500586 // Create the device with valid data
587 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
588 assert.Nil(t, err)
589 assert.NotNil(t, oltDevice)
590
591 // Verify oltDevice exist in the core
592 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
593 assert.Nil(t, err)
594 assert.Equal(t, 1, len(devices.Items))
595 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
596
597 // Enable the oltDevice
598 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
599 assert.Nil(t, err)
600
601 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400602 var vldFunction = func(ports []*voltha.LogicalPort) bool {
603 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500604 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400605 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500606 assert.Nil(t, err)
607
608 // Verify that the devices have been setup correctly
609 nb.verifyDevices(t, nbi)
610
611 // Get latest oltDevice data
612 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
613 assert.Nil(t, err)
614
615 // Verify that the logical device has been setup correctly
616 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500617
618 // Wait until all flows has been sent to the devices successfully
619 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500620}
621
Kent Hagerman2b216042020-04-03 18:28:56 -0400622func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500623 //Get an OLT device
624 oltDevice, err := nb.getADevice(true, nbi)
625 assert.Nil(t, err)
626 assert.NotNil(t, oltDevice)
627
628 // Disable the oltDevice
629 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
630 assert.Nil(t, err)
631
632 // Wait for the old device to be disabled
633 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
634 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
635 }
636 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
637 assert.Nil(t, err)
638
639 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400640 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500641 assert.Nil(t, err)
642 for _, onu := range onuDevices.Items {
643 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
644 assert.Nil(t, err)
645 }
646
647 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400648 var vlFunction = func(ports []*voltha.LogicalPort) bool {
649 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500650 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
651 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
652 return false
653 }
654 }
655 return true
656 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400657 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500658 assert.Nil(t, err)
659
660 // Reenable the oltDevice
661 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
662 assert.Nil(t, err)
663
664 // Wait for the old device to be enabled
665 vdFunction = func(device *voltha.Device) bool {
666 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
667 }
668 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
669 assert.Nil(t, err)
670
671 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400672 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500673 assert.Nil(t, err)
674 for _, onu := range onuDevices.Items {
675 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
676 assert.Nil(t, err)
677 }
678
679 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400680 vlFunction = func(ports []*voltha.LogicalPort) bool {
681 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500682 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
683 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
684 return false
685 }
686 }
687 return true
688 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400689 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500690 assert.Nil(t, err)
691}
692
Kent Hagerman2b216042020-04-03 18:28:56 -0400693func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500694 //Get an OLT device
695 oltDevice, err := nb.getADevice(true, nbi)
696 assert.Nil(t, err)
697 assert.NotNil(t, oltDevice)
698
699 // Disable the oltDevice
700 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
701 assert.Nil(t, err)
702
703 // Wait for the olt device to be disabled
704 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
705 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
706 }
707 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
708 assert.Nil(t, err)
709
710 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400711 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500712 assert.Nil(t, err)
713 for _, onu := range onuDevices.Items {
714 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
715 assert.Nil(t, err)
716 }
717
718 // Delete the oltDevice
719 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
720 assert.Nil(t, err)
721
722 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
723 return devices != nil && len(devices.Items) == 0
724 }
725 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
726 assert.Nil(t, err)
727
728 // Wait for absence of logical device
729 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
730 return lds != nil && len(lds.Items) == 0
731 }
732
733 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
734 assert.Nil(t, err)
735}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400736
737func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400738 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
739 if len(devices.Items) == 0 {
740 // Nothing to do
741 return
742 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400743 //Get an OLT device
744 oltDevice, err := nb.getADevice(true, nbi)
745 assert.Nil(t, err)
746 assert.NotNil(t, oltDevice)
747
748 // Delete the oltDevice
749 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
750 assert.Nil(t, err)
751
752 // Wait for all devices to be deleted
753 vFunction := func(devices *voltha.Devices) bool {
754 return devices != nil && len(devices.Items) == 0
755 }
756 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
757 assert.Nil(t, err)
758
759 // Wait for absence of logical device
760 vlFunction := func(lds *voltha.LogicalDevices) bool {
761 return lds != nil && len(lds.Items) == 0
762 }
763
764 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
765 assert.Nil(t, err)
766}
767
Kent Hagerman2b216042020-04-03 18:28:56 -0400768func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500769 //Create the device with valid data
770 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
771 assert.Nil(t, err)
772 assert.NotNil(t, oltDevice)
773
774 //Get an OLT device
775 oltDevice, err = nb.getADevice(true, nbi)
776 assert.Nil(t, err)
777 assert.NotNil(t, oltDevice)
778
779 // Enable the oltDevice
780 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
781 assert.Nil(t, err)
782
783 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400784 var vldFunction = func(ports []*voltha.LogicalPort) bool {
785 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500786 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400787 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500788 assert.Nil(t, err)
789
790 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400791 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500792 assert.Nil(t, err)
793
794 // Wait for the all onu devices to be enabled
795 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
796 return device.AdminState == voltha.AdminState_ENABLED
797 }
798 for _, onu := range onuDevices.Items {
799 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
800 assert.Nil(t, err)
801 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500802 // Wait for each onu device to get deleted
803 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
804 return device == nil
805 }
806
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500807 // Delete the onuDevice
808 for _, onu := range onuDevices.Items {
809 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
810 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500811 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
812 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500813 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500814
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500815 // Disable the oltDevice
816 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
817 assert.Nil(t, err)
818
819 // Wait for the olt device to be disabled
820 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
821 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
822 }
823 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
824 assert.Nil(t, err)
825
826 // Delete the oltDevice
827 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
828 assert.Nil(t, err)
829
830 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
831 return devices != nil && len(devices.Items) == 0
832 }
833 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
834 assert.Nil(t, err)
835}
Kent Hagerman2b216042020-04-03 18:28:56 -0400836func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500837 //Get an OLT device
838 var cp *voltha.Port
839 oltDevice, err := nb.getADevice(true, nbi)
840 assert.Nil(t, err)
841 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400842 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
843 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500844
Kent Hagerman2a07b862020-06-19 15:23:07 -0400845 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500846 if cp.Type == voltha.Port_PON_OLT {
847 break
848 }
849
850 }
851 assert.NotNil(t, cp)
852 cp.DeviceId = oltDevice.Id
853
854 // Disable the NW Port of oltDevice
855 _, err = nbi.DisablePort(getContext(), cp)
856 assert.Nil(t, err)
857 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400858 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
859 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500860 if port.PortNo == cp.PortNo {
861 return port.AdminState == voltha.AdminState_DISABLED
862 }
863 }
864 return false
865 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400866 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500867 assert.Nil(t, err)
868 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400869 var vlFunction = func(ports []*voltha.LogicalPort) bool {
870 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500871 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
872 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
873 return false
874 }
875 }
876 return true
877 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400878 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500879 assert.Nil(t, err)
880
881 // Enable the NW Port of oltDevice
882 _, err = nbi.EnablePort(getContext(), cp)
883 assert.Nil(t, err)
884
885 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400886 vdFunction = func(ports *voltha.Ports) bool {
887 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500888 if port.PortNo == cp.PortNo {
889 return port.AdminState == voltha.AdminState_ENABLED
890 }
891 }
892 return false
893 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400894 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500895 assert.Nil(t, err)
896 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400897 vlFunction = func(ports []*voltha.LogicalPort) bool {
898 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500899 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
900 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
901 return false
902 }
903 }
904 return true
905 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400906 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500907 assert.Nil(t, err)
908
909 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400910 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500911 if cp.Type != voltha.Port_PON_OLT {
912 break
913 }
914
915 }
916 assert.NotNil(t, cp)
917 cp.DeviceId = oltDevice.Id
918
919 // Disable the NW Port of oltDevice
920 _, err = nbi.DisablePort(getContext(), cp)
921 assert.NotNil(t, err)
922
923}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500924
Kent Hagerman2b216042020-04-03 18:28:56 -0400925func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700926 //Get an OLT device
927 oltDevice, err := nb.getADevice(true, nbi)
928 assert.Nil(t, err)
929 assert.NotNil(t, oltDevice)
930 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
931 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
932
933 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400934 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700935 assert.Nil(t, err)
936 assert.NotNil(t, onuDevices)
937 assert.Greater(t, len(onuDevices.Items), 0)
938
939 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
940 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
941 assert.Nil(t, err)
942
943 var vlFunction0 = func(d *voltha.Device) bool {
944 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
945 }
946
947 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
948 assert.Nil(t, err)
949
950 // Wait for the logical device to satisfy the expected condition
951 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
952 return ld == nil
953 }
954
955 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
956 assert.Nil(t, err)
957
958 // Wait for the device to satisfy the expected condition (device does not have flows)
959 var vlFunction2 = func(d *voltha.Device) bool {
960 var deviceFlows *ofp.Flows
961 var err error
962 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
963 return false
964 }
965 return len(deviceFlows.Items) == 0
966 }
967
968 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
969 assert.Nil(t, err)
970
971 // Wait for the device to satisfy the expected condition (there are no child devices)
972 var vlFunction3 = func(d *voltha.Device) bool {
973 var devices *voltha.Devices
974 var err error
975 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
976 return false
977 }
978 for _, device := range devices.Items {
979 if device.ParentId == d.Id {
980 // We have a child device still left
981 return false
982 }
983 }
984 return true
985 }
986
987 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
988 assert.Nil(t, err)
989
990 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
991 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400992 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700993 assert.Nil(t, err)
994
995 // Verify the device connection and operation states
996 oltDevice, err = nb.getADevice(true, nbi)
997 assert.Nil(t, err)
998 assert.NotNil(t, oltDevice)
999 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
1000 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
1001
1002 // Wait for the logical device to satisfy the expected condition
1003 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1004 return ld != nil
1005 }
1006 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1007 assert.Nil(t, err)
1008
1009 // Verify that logical device is created again
1010 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1011 assert.Nil(t, err)
1012 assert.NotNil(t, logicalDevices)
1013 assert.Equal(t, 1, len(logicalDevices.Items))
1014
1015 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001016 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001017 assert.Nil(t, err)
1018 assert.NotNil(t, onuDevices)
1019 assert.Equal(t, 0, len(onuDevices.Items))
1020}
1021
Kent Hagerman2b216042020-04-03 18:28:56 -04001022func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001023 // -----------------------------------------------------------------------
1024 // SubTest 1: Omci test action should fail due to nonexistent device id
1025
1026 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1027 _, err := nbi.StartOmciTestAction(getContext(), request)
1028 assert.NotNil(t, err)
1029 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1030
1031 // -----------------------------------------------------------------------
1032 // SubTest 2: Error should be returned for device with no adapter registered
1033
1034 // Create a device that has no adapter registered
1035 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1036 assert.Nil(t, err)
1037 assert.NotNil(t, deviceNoAdapter)
1038
1039 // Omci test action should fail due to nonexistent adapter
1040 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1041 _, err = nbi.StartOmciTestAction(getContext(), request)
1042 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001043 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001044
1045 //Remove the device
1046 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1047 assert.Nil(t, err)
1048
1049 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1050 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1051 return devices != nil && len(devices.Items) == 0
1052 }
1053 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1054 assert.Nil(t, err)
1055
1056 // -----------------------------------------------------------------------
1057 // SubTest 3: Omci test action should succeed on valid ONU
1058
1059 // Create the device with valid data
1060 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1061 assert.Nil(t, err)
1062 assert.NotNil(t, oltDevice)
1063
1064 // Verify oltDevice exist in the core
1065 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1066 assert.Nil(t, err)
1067 assert.Equal(t, 1, len(devices.Items))
1068 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1069
1070 // Enable the oltDevice
1071 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1072 assert.Nil(t, err)
1073
1074 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001075 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1076 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001077 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001078 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001079 assert.Nil(t, err)
1080
1081 // Wait for the olt device to be enabled
1082 vdFunction := func(device *voltha.Device) bool {
1083 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1084 }
1085 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1086 assert.Nil(t, err)
1087
Kent Hagerman2b216042020-04-03 18:28:56 -04001088 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001089 assert.Nil(t, err)
1090 assert.Greater(t, len(onuDevices.Items), 0)
1091
1092 onuDevice := onuDevices.Items[0]
1093
1094 // Omci test action should succeed
1095 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1096 resp, err := nbi.StartOmciTestAction(getContext(), request)
1097 assert.Nil(t, err)
1098 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1099
1100 //Remove the device
1101 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1102 assert.Nil(t, err)
1103 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1104 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1105 assert.Nil(t, err)
1106}
1107
khenaidoo67b22152020-03-02 16:01:25 -05001108func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1109 matchFields := make([]*ofp.OfpOxmField, 0)
1110 for _, val := range fa.MatchFields {
1111 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1112 }
1113 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1114}
1115
1116func createMetadata(cTag int, techProfile int, port int) uint64 {
1117 md := 0
1118 md = (md | (cTag & 0xFFFF)) << 16
1119 md = (md | (techProfile & 0xFFFF)) << 32
1120 return uint64(md | (port & 0xFFFFFFFF))
1121}
1122
khenaidoo8b4abbf2020-04-24 17:04:30 -04001123func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001124 expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001125 if flowAddFail {
1126 expectedNumFlows = 0
1127 }
1128 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001129 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -07001130 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
1131 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001132 }
1133 // No timeout implies a success
1134 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1135 assert.Nil(t, err)
1136}
1137
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001138func (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 -05001139 // Send flows for the parent device
1140 var nniPorts []*voltha.LogicalPort
1141 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001142 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001143 if p.RootPort {
1144 nniPorts = append(nniPorts, p)
1145 } else {
1146 uniPorts = append(uniPorts, p)
1147 }
1148 }
1149 assert.Equal(t, 1, len(nniPorts))
1150 //assert.Greater(t, len(uniPorts), 1 )
1151 nniPort := nniPorts[0].OfpPort.PortNo
1152 maxInt32 := uint64(0xFFFFFFFF)
1153 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1154 var fa *flows.FlowArgs
1155 fa = &flows.FlowArgs{
1156 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1157 MatchFields: []*ofp.OfpOxmOfbField{
1158 flows.InPort(nniPort),
1159 flows.EthType(35020),
1160 },
1161 Actions: []*ofp.OfpAction{
1162 flows.Output(controllerPortMask),
1163 },
1164 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001165 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001166 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1167 assert.Nil(t, err)
1168
1169 fa = &flows.FlowArgs{
1170 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1171 MatchFields: []*ofp.OfpOxmOfbField{
1172 flows.InPort(nniPort),
1173 flows.EthType(2048),
1174 flows.IpProto(17),
1175 flows.UdpSrc(67),
1176 flows.UdpDst(68),
1177 },
1178 Actions: []*ofp.OfpAction{
1179 flows.Output(controllerPortMask),
1180 },
1181 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001182 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001183 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1184 assert.Nil(t, err)
1185
1186 fa = &flows.FlowArgs{
1187 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1188 MatchFields: []*ofp.OfpOxmOfbField{
1189 flows.InPort(nniPort),
1190 flows.EthType(34525),
1191 flows.IpProto(17),
1192 flows.UdpSrc(546),
1193 flows.UdpDst(547),
1194 },
1195 Actions: []*ofp.OfpAction{
1196 flows.Output(controllerPortMask),
1197 },
1198 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001199 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001200 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1201 assert.Nil(t, err)
1202
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001203 fa = &flows.FlowArgs{
1204 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1205 MatchFields: []*ofp.OfpOxmOfbField{
1206 flows.InPort(nniPort),
1207 flows.EthType(34915),
1208 },
1209 Actions: []*ofp.OfpAction{
1210 flows.Output(controllerPortMask),
1211 },
1212 }
1213 flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1214 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP)
1215 assert.Nil(t, err)
1216
khenaidoo67b22152020-03-02 16:01:25 -05001217 return len(nniPorts), len(uniPorts)
1218}
1219
Kent Hagerman2b216042020-04-03 18:28:56 -04001220func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001221 maxInt32 := uint64(0xFFFFFFFF)
1222 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1223 fa := &flows.FlowArgs{
1224 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},
1225 MatchFields: []*ofp.OfpOxmOfbField{
1226 flows.InPort(port.PortNo),
1227 flows.EthType(34958),
1228 flows.VlanVid(8187),
1229 },
1230 Actions: []*ofp.OfpAction{
1231 flows.Output(controllerPortMask),
1232 },
1233 }
1234 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001235 maxTries := 3
1236 var err error
1237 for {
1238 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1239 if maxTries < 3 {
1240 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1241 }
1242 break
1243 }
1244 t.Log("Sending EAPOL flows fail:", err)
1245 time.Sleep(50 * time.Millisecond)
1246 maxTries--
1247 if maxTries == 0 {
1248 break
1249 }
1250 }
khenaidoo67b22152020-03-02 16:01:25 -05001251 assert.Nil(t, err)
1252}
1253
khenaidoo0db4c812020-05-27 15:27:30 -04001254func (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 -05001255 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001256
1257 // Clear any existing flows on the adapters
1258 nb.oltAdapter.ClearFlows()
1259 nb.onuAdapter.ClearFlows()
1260
khenaidoo8b4abbf2020-04-24 17:04:30 -04001261 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001262 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1263 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001264
khenaidoo67b22152020-03-02 16:01:25 -05001265 // Wait until a logical device is ready
1266 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1267 if lds == nil || len(lds.Items) != 1 {
1268 return false
1269 }
1270 // Ensure there are both NNI ports and at least one UNI port on the logical device
1271 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001272 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1273 if err != nil {
1274 return false
1275 }
khenaidoo67b22152020-03-02 16:01:25 -05001276 nniPort := false
1277 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001278 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001279 nniPort = nniPort || p.RootPort == true
1280 uniPort = uniPort || p.RootPort == false
1281 if nniPort && uniPort {
1282 return true
1283 }
1284 }
1285 return false
1286 }
1287 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1288 assert.Nil(t, err)
1289
1290 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1291 assert.Nil(t, err)
1292 assert.NotNil(t, logicalDevices)
1293 assert.Equal(t, 1, len(logicalDevices.Items))
1294
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001295 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001296 meterID := rand.Uint32()
1297
1298 // Add a meter to the logical device
1299 meterMod := &ofp.OfpMeterMod{
1300 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1301 Flags: rand.Uint32(),
1302 MeterId: meterID,
1303 Bands: []*ofp.OfpMeterBandHeader{
1304 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1305 Rate: rand.Uint32(),
1306 BurstSize: rand.Uint32(),
1307 Data: nil,
1308 },
1309 },
1310 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001311 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1312 assert.Nil(t, err)
1313
1314 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001315 assert.Nil(t, err)
1316
1317 // Send initial set of Trap flows
1318 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001319 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001320
1321 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001322 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001323 processedNniLogicalPorts := 0
1324 processedUniLogicalPorts := 0
1325
Kent Hagerman45a13e42020-04-13 12:23:50 -04001326 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001327 startingVlan++
1328 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1329 ps := portStatus.PortStatus
1330 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001331 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001332 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001333 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001334 } else {
1335 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001336 }
1337 }
1338 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001339
1340 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001341 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001342 break
1343 }
1344 }
1345 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001346 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001347
khenaidoo8b4abbf2020-04-24 17:04:30 -04001348 // Wait until all flows have been sent to the OLT adapters (or all failed)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001349 expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001350 if flowAddFail {
1351 expectedFlowCount = 0
1352 }
khenaidoo67b22152020-03-02 16:01:25 -05001353 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001354 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001355 }
1356 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1357 assert.Nil(t, err)
1358
khenaidoo8b4abbf2020-04-24 17:04:30 -04001359 // Wait until all flows have been sent to the ONU adapters (or all failed)
1360 expectedFlowCount = numUNIPorts
1361 if flowAddFail {
1362 expectedFlowCount = 0
1363 }
khenaidoo67b22152020-03-02 16:01:25 -05001364 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001365 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001366 }
1367 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1368 assert.Nil(t, err)
1369}
1370
khenaidoo8b4abbf2020-04-24 17:04:30 -04001371func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1372
1373 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1374 var wg sync.WaitGroup
1375 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001376 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001377
1378 // Create the device with valid data
1379 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1380 assert.Nil(t, err)
1381 assert.NotNil(t, oltDevice)
1382
1383 // Verify oltDevice exist in the core
1384 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1385 assert.Nil(t, err)
1386 assert.Equal(t, 1, len(devices.Items))
1387 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1388
1389 // Enable the oltDevice
1390 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1391 assert.Nil(t, err)
1392
1393 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001394 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1395 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001396 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001397 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001398 assert.Nil(t, err)
1399
1400 // Verify that the devices have been setup correctly
1401 nb.verifyDevices(t, nbi)
1402
1403 // Get latest oltDevice data
1404 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1405 assert.Nil(t, err)
1406
1407 // Verify that the logical device has been setup correctly
1408 nb.verifyLogicalDevices(t, oltDevice, nbi)
1409
1410 // Wait until all flows has been sent to the devices successfully
1411 wg.Wait()
1412}
1413
Matteo Scandolod525ae32020-04-02 17:27:29 -07001414func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001415 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001416 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001417 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001418 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001419 }
1420 defer f.Close()
1421 runtime.SetBlockProfileRate(1)
1422 runtime.SetMutexProfileFraction(-1)
1423 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001424 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001425 }
1426 defer pprof.StopCPUProfile()
1427
khenaidoo442e7c72020-03-10 16:13:48 -04001428 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1429
Rohan Agrawal31f21802020-06-12 05:38:46 +00001430 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001431 assert.NotNil(t, nb)
1432
Rohan Agrawal31f21802020-06-12 05:38:46 +00001433 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001434
1435 // Start the Core
1436 nb.startCore(false)
1437
1438 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001439 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001440
1441 // 1. Basic test with no data in Core
1442 nb.testCoreWithoutData(t, nbi)
1443
1444 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001445 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 -07001446 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1447 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001448
1449 // 2. Test adapter registration
1450 nb.testAdapterRegistration(t, nbi)
1451
khenaidoo8b4abbf2020-04-24 17:04:30 -04001452 numberOfTestRuns := 2
1453 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301454
1455 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001456 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001457
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301458 // 4. Test Delete Device Scenarios
1459 nb.testForceDeletePreProvDevice(t, nbi)
1460 nb.testDeletePreProvDevice(t, nbi)
1461 nb.testForceDeleteEnabledDevice(t, nbi)
1462 nb.testDeleteEnabledDevice(t, nbi)
1463 nb.testForceDeleteDeviceFailure(t, nbi)
1464 nb.testDeleteDeviceFailure(t, nbi)
1465
1466 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001467 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001468
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301469 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001470 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001471
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301472 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001473 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001474
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301475 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001476 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1477
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301478 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001479 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001480
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301481 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001482 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001483
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301484 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001485 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001486
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301487 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001488 nb.deleteAllDevices(t, nbi)
1489
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301490 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001491 nb.testFlowAddFailure(t, nbi)
1492
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301493 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001494 nb.deleteAllDevices(t, nbi)
1495 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001496}