blob: 631a56ce88ad36c51a630c3926400b206ffc11e4 [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}))
Himani Chawla9cfc4992021-03-22 12:43:01 +0530126 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout, eventProxy, cfg.VolthaStackID)
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 {
Manindera496f852021-02-22 09:57:56 +0530547 state, err := nbi.GetTransientState(getContext(), oltDevice.Id)
548 if err != nil {
549 return false
550 }
551 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1) &&
552 state == voltha.DeviceTransientState_DELETE_FAILED
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530553 }
554 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
555 assert.Nil(t, err)
556
557 nb.oltAdapter.SetDeleteAction(false)
558
559 // Now Force Delete this device
560 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
561 assert.Nil(t, err)
562
563 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
564 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
565 return devices != nil && len(devices.Items) == 0
566 }
567 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
568 assert.Nil(t, err)
569
570}
khenaidoob64fc8a2019-11-27 15:08:19 -0500571
Kent Hagerman2b216042020-04-03 18:28:56 -0400572func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500573 // Create a device that has no adapter registered
574 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
575 assert.Nil(t, err)
576 assert.NotNil(t, oltDeviceNoAdapter)
577
578 // Try to enable the oltDevice and check the error message
579 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
580 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400581 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500582
583 //Remove the device
584 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
585 assert.Nil(t, err)
586
587 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
588 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
589 return devices != nil && len(devices.Items) == 0
590 }
khenaidoo442e7c72020-03-10 16:13:48 -0400591 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500592 assert.Nil(t, err)
593
khenaidoo67b22152020-03-02 16:01:25 -0500594 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
595 var wg sync.WaitGroup
596 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400597 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500598
khenaidoob64fc8a2019-11-27 15:08:19 -0500599 // Create the device with valid data
600 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
601 assert.Nil(t, err)
602 assert.NotNil(t, oltDevice)
603
604 // Verify oltDevice exist in the core
605 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
606 assert.Nil(t, err)
607 assert.Equal(t, 1, len(devices.Items))
608 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
609
610 // Enable the oltDevice
611 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
612 assert.Nil(t, err)
613
614 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400615 var vldFunction = func(ports []*voltha.LogicalPort) bool {
616 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500617 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400618 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500619 assert.Nil(t, err)
620
621 // Verify that the devices have been setup correctly
622 nb.verifyDevices(t, nbi)
623
624 // Get latest oltDevice data
625 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
626 assert.Nil(t, err)
627
628 // Verify that the logical device has been setup correctly
629 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500630
631 // Wait until all flows has been sent to the devices successfully
632 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500633}
634
Kent Hagerman2b216042020-04-03 18:28:56 -0400635func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500636 //Get an OLT device
637 oltDevice, err := nb.getADevice(true, nbi)
638 assert.Nil(t, err)
639 assert.NotNil(t, oltDevice)
640
641 // Disable the oltDevice
642 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
643 assert.Nil(t, err)
644
645 // Wait for the old device to be disabled
646 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
647 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
648 }
649 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
650 assert.Nil(t, err)
651
652 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400653 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500654 assert.Nil(t, err)
655 for _, onu := range onuDevices.Items {
656 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
657 assert.Nil(t, err)
658 }
659
660 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400661 var vlFunction = func(ports []*voltha.LogicalPort) bool {
662 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500663 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
664 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
665 return false
666 }
667 }
668 return true
669 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400670 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500671 assert.Nil(t, err)
672
673 // Reenable the oltDevice
674 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
675 assert.Nil(t, err)
676
677 // Wait for the old device to be enabled
678 vdFunction = func(device *voltha.Device) bool {
679 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
680 }
681 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
682 assert.Nil(t, err)
683
684 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400685 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500686 assert.Nil(t, err)
687 for _, onu := range onuDevices.Items {
688 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
689 assert.Nil(t, err)
690 }
691
692 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400693 vlFunction = func(ports []*voltha.LogicalPort) bool {
694 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500695 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
696 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
697 return false
698 }
699 }
700 return true
701 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400702 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500703 assert.Nil(t, err)
704}
705
Kent Hagerman2b216042020-04-03 18:28:56 -0400706func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500707 //Get an OLT device
708 oltDevice, err := nb.getADevice(true, nbi)
709 assert.Nil(t, err)
710 assert.NotNil(t, oltDevice)
711
712 // Disable the oltDevice
713 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
714 assert.Nil(t, err)
715
716 // Wait for the olt device to be disabled
717 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
718 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
719 }
720 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
721 assert.Nil(t, err)
722
723 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400724 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500725 assert.Nil(t, err)
726 for _, onu := range onuDevices.Items {
727 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
728 assert.Nil(t, err)
729 }
730
731 // Delete the oltDevice
732 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
733 assert.Nil(t, err)
734
735 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
736 return devices != nil && len(devices.Items) == 0
737 }
738 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
739 assert.Nil(t, err)
740
741 // Wait for absence of logical device
742 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
743 return lds != nil && len(lds.Items) == 0
744 }
745
746 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
747 assert.Nil(t, err)
748}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400749
750func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400751 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
752 if len(devices.Items) == 0 {
753 // Nothing to do
754 return
755 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400756 //Get an OLT device
757 oltDevice, err := nb.getADevice(true, nbi)
758 assert.Nil(t, err)
759 assert.NotNil(t, oltDevice)
760
761 // Delete the oltDevice
762 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
763 assert.Nil(t, err)
764
765 // Wait for all devices to be deleted
766 vFunction := func(devices *voltha.Devices) bool {
767 return devices != nil && len(devices.Items) == 0
768 }
769 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
770 assert.Nil(t, err)
771
772 // Wait for absence of logical device
773 vlFunction := func(lds *voltha.LogicalDevices) bool {
774 return lds != nil && len(lds.Items) == 0
775 }
776
777 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
778 assert.Nil(t, err)
779}
780
Kent Hagerman2b216042020-04-03 18:28:56 -0400781func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500782 //Create the device with valid data
783 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
784 assert.Nil(t, err)
785 assert.NotNil(t, oltDevice)
786
787 //Get an OLT device
788 oltDevice, err = nb.getADevice(true, nbi)
789 assert.Nil(t, err)
790 assert.NotNil(t, oltDevice)
791
792 // Enable the oltDevice
793 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
794 assert.Nil(t, err)
795
796 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400797 var vldFunction = func(ports []*voltha.LogicalPort) bool {
798 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500799 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400800 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500801 assert.Nil(t, err)
802
803 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400804 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500805 assert.Nil(t, err)
806
807 // Wait for the all onu devices to be enabled
808 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
809 return device.AdminState == voltha.AdminState_ENABLED
810 }
811 for _, onu := range onuDevices.Items {
812 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
813 assert.Nil(t, err)
814 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500815 // Wait for each onu device to get deleted
816 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
817 return device == nil
818 }
819
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500820 // Delete the onuDevice
821 for _, onu := range onuDevices.Items {
822 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
823 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500824 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
825 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500826 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500827
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500828 // Disable the oltDevice
829 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
830 assert.Nil(t, err)
831
832 // Wait for the olt device to be disabled
833 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
834 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
835 }
836 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
837 assert.Nil(t, err)
838
839 // Delete the oltDevice
840 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
841 assert.Nil(t, err)
842
843 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
844 return devices != nil && len(devices.Items) == 0
845 }
846 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
847 assert.Nil(t, err)
848}
Kent Hagerman2b216042020-04-03 18:28:56 -0400849func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500850 //Get an OLT device
851 var cp *voltha.Port
852 oltDevice, err := nb.getADevice(true, nbi)
853 assert.Nil(t, err)
854 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400855 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
856 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500857
Kent Hagerman2a07b862020-06-19 15:23:07 -0400858 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500859 if cp.Type == voltha.Port_PON_OLT {
860 break
861 }
862
863 }
864 assert.NotNil(t, cp)
865 cp.DeviceId = oltDevice.Id
866
867 // Disable the NW Port of oltDevice
868 _, err = nbi.DisablePort(getContext(), cp)
869 assert.Nil(t, err)
870 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400871 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
872 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500873 if port.PortNo == cp.PortNo {
874 return port.AdminState == voltha.AdminState_DISABLED
875 }
876 }
877 return false
878 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400879 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500880 assert.Nil(t, err)
881 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400882 var vlFunction = func(ports []*voltha.LogicalPort) bool {
883 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500884 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
885 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
886 return false
887 }
888 }
889 return true
890 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400891 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500892 assert.Nil(t, err)
893
894 // Enable the NW Port of oltDevice
895 _, err = nbi.EnablePort(getContext(), cp)
896 assert.Nil(t, err)
897
898 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400899 vdFunction = func(ports *voltha.Ports) bool {
900 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500901 if port.PortNo == cp.PortNo {
902 return port.AdminState == voltha.AdminState_ENABLED
903 }
904 }
905 return false
906 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400907 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500908 assert.Nil(t, err)
909 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400910 vlFunction = func(ports []*voltha.LogicalPort) bool {
911 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500912 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
913 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
914 return false
915 }
916 }
917 return true
918 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400919 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500920 assert.Nil(t, err)
921
922 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400923 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500924 if cp.Type != voltha.Port_PON_OLT {
925 break
926 }
927
928 }
929 assert.NotNil(t, cp)
930 cp.DeviceId = oltDevice.Id
931
932 // Disable the NW Port of oltDevice
933 _, err = nbi.DisablePort(getContext(), cp)
934 assert.NotNil(t, err)
935
936}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500937
Kent Hagerman2b216042020-04-03 18:28:56 -0400938func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700939 //Get an OLT device
940 oltDevice, err := nb.getADevice(true, nbi)
941 assert.Nil(t, err)
942 assert.NotNil(t, oltDevice)
943 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
944 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
945
946 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400947 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700948 assert.Nil(t, err)
949 assert.NotNil(t, onuDevices)
950 assert.Greater(t, len(onuDevices.Items), 0)
951
952 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
953 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
954 assert.Nil(t, err)
955
956 var vlFunction0 = func(d *voltha.Device) bool {
957 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
958 }
959
960 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
961 assert.Nil(t, err)
962
963 // Wait for the logical device to satisfy the expected condition
964 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
965 return ld == nil
966 }
967
968 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
969 assert.Nil(t, err)
970
971 // Wait for the device to satisfy the expected condition (device does not have flows)
972 var vlFunction2 = func(d *voltha.Device) bool {
973 var deviceFlows *ofp.Flows
974 var err error
975 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
976 return false
977 }
978 return len(deviceFlows.Items) == 0
979 }
980
981 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
982 assert.Nil(t, err)
983
984 // Wait for the device to satisfy the expected condition (there are no child devices)
985 var vlFunction3 = func(d *voltha.Device) bool {
986 var devices *voltha.Devices
987 var err error
988 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
989 return false
990 }
991 for _, device := range devices.Items {
992 if device.ParentId == d.Id {
993 // We have a child device still left
994 return false
995 }
996 }
997 return true
998 }
999
1000 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
1001 assert.Nil(t, err)
1002
1003 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
1004 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -04001005 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -07001006 assert.Nil(t, err)
1007
1008 // Verify the device connection and operation states
1009 oltDevice, err = nb.getADevice(true, nbi)
1010 assert.Nil(t, err)
1011 assert.NotNil(t, oltDevice)
1012 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
1013 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
1014
1015 // Wait for the logical device to satisfy the expected condition
1016 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1017 return ld != nil
1018 }
1019 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1020 assert.Nil(t, err)
1021
1022 // Verify that logical device is created again
1023 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1024 assert.Nil(t, err)
1025 assert.NotNil(t, logicalDevices)
1026 assert.Equal(t, 1, len(logicalDevices.Items))
1027
1028 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001029 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001030 assert.Nil(t, err)
1031 assert.NotNil(t, onuDevices)
1032 assert.Equal(t, 0, len(onuDevices.Items))
1033}
1034
Kent Hagerman2b216042020-04-03 18:28:56 -04001035func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001036 // -----------------------------------------------------------------------
1037 // SubTest 1: Omci test action should fail due to nonexistent device id
1038
1039 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1040 _, err := nbi.StartOmciTestAction(getContext(), request)
1041 assert.NotNil(t, err)
1042 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1043
1044 // -----------------------------------------------------------------------
1045 // SubTest 2: Error should be returned for device with no adapter registered
1046
1047 // Create a device that has no adapter registered
1048 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1049 assert.Nil(t, err)
1050 assert.NotNil(t, deviceNoAdapter)
1051
1052 // Omci test action should fail due to nonexistent adapter
1053 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1054 _, err = nbi.StartOmciTestAction(getContext(), request)
1055 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001056 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001057
1058 //Remove the device
1059 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1060 assert.Nil(t, err)
1061
1062 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1063 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1064 return devices != nil && len(devices.Items) == 0
1065 }
1066 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1067 assert.Nil(t, err)
1068
1069 // -----------------------------------------------------------------------
1070 // SubTest 3: Omci test action should succeed on valid ONU
1071
1072 // Create the device with valid data
1073 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1074 assert.Nil(t, err)
1075 assert.NotNil(t, oltDevice)
1076
1077 // Verify oltDevice exist in the core
1078 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1079 assert.Nil(t, err)
1080 assert.Equal(t, 1, len(devices.Items))
1081 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1082
1083 // Enable the oltDevice
1084 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1085 assert.Nil(t, err)
1086
1087 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001088 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1089 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001090 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001091 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001092 assert.Nil(t, err)
1093
1094 // Wait for the olt device to be enabled
1095 vdFunction := func(device *voltha.Device) bool {
1096 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1097 }
1098 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1099 assert.Nil(t, err)
1100
Kent Hagerman2b216042020-04-03 18:28:56 -04001101 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001102 assert.Nil(t, err)
1103 assert.Greater(t, len(onuDevices.Items), 0)
1104
1105 onuDevice := onuDevices.Items[0]
1106
1107 // Omci test action should succeed
1108 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1109 resp, err := nbi.StartOmciTestAction(getContext(), request)
1110 assert.Nil(t, err)
1111 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1112
1113 //Remove the device
1114 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1115 assert.Nil(t, err)
1116 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1117 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1118 assert.Nil(t, err)
1119}
1120
khenaidoo67b22152020-03-02 16:01:25 -05001121func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1122 matchFields := make([]*ofp.OfpOxmField, 0)
1123 for _, val := range fa.MatchFields {
1124 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1125 }
1126 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1127}
1128
1129func createMetadata(cTag int, techProfile int, port int) uint64 {
1130 md := 0
1131 md = (md | (cTag & 0xFFFF)) << 16
1132 md = (md | (techProfile & 0xFFFF)) << 32
1133 return uint64(md | (port & 0xFFFFFFFF))
1134}
1135
khenaidoo8b4abbf2020-04-24 17:04:30 -04001136func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001137 expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001138 if flowAddFail {
1139 expectedNumFlows = 0
1140 }
1141 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001142 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -07001143 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
1144 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001145 }
1146 // No timeout implies a success
1147 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1148 assert.Nil(t, err)
1149}
1150
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001151func (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 -05001152 // Send flows for the parent device
1153 var nniPorts []*voltha.LogicalPort
1154 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001155 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001156 if p.RootPort {
1157 nniPorts = append(nniPorts, p)
1158 } else {
1159 uniPorts = append(uniPorts, p)
1160 }
1161 }
1162 assert.Equal(t, 1, len(nniPorts))
1163 //assert.Greater(t, len(uniPorts), 1 )
1164 nniPort := nniPorts[0].OfpPort.PortNo
1165 maxInt32 := uint64(0xFFFFFFFF)
1166 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1167 var fa *flows.FlowArgs
1168 fa = &flows.FlowArgs{
1169 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1170 MatchFields: []*ofp.OfpOxmOfbField{
1171 flows.InPort(nniPort),
1172 flows.EthType(35020),
1173 },
1174 Actions: []*ofp.OfpAction{
1175 flows.Output(controllerPortMask),
1176 },
1177 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001178 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001179 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1180 assert.Nil(t, err)
1181
1182 fa = &flows.FlowArgs{
1183 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1184 MatchFields: []*ofp.OfpOxmOfbField{
1185 flows.InPort(nniPort),
1186 flows.EthType(2048),
1187 flows.IpProto(17),
1188 flows.UdpSrc(67),
1189 flows.UdpDst(68),
1190 },
1191 Actions: []*ofp.OfpAction{
1192 flows.Output(controllerPortMask),
1193 },
1194 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001195 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001196 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1197 assert.Nil(t, err)
1198
1199 fa = &flows.FlowArgs{
1200 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1201 MatchFields: []*ofp.OfpOxmOfbField{
1202 flows.InPort(nniPort),
1203 flows.EthType(34525),
1204 flows.IpProto(17),
1205 flows.UdpSrc(546),
1206 flows.UdpDst(547),
1207 },
1208 Actions: []*ofp.OfpAction{
1209 flows.Output(controllerPortMask),
1210 },
1211 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001212 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001213 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1214 assert.Nil(t, err)
1215
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001216 fa = &flows.FlowArgs{
1217 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1218 MatchFields: []*ofp.OfpOxmOfbField{
1219 flows.InPort(nniPort),
1220 flows.EthType(34915),
1221 },
1222 Actions: []*ofp.OfpAction{
1223 flows.Output(controllerPortMask),
1224 },
1225 }
1226 flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1227 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP)
1228 assert.Nil(t, err)
1229
khenaidoo67b22152020-03-02 16:01:25 -05001230 return len(nniPorts), len(uniPorts)
1231}
1232
Kent Hagerman2b216042020-04-03 18:28:56 -04001233func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001234 maxInt32 := uint64(0xFFFFFFFF)
1235 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1236 fa := &flows.FlowArgs{
1237 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},
1238 MatchFields: []*ofp.OfpOxmOfbField{
1239 flows.InPort(port.PortNo),
1240 flows.EthType(34958),
1241 flows.VlanVid(8187),
1242 },
1243 Actions: []*ofp.OfpAction{
1244 flows.Output(controllerPortMask),
1245 },
1246 }
1247 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001248 maxTries := 3
1249 var err error
1250 for {
1251 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1252 if maxTries < 3 {
1253 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1254 }
1255 break
1256 }
1257 t.Log("Sending EAPOL flows fail:", err)
1258 time.Sleep(50 * time.Millisecond)
1259 maxTries--
1260 if maxTries == 0 {
1261 break
1262 }
1263 }
khenaidoo67b22152020-03-02 16:01:25 -05001264 assert.Nil(t, err)
1265}
1266
khenaidoo0db4c812020-05-27 15:27:30 -04001267func (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 -05001268 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001269
1270 // Clear any existing flows on the adapters
1271 nb.oltAdapter.ClearFlows()
1272 nb.onuAdapter.ClearFlows()
1273
khenaidoo8b4abbf2020-04-24 17:04:30 -04001274 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001275 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1276 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001277
khenaidoo67b22152020-03-02 16:01:25 -05001278 // Wait until a logical device is ready
1279 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Matteo Scandoload5e14f2021-03-23 10:32:28 -07001280 if lds == nil || len(lds.Items) != 1 || lds.Items[0] == nil {
khenaidoo67b22152020-03-02 16:01:25 -05001281 return false
1282 }
1283 // Ensure there are both NNI ports and at least one UNI port on the logical device
1284 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001285 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1286 if err != nil {
1287 return false
1288 }
khenaidoo67b22152020-03-02 16:01:25 -05001289 nniPort := false
1290 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001291 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001292 nniPort = nniPort || p.RootPort == true
1293 uniPort = uniPort || p.RootPort == false
1294 if nniPort && uniPort {
1295 return true
1296 }
1297 }
1298 return false
1299 }
1300 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1301 assert.Nil(t, err)
1302
1303 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1304 assert.Nil(t, err)
1305 assert.NotNil(t, logicalDevices)
1306 assert.Equal(t, 1, len(logicalDevices.Items))
1307
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001308 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001309 meterID := rand.Uint32()
1310
1311 // Add a meter to the logical device
1312 meterMod := &ofp.OfpMeterMod{
1313 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1314 Flags: rand.Uint32(),
1315 MeterId: meterID,
1316 Bands: []*ofp.OfpMeterBandHeader{
1317 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1318 Rate: rand.Uint32(),
1319 BurstSize: rand.Uint32(),
1320 Data: nil,
1321 },
1322 },
1323 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001324 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1325 assert.Nil(t, err)
1326
1327 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001328 assert.Nil(t, err)
1329
1330 // Send initial set of Trap flows
1331 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001332 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001333
1334 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001335 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001336 processedNniLogicalPorts := 0
1337 processedUniLogicalPorts := 0
1338
Kent Hagerman45a13e42020-04-13 12:23:50 -04001339 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001340 startingVlan++
1341 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1342 ps := portStatus.PortStatus
1343 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001344 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001345 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001346 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001347 } else {
1348 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001349 }
1350 }
1351 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001352
1353 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001354 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001355 break
1356 }
1357 }
1358 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001359 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001360
khenaidoo8b4abbf2020-04-24 17:04:30 -04001361 // Wait until all flows have been sent to the OLT adapters (or all failed)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001362 expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001363 if flowAddFail {
1364 expectedFlowCount = 0
1365 }
khenaidoo67b22152020-03-02 16:01:25 -05001366 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001367 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001368 }
1369 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1370 assert.Nil(t, err)
1371
khenaidoo8b4abbf2020-04-24 17:04:30 -04001372 // Wait until all flows have been sent to the ONU adapters (or all failed)
1373 expectedFlowCount = numUNIPorts
1374 if flowAddFail {
1375 expectedFlowCount = 0
1376 }
khenaidoo67b22152020-03-02 16:01:25 -05001377 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001378 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001379 }
1380 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1381 assert.Nil(t, err)
1382}
1383
khenaidoo8b4abbf2020-04-24 17:04:30 -04001384func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1385
1386 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1387 var wg sync.WaitGroup
1388 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001389 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001390
1391 // Create the device with valid data
1392 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1393 assert.Nil(t, err)
1394 assert.NotNil(t, oltDevice)
1395
1396 // Verify oltDevice exist in the core
1397 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1398 assert.Nil(t, err)
1399 assert.Equal(t, 1, len(devices.Items))
1400 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1401
1402 // Enable the oltDevice
1403 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1404 assert.Nil(t, err)
1405
1406 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001407 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1408 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001409 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001410 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001411 assert.Nil(t, err)
1412
1413 // Verify that the devices have been setup correctly
1414 nb.verifyDevices(t, nbi)
1415
1416 // Get latest oltDevice data
1417 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1418 assert.Nil(t, err)
1419
1420 // Verify that the logical device has been setup correctly
1421 nb.verifyLogicalDevices(t, oltDevice, nbi)
1422
1423 // Wait until all flows has been sent to the devices successfully
1424 wg.Wait()
1425}
1426
Matteo Scandolod525ae32020-04-02 17:27:29 -07001427func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001428 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001429 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001430 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001431 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001432 }
1433 defer f.Close()
1434 runtime.SetBlockProfileRate(1)
1435 runtime.SetMutexProfileFraction(-1)
1436 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001437 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001438 }
1439 defer pprof.StopCPUProfile()
1440
khenaidoo442e7c72020-03-10 16:13:48 -04001441 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1442
Rohan Agrawal31f21802020-06-12 05:38:46 +00001443 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001444 assert.NotNil(t, nb)
1445
Rohan Agrawal31f21802020-06-12 05:38:46 +00001446 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001447
1448 // Start the Core
1449 nb.startCore(false)
1450
1451 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001452 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001453
1454 // 1. Basic test with no data in Core
1455 nb.testCoreWithoutData(t, nbi)
1456
1457 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001458 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 -07001459 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1460 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001461
1462 // 2. Test adapter registration
1463 nb.testAdapterRegistration(t, nbi)
1464
khenaidoo8b4abbf2020-04-24 17:04:30 -04001465 numberOfTestRuns := 2
1466 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301467
1468 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001469 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001470
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301471 // 4. Test Delete Device Scenarios
1472 nb.testForceDeletePreProvDevice(t, nbi)
1473 nb.testDeletePreProvDevice(t, nbi)
1474 nb.testForceDeleteEnabledDevice(t, nbi)
1475 nb.testDeleteEnabledDevice(t, nbi)
1476 nb.testForceDeleteDeviceFailure(t, nbi)
1477 nb.testDeleteDeviceFailure(t, nbi)
1478
1479 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001480 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001481
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301482 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001483 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001484
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301485 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001486 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001487
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301488 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001489 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1490
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301491 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001492 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001493
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301494 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001495 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001496
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301497 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001498 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001499
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301500 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001501 nb.deleteAllDevices(t, nbi)
1502
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301503 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001504 nb.testFlowAddFailure(t, nbi)
1505
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301506 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001507 nb.deleteAllDevices(t, nbi)
1508 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001509}