blob: 8766c7c7eb88a22f71ef79c1315b73292db1aaaf [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"
Himani Chawlab4c25912020-11-12 17:16:38 +053041 "github.com/opencord/voltha-lib-go/v4/pkg/events"
Maninderdfadc982020-10-28 14:04:33 +053042 "github.com/opencord/voltha-lib-go/v4/pkg/flows"
43 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
44 mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd"
45 mock_kafka "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka"
46 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
47 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050048 "github.com/phayes/freeport"
49 "github.com/stretchr/testify/assert"
50 "google.golang.org/grpc/codes"
51 "google.golang.org/grpc/status"
khenaidoob64fc8a2019-11-27 15:08:19 -050052)
53
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030054const numTrapOnNNIFlows = 4
55
khenaidoob64fc8a2019-11-27 15:08:19 -050056type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070057 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040058 deviceMgr *device.Manager
59 logicalDeviceMgr *device.LogicalManager
60 adapterMgr *adapter.Manager
61 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050062 kClient kafka.Client
Himani Chawlab4c25912020-11-12 17:16:38 +053063 kEventClient kafka.Client
khenaidoo67b22152020-03-02 16:01:25 -050064 kvClientPort int
65 numONUPerOLT int
66 startingUNIPortNo int
67 oltAdapter *cm.OLTAdapter
68 onuAdapter *cm.ONUAdapter
69 oltAdapterName string
70 onuAdapterName string
71 coreInstanceID string
72 defaultTimeout time.Duration
73 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050074}
75
Rohan Agrawal31f21802020-06-12 05:38:46 +000076func newNBTest(ctx context.Context) *NBTest {
khenaidoob64fc8a2019-11-27 15:08:19 -050077 test := &NBTest{}
78 // Start the embedded etcd server
79 var err error
Rohan Agrawal31f21802020-06-12 05:38:46 +000080 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050081 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000082 logger.Fatal(ctx, err)
khenaidoob64fc8a2019-11-27 15:08:19 -050083 }
84 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070085 test.kClient = mock_kafka.NewKafkaClient()
Himani Chawlab4c25912020-11-12 17:16:38 +053086 test.kEventClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050087 test.oltAdapterName = "olt_adapter_mock"
88 test.onuAdapterName = "onu_adapter_mock"
89 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050090 test.defaultTimeout = 10 * time.Second
91 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050092 return test
93}
94
95func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053096 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
97 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050098 cfg := config.NewRWCoreFlags()
serkant.uluderya8ff291d2020-05-20 00:58:00 -070099 cfg.CoreTopic = "rw_core"
Himani Chawlab4c25912020-11-12 17:16:38 +0530100 cfg.EventTopic = "voltha.events"
khenaidoo442e7c72020-03-10 16:13:48 -0400101 cfg.DefaultRequestTimeout = nb.defaultTimeout
102 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +0000103 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500104 grpcPort, err := freeport.GetFreePort()
105 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000106 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500107 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000108 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500109 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000110 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 backend := &db.Backend{
112 Client: client,
113 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000114 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400115 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700116 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400117 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000118 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400119 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700120 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400121
122 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400123 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000124 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
Himani Chawlab4c25912020-11-12 17:16:38 +0530125 eventProxy := events.NewEventProxy(events.MsgClient(nb.kEventClient), events.MsgTopic(kafka.Topic{Name: cfg.EventTopic}))
126 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout, eventProxy)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400127 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400128
Rohan Agrawal31f21802020-06-12 05:38:46 +0000129 if err := nb.kmp.Start(ctx); err != nil {
130 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400131 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400132 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000133 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
134 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400135 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500136}
137
Rohan Agrawal31f21802020-06-12 05:38:46 +0000138func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500139 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000140 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500141 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400142 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000143 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500144 }
145 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000146 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500147 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530148 if nb.kEventClient != nil {
149 nb.kEventClient.Stop(ctx)
150 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500151}
152
Kent Hagerman2b216042020-04-03 18:28:56 -0400153func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500154 // Get the latest set of logical devices
155 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
156 assert.Nil(t, err)
157 assert.NotNil(t, logicalDevices)
158 assert.Equal(t, 1, len(logicalDevices.Items))
159
160 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400161 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
162 assert.Nil(t, err)
163
khenaidoob64fc8a2019-11-27 15:08:19 -0500164 assert.NotEqual(t, "", ld.Id)
165 assert.NotEqual(t, uint64(0), ld.DatapathId)
166 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
167 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
168 assert.NotEqual(t, "", ld.RootDeviceId)
169 assert.NotEqual(t, "", ld.Desc.SerialNum)
170 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
171 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
172 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400173 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500174 assert.Equal(t, oltDevice.ParentId, ld.Id)
175 //Expected port no
176 expectedPortNo := make(map[uint32]bool)
177 expectedPortNo[uint32(2)] = false
178 for i := 0; i < nb.numONUPerOLT; i++ {
179 expectedPortNo[uint32(i+100)] = false
180 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400181 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500182 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
183 assert.Equal(t, uint32(4), p.OfpPort.State)
184 expectedPortNo[p.OfpPort.PortNo] = true
185 if strings.HasPrefix(p.Id, "nni") {
186 assert.Equal(t, true, p.RootPort)
187 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
188 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
189 } else {
190 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
191 assert.Equal(t, false, p.RootPort)
192 }
193 }
194}
195
Kent Hagerman2b216042020-04-03 18:28:56 -0400196func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500197 // Get the latest set of devices
198 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
199 assert.Nil(t, err)
200 assert.NotNil(t, devices)
201
khenaidoo67b22152020-03-02 16:01:25 -0500202 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500203 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
204 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
205 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500206
khenaidoo67b22152020-03-02 16:01:25 -0500207 var wg sync.WaitGroup
208 for _, device := range devices.Items {
209 wg.Add(1)
210 go func(wg *sync.WaitGroup, device *voltha.Device) {
211 // Wait until the device is in the right state
212 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
213 assert.Nil(t, err)
214
215 // Now, verify the details of the device. First get the latest update
216 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
217 assert.Nil(t, err)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400218 dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id})
219 assert.Nil(t, err)
khenaidoo67b22152020-03-02 16:01:25 -0500220 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
221 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
222 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
223 assert.Equal(t, d.Type, d.Adapter)
224 assert.NotEqual(t, "", d.MacAddress)
225 assert.NotEqual(t, "", d.SerialNumber)
226
227 if d.Type == "olt_adapter_mock" {
228 assert.Equal(t, true, d.Root)
229 assert.NotEqual(t, "", d.Id)
230 assert.NotEqual(t, "", d.ParentId)
231 assert.Nil(t, d.ProxyAddress)
232 } else if d.Type == "onu_adapter_mock" {
233 assert.Equal(t, false, d.Root)
234 assert.NotEqual(t, uint32(0), d.Vlan)
235 assert.NotEqual(t, "", d.Id)
236 assert.NotEqual(t, "", d.ParentId)
237 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
238 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500239 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500240 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500241 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400242 assert.Equal(t, 2, len(dPorts.Items))
243 for _, p := range dPorts.Items {
khenaidoo67b22152020-03-02 16:01:25 -0500244 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
245 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
246 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
247 assert.Equal(t, 0, len(p.Peers))
248 } else if p.Type == voltha.Port_PON_OLT {
249 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
250 assert.Equal(t, uint32(1), p.PortNo)
251 } else if p.Type == voltha.Port_PON_ONU {
252 assert.Equal(t, 1, len(p.Peers))
253 assert.Equal(t, uint32(1), p.PortNo)
254 } else {
255 assert.Error(t, errors.New("invalid-port"))
256 }
257 }
258 wg.Done()
259 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500260 }
khenaidoo67b22152020-03-02 16:01:25 -0500261 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500262}
263
Kent Hagerman2b216042020-04-03 18:28:56 -0400264func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500265 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
266 if err != nil {
267 return nil, err
268 }
269 for _, d := range devices.Items {
270 if d.Root == rootDevice {
271 return d, nil
272 }
273 }
274 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
275}
276
Kent Hagerman2b216042020-04-03 18:28:56 -0400277func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500278 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
279 assert.Nil(t, err)
280 assert.NotNil(t, lds)
281 assert.Equal(t, 0, len(lds.Items))
282 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
283 assert.Nil(t, err)
284 assert.NotNil(t, devices)
285 assert.Equal(t, 0, len(devices.Items))
286 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400287 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500288 assert.Nil(t, err)
289 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500290}
291
Kent Hagerman2b216042020-04-03 18:28:56 -0400292func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000293 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500294 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
295 assert.Nil(t, err)
296 assert.NotNil(t, adapters)
297 assert.Equal(t, 2, len(adapters.Items))
298 for _, a := range adapters.Items {
299 switch a.Id {
300 case nb.oltAdapterName:
301 assert.Equal(t, "Voltha-olt", a.Vendor)
302 case nb.onuAdapterName:
303 assert.Equal(t, "Voltha-onu", a.Vendor)
304 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000305 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500306 }
307 }
308 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
309 assert.Nil(t, err)
310 assert.NotNil(t, deviceTypes)
311 assert.Equal(t, 2, len(deviceTypes.Items))
312 for _, dt := range deviceTypes.Items {
313 switch dt.Id {
314 case nb.oltAdapterName:
315 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
316 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
317 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
318 case nb.onuAdapterName:
319 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
320 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
321 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
322 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000323 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500324 }
325 }
326}
327
Kent Hagerman2b216042020-04-03 18:28:56 -0400328func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500329 // Create a valid device
330 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
331 assert.Nil(t, err)
332 assert.NotNil(t, oltDevice)
333 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
334 assert.Nil(t, err)
335 assert.NotNil(t, device)
336 assert.Equal(t, oltDevice.String(), device.String())
337
338 // Try to create the same device
339 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
340 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400341 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500342
343 // Try to create a device with invalid data
344 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
345 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530346 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500347
348 // Ensure we only have 1 device in the Core
349 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
350 assert.Nil(t, err)
351 assert.NotNil(t, devices)
352 assert.Equal(t, 1, len(devices.Items))
353 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
354
355 //Remove the device
356 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
357 assert.Nil(t, err)
358
359 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
360 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
361 return devices != nil && len(devices.Items) == 0
362 }
khenaidoo442e7c72020-03-10 16:13:48 -0400363 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500364 assert.Nil(t, err)
365}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530366func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) {
367 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
368 var wg sync.WaitGroup
369 wg.Add(1)
370 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
371
372 // Enable the oltDevice
373 _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
374 assert.Nil(t, err)
375
376 // Wait for the logical device to be in the ready state
377 var vldFunction = func(ports []*voltha.LogicalPort) bool {
378 return len(ports) == nb.numONUPerOLT+1
379 }
380 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
381 assert.Nil(t, err)
382
383 // Verify that the devices have been setup correctly
384 nb.verifyDevices(t, nbi)
385
386 // Get latest oltDevice data
387 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
388 assert.Nil(t, err)
389
390 // Verify that the logical device has been setup correctly
391 nb.verifyLogicalDevices(t, oltDevice, nbi)
392
393 // Wait until all flows has been sent to the devices successfully
394 wg.Wait()
395
396}
397func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
398 // Create a valid device
399 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
400 assert.Nil(t, err)
401 assert.NotNil(t, oltDevice)
402 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
403 assert.Nil(t, err)
404 assert.NotNil(t, device)
405 assert.Equal(t, oltDevice.String(), device.String())
406
407 // Ensure we only have 1 device in the Core
408 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
409 assert.Nil(t, err)
410 assert.NotNil(t, devices)
411 assert.Equal(t, 1, len(devices.Items))
412 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
413
414 //Remove the device forcefully
415 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
416 assert.Nil(t, err)
417
418 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
419 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
420 return devices != nil && len(devices.Items) == 0
421 }
422 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
423 assert.Nil(t, err)
424}
425
426func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
427 // Create a valid device
428 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
429 assert.Nil(t, err)
430 assert.NotNil(t, oltDevice)
431 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
432 assert.Nil(t, err)
433 assert.NotNil(t, device)
434 assert.Equal(t, oltDevice.String(), device.String())
435
436 nb.enableDevice(t, nbi, oltDevice)
437
438 //Remove the device forcefully
439 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
440 assert.Nil(t, err)
441
442 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
443 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
444 return devices != nil && len(devices.Items) == 0
445 }
446 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
447 assert.Nil(t, err)
448}
449
450func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
451 // Create a valid device
452 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
453 assert.Nil(t, err)
454 assert.NotNil(t, oltDevice)
455 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
456 assert.Nil(t, err)
457 assert.NotNil(t, device)
458 assert.Equal(t, oltDevice.String(), device.String())
459
460 // Ensure we only have 1 device in the Core
461 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
462 assert.Nil(t, err)
463 assert.NotNil(t, devices)
464 assert.Equal(t, 1, len(devices.Items))
465 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
466
467 //Remove the device forcefully
468 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
469 assert.Nil(t, err)
470
471 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
472 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
473 return devices != nil && len(devices.Items) == 0
474 }
475 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
476 assert.Nil(t, err)
477}
478
479func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
480 // Create a valid device
481 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
482 assert.Nil(t, err)
483 assert.NotNil(t, oltDevice)
484 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
485 assert.Nil(t, err)
486 assert.NotNil(t, device)
487 assert.Equal(t, oltDevice.String(), device.String())
488
489 nb.enableDevice(t, nbi, oltDevice)
490
491 //Remove the device
492 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
493 assert.Nil(t, err)
494
495 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
496 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
497 return devices != nil && len(devices.Items) == 0
498 }
499 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
500 assert.Nil(t, err)
501}
502
503func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
504 // Create a valid device
505 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
506 assert.Nil(t, err)
507 assert.NotNil(t, oltDevice)
508 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
509 assert.Nil(t, err)
510 assert.NotNil(t, device)
511 assert.Equal(t, oltDevice.String(), device.String())
512
513 nb.enableDevice(t, nbi, oltDevice)
514 nb.oltAdapter.SetDeleteAction(true)
515 //Remove the device
516 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
517 assert.Nil(t, err)
518
519 //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout
520 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
521 return devices != nil && len(devices.Items) == 0
522 }
523 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
524 assert.Nil(t, err)
525
526}
527
528func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
529 // Create a valid device
530 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
531 assert.Nil(t, err)
532 assert.NotNil(t, oltDevice)
533 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
534 assert.Nil(t, err)
535 assert.NotNil(t, device)
536 assert.Equal(t, oltDevice.String(), device.String())
537
538 nb.enableDevice(t, nbi, oltDevice)
539
540 nb.oltAdapter.SetDeleteAction(true)
541 //Remove the device
542 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
543 assert.Nil(t, err)
544
545 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
546 var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
547 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1)
548 }
549 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
550 assert.Nil(t, err)
551
552 nb.oltAdapter.SetDeleteAction(false)
553
554 // Now Force Delete this device
555 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
556 assert.Nil(t, err)
557
558 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
559 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
560 return devices != nil && len(devices.Items) == 0
561 }
562 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
563 assert.Nil(t, err)
564
565}
khenaidoob64fc8a2019-11-27 15:08:19 -0500566
Kent Hagerman2b216042020-04-03 18:28:56 -0400567func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500568 // Create a device that has no adapter registered
569 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
570 assert.Nil(t, err)
571 assert.NotNil(t, oltDeviceNoAdapter)
572
573 // Try to enable the oltDevice and check the error message
574 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
575 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400576 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500577
578 //Remove the device
579 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
580 assert.Nil(t, err)
581
582 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
583 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
584 return devices != nil && len(devices.Items) == 0
585 }
khenaidoo442e7c72020-03-10 16:13:48 -0400586 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500587 assert.Nil(t, err)
588
khenaidoo67b22152020-03-02 16:01:25 -0500589 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
590 var wg sync.WaitGroup
591 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400592 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500593
khenaidoob64fc8a2019-11-27 15:08:19 -0500594 // Create the device with valid data
595 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
596 assert.Nil(t, err)
597 assert.NotNil(t, oltDevice)
598
599 // Verify oltDevice exist in the core
600 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
601 assert.Nil(t, err)
602 assert.Equal(t, 1, len(devices.Items))
603 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
604
605 // Enable the oltDevice
606 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
607 assert.Nil(t, err)
608
609 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400610 var vldFunction = func(ports []*voltha.LogicalPort) bool {
611 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500612 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400613 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500614 assert.Nil(t, err)
615
616 // Verify that the devices have been setup correctly
617 nb.verifyDevices(t, nbi)
618
619 // Get latest oltDevice data
620 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
621 assert.Nil(t, err)
622
623 // Verify that the logical device has been setup correctly
624 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500625
626 // Wait until all flows has been sent to the devices successfully
627 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500628}
629
Kent Hagerman2b216042020-04-03 18:28:56 -0400630func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500631 //Get an OLT device
632 oltDevice, err := nb.getADevice(true, nbi)
633 assert.Nil(t, err)
634 assert.NotNil(t, oltDevice)
635
636 // Disable the oltDevice
637 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
638 assert.Nil(t, err)
639
640 // Wait for the old device to be disabled
641 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
642 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
643 }
644 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
645 assert.Nil(t, err)
646
647 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400648 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500649 assert.Nil(t, err)
650 for _, onu := range onuDevices.Items {
651 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
652 assert.Nil(t, err)
653 }
654
655 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400656 var vlFunction = func(ports []*voltha.LogicalPort) bool {
657 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500658 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
659 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
660 return false
661 }
662 }
663 return true
664 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400665 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500666 assert.Nil(t, err)
667
668 // Reenable the oltDevice
669 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
670 assert.Nil(t, err)
671
672 // Wait for the old device to be enabled
673 vdFunction = func(device *voltha.Device) bool {
674 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
675 }
676 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
677 assert.Nil(t, err)
678
679 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400680 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500681 assert.Nil(t, err)
682 for _, onu := range onuDevices.Items {
683 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
684 assert.Nil(t, err)
685 }
686
687 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400688 vlFunction = func(ports []*voltha.LogicalPort) bool {
689 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500690 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
691 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
692 return false
693 }
694 }
695 return true
696 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400697 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500698 assert.Nil(t, err)
699}
700
Kent Hagerman2b216042020-04-03 18:28:56 -0400701func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500702 //Get an OLT device
703 oltDevice, err := nb.getADevice(true, nbi)
704 assert.Nil(t, err)
705 assert.NotNil(t, oltDevice)
706
707 // Disable the oltDevice
708 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
709 assert.Nil(t, err)
710
711 // Wait for the olt device to be disabled
712 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
713 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
714 }
715 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
716 assert.Nil(t, err)
717
718 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400719 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500720 assert.Nil(t, err)
721 for _, onu := range onuDevices.Items {
722 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
723 assert.Nil(t, err)
724 }
725
726 // Delete the oltDevice
727 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
728 assert.Nil(t, err)
729
730 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
731 return devices != nil && len(devices.Items) == 0
732 }
733 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
734 assert.Nil(t, err)
735
736 // Wait for absence of logical device
737 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
738 return lds != nil && len(lds.Items) == 0
739 }
740
741 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
742 assert.Nil(t, err)
743}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400744
745func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400746 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
747 if len(devices.Items) == 0 {
748 // Nothing to do
749 return
750 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400751 //Get an OLT device
752 oltDevice, err := nb.getADevice(true, nbi)
753 assert.Nil(t, err)
754 assert.NotNil(t, oltDevice)
755
756 // Delete the oltDevice
757 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
758 assert.Nil(t, err)
759
760 // Wait for all devices to be deleted
761 vFunction := func(devices *voltha.Devices) bool {
762 return devices != nil && len(devices.Items) == 0
763 }
764 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
765 assert.Nil(t, err)
766
767 // Wait for absence of logical device
768 vlFunction := func(lds *voltha.LogicalDevices) bool {
769 return lds != nil && len(lds.Items) == 0
770 }
771
772 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
773 assert.Nil(t, err)
774}
775
Kent Hagerman2b216042020-04-03 18:28:56 -0400776func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500777 //Create the device with valid data
778 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
779 assert.Nil(t, err)
780 assert.NotNil(t, oltDevice)
781
782 //Get an OLT device
783 oltDevice, err = nb.getADevice(true, nbi)
784 assert.Nil(t, err)
785 assert.NotNil(t, oltDevice)
786
787 // Enable the oltDevice
788 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
789 assert.Nil(t, err)
790
791 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400792 var vldFunction = func(ports []*voltha.LogicalPort) bool {
793 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500794 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400795 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500796 assert.Nil(t, err)
797
798 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400799 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500800 assert.Nil(t, err)
801
802 // Wait for the all onu devices to be enabled
803 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
804 return device.AdminState == voltha.AdminState_ENABLED
805 }
806 for _, onu := range onuDevices.Items {
807 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
808 assert.Nil(t, err)
809 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500810 // Wait for each onu device to get deleted
811 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
812 return device == nil
813 }
814
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500815 // Delete the onuDevice
816 for _, onu := range onuDevices.Items {
817 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
818 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500819 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
820 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500821 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500822
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500823 // Disable the oltDevice
824 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
825 assert.Nil(t, err)
826
827 // Wait for the olt device to be disabled
828 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
829 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
830 }
831 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
832 assert.Nil(t, err)
833
834 // Delete the oltDevice
835 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
836 assert.Nil(t, err)
837
838 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
839 return devices != nil && len(devices.Items) == 0
840 }
841 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
842 assert.Nil(t, err)
843}
Kent Hagerman2b216042020-04-03 18:28:56 -0400844func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500845 //Get an OLT device
846 var cp *voltha.Port
847 oltDevice, err := nb.getADevice(true, nbi)
848 assert.Nil(t, err)
849 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400850 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
851 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500852
Kent Hagerman2a07b862020-06-19 15:23:07 -0400853 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500854 if cp.Type == voltha.Port_PON_OLT {
855 break
856 }
857
858 }
859 assert.NotNil(t, cp)
860 cp.DeviceId = oltDevice.Id
861
862 // Disable the NW Port of oltDevice
863 _, err = nbi.DisablePort(getContext(), cp)
864 assert.Nil(t, err)
865 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400866 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
867 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500868 if port.PortNo == cp.PortNo {
869 return port.AdminState == voltha.AdminState_DISABLED
870 }
871 }
872 return false
873 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400874 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500875 assert.Nil(t, err)
876 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400877 var vlFunction = func(ports []*voltha.LogicalPort) bool {
878 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500879 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
880 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
881 return false
882 }
883 }
884 return true
885 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400886 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500887 assert.Nil(t, err)
888
889 // Enable the NW Port of oltDevice
890 _, err = nbi.EnablePort(getContext(), cp)
891 assert.Nil(t, err)
892
893 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400894 vdFunction = func(ports *voltha.Ports) bool {
895 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500896 if port.PortNo == cp.PortNo {
897 return port.AdminState == voltha.AdminState_ENABLED
898 }
899 }
900 return false
901 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400902 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500903 assert.Nil(t, err)
904 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400905 vlFunction = func(ports []*voltha.LogicalPort) bool {
906 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500907 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
908 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
909 return false
910 }
911 }
912 return true
913 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400914 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500915 assert.Nil(t, err)
916
917 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400918 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500919 if cp.Type != voltha.Port_PON_OLT {
920 break
921 }
922
923 }
924 assert.NotNil(t, cp)
925 cp.DeviceId = oltDevice.Id
926
927 // Disable the NW Port of oltDevice
928 _, err = nbi.DisablePort(getContext(), cp)
929 assert.NotNil(t, err)
930
931}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500932
Kent Hagerman2b216042020-04-03 18:28:56 -0400933func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700934 //Get an OLT device
935 oltDevice, err := nb.getADevice(true, nbi)
936 assert.Nil(t, err)
937 assert.NotNil(t, oltDevice)
938 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
939 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
940
941 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400942 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700943 assert.Nil(t, err)
944 assert.NotNil(t, onuDevices)
945 assert.Greater(t, len(onuDevices.Items), 0)
946
947 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
948 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
949 assert.Nil(t, err)
950
951 var vlFunction0 = func(d *voltha.Device) bool {
952 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
953 }
954
955 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
956 assert.Nil(t, err)
957
958 // Wait for the logical device to satisfy the expected condition
959 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
960 return ld == nil
961 }
962
963 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
964 assert.Nil(t, err)
965
966 // Wait for the device to satisfy the expected condition (device does not have flows)
967 var vlFunction2 = func(d *voltha.Device) bool {
968 var deviceFlows *ofp.Flows
969 var err error
970 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
971 return false
972 }
973 return len(deviceFlows.Items) == 0
974 }
975
976 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
977 assert.Nil(t, err)
978
979 // Wait for the device to satisfy the expected condition (there are no child devices)
980 var vlFunction3 = func(d *voltha.Device) bool {
981 var devices *voltha.Devices
982 var err error
983 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
984 return false
985 }
986 for _, device := range devices.Items {
987 if device.ParentId == d.Id {
988 // We have a child device still left
989 return false
990 }
991 }
992 return true
993 }
994
995 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
996 assert.Nil(t, err)
997
998 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
999 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -04001000 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -07001001 assert.Nil(t, err)
1002
1003 // Verify the device connection and operation states
1004 oltDevice, err = nb.getADevice(true, nbi)
1005 assert.Nil(t, err)
1006 assert.NotNil(t, oltDevice)
1007 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
1008 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
1009
1010 // Wait for the logical device to satisfy the expected condition
1011 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1012 return ld != nil
1013 }
1014 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1015 assert.Nil(t, err)
1016
1017 // Verify that logical device is created again
1018 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1019 assert.Nil(t, err)
1020 assert.NotNil(t, logicalDevices)
1021 assert.Equal(t, 1, len(logicalDevices.Items))
1022
1023 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001024 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001025 assert.Nil(t, err)
1026 assert.NotNil(t, onuDevices)
1027 assert.Equal(t, 0, len(onuDevices.Items))
1028}
1029
Kent Hagerman2b216042020-04-03 18:28:56 -04001030func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001031 // -----------------------------------------------------------------------
1032 // SubTest 1: Omci test action should fail due to nonexistent device id
1033
1034 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1035 _, err := nbi.StartOmciTestAction(getContext(), request)
1036 assert.NotNil(t, err)
1037 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1038
1039 // -----------------------------------------------------------------------
1040 // SubTest 2: Error should be returned for device with no adapter registered
1041
1042 // Create a device that has no adapter registered
1043 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1044 assert.Nil(t, err)
1045 assert.NotNil(t, deviceNoAdapter)
1046
1047 // Omci test action should fail due to nonexistent adapter
1048 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1049 _, err = nbi.StartOmciTestAction(getContext(), request)
1050 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001051 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001052
1053 //Remove the device
1054 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1055 assert.Nil(t, err)
1056
1057 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1058 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1059 return devices != nil && len(devices.Items) == 0
1060 }
1061 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1062 assert.Nil(t, err)
1063
1064 // -----------------------------------------------------------------------
1065 // SubTest 3: Omci test action should succeed on valid ONU
1066
1067 // Create the device with valid data
1068 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1069 assert.Nil(t, err)
1070 assert.NotNil(t, oltDevice)
1071
1072 // Verify oltDevice exist in the core
1073 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1074 assert.Nil(t, err)
1075 assert.Equal(t, 1, len(devices.Items))
1076 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1077
1078 // Enable the oltDevice
1079 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1080 assert.Nil(t, err)
1081
1082 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001083 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1084 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001085 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001086 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001087 assert.Nil(t, err)
1088
1089 // Wait for the olt device to be enabled
1090 vdFunction := func(device *voltha.Device) bool {
1091 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1092 }
1093 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1094 assert.Nil(t, err)
1095
Kent Hagerman2b216042020-04-03 18:28:56 -04001096 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001097 assert.Nil(t, err)
1098 assert.Greater(t, len(onuDevices.Items), 0)
1099
1100 onuDevice := onuDevices.Items[0]
1101
1102 // Omci test action should succeed
1103 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1104 resp, err := nbi.StartOmciTestAction(getContext(), request)
1105 assert.Nil(t, err)
1106 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1107
1108 //Remove the device
1109 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1110 assert.Nil(t, err)
1111 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1112 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1113 assert.Nil(t, err)
1114}
1115
khenaidoo67b22152020-03-02 16:01:25 -05001116func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1117 matchFields := make([]*ofp.OfpOxmField, 0)
1118 for _, val := range fa.MatchFields {
1119 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1120 }
1121 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1122}
1123
1124func createMetadata(cTag int, techProfile int, port int) uint64 {
1125 md := 0
1126 md = (md | (cTag & 0xFFFF)) << 16
1127 md = (md | (techProfile & 0xFFFF)) << 32
1128 return uint64(md | (port & 0xFFFFFFFF))
1129}
1130
khenaidoo8b4abbf2020-04-24 17:04:30 -04001131func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001132 expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001133 if flowAddFail {
1134 expectedNumFlows = 0
1135 }
1136 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001137 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -07001138 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
1139 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001140 }
1141 // No timeout implies a success
1142 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1143 assert.Nil(t, err)
1144}
1145
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001146func (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 -05001147 // Send flows for the parent device
1148 var nniPorts []*voltha.LogicalPort
1149 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001150 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001151 if p.RootPort {
1152 nniPorts = append(nniPorts, p)
1153 } else {
1154 uniPorts = append(uniPorts, p)
1155 }
1156 }
1157 assert.Equal(t, 1, len(nniPorts))
1158 //assert.Greater(t, len(uniPorts), 1 )
1159 nniPort := nniPorts[0].OfpPort.PortNo
1160 maxInt32 := uint64(0xFFFFFFFF)
1161 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1162 var fa *flows.FlowArgs
1163 fa = &flows.FlowArgs{
1164 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1165 MatchFields: []*ofp.OfpOxmOfbField{
1166 flows.InPort(nniPort),
1167 flows.EthType(35020),
1168 },
1169 Actions: []*ofp.OfpAction{
1170 flows.Output(controllerPortMask),
1171 },
1172 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001173 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001174 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1175 assert.Nil(t, err)
1176
1177 fa = &flows.FlowArgs{
1178 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1179 MatchFields: []*ofp.OfpOxmOfbField{
1180 flows.InPort(nniPort),
1181 flows.EthType(2048),
1182 flows.IpProto(17),
1183 flows.UdpSrc(67),
1184 flows.UdpDst(68),
1185 },
1186 Actions: []*ofp.OfpAction{
1187 flows.Output(controllerPortMask),
1188 },
1189 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001190 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001191 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1192 assert.Nil(t, err)
1193
1194 fa = &flows.FlowArgs{
1195 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1196 MatchFields: []*ofp.OfpOxmOfbField{
1197 flows.InPort(nniPort),
1198 flows.EthType(34525),
1199 flows.IpProto(17),
1200 flows.UdpSrc(546),
1201 flows.UdpDst(547),
1202 },
1203 Actions: []*ofp.OfpAction{
1204 flows.Output(controllerPortMask),
1205 },
1206 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001207 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001208 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1209 assert.Nil(t, err)
1210
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001211 fa = &flows.FlowArgs{
1212 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1213 MatchFields: []*ofp.OfpOxmOfbField{
1214 flows.InPort(nniPort),
1215 flows.EthType(34915),
1216 },
1217 Actions: []*ofp.OfpAction{
1218 flows.Output(controllerPortMask),
1219 },
1220 }
1221 flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1222 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP)
1223 assert.Nil(t, err)
1224
khenaidoo67b22152020-03-02 16:01:25 -05001225 return len(nniPorts), len(uniPorts)
1226}
1227
Kent Hagerman2b216042020-04-03 18:28:56 -04001228func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001229 maxInt32 := uint64(0xFFFFFFFF)
1230 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1231 fa := &flows.FlowArgs{
1232 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},
1233 MatchFields: []*ofp.OfpOxmOfbField{
1234 flows.InPort(port.PortNo),
1235 flows.EthType(34958),
1236 flows.VlanVid(8187),
1237 },
1238 Actions: []*ofp.OfpAction{
1239 flows.Output(controllerPortMask),
1240 },
1241 }
1242 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001243 maxTries := 3
1244 var err error
1245 for {
1246 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1247 if maxTries < 3 {
1248 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1249 }
1250 break
1251 }
1252 t.Log("Sending EAPOL flows fail:", err)
1253 time.Sleep(50 * time.Millisecond)
1254 maxTries--
1255 if maxTries == 0 {
1256 break
1257 }
1258 }
khenaidoo67b22152020-03-02 16:01:25 -05001259 assert.Nil(t, err)
1260}
1261
khenaidoo0db4c812020-05-27 15:27:30 -04001262func (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 -05001263 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001264
1265 // Clear any existing flows on the adapters
1266 nb.oltAdapter.ClearFlows()
1267 nb.onuAdapter.ClearFlows()
1268
khenaidoo8b4abbf2020-04-24 17:04:30 -04001269 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001270 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1271 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001272
khenaidoo67b22152020-03-02 16:01:25 -05001273 // Wait until a logical device is ready
1274 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1275 if lds == nil || len(lds.Items) != 1 {
1276 return false
1277 }
1278 // Ensure there are both NNI ports and at least one UNI port on the logical device
1279 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001280 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1281 if err != nil {
1282 return false
1283 }
khenaidoo67b22152020-03-02 16:01:25 -05001284 nniPort := false
1285 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001286 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001287 nniPort = nniPort || p.RootPort == true
1288 uniPort = uniPort || p.RootPort == false
1289 if nniPort && uniPort {
1290 return true
1291 }
1292 }
1293 return false
1294 }
1295 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1296 assert.Nil(t, err)
1297
1298 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1299 assert.Nil(t, err)
1300 assert.NotNil(t, logicalDevices)
1301 assert.Equal(t, 1, len(logicalDevices.Items))
1302
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001303 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001304 meterID := rand.Uint32()
1305
1306 // Add a meter to the logical device
1307 meterMod := &ofp.OfpMeterMod{
1308 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1309 Flags: rand.Uint32(),
1310 MeterId: meterID,
1311 Bands: []*ofp.OfpMeterBandHeader{
1312 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1313 Rate: rand.Uint32(),
1314 BurstSize: rand.Uint32(),
1315 Data: nil,
1316 },
1317 },
1318 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001319 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1320 assert.Nil(t, err)
1321
1322 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001323 assert.Nil(t, err)
1324
1325 // Send initial set of Trap flows
1326 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001327 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001328
1329 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001330 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001331 processedNniLogicalPorts := 0
1332 processedUniLogicalPorts := 0
1333
Kent Hagerman45a13e42020-04-13 12:23:50 -04001334 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001335 startingVlan++
1336 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1337 ps := portStatus.PortStatus
1338 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001339 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001340 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001341 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001342 } else {
1343 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001344 }
1345 }
1346 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001347
1348 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001349 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001350 break
1351 }
1352 }
1353 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001354 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001355
khenaidoo8b4abbf2020-04-24 17:04:30 -04001356 // Wait until all flows have been sent to the OLT adapters (or all failed)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001357 expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001358 if flowAddFail {
1359 expectedFlowCount = 0
1360 }
khenaidoo67b22152020-03-02 16:01:25 -05001361 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001362 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001363 }
1364 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1365 assert.Nil(t, err)
1366
khenaidoo8b4abbf2020-04-24 17:04:30 -04001367 // Wait until all flows have been sent to the ONU adapters (or all failed)
1368 expectedFlowCount = numUNIPorts
1369 if flowAddFail {
1370 expectedFlowCount = 0
1371 }
khenaidoo67b22152020-03-02 16:01:25 -05001372 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001373 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001374 }
1375 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1376 assert.Nil(t, err)
1377}
1378
khenaidoo8b4abbf2020-04-24 17:04:30 -04001379func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1380
1381 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1382 var wg sync.WaitGroup
1383 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001384 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001385
1386 // Create the device with valid data
1387 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1388 assert.Nil(t, err)
1389 assert.NotNil(t, oltDevice)
1390
1391 // Verify oltDevice exist in the core
1392 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1393 assert.Nil(t, err)
1394 assert.Equal(t, 1, len(devices.Items))
1395 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1396
1397 // Enable the oltDevice
1398 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1399 assert.Nil(t, err)
1400
1401 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001402 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1403 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001404 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001405 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001406 assert.Nil(t, err)
1407
1408 // Verify that the devices have been setup correctly
1409 nb.verifyDevices(t, nbi)
1410
1411 // Get latest oltDevice data
1412 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1413 assert.Nil(t, err)
1414
1415 // Verify that the logical device has been setup correctly
1416 nb.verifyLogicalDevices(t, oltDevice, nbi)
1417
1418 // Wait until all flows has been sent to the devices successfully
1419 wg.Wait()
1420}
1421
Matteo Scandolod525ae32020-04-02 17:27:29 -07001422func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001423 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001424 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001425 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001426 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001427 }
1428 defer f.Close()
1429 runtime.SetBlockProfileRate(1)
1430 runtime.SetMutexProfileFraction(-1)
1431 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001432 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001433 }
1434 defer pprof.StopCPUProfile()
1435
khenaidoo442e7c72020-03-10 16:13:48 -04001436 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1437
Rohan Agrawal31f21802020-06-12 05:38:46 +00001438 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001439 assert.NotNil(t, nb)
1440
Rohan Agrawal31f21802020-06-12 05:38:46 +00001441 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001442
1443 // Start the Core
1444 nb.startCore(false)
1445
1446 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001447 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001448
1449 // 1. Basic test with no data in Core
1450 nb.testCoreWithoutData(t, nbi)
1451
1452 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001453 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 -07001454 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1455 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001456
1457 // 2. Test adapter registration
1458 nb.testAdapterRegistration(t, nbi)
1459
khenaidoo8b4abbf2020-04-24 17:04:30 -04001460 numberOfTestRuns := 2
1461 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301462
1463 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001464 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001465
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301466 // 4. Test Delete Device Scenarios
1467 nb.testForceDeletePreProvDevice(t, nbi)
1468 nb.testDeletePreProvDevice(t, nbi)
1469 nb.testForceDeleteEnabledDevice(t, nbi)
1470 nb.testDeleteEnabledDevice(t, nbi)
1471 nb.testForceDeleteDeviceFailure(t, nbi)
1472 nb.testDeleteDeviceFailure(t, nbi)
1473
1474 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001475 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001476
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301477 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001478 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001479
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301480 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001481 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001482
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301483 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001484 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1485
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301486 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001487 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001488
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301489 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001490 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001491
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301492 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001493 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001494
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301495 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001496 nb.deleteAllDevices(t, nbi)
1497
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301498 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001499 nb.testFlowAddFailure(t, nbi)
1500
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301501 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001502 nb.deleteAllDevices(t, nbi)
1503 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001504}