blob: 622257df876b77ca792811bb25b947d00be1fefd [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"
yasin sapli5458a1c2021-06-14 22:24:38 +000040 "github.com/opencord/voltha-lib-go/v5/pkg/db"
41 "github.com/opencord/voltha-lib-go/v5/pkg/events"
42 "github.com/opencord/voltha-lib-go/v5/pkg/flows"
43 "github.com/opencord/voltha-lib-go/v5/pkg/kafka"
44 mock_etcd "github.com/opencord/voltha-lib-go/v5/pkg/mocks/etcd"
45 mock_kafka "github.com/opencord/voltha-lib-go/v5/pkg/mocks/kafka"
Maninderdfadc982020-10-28 14:04:33 +053046 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()
David K. Bainbridge6080c172021-07-24 00:22:28 +000098 cfg := &config.RWCoreFlags{}
99 cfg.ParseCommandArguments([]string{}) // sets defaults
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700100 cfg.CoreTopic = "rw_core"
Himani Chawlab4c25912020-11-12 17:16:38 +0530101 cfg.EventTopic = "voltha.events"
khenaidoo442e7c72020-03-10 16:13:48 -0400102 cfg.DefaultRequestTimeout = nb.defaultTimeout
103 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +0000104 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500105 grpcPort, err := freeport.GetFreePort()
106 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000107 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500108 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000109 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500110 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000111 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400112 backend := &db.Backend{
113 Client: client,
114 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000115 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400116 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700117 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400118 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000119 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400120 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700121 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400122
123 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400124 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000125 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
Himani Chawlab4c25912020-11-12 17:16:38 +0530126 eventProxy := events.NewEventProxy(events.MsgClient(nb.kEventClient), events.MsgTopic(kafka.Topic{Name: cfg.EventTopic}))
Maninder0aabf0c2021-03-17 14:55:14 +0530127 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg, nb.coreInstanceID, eventProxy)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400128 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400129
Rohan Agrawal31f21802020-06-12 05:38:46 +0000130 if err := nb.kmp.Start(ctx); err != nil {
131 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400132 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400133 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000134 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
135 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400136 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500137}
138
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500140 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000141 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500142 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400143 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000144 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500145 }
146 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000147 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500148 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530149 if nb.kEventClient != nil {
150 nb.kEventClient.Stop(ctx)
151 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500152}
153
Kent Hagerman2b216042020-04-03 18:28:56 -0400154func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500155 // Get the latest set of logical devices
156 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
157 assert.Nil(t, err)
158 assert.NotNil(t, logicalDevices)
159 assert.Equal(t, 1, len(logicalDevices.Items))
160
161 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400162 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
163 assert.Nil(t, err)
164
khenaidoob64fc8a2019-11-27 15:08:19 -0500165 assert.NotEqual(t, "", ld.Id)
166 assert.NotEqual(t, uint64(0), ld.DatapathId)
167 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
168 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
169 assert.NotEqual(t, "", ld.RootDeviceId)
170 assert.NotEqual(t, "", ld.Desc.SerialNum)
171 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
172 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
173 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400174 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500175 assert.Equal(t, oltDevice.ParentId, ld.Id)
176 //Expected port no
177 expectedPortNo := make(map[uint32]bool)
178 expectedPortNo[uint32(2)] = false
179 for i := 0; i < nb.numONUPerOLT; i++ {
180 expectedPortNo[uint32(i+100)] = false
181 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400182 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500183 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
184 assert.Equal(t, uint32(4), p.OfpPort.State)
185 expectedPortNo[p.OfpPort.PortNo] = true
186 if strings.HasPrefix(p.Id, "nni") {
187 assert.Equal(t, true, p.RootPort)
188 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
189 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
190 } else {
191 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
192 assert.Equal(t, false, p.RootPort)
193 }
194 }
195}
196
Kent Hagerman2b216042020-04-03 18:28:56 -0400197func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500198 // Get the latest set of devices
199 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
200 assert.Nil(t, err)
201 assert.NotNil(t, devices)
202
khenaidoo67b22152020-03-02 16:01:25 -0500203 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500204 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
205 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
206 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500207
khenaidoo67b22152020-03-02 16:01:25 -0500208 var wg sync.WaitGroup
209 for _, device := range devices.Items {
210 wg.Add(1)
211 go func(wg *sync.WaitGroup, device *voltha.Device) {
212 // Wait until the device is in the right state
213 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
214 assert.Nil(t, err)
215
216 // Now, verify the details of the device. First get the latest update
217 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
218 assert.Nil(t, err)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400219 dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id})
220 assert.Nil(t, err)
khenaidoo67b22152020-03-02 16:01:25 -0500221 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
222 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
223 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
224 assert.Equal(t, d.Type, d.Adapter)
225 assert.NotEqual(t, "", d.MacAddress)
226 assert.NotEqual(t, "", d.SerialNumber)
227
228 if d.Type == "olt_adapter_mock" {
229 assert.Equal(t, true, d.Root)
230 assert.NotEqual(t, "", d.Id)
231 assert.NotEqual(t, "", d.ParentId)
232 assert.Nil(t, d.ProxyAddress)
233 } else if d.Type == "onu_adapter_mock" {
234 assert.Equal(t, false, d.Root)
235 assert.NotEqual(t, uint32(0), d.Vlan)
236 assert.NotEqual(t, "", d.Id)
237 assert.NotEqual(t, "", d.ParentId)
238 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
239 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500240 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500241 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500242 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400243 assert.Equal(t, 2, len(dPorts.Items))
244 for _, p := range dPorts.Items {
khenaidoo67b22152020-03-02 16:01:25 -0500245 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
246 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
247 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
248 assert.Equal(t, 0, len(p.Peers))
249 } else if p.Type == voltha.Port_PON_OLT {
250 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
251 assert.Equal(t, uint32(1), p.PortNo)
252 } else if p.Type == voltha.Port_PON_ONU {
253 assert.Equal(t, 1, len(p.Peers))
254 assert.Equal(t, uint32(1), p.PortNo)
255 } else {
256 assert.Error(t, errors.New("invalid-port"))
257 }
258 }
259 wg.Done()
260 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500261 }
khenaidoo67b22152020-03-02 16:01:25 -0500262 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500263}
264
Kent Hagerman2b216042020-04-03 18:28:56 -0400265func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500266 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
267 if err != nil {
268 return nil, err
269 }
270 for _, d := range devices.Items {
271 if d.Root == rootDevice {
272 return d, nil
273 }
274 }
275 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
276}
277
Kent Hagerman2b216042020-04-03 18:28:56 -0400278func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500279 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
280 assert.Nil(t, err)
281 assert.NotNil(t, lds)
282 assert.Equal(t, 0, len(lds.Items))
283 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
284 assert.Nil(t, err)
285 assert.NotNil(t, devices)
286 assert.Equal(t, 0, len(devices.Items))
287 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400288 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500289 assert.Nil(t, err)
290 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500291}
292
Kent Hagerman2b216042020-04-03 18:28:56 -0400293func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000294 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500295 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
296 assert.Nil(t, err)
297 assert.NotNil(t, adapters)
298 assert.Equal(t, 2, len(adapters.Items))
299 for _, a := range adapters.Items {
300 switch a.Id {
301 case nb.oltAdapterName:
302 assert.Equal(t, "Voltha-olt", a.Vendor)
303 case nb.onuAdapterName:
304 assert.Equal(t, "Voltha-onu", a.Vendor)
305 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000306 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500307 }
308 }
309 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
310 assert.Nil(t, err)
311 assert.NotNil(t, deviceTypes)
312 assert.Equal(t, 2, len(deviceTypes.Items))
313 for _, dt := range deviceTypes.Items {
314 switch dt.Id {
315 case nb.oltAdapterName:
316 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
317 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
318 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
319 case nb.onuAdapterName:
320 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
321 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
322 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
323 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000324 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500325 }
326 }
327}
328
Kent Hagerman2b216042020-04-03 18:28:56 -0400329func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500330 // Create a valid device
331 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
332 assert.Nil(t, err)
333 assert.NotNil(t, oltDevice)
334 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
335 assert.Nil(t, err)
336 assert.NotNil(t, device)
337 assert.Equal(t, oltDevice.String(), device.String())
338
339 // Try to create the same device
340 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
341 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400342 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500343
344 // Try to create a device with invalid data
345 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
346 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530347 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500348
349 // Ensure we only have 1 device in the Core
350 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
351 assert.Nil(t, err)
352 assert.NotNil(t, devices)
353 assert.Equal(t, 1, len(devices.Items))
354 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
355
356 //Remove the device
357 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
358 assert.Nil(t, err)
359
360 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
361 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
362 return devices != nil && len(devices.Items) == 0
363 }
khenaidoo442e7c72020-03-10 16:13:48 -0400364 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500365 assert.Nil(t, err)
366}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530367func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) {
368 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
369 var wg sync.WaitGroup
370 wg.Add(1)
371 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
372
373 // Enable the oltDevice
374 _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
375 assert.Nil(t, err)
376
377 // Wait for the logical device to be in the ready state
378 var vldFunction = func(ports []*voltha.LogicalPort) bool {
379 return len(ports) == nb.numONUPerOLT+1
380 }
381 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
382 assert.Nil(t, err)
383
384 // Verify that the devices have been setup correctly
385 nb.verifyDevices(t, nbi)
386
387 // Get latest oltDevice data
388 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
389 assert.Nil(t, err)
390
391 // Verify that the logical device has been setup correctly
392 nb.verifyLogicalDevices(t, oltDevice, nbi)
393
394 // Wait until all flows has been sent to the devices successfully
395 wg.Wait()
396
397}
398func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
399 // Create a valid device
400 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
401 assert.Nil(t, err)
402 assert.NotNil(t, oltDevice)
403 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
404 assert.Nil(t, err)
405 assert.NotNil(t, device)
406 assert.Equal(t, oltDevice.String(), device.String())
407
408 // Ensure we only have 1 device in the Core
409 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
410 assert.Nil(t, err)
411 assert.NotNil(t, devices)
412 assert.Equal(t, 1, len(devices.Items))
413 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
414
415 //Remove the device forcefully
416 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
417 assert.Nil(t, err)
418
419 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
420 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
421 return devices != nil && len(devices.Items) == 0
422 }
423 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
424 assert.Nil(t, err)
425}
426
427func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
428 // Create a valid device
429 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
430 assert.Nil(t, err)
431 assert.NotNil(t, oltDevice)
432 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
433 assert.Nil(t, err)
434 assert.NotNil(t, device)
435 assert.Equal(t, oltDevice.String(), device.String())
436
437 nb.enableDevice(t, nbi, oltDevice)
438
439 //Remove the device forcefully
440 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
441 assert.Nil(t, err)
442
443 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
444 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
445 return devices != nil && len(devices.Items) == 0
446 }
447 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
448 assert.Nil(t, err)
449}
450
451func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
452 // Create a valid device
453 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
454 assert.Nil(t, err)
455 assert.NotNil(t, oltDevice)
456 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
457 assert.Nil(t, err)
458 assert.NotNil(t, device)
459 assert.Equal(t, oltDevice.String(), device.String())
460
461 // Ensure we only have 1 device in the Core
462 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
463 assert.Nil(t, err)
464 assert.NotNil(t, devices)
465 assert.Equal(t, 1, len(devices.Items))
466 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
467
468 //Remove the device forcefully
469 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
470 assert.Nil(t, err)
471
472 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
473 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
474 return devices != nil && len(devices.Items) == 0
475 }
476 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
477 assert.Nil(t, err)
478}
479
480func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
481 // Create a valid device
482 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
483 assert.Nil(t, err)
484 assert.NotNil(t, oltDevice)
485 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
486 assert.Nil(t, err)
487 assert.NotNil(t, device)
488 assert.Equal(t, oltDevice.String(), device.String())
489
490 nb.enableDevice(t, nbi, oltDevice)
491
492 //Remove the device
493 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
494 assert.Nil(t, err)
495
496 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
497 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
498 return devices != nil && len(devices.Items) == 0
499 }
500 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
501 assert.Nil(t, err)
502}
503
504func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
505 // Create a valid device
506 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
507 assert.Nil(t, err)
508 assert.NotNil(t, oltDevice)
509 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
510 assert.Nil(t, err)
511 assert.NotNil(t, device)
512 assert.Equal(t, oltDevice.String(), device.String())
513
514 nb.enableDevice(t, nbi, oltDevice)
515 nb.oltAdapter.SetDeleteAction(true)
516 //Remove the device
517 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
518 assert.Nil(t, err)
519
520 //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout
521 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
522 return devices != nil && len(devices.Items) == 0
523 }
524 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
525 assert.Nil(t, err)
526
527}
528
529func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
530 // Create a valid device
531 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
532 assert.Nil(t, err)
533 assert.NotNil(t, oltDevice)
534 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
535 assert.Nil(t, err)
536 assert.NotNil(t, device)
537 assert.Equal(t, oltDevice.String(), device.String())
538
539 nb.enableDevice(t, nbi, oltDevice)
540
541 nb.oltAdapter.SetDeleteAction(true)
542 //Remove the device
543 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
544 assert.Nil(t, err)
545
546 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
547 var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
Manindera496f852021-02-22 09:57:56 +0530548 state, err := nbi.GetTransientState(getContext(), oltDevice.Id)
549 if err != nil {
550 return false
551 }
552 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1) &&
553 state == voltha.DeviceTransientState_DELETE_FAILED
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530554 }
555 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
556 assert.Nil(t, err)
557
558 nb.oltAdapter.SetDeleteAction(false)
559
560 // Now Force Delete this device
561 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
562 assert.Nil(t, err)
563
564 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
565 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
566 return devices != nil && len(devices.Items) == 0
567 }
568 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
569 assert.Nil(t, err)
570
571}
khenaidoob64fc8a2019-11-27 15:08:19 -0500572
Kent Hagerman2b216042020-04-03 18:28:56 -0400573func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500574 // Create a device that has no adapter registered
575 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
576 assert.Nil(t, err)
577 assert.NotNil(t, oltDeviceNoAdapter)
578
579 // Try to enable the oltDevice and check the error message
580 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
581 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400582 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500583
584 //Remove the device
585 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
586 assert.Nil(t, err)
587
588 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
589 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
590 return devices != nil && len(devices.Items) == 0
591 }
khenaidoo442e7c72020-03-10 16:13:48 -0400592 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500593 assert.Nil(t, err)
594
khenaidoo67b22152020-03-02 16:01:25 -0500595 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
596 var wg sync.WaitGroup
597 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400598 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500599
khenaidoob64fc8a2019-11-27 15:08:19 -0500600 // Create the device with valid data
601 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
602 assert.Nil(t, err)
603 assert.NotNil(t, oltDevice)
604
605 // Verify oltDevice exist in the core
606 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
607 assert.Nil(t, err)
608 assert.Equal(t, 1, len(devices.Items))
609 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
610
611 // Enable the oltDevice
612 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
613 assert.Nil(t, err)
614
615 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400616 var vldFunction = func(ports []*voltha.LogicalPort) bool {
617 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500618 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400619 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500620 assert.Nil(t, err)
621
622 // Verify that the devices have been setup correctly
623 nb.verifyDevices(t, nbi)
624
625 // Get latest oltDevice data
626 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
627 assert.Nil(t, err)
628
629 // Verify that the logical device has been setup correctly
630 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500631
632 // Wait until all flows has been sent to the devices successfully
633 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500634}
635
Kent Hagerman2b216042020-04-03 18:28:56 -0400636func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500637 //Get an OLT device
638 oltDevice, err := nb.getADevice(true, nbi)
639 assert.Nil(t, err)
640 assert.NotNil(t, oltDevice)
641
642 // Disable the oltDevice
643 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
644 assert.Nil(t, err)
645
646 // Wait for the old device to be disabled
647 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
648 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
649 }
650 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
651 assert.Nil(t, err)
652
653 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400654 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500655 assert.Nil(t, err)
656 for _, onu := range onuDevices.Items {
657 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
658 assert.Nil(t, err)
659 }
660
661 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400662 var vlFunction = func(ports []*voltha.LogicalPort) bool {
663 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500664 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
665 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
666 return false
667 }
668 }
669 return true
670 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400671 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500672 assert.Nil(t, err)
673
674 // Reenable the oltDevice
675 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
676 assert.Nil(t, err)
677
678 // Wait for the old device to be enabled
679 vdFunction = func(device *voltha.Device) bool {
680 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
681 }
682 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
683 assert.Nil(t, err)
684
685 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400686 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500687 assert.Nil(t, err)
688 for _, onu := range onuDevices.Items {
689 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
690 assert.Nil(t, err)
691 }
692
693 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400694 vlFunction = func(ports []*voltha.LogicalPort) bool {
695 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500696 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
697 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
698 return false
699 }
700 }
701 return true
702 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400703 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500704 assert.Nil(t, err)
705}
706
Kent Hagerman2b216042020-04-03 18:28:56 -0400707func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500708 //Get an OLT device
709 oltDevice, err := nb.getADevice(true, nbi)
710 assert.Nil(t, err)
711 assert.NotNil(t, oltDevice)
712
713 // Disable the oltDevice
714 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
715 assert.Nil(t, err)
716
717 // Wait for the olt device to be disabled
718 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
719 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
720 }
721 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
722 assert.Nil(t, err)
723
724 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400725 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500726 assert.Nil(t, err)
727 for _, onu := range onuDevices.Items {
728 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
729 assert.Nil(t, err)
730 }
731
732 // Delete the oltDevice
733 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
734 assert.Nil(t, err)
735
736 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
737 return devices != nil && len(devices.Items) == 0
738 }
739 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
740 assert.Nil(t, err)
741
742 // Wait for absence of logical device
743 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
744 return lds != nil && len(lds.Items) == 0
745 }
746
747 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
748 assert.Nil(t, err)
749}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400750
751func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400752 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
753 if len(devices.Items) == 0 {
754 // Nothing to do
755 return
756 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400757 //Get an OLT device
758 oltDevice, err := nb.getADevice(true, nbi)
759 assert.Nil(t, err)
760 assert.NotNil(t, oltDevice)
761
762 // Delete the oltDevice
763 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
764 assert.Nil(t, err)
765
766 // Wait for all devices to be deleted
767 vFunction := func(devices *voltha.Devices) bool {
768 return devices != nil && len(devices.Items) == 0
769 }
770 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
771 assert.Nil(t, err)
772
773 // Wait for absence of logical device
774 vlFunction := func(lds *voltha.LogicalDevices) bool {
775 return lds != nil && len(lds.Items) == 0
776 }
777
778 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
779 assert.Nil(t, err)
780}
781
Kent Hagerman2b216042020-04-03 18:28:56 -0400782func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500783 //Create the device with valid data
784 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
785 assert.Nil(t, err)
786 assert.NotNil(t, oltDevice)
787
788 //Get an OLT device
789 oltDevice, err = nb.getADevice(true, nbi)
790 assert.Nil(t, err)
791 assert.NotNil(t, oltDevice)
792
793 // Enable the oltDevice
794 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
795 assert.Nil(t, err)
796
797 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400798 var vldFunction = func(ports []*voltha.LogicalPort) bool {
799 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500800 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400801 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500802 assert.Nil(t, err)
803
804 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400805 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500806 assert.Nil(t, err)
807
808 // Wait for the all onu devices to be enabled
809 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
810 return device.AdminState == voltha.AdminState_ENABLED
811 }
812 for _, onu := range onuDevices.Items {
813 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
814 assert.Nil(t, err)
815 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500816 // Wait for each onu device to get deleted
817 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
818 return device == nil
819 }
820
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500821 // Delete the onuDevice
822 for _, onu := range onuDevices.Items {
823 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
824 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500825 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
826 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500827 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500828
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500829 // Disable the oltDevice
830 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
831 assert.Nil(t, err)
832
833 // Wait for the olt device to be disabled
834 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
835 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
836 }
837 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
838 assert.Nil(t, err)
839
840 // Delete the oltDevice
841 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
842 assert.Nil(t, err)
843
844 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
845 return devices != nil && len(devices.Items) == 0
846 }
847 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
848 assert.Nil(t, err)
849}
Kent Hagerman2b216042020-04-03 18:28:56 -0400850func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500851 //Get an OLT device
852 var cp *voltha.Port
853 oltDevice, err := nb.getADevice(true, nbi)
854 assert.Nil(t, err)
855 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400856 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
857 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500858
Kent Hagerman2a07b862020-06-19 15:23:07 -0400859 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500860 if cp.Type == voltha.Port_PON_OLT {
861 break
862 }
863
864 }
865 assert.NotNil(t, cp)
866 cp.DeviceId = oltDevice.Id
867
868 // Disable the NW Port of oltDevice
869 _, err = nbi.DisablePort(getContext(), cp)
870 assert.Nil(t, err)
871 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400872 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
873 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500874 if port.PortNo == cp.PortNo {
875 return port.AdminState == voltha.AdminState_DISABLED
876 }
877 }
878 return false
879 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400880 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500881 assert.Nil(t, err)
882 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400883 var vlFunction = func(ports []*voltha.LogicalPort) bool {
884 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500885 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
886 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
887 return false
888 }
889 }
890 return true
891 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400892 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500893 assert.Nil(t, err)
894
895 // Enable the NW Port of oltDevice
896 _, err = nbi.EnablePort(getContext(), cp)
897 assert.Nil(t, err)
898
899 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400900 vdFunction = func(ports *voltha.Ports) bool {
901 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500902 if port.PortNo == cp.PortNo {
903 return port.AdminState == voltha.AdminState_ENABLED
904 }
905 }
906 return false
907 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400908 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500909 assert.Nil(t, err)
910 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400911 vlFunction = func(ports []*voltha.LogicalPort) bool {
912 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500913 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
914 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
915 return false
916 }
917 }
918 return true
919 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400920 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500921 assert.Nil(t, err)
922
923 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400924 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500925 if cp.Type != voltha.Port_PON_OLT {
926 break
927 }
928
929 }
930 assert.NotNil(t, cp)
931 cp.DeviceId = oltDevice.Id
932
933 // Disable the NW Port of oltDevice
934 _, err = nbi.DisablePort(getContext(), cp)
935 assert.NotNil(t, err)
936
937}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500938
Kent Hagerman2b216042020-04-03 18:28:56 -0400939func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700940 //Get an OLT device
941 oltDevice, err := nb.getADevice(true, nbi)
942 assert.Nil(t, err)
943 assert.NotNil(t, oltDevice)
944 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
945 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
946
947 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400948 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700949 assert.Nil(t, err)
950 assert.NotNil(t, onuDevices)
951 assert.Greater(t, len(onuDevices.Items), 0)
952
953 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
954 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
955 assert.Nil(t, err)
956
957 var vlFunction0 = func(d *voltha.Device) bool {
958 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
959 }
960
961 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
962 assert.Nil(t, err)
963
964 // Wait for the logical device to satisfy the expected condition
965 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
966 return ld == nil
967 }
968
969 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
970 assert.Nil(t, err)
971
972 // Wait for the device to satisfy the expected condition (device does not have flows)
973 var vlFunction2 = func(d *voltha.Device) bool {
974 var deviceFlows *ofp.Flows
975 var err error
976 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
977 return false
978 }
979 return len(deviceFlows.Items) == 0
980 }
981
982 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
983 assert.Nil(t, err)
984
985 // Wait for the device to satisfy the expected condition (there are no child devices)
986 var vlFunction3 = func(d *voltha.Device) bool {
987 var devices *voltha.Devices
988 var err error
989 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
990 return false
991 }
992 for _, device := range devices.Items {
993 if device.ParentId == d.Id {
994 // We have a child device still left
995 return false
996 }
997 }
998 return true
999 }
1000
1001 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
1002 assert.Nil(t, err)
1003
1004 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
1005 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -04001006 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -07001007 assert.Nil(t, err)
1008
1009 // Verify the device connection and operation states
1010 oltDevice, err = nb.getADevice(true, nbi)
1011 assert.Nil(t, err)
1012 assert.NotNil(t, oltDevice)
1013 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
1014 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
1015
1016 // Wait for the logical device to satisfy the expected condition
1017 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1018 return ld != nil
1019 }
1020 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1021 assert.Nil(t, err)
1022
1023 // Verify that logical device is created again
1024 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1025 assert.Nil(t, err)
1026 assert.NotNil(t, logicalDevices)
1027 assert.Equal(t, 1, len(logicalDevices.Items))
1028
1029 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001030 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001031 assert.Nil(t, err)
1032 assert.NotNil(t, onuDevices)
1033 assert.Equal(t, 0, len(onuDevices.Items))
1034}
1035
Kent Hagerman2b216042020-04-03 18:28:56 -04001036func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001037 // -----------------------------------------------------------------------
1038 // SubTest 1: Omci test action should fail due to nonexistent device id
1039
1040 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1041 _, err := nbi.StartOmciTestAction(getContext(), request)
1042 assert.NotNil(t, err)
1043 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1044
1045 // -----------------------------------------------------------------------
1046 // SubTest 2: Error should be returned for device with no adapter registered
1047
1048 // Create a device that has no adapter registered
1049 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1050 assert.Nil(t, err)
1051 assert.NotNil(t, deviceNoAdapter)
1052
1053 // Omci test action should fail due to nonexistent adapter
1054 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1055 _, err = nbi.StartOmciTestAction(getContext(), request)
1056 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001057 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001058
1059 //Remove the device
1060 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1061 assert.Nil(t, err)
1062
1063 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1064 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1065 return devices != nil && len(devices.Items) == 0
1066 }
1067 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1068 assert.Nil(t, err)
1069
1070 // -----------------------------------------------------------------------
1071 // SubTest 3: Omci test action should succeed on valid ONU
1072
1073 // Create the device with valid data
1074 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1075 assert.Nil(t, err)
1076 assert.NotNil(t, oltDevice)
1077
1078 // Verify oltDevice exist in the core
1079 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1080 assert.Nil(t, err)
1081 assert.Equal(t, 1, len(devices.Items))
1082 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1083
1084 // Enable the oltDevice
1085 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1086 assert.Nil(t, err)
1087
1088 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001089 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1090 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001091 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001092 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001093 assert.Nil(t, err)
1094
1095 // Wait for the olt device to be enabled
1096 vdFunction := func(device *voltha.Device) bool {
1097 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1098 }
1099 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1100 assert.Nil(t, err)
1101
Kent Hagerman2b216042020-04-03 18:28:56 -04001102 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001103 assert.Nil(t, err)
1104 assert.Greater(t, len(onuDevices.Items), 0)
1105
1106 onuDevice := onuDevices.Items[0]
1107
1108 // Omci test action should succeed
1109 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1110 resp, err := nbi.StartOmciTestAction(getContext(), request)
1111 assert.Nil(t, err)
1112 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1113
1114 //Remove the device
1115 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1116 assert.Nil(t, err)
1117 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1118 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1119 assert.Nil(t, err)
1120}
1121
khenaidoo67b22152020-03-02 16:01:25 -05001122func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1123 matchFields := make([]*ofp.OfpOxmField, 0)
1124 for _, val := range fa.MatchFields {
1125 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1126 }
1127 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1128}
1129
1130func createMetadata(cTag int, techProfile int, port int) uint64 {
1131 md := 0
1132 md = (md | (cTag & 0xFFFF)) << 16
1133 md = (md | (techProfile & 0xFFFF)) << 32
1134 return uint64(md | (port & 0xFFFFFFFF))
1135}
1136
khenaidoo8b4abbf2020-04-24 17:04:30 -04001137func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001138 expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001139 if flowAddFail {
1140 expectedNumFlows = 0
1141 }
1142 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001143 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +03001144 id := ""
1145 if lds != nil {
1146 id = lds.Items[0].Id
1147 }
1148 flws, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: id})
1149 return lds != nil && len(lds.Items) == 1 && len(flws.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001150 }
1151 // No timeout implies a success
1152 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1153 assert.Nil(t, err)
1154}
1155
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001156func (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 -05001157 // Send flows for the parent device
1158 var nniPorts []*voltha.LogicalPort
1159 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001160 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001161 if p.RootPort {
1162 nniPorts = append(nniPorts, p)
1163 } else {
1164 uniPorts = append(uniPorts, p)
1165 }
1166 }
1167 assert.Equal(t, 1, len(nniPorts))
1168 //assert.Greater(t, len(uniPorts), 1 )
1169 nniPort := nniPorts[0].OfpPort.PortNo
1170 maxInt32 := uint64(0xFFFFFFFF)
1171 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1172 var fa *flows.FlowArgs
1173 fa = &flows.FlowArgs{
1174 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1175 MatchFields: []*ofp.OfpOxmOfbField{
1176 flows.InPort(nniPort),
1177 flows.EthType(35020),
1178 },
1179 Actions: []*ofp.OfpAction{
1180 flows.Output(controllerPortMask),
1181 },
1182 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001183 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001184 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1185 assert.Nil(t, err)
1186
1187 fa = &flows.FlowArgs{
1188 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1189 MatchFields: []*ofp.OfpOxmOfbField{
1190 flows.InPort(nniPort),
1191 flows.EthType(2048),
1192 flows.IpProto(17),
1193 flows.UdpSrc(67),
1194 flows.UdpDst(68),
1195 },
1196 Actions: []*ofp.OfpAction{
1197 flows.Output(controllerPortMask),
1198 },
1199 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001200 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001201 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1202 assert.Nil(t, err)
1203
1204 fa = &flows.FlowArgs{
1205 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1206 MatchFields: []*ofp.OfpOxmOfbField{
1207 flows.InPort(nniPort),
1208 flows.EthType(34525),
1209 flows.IpProto(17),
1210 flows.UdpSrc(546),
1211 flows.UdpDst(547),
1212 },
1213 Actions: []*ofp.OfpAction{
1214 flows.Output(controllerPortMask),
1215 },
1216 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001217 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001218 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1219 assert.Nil(t, err)
1220
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001221 fa = &flows.FlowArgs{
1222 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1223 MatchFields: []*ofp.OfpOxmOfbField{
1224 flows.InPort(nniPort),
1225 flows.EthType(34915),
1226 },
1227 Actions: []*ofp.OfpAction{
1228 flows.Output(controllerPortMask),
1229 },
1230 }
1231 flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1232 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP)
1233 assert.Nil(t, err)
1234
khenaidoo67b22152020-03-02 16:01:25 -05001235 return len(nniPorts), len(uniPorts)
1236}
1237
Kent Hagerman2b216042020-04-03 18:28:56 -04001238func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001239 maxInt32 := uint64(0xFFFFFFFF)
1240 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1241 fa := &flows.FlowArgs{
1242 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},
1243 MatchFields: []*ofp.OfpOxmOfbField{
1244 flows.InPort(port.PortNo),
1245 flows.EthType(34958),
1246 flows.VlanVid(8187),
1247 },
1248 Actions: []*ofp.OfpAction{
1249 flows.Output(controllerPortMask),
1250 },
1251 }
1252 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001253 maxTries := 3
1254 var err error
1255 for {
1256 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1257 if maxTries < 3 {
1258 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1259 }
1260 break
1261 }
1262 t.Log("Sending EAPOL flows fail:", err)
1263 time.Sleep(50 * time.Millisecond)
1264 maxTries--
1265 if maxTries == 0 {
1266 break
1267 }
1268 }
khenaidoo67b22152020-03-02 16:01:25 -05001269 assert.Nil(t, err)
1270}
1271
khenaidoo0db4c812020-05-27 15:27:30 -04001272func (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 -05001273 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001274
1275 // Clear any existing flows on the adapters
1276 nb.oltAdapter.ClearFlows()
1277 nb.onuAdapter.ClearFlows()
1278
khenaidoo8b4abbf2020-04-24 17:04:30 -04001279 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001280 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1281 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001282
khenaidoo67b22152020-03-02 16:01:25 -05001283 // Wait until a logical device is ready
1284 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Matteo Scandoload5e14f2021-03-23 10:32:28 -07001285 if lds == nil || len(lds.Items) != 1 || lds.Items[0] == nil {
khenaidoo67b22152020-03-02 16:01:25 -05001286 return false
1287 }
1288 // Ensure there are both NNI ports and at least one UNI port on the logical device
1289 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001290 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1291 if err != nil {
1292 return false
1293 }
khenaidoo67b22152020-03-02 16:01:25 -05001294 nniPort := false
1295 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001296 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001297 nniPort = nniPort || p.RootPort == true
1298 uniPort = uniPort || p.RootPort == false
1299 if nniPort && uniPort {
1300 return true
1301 }
1302 }
1303 return false
1304 }
1305 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1306 assert.Nil(t, err)
1307
1308 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1309 assert.Nil(t, err)
1310 assert.NotNil(t, logicalDevices)
1311 assert.Equal(t, 1, len(logicalDevices.Items))
1312
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001313 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001314 meterID := rand.Uint32()
1315
1316 // Add a meter to the logical device
1317 meterMod := &ofp.OfpMeterMod{
1318 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1319 Flags: rand.Uint32(),
1320 MeterId: meterID,
1321 Bands: []*ofp.OfpMeterBandHeader{
1322 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1323 Rate: rand.Uint32(),
1324 BurstSize: rand.Uint32(),
1325 Data: nil,
1326 },
1327 },
1328 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001329 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1330 assert.Nil(t, err)
1331
1332 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001333 assert.Nil(t, err)
1334
1335 // Send initial set of Trap flows
1336 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001337 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001338
1339 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001340 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001341 processedNniLogicalPorts := 0
1342 processedUniLogicalPorts := 0
1343
Kent Hagerman45a13e42020-04-13 12:23:50 -04001344 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001345 startingVlan++
1346 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1347 ps := portStatus.PortStatus
1348 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001349 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001350 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001351 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001352 } else {
1353 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001354 }
1355 }
1356 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001357
1358 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001359 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001360 break
1361 }
1362 }
1363 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001364 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001365
khenaidoo8b4abbf2020-04-24 17:04:30 -04001366 // Wait until all flows have been sent to the OLT adapters (or all failed)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001367 expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001368 if flowAddFail {
1369 expectedFlowCount = 0
1370 }
khenaidoo67b22152020-03-02 16:01:25 -05001371 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001372 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001373 }
1374 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1375 assert.Nil(t, err)
1376
khenaidoo8b4abbf2020-04-24 17:04:30 -04001377 // Wait until all flows have been sent to the ONU adapters (or all failed)
1378 expectedFlowCount = numUNIPorts
1379 if flowAddFail {
1380 expectedFlowCount = 0
1381 }
khenaidoo67b22152020-03-02 16:01:25 -05001382 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001383 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001384 }
1385 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1386 assert.Nil(t, err)
1387}
1388
khenaidoo8b4abbf2020-04-24 17:04:30 -04001389func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1390
1391 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1392 var wg sync.WaitGroup
1393 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001394 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001395
1396 // Create the device with valid data
1397 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1398 assert.Nil(t, err)
1399 assert.NotNil(t, oltDevice)
1400
1401 // Verify oltDevice exist in the core
1402 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1403 assert.Nil(t, err)
1404 assert.Equal(t, 1, len(devices.Items))
1405 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1406
1407 // Enable the oltDevice
1408 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1409 assert.Nil(t, err)
1410
1411 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001412 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1413 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001414 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001415 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001416 assert.Nil(t, err)
1417
1418 // Verify that the devices have been setup correctly
1419 nb.verifyDevices(t, nbi)
1420
1421 // Get latest oltDevice data
1422 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1423 assert.Nil(t, err)
1424
1425 // Verify that the logical device has been setup correctly
1426 nb.verifyLogicalDevices(t, oltDevice, nbi)
1427
1428 // Wait until all flows has been sent to the devices successfully
1429 wg.Wait()
1430}
1431
Matteo Scandolod525ae32020-04-02 17:27:29 -07001432func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001433 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001434 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001435 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001436 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001437 }
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +03001438 defer func() {
1439 err = f.Close()
1440 if err != nil {
1441 logger.Errorf(ctx, "failed to close file: %v\n", err)
1442 }
1443 }()
khenaidoo67b22152020-03-02 16:01:25 -05001444 runtime.SetBlockProfileRate(1)
1445 runtime.SetMutexProfileFraction(-1)
1446 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001447 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001448 }
1449 defer pprof.StopCPUProfile()
1450
khenaidoo442e7c72020-03-10 16:13:48 -04001451 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1452
Rohan Agrawal31f21802020-06-12 05:38:46 +00001453 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001454 assert.NotNil(t, nb)
1455
Rohan Agrawal31f21802020-06-12 05:38:46 +00001456 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001457
1458 // Start the Core
1459 nb.startCore(false)
1460
1461 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001462 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001463
1464 // 1. Basic test with no data in Core
1465 nb.testCoreWithoutData(t, nbi)
1466
1467 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001468 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 -07001469 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1470 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001471
1472 // 2. Test adapter registration
1473 nb.testAdapterRegistration(t, nbi)
1474
khenaidoo8b4abbf2020-04-24 17:04:30 -04001475 numberOfTestRuns := 2
1476 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301477
1478 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001479 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001480
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301481 // 4. Test Delete Device Scenarios
1482 nb.testForceDeletePreProvDevice(t, nbi)
1483 nb.testDeletePreProvDevice(t, nbi)
1484 nb.testForceDeleteEnabledDevice(t, nbi)
1485 nb.testDeleteEnabledDevice(t, nbi)
1486 nb.testForceDeleteDeviceFailure(t, nbi)
1487 nb.testDeleteDeviceFailure(t, nbi)
1488
1489 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001490 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001491
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301492 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001493 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001494
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301495 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001496 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001497
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301498 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001499 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1500
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301501 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001502 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001503
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301504 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001505 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001506
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301507 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001508 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001509
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301510 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001511 nb.deleteAllDevices(t, nbi)
1512
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301513 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001514 nb.testFlowAddFailure(t, nbi)
1515
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301516 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001517 nb.deleteAllDevices(t, nbi)
1518 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001519}