blob: 6b37a148936ecbe37e61225d905da5a49eca5935 [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"
Kent Hagerman2b216042020-04-03 18:28:56 -040040 "github.com/opencord/voltha-lib-go/v3/pkg/db"
41 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080042 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
Matteo Scandolod525ae32020-04-02 17:27:29 -070043 mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
44 mock_kafka "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080045 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
46 "github.com/opencord/voltha-protos/v3/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}
356
Kent Hagerman2b216042020-04-03 18:28:56 -0400357func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500358 // Create a device that has no adapter registered
359 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
360 assert.Nil(t, err)
361 assert.NotNil(t, oltDeviceNoAdapter)
362
363 // Try to enable the oltDevice and check the error message
364 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
365 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400366 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500367
368 //Remove the device
369 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
370 assert.Nil(t, err)
371
372 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
373 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
374 return devices != nil && len(devices.Items) == 0
375 }
khenaidoo442e7c72020-03-10 16:13:48 -0400376 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500377 assert.Nil(t, err)
378
khenaidoo67b22152020-03-02 16:01:25 -0500379 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
380 var wg sync.WaitGroup
381 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400382 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500383
khenaidoob64fc8a2019-11-27 15:08:19 -0500384 // Create the device with valid data
385 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
386 assert.Nil(t, err)
387 assert.NotNil(t, oltDevice)
388
389 // Verify oltDevice exist in the core
390 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
391 assert.Nil(t, err)
392 assert.Equal(t, 1, len(devices.Items))
393 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
394
395 // Enable the oltDevice
396 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
397 assert.Nil(t, err)
398
399 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400400 var vldFunction = func(ports []*voltha.LogicalPort) bool {
401 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500402 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400403 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500404 assert.Nil(t, err)
405
406 // Verify that the devices have been setup correctly
407 nb.verifyDevices(t, nbi)
408
409 // Get latest oltDevice data
410 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
411 assert.Nil(t, err)
412
413 // Verify that the logical device has been setup correctly
414 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500415
416 // Wait until all flows has been sent to the devices successfully
417 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500418}
419
Kent Hagerman2b216042020-04-03 18:28:56 -0400420func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500421 //Get an OLT device
422 oltDevice, err := nb.getADevice(true, nbi)
423 assert.Nil(t, err)
424 assert.NotNil(t, oltDevice)
425
426 // Disable the oltDevice
427 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
428 assert.Nil(t, err)
429
430 // Wait for the old device to be disabled
431 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
432 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
433 }
434 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
435 assert.Nil(t, err)
436
437 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400438 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500439 assert.Nil(t, err)
440 for _, onu := range onuDevices.Items {
441 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
442 assert.Nil(t, err)
443 }
444
445 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400446 var vlFunction = func(ports []*voltha.LogicalPort) bool {
447 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500448 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
449 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
450 return false
451 }
452 }
453 return true
454 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400455 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500456 assert.Nil(t, err)
457
458 // Reenable the oltDevice
459 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
460 assert.Nil(t, err)
461
462 // Wait for the old device to be enabled
463 vdFunction = func(device *voltha.Device) bool {
464 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
465 }
466 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
467 assert.Nil(t, err)
468
469 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400470 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500471 assert.Nil(t, err)
472 for _, onu := range onuDevices.Items {
473 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
474 assert.Nil(t, err)
475 }
476
477 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400478 vlFunction = func(ports []*voltha.LogicalPort) bool {
479 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500480 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
481 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
482 return false
483 }
484 }
485 return true
486 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400487 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500488 assert.Nil(t, err)
489}
490
Kent Hagerman2b216042020-04-03 18:28:56 -0400491func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500492 //Get an OLT device
493 oltDevice, err := nb.getADevice(true, nbi)
494 assert.Nil(t, err)
495 assert.NotNil(t, oltDevice)
496
497 // Disable the oltDevice
498 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
499 assert.Nil(t, err)
500
501 // Wait for the olt device to be disabled
502 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
503 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
504 }
505 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
506 assert.Nil(t, err)
507
508 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400509 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500510 assert.Nil(t, err)
511 for _, onu := range onuDevices.Items {
512 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
513 assert.Nil(t, err)
514 }
515
516 // Delete the oltDevice
517 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
518 assert.Nil(t, err)
519
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 // Wait for absence of logical device
527 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
528 return lds != nil && len(lds.Items) == 0
529 }
530
531 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
532 assert.Nil(t, err)
533}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400534
535func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400536 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
537 if len(devices.Items) == 0 {
538 // Nothing to do
539 return
540 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400541 //Get an OLT device
542 oltDevice, err := nb.getADevice(true, nbi)
543 assert.Nil(t, err)
544 assert.NotNil(t, oltDevice)
545
546 // Delete the oltDevice
547 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
548 assert.Nil(t, err)
549
550 // Wait for all devices to be deleted
551 vFunction := func(devices *voltha.Devices) bool {
552 return devices != nil && len(devices.Items) == 0
553 }
554 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
555 assert.Nil(t, err)
556
557 // Wait for absence of logical device
558 vlFunction := func(lds *voltha.LogicalDevices) bool {
559 return lds != nil && len(lds.Items) == 0
560 }
561
562 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
563 assert.Nil(t, err)
564}
565
Kent Hagerman2b216042020-04-03 18:28:56 -0400566func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500567 //Create the device with valid data
568 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
569 assert.Nil(t, err)
570 assert.NotNil(t, oltDevice)
571
572 //Get an OLT device
573 oltDevice, err = nb.getADevice(true, nbi)
574 assert.Nil(t, err)
575 assert.NotNil(t, oltDevice)
576
577 // Enable the oltDevice
578 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
579 assert.Nil(t, err)
580
581 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400582 var vldFunction = func(ports []*voltha.LogicalPort) bool {
583 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500584 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400585 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500586 assert.Nil(t, err)
587
588 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400589 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500590 assert.Nil(t, err)
591
592 // Wait for the all onu devices to be enabled
593 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
594 return device.AdminState == voltha.AdminState_ENABLED
595 }
596 for _, onu := range onuDevices.Items {
597 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
598 assert.Nil(t, err)
599 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500600 // Wait for each onu device to get deleted
601 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
602 return device == nil
603 }
604
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500605 // Delete the onuDevice
606 for _, onu := range onuDevices.Items {
607 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
608 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500609 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
610 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500611 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500612
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500613 // Disable the oltDevice
614 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
615 assert.Nil(t, err)
616
617 // Wait for the olt device to be disabled
618 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
619 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
620 }
621 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
622 assert.Nil(t, err)
623
624 // Delete the oltDevice
625 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
626 assert.Nil(t, err)
627
628 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
629 return devices != nil && len(devices.Items) == 0
630 }
631 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
632 assert.Nil(t, err)
633}
Kent Hagerman2b216042020-04-03 18:28:56 -0400634func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500635 //Get an OLT device
636 var cp *voltha.Port
637 oltDevice, err := nb.getADevice(true, nbi)
638 assert.Nil(t, err)
639 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400640 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
641 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500642
Kent Hagerman2a07b862020-06-19 15:23:07 -0400643 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500644 if cp.Type == voltha.Port_PON_OLT {
645 break
646 }
647
648 }
649 assert.NotNil(t, cp)
650 cp.DeviceId = oltDevice.Id
651
652 // Disable the NW Port of oltDevice
653 _, err = nbi.DisablePort(getContext(), cp)
654 assert.Nil(t, err)
655 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400656 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
657 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500658 if port.PortNo == cp.PortNo {
659 return port.AdminState == voltha.AdminState_DISABLED
660 }
661 }
662 return false
663 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400664 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500665 assert.Nil(t, err)
666 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400667 var vlFunction = func(ports []*voltha.LogicalPort) bool {
668 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500669 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
670 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
671 return false
672 }
673 }
674 return true
675 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400676 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500677 assert.Nil(t, err)
678
679 // Enable the NW Port of oltDevice
680 _, err = nbi.EnablePort(getContext(), cp)
681 assert.Nil(t, err)
682
683 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400684 vdFunction = func(ports *voltha.Ports) bool {
685 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500686 if port.PortNo == cp.PortNo {
687 return port.AdminState == voltha.AdminState_ENABLED
688 }
689 }
690 return false
691 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400692 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500693 assert.Nil(t, err)
694 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400695 vlFunction = func(ports []*voltha.LogicalPort) bool {
696 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500697 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
698 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
699 return false
700 }
701 }
702 return true
703 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400704 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500705 assert.Nil(t, err)
706
707 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400708 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500709 if cp.Type != voltha.Port_PON_OLT {
710 break
711 }
712
713 }
714 assert.NotNil(t, cp)
715 cp.DeviceId = oltDevice.Id
716
717 // Disable the NW Port of oltDevice
718 _, err = nbi.DisablePort(getContext(), cp)
719 assert.NotNil(t, err)
720
721}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500722
Kent Hagerman2b216042020-04-03 18:28:56 -0400723func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700724 //Get an OLT device
725 oltDevice, err := nb.getADevice(true, nbi)
726 assert.Nil(t, err)
727 assert.NotNil(t, oltDevice)
728 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
729 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
730
731 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400732 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700733 assert.Nil(t, err)
734 assert.NotNil(t, onuDevices)
735 assert.Greater(t, len(onuDevices.Items), 0)
736
737 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
738 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
739 assert.Nil(t, err)
740
741 var vlFunction0 = func(d *voltha.Device) bool {
742 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
743 }
744
745 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
746 assert.Nil(t, err)
747
748 // Wait for the logical device to satisfy the expected condition
749 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
750 return ld == nil
751 }
752
753 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
754 assert.Nil(t, err)
755
756 // Wait for the device to satisfy the expected condition (device does not have flows)
757 var vlFunction2 = func(d *voltha.Device) bool {
758 var deviceFlows *ofp.Flows
759 var err error
760 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
761 return false
762 }
763 return len(deviceFlows.Items) == 0
764 }
765
766 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
767 assert.Nil(t, err)
768
769 // Wait for the device to satisfy the expected condition (there are no child devices)
770 var vlFunction3 = func(d *voltha.Device) bool {
771 var devices *voltha.Devices
772 var err error
773 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
774 return false
775 }
776 for _, device := range devices.Items {
777 if device.ParentId == d.Id {
778 // We have a child device still left
779 return false
780 }
781 }
782 return true
783 }
784
785 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
786 assert.Nil(t, err)
787
788 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
789 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400790 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700791 assert.Nil(t, err)
792
793 // Verify the device connection and operation states
794 oltDevice, err = nb.getADevice(true, nbi)
795 assert.Nil(t, err)
796 assert.NotNil(t, oltDevice)
797 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
798 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
799
800 // Wait for the logical device to satisfy the expected condition
801 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
802 return ld != nil
803 }
804 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
805 assert.Nil(t, err)
806
807 // Verify that logical device is created again
808 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
809 assert.Nil(t, err)
810 assert.NotNil(t, logicalDevices)
811 assert.Equal(t, 1, len(logicalDevices.Items))
812
813 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400814 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700815 assert.Nil(t, err)
816 assert.NotNil(t, onuDevices)
817 assert.Equal(t, 0, len(onuDevices.Items))
818}
819
Kent Hagerman2b216042020-04-03 18:28:56 -0400820func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700821 // -----------------------------------------------------------------------
822 // SubTest 1: Omci test action should fail due to nonexistent device id
823
824 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
825 _, err := nbi.StartOmciTestAction(getContext(), request)
826 assert.NotNil(t, err)
827 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
828
829 // -----------------------------------------------------------------------
830 // SubTest 2: Error should be returned for device with no adapter registered
831
832 // Create a device that has no adapter registered
833 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
834 assert.Nil(t, err)
835 assert.NotNil(t, deviceNoAdapter)
836
837 // Omci test action should fail due to nonexistent adapter
838 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
839 _, err = nbi.StartOmciTestAction(getContext(), request)
840 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400841 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700842
843 //Remove the device
844 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
845 assert.Nil(t, err)
846
847 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
848 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
849 return devices != nil && len(devices.Items) == 0
850 }
851 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
852 assert.Nil(t, err)
853
854 // -----------------------------------------------------------------------
855 // SubTest 3: Omci test action should succeed on valid ONU
856
857 // Create the device with valid data
858 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
859 assert.Nil(t, err)
860 assert.NotNil(t, oltDevice)
861
862 // Verify oltDevice exist in the core
863 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
864 assert.Nil(t, err)
865 assert.Equal(t, 1, len(devices.Items))
866 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
867
868 // Enable the oltDevice
869 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
870 assert.Nil(t, err)
871
872 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400873 var vldFunction = func(ports []*voltha.LogicalPort) bool {
874 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -0700875 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400876 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -0700877 assert.Nil(t, err)
878
879 // Wait for the olt device to be enabled
880 vdFunction := func(device *voltha.Device) bool {
881 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
882 }
883 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
884 assert.Nil(t, err)
885
Kent Hagerman2b216042020-04-03 18:28:56 -0400886 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700887 assert.Nil(t, err)
888 assert.Greater(t, len(onuDevices.Items), 0)
889
890 onuDevice := onuDevices.Items[0]
891
892 // Omci test action should succeed
893 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
894 resp, err := nbi.StartOmciTestAction(getContext(), request)
895 assert.Nil(t, err)
896 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
897
898 //Remove the device
899 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
900 assert.Nil(t, err)
901 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
902 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
903 assert.Nil(t, err)
904}
905
khenaidoo67b22152020-03-02 16:01:25 -0500906func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
907 matchFields := make([]*ofp.OfpOxmField, 0)
908 for _, val := range fa.MatchFields {
909 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
910 }
911 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
912}
913
914func createMetadata(cTag int, techProfile int, port int) uint64 {
915 md := 0
916 md = (md | (cTag & 0xFFFF)) << 16
917 md = (md | (techProfile & 0xFFFF)) << 32
918 return uint64(md | (port & 0xFFFFFFFF))
919}
920
khenaidoo8b4abbf2020-04-24 17:04:30 -0400921func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500922 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400923 if flowAddFail {
924 expectedNumFlows = 0
925 }
926 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500927 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700928 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
929 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500930 }
931 // No timeout implies a success
932 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
933 assert.Nil(t, err)
934}
935
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400936func (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 -0500937 // Send flows for the parent device
938 var nniPorts []*voltha.LogicalPort
939 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400940 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -0500941 if p.RootPort {
942 nniPorts = append(nniPorts, p)
943 } else {
944 uniPorts = append(uniPorts, p)
945 }
946 }
947 assert.Equal(t, 1, len(nniPorts))
948 //assert.Greater(t, len(uniPorts), 1 )
949 nniPort := nniPorts[0].OfpPort.PortNo
950 maxInt32 := uint64(0xFFFFFFFF)
951 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
952 var fa *flows.FlowArgs
953 fa = &flows.FlowArgs{
954 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
955 MatchFields: []*ofp.OfpOxmOfbField{
956 flows.InPort(nniPort),
957 flows.EthType(35020),
958 },
959 Actions: []*ofp.OfpAction{
960 flows.Output(controllerPortMask),
961 },
962 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400963 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500964 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
965 assert.Nil(t, err)
966
967 fa = &flows.FlowArgs{
968 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
969 MatchFields: []*ofp.OfpOxmOfbField{
970 flows.InPort(nniPort),
971 flows.EthType(2048),
972 flows.IpProto(17),
973 flows.UdpSrc(67),
974 flows.UdpDst(68),
975 },
976 Actions: []*ofp.OfpAction{
977 flows.Output(controllerPortMask),
978 },
979 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400980 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500981 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
982 assert.Nil(t, err)
983
984 fa = &flows.FlowArgs{
985 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
986 MatchFields: []*ofp.OfpOxmOfbField{
987 flows.InPort(nniPort),
988 flows.EthType(34525),
989 flows.IpProto(17),
990 flows.UdpSrc(546),
991 flows.UdpDst(547),
992 },
993 Actions: []*ofp.OfpAction{
994 flows.Output(controllerPortMask),
995 },
996 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400997 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500998 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
999 assert.Nil(t, err)
1000
1001 return len(nniPorts), len(uniPorts)
1002}
1003
Kent Hagerman2b216042020-04-03 18:28:56 -04001004func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001005 maxInt32 := uint64(0xFFFFFFFF)
1006 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1007 fa := &flows.FlowArgs{
1008 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},
1009 MatchFields: []*ofp.OfpOxmOfbField{
1010 flows.InPort(port.PortNo),
1011 flows.EthType(34958),
1012 flows.VlanVid(8187),
1013 },
1014 Actions: []*ofp.OfpAction{
1015 flows.Output(controllerPortMask),
1016 },
1017 }
1018 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001019 maxTries := 3
1020 var err error
1021 for {
1022 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1023 if maxTries < 3 {
1024 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1025 }
1026 break
1027 }
1028 t.Log("Sending EAPOL flows fail:", err)
1029 time.Sleep(50 * time.Millisecond)
1030 maxTries--
1031 if maxTries == 0 {
1032 break
1033 }
1034 }
khenaidoo67b22152020-03-02 16:01:25 -05001035 assert.Nil(t, err)
1036}
1037
khenaidoo0db4c812020-05-27 15:27:30 -04001038func (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 -05001039 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001040
1041 // Clear any existing flows on the adapters
1042 nb.oltAdapter.ClearFlows()
1043 nb.onuAdapter.ClearFlows()
1044
khenaidoo8b4abbf2020-04-24 17:04:30 -04001045 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001046 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1047 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001048
khenaidoo67b22152020-03-02 16:01:25 -05001049 // Wait until a logical device is ready
1050 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1051 if lds == nil || len(lds.Items) != 1 {
1052 return false
1053 }
1054 // Ensure there are both NNI ports and at least one UNI port on the logical device
1055 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001056 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1057 if err != nil {
1058 return false
1059 }
khenaidoo67b22152020-03-02 16:01:25 -05001060 nniPort := false
1061 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001062 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001063 nniPort = nniPort || p.RootPort == true
1064 uniPort = uniPort || p.RootPort == false
1065 if nniPort && uniPort {
1066 return true
1067 }
1068 }
1069 return false
1070 }
1071 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1072 assert.Nil(t, err)
1073
1074 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1075 assert.Nil(t, err)
1076 assert.NotNil(t, logicalDevices)
1077 assert.Equal(t, 1, len(logicalDevices.Items))
1078
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001079 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001080 meterID := rand.Uint32()
1081
1082 // Add a meter to the logical device
1083 meterMod := &ofp.OfpMeterMod{
1084 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1085 Flags: rand.Uint32(),
1086 MeterId: meterID,
1087 Bands: []*ofp.OfpMeterBandHeader{
1088 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1089 Rate: rand.Uint32(),
1090 BurstSize: rand.Uint32(),
1091 Data: nil,
1092 },
1093 },
1094 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001095 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1096 assert.Nil(t, err)
1097
1098 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001099 assert.Nil(t, err)
1100
1101 // Send initial set of Trap flows
1102 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001103 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001104
1105 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001106 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001107 processedNniLogicalPorts := 0
1108 processedUniLogicalPorts := 0
1109
Kent Hagerman45a13e42020-04-13 12:23:50 -04001110 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001111 startingVlan++
1112 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1113 ps := portStatus.PortStatus
1114 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001115 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001116 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001117 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001118 } else {
1119 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001120 }
1121 }
1122 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001123
1124 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001125 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001126 break
1127 }
1128 }
1129 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001130 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001131
khenaidoo8b4abbf2020-04-24 17:04:30 -04001132 // Wait until all flows have been sent to the OLT adapters (or all failed)
1133 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1134 if flowAddFail {
1135 expectedFlowCount = 0
1136 }
khenaidoo67b22152020-03-02 16:01:25 -05001137 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001138 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001139 }
1140 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1141 assert.Nil(t, err)
1142
khenaidoo8b4abbf2020-04-24 17:04:30 -04001143 // Wait until all flows have been sent to the ONU adapters (or all failed)
1144 expectedFlowCount = numUNIPorts
1145 if flowAddFail {
1146 expectedFlowCount = 0
1147 }
khenaidoo67b22152020-03-02 16:01:25 -05001148 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001149 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001150 }
1151 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1152 assert.Nil(t, err)
1153}
1154
khenaidoo8b4abbf2020-04-24 17:04:30 -04001155func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1156
1157 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1158 var wg sync.WaitGroup
1159 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001160 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001161
1162 // Create the device with valid data
1163 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1164 assert.Nil(t, err)
1165 assert.NotNil(t, oltDevice)
1166
1167 // Verify oltDevice exist in the core
1168 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1169 assert.Nil(t, err)
1170 assert.Equal(t, 1, len(devices.Items))
1171 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1172
1173 // Enable the oltDevice
1174 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1175 assert.Nil(t, err)
1176
1177 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001178 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1179 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001180 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001181 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001182 assert.Nil(t, err)
1183
1184 // Verify that the devices have been setup correctly
1185 nb.verifyDevices(t, nbi)
1186
1187 // Get latest oltDevice data
1188 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1189 assert.Nil(t, err)
1190
1191 // Verify that the logical device has been setup correctly
1192 nb.verifyLogicalDevices(t, oltDevice, nbi)
1193
1194 // Wait until all flows has been sent to the devices successfully
1195 wg.Wait()
1196}
1197
Matteo Scandolod525ae32020-04-02 17:27:29 -07001198func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001199 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001200 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001201 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001202 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001203 }
1204 defer f.Close()
1205 runtime.SetBlockProfileRate(1)
1206 runtime.SetMutexProfileFraction(-1)
1207 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001208 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001209 }
1210 defer pprof.StopCPUProfile()
1211
khenaidoo442e7c72020-03-10 16:13:48 -04001212 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1213
Rohan Agrawal31f21802020-06-12 05:38:46 +00001214 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001215 assert.NotNil(t, nb)
1216
Rohan Agrawal31f21802020-06-12 05:38:46 +00001217 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001218
1219 // Start the Core
1220 nb.startCore(false)
1221
1222 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001223 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001224
1225 // 1. Basic test with no data in Core
1226 nb.testCoreWithoutData(t, nbi)
1227
1228 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001229 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 -07001230 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1231 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001232
1233 // 2. Test adapter registration
1234 nb.testAdapterRegistration(t, nbi)
1235
khenaidoo8b4abbf2020-04-24 17:04:30 -04001236 numberOfTestRuns := 2
1237 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001238 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001239 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001240
khenaidoo93d5a3d2020-01-15 12:37:05 -05001241 // 4. Test Enable a device
1242 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001243
khenaidoo0db4c812020-05-27 15:27:30 -04001244 //// 5. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001245 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001246
kesavandbc2d1622020-01-21 00:42:01 -05001247 // 6. Test disable and Enable pon port of OLT device
1248 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001249
Girish Gowdra408cd962020-03-11 14:31:31 -07001250 // 7.Test Device unreachable when OLT is enabled
1251 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1252
1253 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001254 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001255
Girish Gowdra408cd962020-03-11 14:31:31 -07001256 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001257 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001258
1259 // 10. Test omci test
1260 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001261
khenaidoo0db4c812020-05-27 15:27:30 -04001262 // 11. Remove all devices from tests above
1263 nb.deleteAllDevices(t, nbi)
1264
khenaidoo8b4abbf2020-04-24 17:04:30 -04001265 // 11. Test flow add failure
1266 nb.testFlowAddFailure(t, nbi)
1267
1268 // 12. Clean up
1269 nb.deleteAllDevices(t, nbi)
1270 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001271}