blob: 77c0b060ebb20b64209a973c5a25805c438fe5d4 [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
72func newNBTest() *NBTest {
73 test := &NBTest{}
74 // Start the embedded etcd server
75 var err error
Mahir Gunyel03de0d32020-06-03 01:36:59 -070076 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050077 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000078 logger.Fatal(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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000100 logger.Fatal("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)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700104 client := tst.SetupKVClient(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)
Kent Hagerman2b216042020-04-03 18:28:56 -0400118 nb.adapterMgr = adapter.NewAdapterManager(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
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400122 if err := nb.kmp.Start(); err != nil {
Kent Hagerman2b216042020-04-03 18:28:56 -0400123 logger.Fatalf("Cannot start InterContainerProxy: %s", err)
124 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400125 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Kent Hagerman2b216042020-04-03 18:28:56 -0400126 if err := nb.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
127 logger.Fatalf("Cannot add request handler: %s", err)
128 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500129}
130
khenaidoob64fc8a2019-11-27 15:08:19 -0500131func (nb *NBTest) stopAll() {
132 if nb.kClient != nil {
133 nb.kClient.Stop()
134 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400135 if nb.kmp != nil {
136 nb.kmp.Stop()
khenaidoob64fc8a2019-11-27 15:08:19 -0500137 }
138 if nb.etcdServer != nil {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700139 tst.StopEmbeddedEtcdServer(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)
208 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
209 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
210 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
211 assert.Equal(t, d.Type, d.Adapter)
212 assert.NotEqual(t, "", d.MacAddress)
213 assert.NotEqual(t, "", d.SerialNumber)
214
215 if d.Type == "olt_adapter_mock" {
216 assert.Equal(t, true, d.Root)
217 assert.NotEqual(t, "", d.Id)
218 assert.NotEqual(t, "", d.ParentId)
219 assert.Nil(t, d.ProxyAddress)
220 } else if d.Type == "onu_adapter_mock" {
221 assert.Equal(t, false, d.Root)
222 assert.NotEqual(t, uint32(0), d.Vlan)
223 assert.NotEqual(t, "", d.Id)
224 assert.NotEqual(t, "", d.ParentId)
225 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
226 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500227 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500228 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500229 }
khenaidoo67b22152020-03-02 16:01:25 -0500230 assert.Equal(t, 2, len(d.Ports))
231 for _, p := range d.Ports {
232 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
233 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
234 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
235 assert.Equal(t, 0, len(p.Peers))
236 } else if p.Type == voltha.Port_PON_OLT {
237 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
238 assert.Equal(t, uint32(1), p.PortNo)
239 } else if p.Type == voltha.Port_PON_ONU {
240 assert.Equal(t, 1, len(p.Peers))
241 assert.Equal(t, uint32(1), p.PortNo)
242 } else {
243 assert.Error(t, errors.New("invalid-port"))
244 }
245 }
246 wg.Done()
247 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500248 }
khenaidoo67b22152020-03-02 16:01:25 -0500249 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500250}
251
Kent Hagerman2b216042020-04-03 18:28:56 -0400252func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500253 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
254 if err != nil {
255 return nil, err
256 }
257 for _, d := range devices.Items {
258 if d.Root == rootDevice {
259 return d, nil
260 }
261 }
262 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
263}
264
Kent Hagerman2b216042020-04-03 18:28:56 -0400265func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500266 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
267 assert.Nil(t, err)
268 assert.NotNil(t, lds)
269 assert.Equal(t, 0, len(lds.Items))
270 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
271 assert.Nil(t, err)
272 assert.NotNil(t, devices)
273 assert.Equal(t, 0, len(devices.Items))
274 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400275 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500276 assert.Nil(t, err)
277 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500278}
279
Kent Hagerman2b216042020-04-03 18:28:56 -0400280func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500281 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
282 assert.Nil(t, err)
283 assert.NotNil(t, adapters)
284 assert.Equal(t, 2, len(adapters.Items))
285 for _, a := range adapters.Items {
286 switch a.Id {
287 case nb.oltAdapterName:
288 assert.Equal(t, "Voltha-olt", a.Vendor)
289 case nb.onuAdapterName:
290 assert.Equal(t, "Voltha-onu", a.Vendor)
291 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000292 logger.Fatal("unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500293 }
294 }
295 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
296 assert.Nil(t, err)
297 assert.NotNil(t, deviceTypes)
298 assert.Equal(t, 2, len(deviceTypes.Items))
299 for _, dt := range deviceTypes.Items {
300 switch dt.Id {
301 case nb.oltAdapterName:
302 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
303 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
304 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
305 case nb.onuAdapterName:
306 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
307 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
308 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
309 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000310 logger.Fatal("invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500311 }
312 }
313}
314
Kent Hagerman2b216042020-04-03 18:28:56 -0400315func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500316 // Create a valid device
317 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
318 assert.Nil(t, err)
319 assert.NotNil(t, oltDevice)
320 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
321 assert.Nil(t, err)
322 assert.NotNil(t, device)
323 assert.Equal(t, oltDevice.String(), device.String())
324
325 // Try to create the same device
326 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
327 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400328 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500329
330 // Try to create a device with invalid data
331 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
332 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530333 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500334
335 // Ensure we only have 1 device in the Core
336 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
337 assert.Nil(t, err)
338 assert.NotNil(t, devices)
339 assert.Equal(t, 1, len(devices.Items))
340 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
341
342 //Remove the device
343 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
344 assert.Nil(t, err)
345
346 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
347 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
348 return devices != nil && len(devices.Items) == 0
349 }
khenaidoo442e7c72020-03-10 16:13:48 -0400350 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500351 assert.Nil(t, err)
352}
353
Kent Hagerman2b216042020-04-03 18:28:56 -0400354func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500355 // Create a device that has no adapter registered
356 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
357 assert.Nil(t, err)
358 assert.NotNil(t, oltDeviceNoAdapter)
359
360 // Try to enable the oltDevice and check the error message
361 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
362 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400363 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500364
365 //Remove the device
366 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
367 assert.Nil(t, err)
368
369 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
370 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
371 return devices != nil && len(devices.Items) == 0
372 }
khenaidoo442e7c72020-03-10 16:13:48 -0400373 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500374 assert.Nil(t, err)
375
khenaidoo67b22152020-03-02 16:01:25 -0500376 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
377 var wg sync.WaitGroup
378 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400379 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500380
khenaidoob64fc8a2019-11-27 15:08:19 -0500381 // Create the device with valid data
382 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
383 assert.Nil(t, err)
384 assert.NotNil(t, oltDevice)
385
386 // Verify oltDevice exist in the core
387 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
388 assert.Nil(t, err)
389 assert.Equal(t, 1, len(devices.Items))
390 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
391
392 // Enable the oltDevice
393 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
394 assert.Nil(t, err)
395
396 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400397 var vldFunction = func(ports []*voltha.LogicalPort) bool {
398 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500399 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400400 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500401 assert.Nil(t, err)
402
403 // Verify that the devices have been setup correctly
404 nb.verifyDevices(t, nbi)
405
406 // Get latest oltDevice data
407 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
408 assert.Nil(t, err)
409
410 // Verify that the logical device has been setup correctly
411 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500412
413 // Wait until all flows has been sent to the devices successfully
414 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500415}
416
Kent Hagerman2b216042020-04-03 18:28:56 -0400417func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500418 //Get an OLT device
419 oltDevice, err := nb.getADevice(true, nbi)
420 assert.Nil(t, err)
421 assert.NotNil(t, oltDevice)
422
423 // Disable the oltDevice
424 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
425 assert.Nil(t, err)
426
427 // Wait for the old device to be disabled
428 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
429 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
430 }
431 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
432 assert.Nil(t, err)
433
434 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400435 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500436 assert.Nil(t, err)
437 for _, onu := range onuDevices.Items {
438 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
439 assert.Nil(t, err)
440 }
441
442 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400443 var vlFunction = func(ports []*voltha.LogicalPort) bool {
444 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500445 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
446 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
447 return false
448 }
449 }
450 return true
451 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400452 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500453 assert.Nil(t, err)
454
455 // Reenable the oltDevice
456 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
457 assert.Nil(t, err)
458
459 // Wait for the old device to be enabled
460 vdFunction = func(device *voltha.Device) bool {
461 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
462 }
463 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
464 assert.Nil(t, err)
465
466 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400467 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500468 assert.Nil(t, err)
469 for _, onu := range onuDevices.Items {
470 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
471 assert.Nil(t, err)
472 }
473
474 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400475 vlFunction = func(ports []*voltha.LogicalPort) bool {
476 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500477 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
478 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
479 return false
480 }
481 }
482 return true
483 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400484 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500485 assert.Nil(t, err)
486}
487
Kent Hagerman2b216042020-04-03 18:28:56 -0400488func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500489 //Get an OLT device
490 oltDevice, err := nb.getADevice(true, nbi)
491 assert.Nil(t, err)
492 assert.NotNil(t, oltDevice)
493
494 // Disable the oltDevice
495 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
496 assert.Nil(t, err)
497
498 // Wait for the olt device to be disabled
499 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
500 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
501 }
502 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
503 assert.Nil(t, err)
504
505 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400506 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500507 assert.Nil(t, err)
508 for _, onu := range onuDevices.Items {
509 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
510 assert.Nil(t, err)
511 }
512
513 // Delete the oltDevice
514 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
515 assert.Nil(t, err)
516
517 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
518 return devices != nil && len(devices.Items) == 0
519 }
520 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
521 assert.Nil(t, err)
522
523 // Wait for absence of logical device
524 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
525 return lds != nil && len(lds.Items) == 0
526 }
527
528 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
529 assert.Nil(t, err)
530}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400531
532func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400533 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
534 if len(devices.Items) == 0 {
535 // Nothing to do
536 return
537 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400538 //Get an OLT device
539 oltDevice, err := nb.getADevice(true, nbi)
540 assert.Nil(t, err)
541 assert.NotNil(t, oltDevice)
542
543 // Delete the oltDevice
544 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
545 assert.Nil(t, err)
546
547 // Wait for all devices to be deleted
548 vFunction := func(devices *voltha.Devices) bool {
549 return devices != nil && len(devices.Items) == 0
550 }
551 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
552 assert.Nil(t, err)
553
554 // Wait for absence of logical device
555 vlFunction := func(lds *voltha.LogicalDevices) bool {
556 return lds != nil && len(lds.Items) == 0
557 }
558
559 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
560 assert.Nil(t, err)
561}
562
Kent Hagerman2b216042020-04-03 18:28:56 -0400563func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500564 //Create the device with valid data
565 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
566 assert.Nil(t, err)
567 assert.NotNil(t, oltDevice)
568
569 //Get an OLT device
570 oltDevice, err = nb.getADevice(true, nbi)
571 assert.Nil(t, err)
572 assert.NotNil(t, oltDevice)
573
574 // Enable the oltDevice
575 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
576 assert.Nil(t, err)
577
578 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400579 var vldFunction = func(ports []*voltha.LogicalPort) bool {
580 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500581 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400582 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500583 assert.Nil(t, err)
584
585 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400586 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500587 assert.Nil(t, err)
588
589 // Wait for the all onu devices to be enabled
590 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
591 return device.AdminState == voltha.AdminState_ENABLED
592 }
593 for _, onu := range onuDevices.Items {
594 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
595 assert.Nil(t, err)
596 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500597 // Wait for each onu device to get deleted
598 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
599 return device == nil
600 }
601
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500602 // Delete the onuDevice
603 for _, onu := range onuDevices.Items {
604 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
605 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500606 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
607 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500608 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500609
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500610 // Disable the oltDevice
611 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
612 assert.Nil(t, err)
613
614 // Wait for the olt device to be disabled
615 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
616 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
617 }
618 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
619 assert.Nil(t, err)
620
621 // Delete the oltDevice
622 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
623 assert.Nil(t, err)
624
625 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
626 return devices != nil && len(devices.Items) == 0
627 }
628 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
629 assert.Nil(t, err)
630}
Kent Hagerman2b216042020-04-03 18:28:56 -0400631func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500632 //Get an OLT device
633 var cp *voltha.Port
634 oltDevice, err := nb.getADevice(true, nbi)
635 assert.Nil(t, err)
636 assert.NotNil(t, oltDevice)
637
638 for _, cp = range oltDevice.Ports {
639 if cp.Type == voltha.Port_PON_OLT {
640 break
641 }
642
643 }
644 assert.NotNil(t, cp)
645 cp.DeviceId = oltDevice.Id
646
647 // Disable the NW Port of oltDevice
648 _, err = nbi.DisablePort(getContext(), cp)
649 assert.Nil(t, err)
650 // Wait for the olt device Port to be disabled
651 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
652 for _, port := range device.Ports {
653 if port.PortNo == cp.PortNo {
654 return port.AdminState == voltha.AdminState_DISABLED
655 }
656 }
657 return false
658 }
659 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
660 assert.Nil(t, err)
661 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400662 var vlFunction = func(ports []*voltha.LogicalPort) bool {
663 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500664 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
665 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
666 return false
667 }
668 }
669 return true
670 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400671 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500672 assert.Nil(t, err)
673
674 // Enable the NW Port of oltDevice
675 _, err = nbi.EnablePort(getContext(), cp)
676 assert.Nil(t, err)
677
678 // Wait for the olt device Port to be enabled
679 vdFunction = func(device *voltha.Device) bool {
680 for _, port := range device.Ports {
681 if port.PortNo == cp.PortNo {
682 return port.AdminState == voltha.AdminState_ENABLED
683 }
684 }
685 return false
686 }
687 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
688 assert.Nil(t, err)
689 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400690 vlFunction = func(ports []*voltha.LogicalPort) bool {
691 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500692 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
693 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
694 return false
695 }
696 }
697 return true
698 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400699 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500700 assert.Nil(t, err)
701
702 // Disable a non-PON port
703 for _, cp = range oltDevice.Ports {
704 if cp.Type != voltha.Port_PON_OLT {
705 break
706 }
707
708 }
709 assert.NotNil(t, cp)
710 cp.DeviceId = oltDevice.Id
711
712 // Disable the NW Port of oltDevice
713 _, err = nbi.DisablePort(getContext(), cp)
714 assert.NotNil(t, err)
715
716}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500717
Kent Hagerman2b216042020-04-03 18:28:56 -0400718func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700719 //Get an OLT device
720 oltDevice, err := nb.getADevice(true, nbi)
721 assert.Nil(t, err)
722 assert.NotNil(t, oltDevice)
723 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
724 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
725
726 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400727 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700728 assert.Nil(t, err)
729 assert.NotNil(t, onuDevices)
730 assert.Greater(t, len(onuDevices.Items), 0)
731
732 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
733 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
734 assert.Nil(t, err)
735
736 var vlFunction0 = func(d *voltha.Device) bool {
737 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
738 }
739
740 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
741 assert.Nil(t, err)
742
743 // Wait for the logical device to satisfy the expected condition
744 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
745 return ld == nil
746 }
747
748 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
749 assert.Nil(t, err)
750
751 // Wait for the device to satisfy the expected condition (device does not have flows)
752 var vlFunction2 = func(d *voltha.Device) bool {
753 var deviceFlows *ofp.Flows
754 var err error
755 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
756 return false
757 }
758 return len(deviceFlows.Items) == 0
759 }
760
761 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
762 assert.Nil(t, err)
763
764 // Wait for the device to satisfy the expected condition (there are no child devices)
765 var vlFunction3 = func(d *voltha.Device) bool {
766 var devices *voltha.Devices
767 var err error
768 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
769 return false
770 }
771 for _, device := range devices.Items {
772 if device.ParentId == d.Id {
773 // We have a child device still left
774 return false
775 }
776 }
777 return true
778 }
779
780 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
781 assert.Nil(t, err)
782
783 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
784 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400785 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700786 assert.Nil(t, err)
787
788 // Verify the device connection and operation states
789 oltDevice, err = nb.getADevice(true, nbi)
790 assert.Nil(t, err)
791 assert.NotNil(t, oltDevice)
792 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
793 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
794
795 // Wait for the logical device to satisfy the expected condition
796 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
797 return ld != nil
798 }
799 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
800 assert.Nil(t, err)
801
802 // Verify that logical device is created again
803 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
804 assert.Nil(t, err)
805 assert.NotNil(t, logicalDevices)
806 assert.Equal(t, 1, len(logicalDevices.Items))
807
808 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400809 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700810 assert.Nil(t, err)
811 assert.NotNil(t, onuDevices)
812 assert.Equal(t, 0, len(onuDevices.Items))
813}
814
Kent Hagerman2b216042020-04-03 18:28:56 -0400815func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700816 // -----------------------------------------------------------------------
817 // SubTest 1: Omci test action should fail due to nonexistent device id
818
819 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
820 _, err := nbi.StartOmciTestAction(getContext(), request)
821 assert.NotNil(t, err)
822 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
823
824 // -----------------------------------------------------------------------
825 // SubTest 2: Error should be returned for device with no adapter registered
826
827 // Create a device that has no adapter registered
828 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
829 assert.Nil(t, err)
830 assert.NotNil(t, deviceNoAdapter)
831
832 // Omci test action should fail due to nonexistent adapter
833 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
834 _, err = nbi.StartOmciTestAction(getContext(), request)
835 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400836 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700837
838 //Remove the device
839 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
840 assert.Nil(t, err)
841
842 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
843 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
844 return devices != nil && len(devices.Items) == 0
845 }
846 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
847 assert.Nil(t, err)
848
849 // -----------------------------------------------------------------------
850 // SubTest 3: Omci test action should succeed on valid ONU
851
852 // Create the device with valid data
853 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
854 assert.Nil(t, err)
855 assert.NotNil(t, oltDevice)
856
857 // Verify oltDevice exist in the core
858 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
859 assert.Nil(t, err)
860 assert.Equal(t, 1, len(devices.Items))
861 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
862
863 // Enable the oltDevice
864 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
865 assert.Nil(t, err)
866
867 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400868 var vldFunction = func(ports []*voltha.LogicalPort) bool {
869 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -0700870 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400871 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -0700872 assert.Nil(t, err)
873
874 // Wait for the olt device to be enabled
875 vdFunction := func(device *voltha.Device) bool {
876 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
877 }
878 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
879 assert.Nil(t, err)
880
Kent Hagerman2b216042020-04-03 18:28:56 -0400881 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700882 assert.Nil(t, err)
883 assert.Greater(t, len(onuDevices.Items), 0)
884
885 onuDevice := onuDevices.Items[0]
886
887 // Omci test action should succeed
888 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
889 resp, err := nbi.StartOmciTestAction(getContext(), request)
890 assert.Nil(t, err)
891 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
892
893 //Remove the device
894 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
895 assert.Nil(t, err)
896 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
897 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
898 assert.Nil(t, err)
899}
900
khenaidoo67b22152020-03-02 16:01:25 -0500901func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
902 matchFields := make([]*ofp.OfpOxmField, 0)
903 for _, val := range fa.MatchFields {
904 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
905 }
906 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
907}
908
909func createMetadata(cTag int, techProfile int, port int) uint64 {
910 md := 0
911 md = (md | (cTag & 0xFFFF)) << 16
912 md = (md | (techProfile & 0xFFFF)) << 32
913 return uint64(md | (port & 0xFFFFFFFF))
914}
915
khenaidoo8b4abbf2020-04-24 17:04:30 -0400916func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500917 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400918 if flowAddFail {
919 expectedNumFlows = 0
920 }
921 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500922 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700923 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
924 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500925 }
926 // No timeout implies a success
927 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
928 assert.Nil(t, err)
929}
930
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400931func (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 -0500932 // Send flows for the parent device
933 var nniPorts []*voltha.LogicalPort
934 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400935 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -0500936 if p.RootPort {
937 nniPorts = append(nniPorts, p)
938 } else {
939 uniPorts = append(uniPorts, p)
940 }
941 }
942 assert.Equal(t, 1, len(nniPorts))
943 //assert.Greater(t, len(uniPorts), 1 )
944 nniPort := nniPorts[0].OfpPort.PortNo
945 maxInt32 := uint64(0xFFFFFFFF)
946 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
947 var fa *flows.FlowArgs
948 fa = &flows.FlowArgs{
949 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
950 MatchFields: []*ofp.OfpOxmOfbField{
951 flows.InPort(nniPort),
952 flows.EthType(35020),
953 },
954 Actions: []*ofp.OfpAction{
955 flows.Output(controllerPortMask),
956 },
957 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400958 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500959 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
960 assert.Nil(t, err)
961
962 fa = &flows.FlowArgs{
963 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
964 MatchFields: []*ofp.OfpOxmOfbField{
965 flows.InPort(nniPort),
966 flows.EthType(2048),
967 flows.IpProto(17),
968 flows.UdpSrc(67),
969 flows.UdpDst(68),
970 },
971 Actions: []*ofp.OfpAction{
972 flows.Output(controllerPortMask),
973 },
974 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400975 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500976 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
977 assert.Nil(t, err)
978
979 fa = &flows.FlowArgs{
980 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
981 MatchFields: []*ofp.OfpOxmOfbField{
982 flows.InPort(nniPort),
983 flows.EthType(34525),
984 flows.IpProto(17),
985 flows.UdpSrc(546),
986 flows.UdpDst(547),
987 },
988 Actions: []*ofp.OfpAction{
989 flows.Output(controllerPortMask),
990 },
991 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400992 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500993 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
994 assert.Nil(t, err)
995
996 return len(nniPorts), len(uniPorts)
997}
998
Kent Hagerman2b216042020-04-03 18:28:56 -0400999func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001000 maxInt32 := uint64(0xFFFFFFFF)
1001 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1002 fa := &flows.FlowArgs{
1003 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},
1004 MatchFields: []*ofp.OfpOxmOfbField{
1005 flows.InPort(port.PortNo),
1006 flows.EthType(34958),
1007 flows.VlanVid(8187),
1008 },
1009 Actions: []*ofp.OfpAction{
1010 flows.Output(controllerPortMask),
1011 },
1012 }
1013 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001014 maxTries := 3
1015 var err error
1016 for {
1017 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1018 if maxTries < 3 {
1019 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1020 }
1021 break
1022 }
1023 t.Log("Sending EAPOL flows fail:", err)
1024 time.Sleep(50 * time.Millisecond)
1025 maxTries--
1026 if maxTries == 0 {
1027 break
1028 }
1029 }
khenaidoo67b22152020-03-02 16:01:25 -05001030 assert.Nil(t, err)
1031}
1032
khenaidoo0db4c812020-05-27 15:27:30 -04001033func (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 -05001034 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001035
1036 // Clear any existing flows on the adapters
1037 nb.oltAdapter.ClearFlows()
1038 nb.onuAdapter.ClearFlows()
1039
khenaidoo8b4abbf2020-04-24 17:04:30 -04001040 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001041 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1042 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001043
khenaidoo67b22152020-03-02 16:01:25 -05001044 // Wait until a logical device is ready
1045 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1046 if lds == nil || len(lds.Items) != 1 {
1047 return false
1048 }
1049 // Ensure there are both NNI ports and at least one UNI port on the logical device
1050 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001051 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1052 if err != nil {
1053 return false
1054 }
khenaidoo67b22152020-03-02 16:01:25 -05001055 nniPort := false
1056 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001057 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001058 nniPort = nniPort || p.RootPort == true
1059 uniPort = uniPort || p.RootPort == false
1060 if nniPort && uniPort {
1061 return true
1062 }
1063 }
1064 return false
1065 }
1066 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1067 assert.Nil(t, err)
1068
1069 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1070 assert.Nil(t, err)
1071 assert.NotNil(t, logicalDevices)
1072 assert.Equal(t, 1, len(logicalDevices.Items))
1073
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001074 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001075 meterID := rand.Uint32()
1076
1077 // Add a meter to the logical device
1078 meterMod := &ofp.OfpMeterMod{
1079 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1080 Flags: rand.Uint32(),
1081 MeterId: meterID,
1082 Bands: []*ofp.OfpMeterBandHeader{
1083 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1084 Rate: rand.Uint32(),
1085 BurstSize: rand.Uint32(),
1086 Data: nil,
1087 },
1088 },
1089 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001090 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1091 assert.Nil(t, err)
1092
1093 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001094 assert.Nil(t, err)
1095
1096 // Send initial set of Trap flows
1097 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001098 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001099
1100 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001101 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001102 processedNniLogicalPorts := 0
1103 processedUniLogicalPorts := 0
1104
Kent Hagerman45a13e42020-04-13 12:23:50 -04001105 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001106 startingVlan++
1107 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1108 ps := portStatus.PortStatus
1109 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001110 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001111 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001112 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001113 } else {
1114 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001115 }
1116 }
1117 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001118
1119 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001120 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001121 break
1122 }
1123 }
1124 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001125 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001126
khenaidoo8b4abbf2020-04-24 17:04:30 -04001127 // Wait until all flows have been sent to the OLT adapters (or all failed)
1128 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1129 if flowAddFail {
1130 expectedFlowCount = 0
1131 }
khenaidoo67b22152020-03-02 16:01:25 -05001132 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001133 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001134 }
1135 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1136 assert.Nil(t, err)
1137
khenaidoo8b4abbf2020-04-24 17:04:30 -04001138 // Wait until all flows have been sent to the ONU adapters (or all failed)
1139 expectedFlowCount = numUNIPorts
1140 if flowAddFail {
1141 expectedFlowCount = 0
1142 }
khenaidoo67b22152020-03-02 16:01:25 -05001143 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001144 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001145 }
1146 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1147 assert.Nil(t, err)
1148}
1149
khenaidoo8b4abbf2020-04-24 17:04:30 -04001150func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1151
1152 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1153 var wg sync.WaitGroup
1154 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001155 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001156
1157 // Create the device with valid data
1158 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1159 assert.Nil(t, err)
1160 assert.NotNil(t, oltDevice)
1161
1162 // Verify oltDevice exist in the core
1163 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1164 assert.Nil(t, err)
1165 assert.Equal(t, 1, len(devices.Items))
1166 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1167
1168 // Enable the oltDevice
1169 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1170 assert.Nil(t, err)
1171
1172 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001173 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1174 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001175 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001176 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001177 assert.Nil(t, err)
1178
1179 // Verify that the devices have been setup correctly
1180 nb.verifyDevices(t, nbi)
1181
1182 // Get latest oltDevice data
1183 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1184 assert.Nil(t, err)
1185
1186 // Verify that the logical device has been setup correctly
1187 nb.verifyLogicalDevices(t, oltDevice, nbi)
1188
1189 // Wait until all flows has been sent to the devices successfully
1190 wg.Wait()
1191}
1192
Matteo Scandolod525ae32020-04-02 17:27:29 -07001193func TestSuiteNbiApiHandler(t *testing.T) {
Kent Hagerman2b216042020-04-03 18:28:56 -04001194 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001195 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001196 logger.Fatalf("could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001197 }
1198 defer f.Close()
1199 runtime.SetBlockProfileRate(1)
1200 runtime.SetMutexProfileFraction(-1)
1201 if err := pprof.StartCPUProfile(f); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001202 logger.Fatalf("could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001203 }
1204 defer pprof.StopCPUProfile()
1205
khenaidoo442e7c72020-03-10 16:13:48 -04001206 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1207
khenaidoob64fc8a2019-11-27 15:08:19 -05001208 nb := newNBTest()
1209 assert.NotNil(t, nb)
1210
1211 defer nb.stopAll()
1212
1213 // Start the Core
1214 nb.startCore(false)
1215
1216 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001217 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001218
1219 // 1. Basic test with no data in Core
1220 nb.testCoreWithoutData(t, nbi)
1221
1222 // Create/register the adapters
Mahir Gunyel03de0d32020-06-03 01:36:59 -07001223 nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr)
1224 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1225 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001226
1227 // 2. Test adapter registration
1228 nb.testAdapterRegistration(t, nbi)
1229
khenaidoo8b4abbf2020-04-24 17:04:30 -04001230 numberOfTestRuns := 2
1231 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001232 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001233 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001234
khenaidoo93d5a3d2020-01-15 12:37:05 -05001235 // 4. Test Enable a device
1236 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001237
khenaidoo0db4c812020-05-27 15:27:30 -04001238 //// 5. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001239 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001240
kesavandbc2d1622020-01-21 00:42:01 -05001241 // 6. Test disable and Enable pon port of OLT device
1242 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001243
Girish Gowdra408cd962020-03-11 14:31:31 -07001244 // 7.Test Device unreachable when OLT is enabled
1245 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1246
1247 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001248 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001249
Girish Gowdra408cd962020-03-11 14:31:31 -07001250 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001251 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001252
1253 // 10. Test omci test
1254 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001255
khenaidoo0db4c812020-05-27 15:27:30 -04001256 // 11. Remove all devices from tests above
1257 nb.deleteAllDevices(t, nbi)
1258
khenaidoo8b4abbf2020-04-24 17:04:30 -04001259 // 11. Test flow add failure
1260 nb.testFlowAddFailure(t, nbi)
1261
1262 // 12. Clean up
1263 nb.deleteAllDevices(t, nbi)
1264 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001265}