blob: 2891c361008f36aa88e7affc2b392d8f11cbe92a [file] [log] [blame]
khenaidoob64fc8a2019-11-27 15:08:19 -05001/*
Kent Hagerman45a13e42020-04-13 12:23:50 -04002 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
khenaidoob64fc8a2019-11-27 15:08:19 -050015 */
Kent Hagerman45a13e42020-04-13 12:23:50 -040016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package api
khenaidoob64fc8a2019-11-27 15:08:19 -050018
19import (
20 "context"
21 "errors"
22 "fmt"
khenaidoo67b22152020-03-02 16:01:25 -050023 "math/rand"
24 "os"
25 "runtime"
26 "runtime/pprof"
Neha Sharmad1387da2020-05-07 20:07:28 +000027 "strconv"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080028 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050029 "sync"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "testing"
31 "time"
32
khenaidoob64fc8a2019-11-27 15:08:19 -050033 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040034 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050035 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040036 "github.com/opencord/voltha-go/rw_core/core/adapter"
37 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050038 cm "github.com/opencord/voltha-go/rw_core/mocks"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070039 tst "github.com/opencord/voltha-go/rw_core/test"
Maninderdfadc982020-10-28 14:04:33 +053040 "github.com/opencord/voltha-lib-go/v4/pkg/db"
41 "github.com/opencord/voltha-lib-go/v4/pkg/flows"
42 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
43 mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd"
44 mock_kafka "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka"
45 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
46 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050047 "github.com/phayes/freeport"
48 "github.com/stretchr/testify/assert"
49 "google.golang.org/grpc/codes"
50 "google.golang.org/grpc/status"
khenaidoob64fc8a2019-11-27 15:08:19 -050051)
52
53type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070054 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040055 deviceMgr *device.Manager
56 logicalDeviceMgr *device.LogicalManager
57 adapterMgr *adapter.Manager
58 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050059 kClient kafka.Client
60 kvClientPort int
61 numONUPerOLT int
62 startingUNIPortNo int
63 oltAdapter *cm.OLTAdapter
64 onuAdapter *cm.ONUAdapter
65 oltAdapterName string
66 onuAdapterName string
67 coreInstanceID string
68 defaultTimeout time.Duration
69 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050070}
71
Rohan Agrawal31f21802020-06-12 05:38:46 +000072func newNBTest(ctx context.Context) *NBTest {
khenaidoob64fc8a2019-11-27 15:08:19 -050073 test := &NBTest{}
74 // Start the embedded etcd server
75 var err error
Rohan Agrawal31f21802020-06-12 05:38:46 +000076 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050077 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000078 logger.Fatal(ctx, err)
khenaidoob64fc8a2019-11-27 15:08:19 -050079 }
80 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070081 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050082 test.oltAdapterName = "olt_adapter_mock"
83 test.onuAdapterName = "onu_adapter_mock"
84 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050085 test.defaultTimeout = 10 * time.Second
86 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050087 return test
88}
89
90func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053091 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
92 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050093 cfg := config.NewRWCoreFlags()
serkant.uluderya8ff291d2020-05-20 00:58:00 -070094 cfg.CoreTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040095 cfg.DefaultRequestTimeout = nb.defaultTimeout
96 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +000097 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -050098 grpcPort, err := freeport.GetFreePort()
99 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000100 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500101 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000102 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500103 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000104 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400105 backend := &db.Backend{
106 Client: client,
107 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000108 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400109 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700110 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000112 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400113 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700114 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400115
116 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400117 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700119 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400120 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400121
Rohan Agrawal31f21802020-06-12 05:38:46 +0000122 if err := nb.kmp.Start(ctx); err != nil {
123 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400124 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400125 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000126 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
127 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400128 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500129}
130
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500132 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000133 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500134 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400135 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500137 }
138 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500140 }
141}
142
Kent Hagerman2b216042020-04-03 18:28:56 -0400143func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500144 // Get the latest set of logical devices
145 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
146 assert.Nil(t, err)
147 assert.NotNil(t, logicalDevices)
148 assert.Equal(t, 1, len(logicalDevices.Items))
149
150 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400151 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
152 assert.Nil(t, err)
153
khenaidoob64fc8a2019-11-27 15:08:19 -0500154 assert.NotEqual(t, "", ld.Id)
155 assert.NotEqual(t, uint64(0), ld.DatapathId)
156 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
157 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
158 assert.NotEqual(t, "", ld.RootDeviceId)
159 assert.NotEqual(t, "", ld.Desc.SerialNum)
160 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
161 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
162 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400163 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500164 assert.Equal(t, oltDevice.ParentId, ld.Id)
165 //Expected port no
166 expectedPortNo := make(map[uint32]bool)
167 expectedPortNo[uint32(2)] = false
168 for i := 0; i < nb.numONUPerOLT; i++ {
169 expectedPortNo[uint32(i+100)] = false
170 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400171 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500172 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
173 assert.Equal(t, uint32(4), p.OfpPort.State)
174 expectedPortNo[p.OfpPort.PortNo] = true
175 if strings.HasPrefix(p.Id, "nni") {
176 assert.Equal(t, true, p.RootPort)
177 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
178 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
179 } else {
180 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
181 assert.Equal(t, false, p.RootPort)
182 }
183 }
184}
185
Kent Hagerman2b216042020-04-03 18:28:56 -0400186func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500187 // Get the latest set of devices
188 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
189 assert.Nil(t, err)
190 assert.NotNil(t, devices)
191
khenaidoo67b22152020-03-02 16:01:25 -0500192 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500193 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
194 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
195 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500196
khenaidoo67b22152020-03-02 16:01:25 -0500197 var wg sync.WaitGroup
198 for _, device := range devices.Items {
199 wg.Add(1)
200 go func(wg *sync.WaitGroup, device *voltha.Device) {
201 // Wait until the device is in the right state
202 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
203 assert.Nil(t, err)
204
205 // Now, verify the details of the device. First get the latest update
206 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
207 assert.Nil(t, err)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400208 dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id})
209 assert.Nil(t, err)
khenaidoo67b22152020-03-02 16:01:25 -0500210 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
211 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
212 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
213 assert.Equal(t, d.Type, d.Adapter)
214 assert.NotEqual(t, "", d.MacAddress)
215 assert.NotEqual(t, "", d.SerialNumber)
216
217 if d.Type == "olt_adapter_mock" {
218 assert.Equal(t, true, d.Root)
219 assert.NotEqual(t, "", d.Id)
220 assert.NotEqual(t, "", d.ParentId)
221 assert.Nil(t, d.ProxyAddress)
222 } else if d.Type == "onu_adapter_mock" {
223 assert.Equal(t, false, d.Root)
224 assert.NotEqual(t, uint32(0), d.Vlan)
225 assert.NotEqual(t, "", d.Id)
226 assert.NotEqual(t, "", d.ParentId)
227 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
228 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500229 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500230 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500231 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400232 assert.Equal(t, 2, len(dPorts.Items))
233 for _, p := range dPorts.Items {
khenaidoo67b22152020-03-02 16:01:25 -0500234 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
235 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
236 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
237 assert.Equal(t, 0, len(p.Peers))
238 } else if p.Type == voltha.Port_PON_OLT {
239 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
240 assert.Equal(t, uint32(1), p.PortNo)
241 } else if p.Type == voltha.Port_PON_ONU {
242 assert.Equal(t, 1, len(p.Peers))
243 assert.Equal(t, uint32(1), p.PortNo)
244 } else {
245 assert.Error(t, errors.New("invalid-port"))
246 }
247 }
248 wg.Done()
249 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500250 }
khenaidoo67b22152020-03-02 16:01:25 -0500251 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500252}
253
Kent Hagerman2b216042020-04-03 18:28:56 -0400254func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500255 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
256 if err != nil {
257 return nil, err
258 }
259 for _, d := range devices.Items {
260 if d.Root == rootDevice {
261 return d, nil
262 }
263 }
264 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
265}
266
Kent Hagerman2b216042020-04-03 18:28:56 -0400267func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500268 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
269 assert.Nil(t, err)
270 assert.NotNil(t, lds)
271 assert.Equal(t, 0, len(lds.Items))
272 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
273 assert.Nil(t, err)
274 assert.NotNil(t, devices)
275 assert.Equal(t, 0, len(devices.Items))
276 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400277 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500278 assert.Nil(t, err)
279 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500280}
281
Kent Hagerman2b216042020-04-03 18:28:56 -0400282func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000283 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500284 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
285 assert.Nil(t, err)
286 assert.NotNil(t, adapters)
287 assert.Equal(t, 2, len(adapters.Items))
288 for _, a := range adapters.Items {
289 switch a.Id {
290 case nb.oltAdapterName:
291 assert.Equal(t, "Voltha-olt", a.Vendor)
292 case nb.onuAdapterName:
293 assert.Equal(t, "Voltha-onu", a.Vendor)
294 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000295 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500296 }
297 }
298 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
299 assert.Nil(t, err)
300 assert.NotNil(t, deviceTypes)
301 assert.Equal(t, 2, len(deviceTypes.Items))
302 for _, dt := range deviceTypes.Items {
303 switch dt.Id {
304 case nb.oltAdapterName:
305 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
306 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
307 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
308 case nb.onuAdapterName:
309 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
310 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
311 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
312 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000313 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500314 }
315 }
316}
317
Kent Hagerman2b216042020-04-03 18:28:56 -0400318func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500319 // Create a valid device
320 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
321 assert.Nil(t, err)
322 assert.NotNil(t, oltDevice)
323 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
324 assert.Nil(t, err)
325 assert.NotNil(t, device)
326 assert.Equal(t, oltDevice.String(), device.String())
327
328 // Try to create the same device
329 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
330 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400331 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500332
333 // Try to create a device with invalid data
334 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
335 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530336 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500337
338 // Ensure we only have 1 device in the Core
339 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
340 assert.Nil(t, err)
341 assert.NotNil(t, devices)
342 assert.Equal(t, 1, len(devices.Items))
343 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
344
345 //Remove the device
346 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
347 assert.Nil(t, err)
348
349 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
350 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
351 return devices != nil && len(devices.Items) == 0
352 }
khenaidoo442e7c72020-03-10 16:13:48 -0400353 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500354 assert.Nil(t, err)
355}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530356func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) {
357 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
358 var wg sync.WaitGroup
359 wg.Add(1)
360 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
361
362 // Enable the oltDevice
363 _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
364 assert.Nil(t, err)
365
366 // Wait for the logical device to be in the ready state
367 var vldFunction = func(ports []*voltha.LogicalPort) bool {
368 return len(ports) == nb.numONUPerOLT+1
369 }
370 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
371 assert.Nil(t, err)
372
373 // Verify that the devices have been setup correctly
374 nb.verifyDevices(t, nbi)
375
376 // Get latest oltDevice data
377 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
378 assert.Nil(t, err)
379
380 // Verify that the logical device has been setup correctly
381 nb.verifyLogicalDevices(t, oltDevice, nbi)
382
383 // Wait until all flows has been sent to the devices successfully
384 wg.Wait()
385
386}
387func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
388 // Create a valid device
389 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
390 assert.Nil(t, err)
391 assert.NotNil(t, oltDevice)
392 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
393 assert.Nil(t, err)
394 assert.NotNil(t, device)
395 assert.Equal(t, oltDevice.String(), device.String())
396
397 // Ensure we only have 1 device in the Core
398 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
399 assert.Nil(t, err)
400 assert.NotNil(t, devices)
401 assert.Equal(t, 1, len(devices.Items))
402 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
403
404 //Remove the device forcefully
405 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
406 assert.Nil(t, err)
407
408 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
409 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
410 return devices != nil && len(devices.Items) == 0
411 }
412 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
413 assert.Nil(t, err)
414}
415
416func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
417 // Create a valid device
418 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
419 assert.Nil(t, err)
420 assert.NotNil(t, oltDevice)
421 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
422 assert.Nil(t, err)
423 assert.NotNil(t, device)
424 assert.Equal(t, oltDevice.String(), device.String())
425
426 nb.enableDevice(t, nbi, oltDevice)
427
428 //Remove the device forcefully
429 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
430 assert.Nil(t, err)
431
432 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
433 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
434 return devices != nil && len(devices.Items) == 0
435 }
436 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
437 assert.Nil(t, err)
438}
439
440func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
441 // Create a valid device
442 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
443 assert.Nil(t, err)
444 assert.NotNil(t, oltDevice)
445 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
446 assert.Nil(t, err)
447 assert.NotNil(t, device)
448 assert.Equal(t, oltDevice.String(), device.String())
449
450 // Ensure we only have 1 device in the Core
451 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
452 assert.Nil(t, err)
453 assert.NotNil(t, devices)
454 assert.Equal(t, 1, len(devices.Items))
455 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
456
457 //Remove the device forcefully
458 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
459 assert.Nil(t, err)
460
461 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
462 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
463 return devices != nil && len(devices.Items) == 0
464 }
465 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
466 assert.Nil(t, err)
467}
468
469func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
470 // Create a valid device
471 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
472 assert.Nil(t, err)
473 assert.NotNil(t, oltDevice)
474 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
475 assert.Nil(t, err)
476 assert.NotNil(t, device)
477 assert.Equal(t, oltDevice.String(), device.String())
478
479 nb.enableDevice(t, nbi, oltDevice)
480
481 //Remove the device
482 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
483 assert.Nil(t, err)
484
485 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
486 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
487 return devices != nil && len(devices.Items) == 0
488 }
489 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
490 assert.Nil(t, err)
491}
492
493func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
494 // Create a valid device
495 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
496 assert.Nil(t, err)
497 assert.NotNil(t, oltDevice)
498 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
499 assert.Nil(t, err)
500 assert.NotNil(t, device)
501 assert.Equal(t, oltDevice.String(), device.String())
502
503 nb.enableDevice(t, nbi, oltDevice)
504 nb.oltAdapter.SetDeleteAction(true)
505 //Remove the device
506 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
507 assert.Nil(t, err)
508
509 //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout
510 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
511 return devices != nil && len(devices.Items) == 0
512 }
513 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
514 assert.Nil(t, err)
515
516}
517
518func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
519 // Create a valid device
520 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
521 assert.Nil(t, err)
522 assert.NotNil(t, oltDevice)
523 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
524 assert.Nil(t, err)
525 assert.NotNil(t, device)
526 assert.Equal(t, oltDevice.String(), device.String())
527
528 nb.enableDevice(t, nbi, oltDevice)
529
530 nb.oltAdapter.SetDeleteAction(true)
531 //Remove the device
532 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
533 assert.Nil(t, err)
534
535 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
536 var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
537 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1)
538 }
539 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
540 assert.Nil(t, err)
541
542 nb.oltAdapter.SetDeleteAction(false)
543
544 // Now Force Delete this device
545 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
546 assert.Nil(t, err)
547
548 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
549 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
550 return devices != nil && len(devices.Items) == 0
551 }
552 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
553 assert.Nil(t, err)
554
555}
khenaidoob64fc8a2019-11-27 15:08:19 -0500556
Kent Hagerman2b216042020-04-03 18:28:56 -0400557func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500558 // Create a device that has no adapter registered
559 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
560 assert.Nil(t, err)
561 assert.NotNil(t, oltDeviceNoAdapter)
562
563 // Try to enable the oltDevice and check the error message
564 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
565 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400566 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500567
568 //Remove the device
569 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
570 assert.Nil(t, err)
571
572 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
573 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
574 return devices != nil && len(devices.Items) == 0
575 }
khenaidoo442e7c72020-03-10 16:13:48 -0400576 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500577 assert.Nil(t, err)
578
khenaidoo67b22152020-03-02 16:01:25 -0500579 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
580 var wg sync.WaitGroup
581 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400582 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500583
khenaidoob64fc8a2019-11-27 15:08:19 -0500584 // Create the device with valid data
585 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
586 assert.Nil(t, err)
587 assert.NotNil(t, oltDevice)
588
589 // Verify oltDevice exist in the core
590 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
591 assert.Nil(t, err)
592 assert.Equal(t, 1, len(devices.Items))
593 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
594
595 // Enable the oltDevice
596 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
597 assert.Nil(t, err)
598
599 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400600 var vldFunction = func(ports []*voltha.LogicalPort) bool {
601 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500602 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400603 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500604 assert.Nil(t, err)
605
606 // Verify that the devices have been setup correctly
607 nb.verifyDevices(t, nbi)
608
609 // Get latest oltDevice data
610 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
611 assert.Nil(t, err)
612
613 // Verify that the logical device has been setup correctly
614 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500615
616 // Wait until all flows has been sent to the devices successfully
617 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500618}
619
Kent Hagerman2b216042020-04-03 18:28:56 -0400620func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500621 //Get an OLT device
622 oltDevice, err := nb.getADevice(true, nbi)
623 assert.Nil(t, err)
624 assert.NotNil(t, oltDevice)
625
626 // Disable the oltDevice
627 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
628 assert.Nil(t, err)
629
630 // Wait for the old device to be disabled
631 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
632 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
633 }
634 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
635 assert.Nil(t, err)
636
637 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400638 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500639 assert.Nil(t, err)
640 for _, onu := range onuDevices.Items {
641 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
642 assert.Nil(t, err)
643 }
644
645 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400646 var vlFunction = func(ports []*voltha.LogicalPort) bool {
647 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500648 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
649 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
650 return false
651 }
652 }
653 return true
654 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400655 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500656 assert.Nil(t, err)
657
658 // Reenable the oltDevice
659 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
660 assert.Nil(t, err)
661
662 // Wait for the old device to be enabled
663 vdFunction = func(device *voltha.Device) bool {
664 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
665 }
666 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
667 assert.Nil(t, err)
668
669 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400670 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500671 assert.Nil(t, err)
672 for _, onu := range onuDevices.Items {
673 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
674 assert.Nil(t, err)
675 }
676
677 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400678 vlFunction = func(ports []*voltha.LogicalPort) bool {
679 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500680 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
681 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
682 return false
683 }
684 }
685 return true
686 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400687 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500688 assert.Nil(t, err)
689}
690
Kent Hagerman2b216042020-04-03 18:28:56 -0400691func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500692 //Get an OLT device
693 oltDevice, err := nb.getADevice(true, nbi)
694 assert.Nil(t, err)
695 assert.NotNil(t, oltDevice)
696
697 // Disable the oltDevice
698 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
699 assert.Nil(t, err)
700
701 // Wait for the olt device to be disabled
702 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
703 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
704 }
705 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
706 assert.Nil(t, err)
707
708 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400709 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500710 assert.Nil(t, err)
711 for _, onu := range onuDevices.Items {
712 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
713 assert.Nil(t, err)
714 }
715
716 // Delete the oltDevice
717 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
718 assert.Nil(t, err)
719
720 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
721 return devices != nil && len(devices.Items) == 0
722 }
723 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
724 assert.Nil(t, err)
725
726 // Wait for absence of logical device
727 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
728 return lds != nil && len(lds.Items) == 0
729 }
730
731 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
732 assert.Nil(t, err)
733}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400734
735func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400736 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
737 if len(devices.Items) == 0 {
738 // Nothing to do
739 return
740 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400741 //Get an OLT device
742 oltDevice, err := nb.getADevice(true, nbi)
743 assert.Nil(t, err)
744 assert.NotNil(t, oltDevice)
745
746 // Delete the oltDevice
747 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
748 assert.Nil(t, err)
749
750 // Wait for all devices to be deleted
751 vFunction := func(devices *voltha.Devices) bool {
752 return devices != nil && len(devices.Items) == 0
753 }
754 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
755 assert.Nil(t, err)
756
757 // Wait for absence of logical device
758 vlFunction := func(lds *voltha.LogicalDevices) bool {
759 return lds != nil && len(lds.Items) == 0
760 }
761
762 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
763 assert.Nil(t, err)
764}
765
Kent Hagerman2b216042020-04-03 18:28:56 -0400766func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500767 //Create the device with valid data
768 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
769 assert.Nil(t, err)
770 assert.NotNil(t, oltDevice)
771
772 //Get an OLT device
773 oltDevice, err = nb.getADevice(true, nbi)
774 assert.Nil(t, err)
775 assert.NotNil(t, oltDevice)
776
777 // Enable the oltDevice
778 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
779 assert.Nil(t, err)
780
781 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400782 var vldFunction = func(ports []*voltha.LogicalPort) bool {
783 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500784 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400785 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500786 assert.Nil(t, err)
787
788 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400789 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500790 assert.Nil(t, err)
791
792 // Wait for the all onu devices to be enabled
793 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
794 return device.AdminState == voltha.AdminState_ENABLED
795 }
796 for _, onu := range onuDevices.Items {
797 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
798 assert.Nil(t, err)
799 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500800 // Wait for each onu device to get deleted
801 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
802 return device == nil
803 }
804
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500805 // Delete the onuDevice
806 for _, onu := range onuDevices.Items {
807 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
808 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500809 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
810 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500811 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500812
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500813 // Disable the oltDevice
814 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
815 assert.Nil(t, err)
816
817 // Wait for the olt device to be disabled
818 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
819 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
820 }
821 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
822 assert.Nil(t, err)
823
824 // Delete the oltDevice
825 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
826 assert.Nil(t, err)
827
828 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
829 return devices != nil && len(devices.Items) == 0
830 }
831 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
832 assert.Nil(t, err)
833}
Kent Hagerman2b216042020-04-03 18:28:56 -0400834func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500835 //Get an OLT device
836 var cp *voltha.Port
837 oltDevice, err := nb.getADevice(true, nbi)
838 assert.Nil(t, err)
839 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400840 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
841 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500842
Kent Hagerman2a07b862020-06-19 15:23:07 -0400843 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500844 if cp.Type == voltha.Port_PON_OLT {
845 break
846 }
847
848 }
849 assert.NotNil(t, cp)
850 cp.DeviceId = oltDevice.Id
851
852 // Disable the NW Port of oltDevice
853 _, err = nbi.DisablePort(getContext(), cp)
854 assert.Nil(t, err)
855 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400856 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
857 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500858 if port.PortNo == cp.PortNo {
859 return port.AdminState == voltha.AdminState_DISABLED
860 }
861 }
862 return false
863 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400864 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500865 assert.Nil(t, err)
866 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400867 var vlFunction = func(ports []*voltha.LogicalPort) bool {
868 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500869 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
870 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
871 return false
872 }
873 }
874 return true
875 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400876 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500877 assert.Nil(t, err)
878
879 // Enable the NW Port of oltDevice
880 _, err = nbi.EnablePort(getContext(), cp)
881 assert.Nil(t, err)
882
883 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400884 vdFunction = func(ports *voltha.Ports) bool {
885 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500886 if port.PortNo == cp.PortNo {
887 return port.AdminState == voltha.AdminState_ENABLED
888 }
889 }
890 return false
891 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400892 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500893 assert.Nil(t, err)
894 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400895 vlFunction = func(ports []*voltha.LogicalPort) bool {
896 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500897 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
898 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
899 return false
900 }
901 }
902 return true
903 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400904 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500905 assert.Nil(t, err)
906
907 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400908 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500909 if cp.Type != voltha.Port_PON_OLT {
910 break
911 }
912
913 }
914 assert.NotNil(t, cp)
915 cp.DeviceId = oltDevice.Id
916
917 // Disable the NW Port of oltDevice
918 _, err = nbi.DisablePort(getContext(), cp)
919 assert.NotNil(t, err)
920
921}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500922
Kent Hagerman2b216042020-04-03 18:28:56 -0400923func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700924 //Get an OLT device
925 oltDevice, err := nb.getADevice(true, nbi)
926 assert.Nil(t, err)
927 assert.NotNil(t, oltDevice)
928 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
929 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
930
931 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400932 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700933 assert.Nil(t, err)
934 assert.NotNil(t, onuDevices)
935 assert.Greater(t, len(onuDevices.Items), 0)
936
937 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
938 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
939 assert.Nil(t, err)
940
941 var vlFunction0 = func(d *voltha.Device) bool {
942 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
943 }
944
945 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
946 assert.Nil(t, err)
947
948 // Wait for the logical device to satisfy the expected condition
949 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
950 return ld == nil
951 }
952
953 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
954 assert.Nil(t, err)
955
956 // Wait for the device to satisfy the expected condition (device does not have flows)
957 var vlFunction2 = func(d *voltha.Device) bool {
958 var deviceFlows *ofp.Flows
959 var err error
960 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
961 return false
962 }
963 return len(deviceFlows.Items) == 0
964 }
965
966 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
967 assert.Nil(t, err)
968
969 // Wait for the device to satisfy the expected condition (there are no child devices)
970 var vlFunction3 = func(d *voltha.Device) bool {
971 var devices *voltha.Devices
972 var err error
973 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
974 return false
975 }
976 for _, device := range devices.Items {
977 if device.ParentId == d.Id {
978 // We have a child device still left
979 return false
980 }
981 }
982 return true
983 }
984
985 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
986 assert.Nil(t, err)
987
988 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
989 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400990 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700991 assert.Nil(t, err)
992
993 // Verify the device connection and operation states
994 oltDevice, err = nb.getADevice(true, nbi)
995 assert.Nil(t, err)
996 assert.NotNil(t, oltDevice)
997 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
998 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
999
1000 // Wait for the logical device to satisfy the expected condition
1001 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1002 return ld != nil
1003 }
1004 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1005 assert.Nil(t, err)
1006
1007 // Verify that logical device is created again
1008 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1009 assert.Nil(t, err)
1010 assert.NotNil(t, logicalDevices)
1011 assert.Equal(t, 1, len(logicalDevices.Items))
1012
1013 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001014 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001015 assert.Nil(t, err)
1016 assert.NotNil(t, onuDevices)
1017 assert.Equal(t, 0, len(onuDevices.Items))
1018}
1019
Kent Hagerman2b216042020-04-03 18:28:56 -04001020func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001021 // -----------------------------------------------------------------------
1022 // SubTest 1: Omci test action should fail due to nonexistent device id
1023
1024 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1025 _, err := nbi.StartOmciTestAction(getContext(), request)
1026 assert.NotNil(t, err)
1027 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1028
1029 // -----------------------------------------------------------------------
1030 // SubTest 2: Error should be returned for device with no adapter registered
1031
1032 // Create a device that has no adapter registered
1033 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1034 assert.Nil(t, err)
1035 assert.NotNil(t, deviceNoAdapter)
1036
1037 // Omci test action should fail due to nonexistent adapter
1038 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1039 _, err = nbi.StartOmciTestAction(getContext(), request)
1040 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001041 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001042
1043 //Remove the device
1044 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1045 assert.Nil(t, err)
1046
1047 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1048 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1049 return devices != nil && len(devices.Items) == 0
1050 }
1051 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1052 assert.Nil(t, err)
1053
1054 // -----------------------------------------------------------------------
1055 // SubTest 3: Omci test action should succeed on valid ONU
1056
1057 // Create the device with valid data
1058 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1059 assert.Nil(t, err)
1060 assert.NotNil(t, oltDevice)
1061
1062 // Verify oltDevice exist in the core
1063 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1064 assert.Nil(t, err)
1065 assert.Equal(t, 1, len(devices.Items))
1066 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1067
1068 // Enable the oltDevice
1069 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1070 assert.Nil(t, err)
1071
1072 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001073 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1074 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001075 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001076 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001077 assert.Nil(t, err)
1078
1079 // Wait for the olt device to be enabled
1080 vdFunction := func(device *voltha.Device) bool {
1081 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1082 }
1083 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1084 assert.Nil(t, err)
1085
Kent Hagerman2b216042020-04-03 18:28:56 -04001086 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001087 assert.Nil(t, err)
1088 assert.Greater(t, len(onuDevices.Items), 0)
1089
1090 onuDevice := onuDevices.Items[0]
1091
1092 // Omci test action should succeed
1093 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1094 resp, err := nbi.StartOmciTestAction(getContext(), request)
1095 assert.Nil(t, err)
1096 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1097
1098 //Remove the device
1099 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1100 assert.Nil(t, err)
1101 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1102 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1103 assert.Nil(t, err)
1104}
1105
khenaidoo67b22152020-03-02 16:01:25 -05001106func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1107 matchFields := make([]*ofp.OfpOxmField, 0)
1108 for _, val := range fa.MatchFields {
1109 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1110 }
1111 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1112}
1113
1114func createMetadata(cTag int, techProfile int, port int) uint64 {
1115 md := 0
1116 md = (md | (cTag & 0xFFFF)) << 16
1117 md = (md | (techProfile & 0xFFFF)) << 32
1118 return uint64(md | (port & 0xFFFFFFFF))
1119}
1120
khenaidoo8b4abbf2020-04-24 17:04:30 -04001121func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -05001122 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001123 if flowAddFail {
1124 expectedNumFlows = 0
1125 }
1126 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001127 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -07001128 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
1129 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001130 }
1131 // No timeout implies a success
1132 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1133 assert.Nil(t, err)
1134}
1135
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001136func (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 -05001137 // Send flows for the parent device
1138 var nniPorts []*voltha.LogicalPort
1139 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001140 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001141 if p.RootPort {
1142 nniPorts = append(nniPorts, p)
1143 } else {
1144 uniPorts = append(uniPorts, p)
1145 }
1146 }
1147 assert.Equal(t, 1, len(nniPorts))
1148 //assert.Greater(t, len(uniPorts), 1 )
1149 nniPort := nniPorts[0].OfpPort.PortNo
1150 maxInt32 := uint64(0xFFFFFFFF)
1151 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1152 var fa *flows.FlowArgs
1153 fa = &flows.FlowArgs{
1154 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1155 MatchFields: []*ofp.OfpOxmOfbField{
1156 flows.InPort(nniPort),
1157 flows.EthType(35020),
1158 },
1159 Actions: []*ofp.OfpAction{
1160 flows.Output(controllerPortMask),
1161 },
1162 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001163 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001164 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1165 assert.Nil(t, err)
1166
1167 fa = &flows.FlowArgs{
1168 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1169 MatchFields: []*ofp.OfpOxmOfbField{
1170 flows.InPort(nniPort),
1171 flows.EthType(2048),
1172 flows.IpProto(17),
1173 flows.UdpSrc(67),
1174 flows.UdpDst(68),
1175 },
1176 Actions: []*ofp.OfpAction{
1177 flows.Output(controllerPortMask),
1178 },
1179 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001180 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001181 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1182 assert.Nil(t, err)
1183
1184 fa = &flows.FlowArgs{
1185 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1186 MatchFields: []*ofp.OfpOxmOfbField{
1187 flows.InPort(nniPort),
1188 flows.EthType(34525),
1189 flows.IpProto(17),
1190 flows.UdpSrc(546),
1191 flows.UdpDst(547),
1192 },
1193 Actions: []*ofp.OfpAction{
1194 flows.Output(controllerPortMask),
1195 },
1196 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001197 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001198 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1199 assert.Nil(t, err)
1200
1201 return len(nniPorts), len(uniPorts)
1202}
1203
Kent Hagerman2b216042020-04-03 18:28:56 -04001204func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001205 maxInt32 := uint64(0xFFFFFFFF)
1206 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1207 fa := &flows.FlowArgs{
1208 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},
1209 MatchFields: []*ofp.OfpOxmOfbField{
1210 flows.InPort(port.PortNo),
1211 flows.EthType(34958),
1212 flows.VlanVid(8187),
1213 },
1214 Actions: []*ofp.OfpAction{
1215 flows.Output(controllerPortMask),
1216 },
1217 }
1218 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001219 maxTries := 3
1220 var err error
1221 for {
1222 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1223 if maxTries < 3 {
1224 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1225 }
1226 break
1227 }
1228 t.Log("Sending EAPOL flows fail:", err)
1229 time.Sleep(50 * time.Millisecond)
1230 maxTries--
1231 if maxTries == 0 {
1232 break
1233 }
1234 }
khenaidoo67b22152020-03-02 16:01:25 -05001235 assert.Nil(t, err)
1236}
1237
khenaidoo0db4c812020-05-27 15:27:30 -04001238func (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 -05001239 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001240
1241 // Clear any existing flows on the adapters
1242 nb.oltAdapter.ClearFlows()
1243 nb.onuAdapter.ClearFlows()
1244
khenaidoo8b4abbf2020-04-24 17:04:30 -04001245 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001246 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1247 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001248
khenaidoo67b22152020-03-02 16:01:25 -05001249 // Wait until a logical device is ready
1250 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1251 if lds == nil || len(lds.Items) != 1 {
1252 return false
1253 }
1254 // Ensure there are both NNI ports and at least one UNI port on the logical device
1255 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001256 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1257 if err != nil {
1258 return false
1259 }
khenaidoo67b22152020-03-02 16:01:25 -05001260 nniPort := false
1261 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001262 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001263 nniPort = nniPort || p.RootPort == true
1264 uniPort = uniPort || p.RootPort == false
1265 if nniPort && uniPort {
1266 return true
1267 }
1268 }
1269 return false
1270 }
1271 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1272 assert.Nil(t, err)
1273
1274 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1275 assert.Nil(t, err)
1276 assert.NotNil(t, logicalDevices)
1277 assert.Equal(t, 1, len(logicalDevices.Items))
1278
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001279 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001280 meterID := rand.Uint32()
1281
1282 // Add a meter to the logical device
1283 meterMod := &ofp.OfpMeterMod{
1284 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1285 Flags: rand.Uint32(),
1286 MeterId: meterID,
1287 Bands: []*ofp.OfpMeterBandHeader{
1288 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1289 Rate: rand.Uint32(),
1290 BurstSize: rand.Uint32(),
1291 Data: nil,
1292 },
1293 },
1294 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001295 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1296 assert.Nil(t, err)
1297
1298 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001299 assert.Nil(t, err)
1300
1301 // Send initial set of Trap flows
1302 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001303 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001304
1305 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001306 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001307 processedNniLogicalPorts := 0
1308 processedUniLogicalPorts := 0
1309
Kent Hagerman45a13e42020-04-13 12:23:50 -04001310 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001311 startingVlan++
1312 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1313 ps := portStatus.PortStatus
1314 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001315 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001316 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001317 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001318 } else {
1319 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001320 }
1321 }
1322 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001323
1324 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001325 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001326 break
1327 }
1328 }
1329 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001330 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001331
khenaidoo8b4abbf2020-04-24 17:04:30 -04001332 // Wait until all flows have been sent to the OLT adapters (or all failed)
1333 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1334 if flowAddFail {
1335 expectedFlowCount = 0
1336 }
khenaidoo67b22152020-03-02 16:01:25 -05001337 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001338 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001339 }
1340 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1341 assert.Nil(t, err)
1342
khenaidoo8b4abbf2020-04-24 17:04:30 -04001343 // Wait until all flows have been sent to the ONU adapters (or all failed)
1344 expectedFlowCount = numUNIPorts
1345 if flowAddFail {
1346 expectedFlowCount = 0
1347 }
khenaidoo67b22152020-03-02 16:01:25 -05001348 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001349 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001350 }
1351 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1352 assert.Nil(t, err)
1353}
1354
khenaidoo8b4abbf2020-04-24 17:04:30 -04001355func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1356
1357 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1358 var wg sync.WaitGroup
1359 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001360 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001361
1362 // Create the device with valid data
1363 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1364 assert.Nil(t, err)
1365 assert.NotNil(t, oltDevice)
1366
1367 // Verify oltDevice exist in the core
1368 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1369 assert.Nil(t, err)
1370 assert.Equal(t, 1, len(devices.Items))
1371 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1372
1373 // Enable the oltDevice
1374 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1375 assert.Nil(t, err)
1376
1377 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001378 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1379 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001380 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001381 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001382 assert.Nil(t, err)
1383
1384 // Verify that the devices have been setup correctly
1385 nb.verifyDevices(t, nbi)
1386
1387 // Get latest oltDevice data
1388 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1389 assert.Nil(t, err)
1390
1391 // Verify that the logical device has been setup correctly
1392 nb.verifyLogicalDevices(t, oltDevice, nbi)
1393
1394 // Wait until all flows has been sent to the devices successfully
1395 wg.Wait()
1396}
1397
Matteo Scandolod525ae32020-04-02 17:27:29 -07001398func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001399 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001400 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001401 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001402 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001403 }
1404 defer f.Close()
1405 runtime.SetBlockProfileRate(1)
1406 runtime.SetMutexProfileFraction(-1)
1407 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001408 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001409 }
1410 defer pprof.StopCPUProfile()
1411
khenaidoo442e7c72020-03-10 16:13:48 -04001412 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1413
Rohan Agrawal31f21802020-06-12 05:38:46 +00001414 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001415 assert.NotNil(t, nb)
1416
Rohan Agrawal31f21802020-06-12 05:38:46 +00001417 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001418
1419 // Start the Core
1420 nb.startCore(false)
1421
1422 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001423 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001424
1425 // 1. Basic test with no data in Core
1426 nb.testCoreWithoutData(t, nbi)
1427
1428 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001429 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 -07001430 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1431 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001432
1433 // 2. Test adapter registration
1434 nb.testAdapterRegistration(t, nbi)
1435
khenaidoo8b4abbf2020-04-24 17:04:30 -04001436 numberOfTestRuns := 2
1437 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301438
1439 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001440 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001441
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301442 // 4. Test Delete Device Scenarios
1443 nb.testForceDeletePreProvDevice(t, nbi)
1444 nb.testDeletePreProvDevice(t, nbi)
1445 nb.testForceDeleteEnabledDevice(t, nbi)
1446 nb.testDeleteEnabledDevice(t, nbi)
1447 nb.testForceDeleteDeviceFailure(t, nbi)
1448 nb.testDeleteDeviceFailure(t, nbi)
1449
1450 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001451 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001452
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301453 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001454 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001455
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301456 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001457 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001458
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301459 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001460 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1461
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301462 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001463 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001464
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301465 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001466 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001467
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301468 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001469 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001470
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301471 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001472 nb.deleteAllDevices(t, nbi)
1473
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301474 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001475 nb.testFlowAddFailure(t, nbi)
1476
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301477 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001478 nb.deleteAllDevices(t, nbi)
1479 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001480}