blob: 592ccea5a9c7a926340cade9d05e300c15da0f36 [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050028 "sync"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080029 "testing"
30 "time"
31
khenaidoob64fc8a2019-11-27 15:08:19 -050032 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040033 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050034 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040035 "github.com/opencord/voltha-go/rw_core/core/adapter"
36 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050037 cm "github.com/opencord/voltha-go/rw_core/mocks"
Kent Hagerman2b216042020-04-03 18:28:56 -040038 "github.com/opencord/voltha-lib-go/v3/pkg/db"
39 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080040 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
41 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Matteo Scandolod525ae32020-04-02 17:27:29 -070042 mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
43 mock_kafka "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080044 "github.com/opencord/voltha-lib-go/v3/pkg/version"
45 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
Kent Hagerman2b216042020-04-03 18:28:56 -040053const (
54 coreName = "rw_core"
55)
56
khenaidoob64fc8a2019-11-27 15:08:19 -050057type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070058 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040059 deviceMgr *device.Manager
60 logicalDeviceMgr *device.LogicalManager
61 adapterMgr *adapter.Manager
62 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050063 kClient kafka.Client
64 kvClientPort int
65 numONUPerOLT int
66 startingUNIPortNo int
67 oltAdapter *cm.OLTAdapter
68 onuAdapter *cm.ONUAdapter
69 oltAdapterName string
70 onuAdapterName string
71 coreInstanceID string
72 defaultTimeout time.Duration
73 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050074}
75
76func newNBTest() *NBTest {
77 test := &NBTest{}
78 // Start the embedded etcd server
79 var err error
80 test.etcdServer, test.kvClientPort, err = startEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
81 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000082 logger.Fatal(err)
khenaidoob64fc8a2019-11-27 15:08:19 -050083 }
84 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070085 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050086 test.oltAdapterName = "olt_adapter_mock"
87 test.onuAdapterName = "onu_adapter_mock"
88 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050089 test.defaultTimeout = 10 * time.Second
90 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050091 return test
92}
93
94func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053095 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
96 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050097 cfg := config.NewRWCoreFlags()
98 cfg.CorePairTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040099 cfg.DefaultRequestTimeout = nb.defaultTimeout
100 cfg.DefaultCoreTimeout = nb.defaultTimeout
khenaidoob64fc8a2019-11-27 15:08:19 -0500101 cfg.KVStorePort = nb.kvClientPort
102 cfg.InCompetingMode = inCompeteMode
103 grpcPort, err := freeport.GetFreePort()
104 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000105 logger.Fatal("Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500106 }
107 cfg.GrpcPort = grpcPort
108 cfg.GrpcHost = "127.0.0.1"
109 setCoreCompeteMode(inCompeteMode)
110 client := setupKVClient(cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 backend := &db.Backend{
112 Client: client,
113 StoreType: cfg.KVStoreType,
114 Host: cfg.KVStoreHost,
115 Port: cfg.KVStorePort,
116 Timeout: cfg.KVStoreTimeout,
117 LivenessChannelInterval: cfg.LiveProbeInterval / 2,
118 PathPrefix: cfg.KVStoreDataPrefix}
119 nb.kmp = kafka.NewInterContainerProxy(
120 kafka.InterContainerHost(cfg.KafkaAdapterHost),
121 kafka.InterContainerPort(cfg.KafkaAdapterPort),
122 kafka.MsgClient(nb.kClient),
123 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}),
124 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: cfg.AffinityRouterTopic}))
125
126 endpointMgr := kafka.NewEndpointManager(backend)
127 proxy := model.NewProxy(backend, "/")
128 nb.adapterMgr = adapter.NewAdapterManager(proxy, nb.coreInstanceID, nb.kClient)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400129 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CorePairTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400130 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400131
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400132 if err := nb.kmp.Start(); err != nil {
Kent Hagerman2b216042020-04-03 18:28:56 -0400133 logger.Fatalf("Cannot start InterContainerProxy: %s", err)
134 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400135 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Kent Hagerman2b216042020-04-03 18:28:56 -0400136 if err := nb.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
137 logger.Fatalf("Cannot add request handler: %s", err)
138 }
139 if err := nb.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: cfg.CorePairTopic}, kafka.OffsetNewest); err != nil {
140 logger.Fatalf("Cannot add default request handler: %s", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530141 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500142}
143
Thomas Lee Se5a44012019-11-07 20:32:24 +0530144func (nb *NBTest) createAndregisterAdapters(t *testing.T) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500145 // Setup the mock OLT adapter
146 oltAdapter, err := createMockAdapter(OltAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.oltAdapterName)
147 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000148 logger.Fatalw("setting-mock-olt-adapter-failed", log.Fields{"error": err})
khenaidoob64fc8a2019-11-27 15:08:19 -0500149 }
khenaidoo67b22152020-03-02 16:01:25 -0500150 nb.oltAdapter = (oltAdapter).(*cm.OLTAdapter)
151 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
152 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
153
khenaidoob64fc8a2019-11-27 15:08:19 -0500154 // Register the adapter
155 registrationData := &voltha.Adapter{
Matteo Scandolod525ae32020-04-02 17:27:29 -0700156 Id: nb.oltAdapterName,
157 Vendor: "Voltha-olt",
158 Version: version.VersionInfo.Version,
159 Type: nb.oltAdapterName,
160 CurrentReplica: 1,
161 TotalReplicas: 1,
162 Endpoint: nb.oltAdapterName,
khenaidoob64fc8a2019-11-27 15:08:19 -0500163 }
164 types := []*voltha.DeviceType{{Id: nb.oltAdapterName, Adapter: nb.oltAdapterName, AcceptsAddRemoveFlowUpdates: true}}
165 deviceTypes := &voltha.DeviceTypes{Items: types}
Kent Hagerman2b216042020-04-03 18:28:56 -0400166 if _, err := nb.adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000167 logger.Errorw("failed-to-register-adapter", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530168 assert.NotNil(t, err)
169 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500170
171 // Setup the mock ONU adapter
khenaidoo67b22152020-03-02 16:01:25 -0500172 onuAdapter, err := createMockAdapter(OnuAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.onuAdapterName)
173 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000174 logger.Fatalw("setting-mock-onu-adapter-failed", log.Fields{"error": err})
khenaidoob64fc8a2019-11-27 15:08:19 -0500175 }
khenaidoo67b22152020-03-02 16:01:25 -0500176 nb.onuAdapter = (onuAdapter).(*cm.ONUAdapter)
177
khenaidoob64fc8a2019-11-27 15:08:19 -0500178 // Register the adapter
179 registrationData = &voltha.Adapter{
Matteo Scandolod525ae32020-04-02 17:27:29 -0700180 Id: nb.onuAdapterName,
181 Vendor: "Voltha-onu",
182 Version: version.VersionInfo.Version,
183 Type: nb.onuAdapterName,
184 CurrentReplica: 1,
185 TotalReplicas: 1,
186 Endpoint: nb.onuAdapterName,
khenaidoob64fc8a2019-11-27 15:08:19 -0500187 }
188 types = []*voltha.DeviceType{{Id: nb.onuAdapterName, Adapter: nb.onuAdapterName, AcceptsAddRemoveFlowUpdates: true}}
189 deviceTypes = &voltha.DeviceTypes{Items: types}
Kent Hagerman2b216042020-04-03 18:28:56 -0400190 if _, err := nb.adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000191 logger.Errorw("failed-to-register-adapter", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530192 assert.NotNil(t, err)
193 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500194}
195
196func (nb *NBTest) stopAll() {
197 if nb.kClient != nil {
198 nb.kClient.Stop()
199 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400200 if nb.kmp != nil {
201 nb.kmp.Stop()
khenaidoob64fc8a2019-11-27 15:08:19 -0500202 }
203 if nb.etcdServer != nil {
204 stopEmbeddedEtcdServer(nb.etcdServer)
205 }
206}
207
Kent Hagerman2b216042020-04-03 18:28:56 -0400208func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500209 // Get the latest set of logical devices
210 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
211 assert.Nil(t, err)
212 assert.NotNil(t, logicalDevices)
213 assert.Equal(t, 1, len(logicalDevices.Items))
214
215 ld := logicalDevices.Items[0]
216 assert.NotEqual(t, "", ld.Id)
217 assert.NotEqual(t, uint64(0), ld.DatapathId)
218 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
219 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
220 assert.NotEqual(t, "", ld.RootDeviceId)
221 assert.NotEqual(t, "", ld.Desc.SerialNum)
222 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
223 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
224 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
225 assert.Equal(t, 1+nb.numONUPerOLT, len(ld.Ports))
226 assert.Equal(t, oltDevice.ParentId, ld.Id)
227 //Expected port no
228 expectedPortNo := make(map[uint32]bool)
229 expectedPortNo[uint32(2)] = false
230 for i := 0; i < nb.numONUPerOLT; i++ {
231 expectedPortNo[uint32(i+100)] = false
232 }
233 for _, p := range ld.Ports {
234 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
235 assert.Equal(t, uint32(4), p.OfpPort.State)
236 expectedPortNo[p.OfpPort.PortNo] = true
237 if strings.HasPrefix(p.Id, "nni") {
238 assert.Equal(t, true, p.RootPort)
239 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
240 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
241 } else {
242 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
243 assert.Equal(t, false, p.RootPort)
244 }
245 }
246}
247
Kent Hagerman2b216042020-04-03 18:28:56 -0400248func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500249 // Get the latest set of devices
250 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
251 assert.Nil(t, err)
252 assert.NotNil(t, devices)
253
khenaidoo67b22152020-03-02 16:01:25 -0500254 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500255 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
256 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
257 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500258
khenaidoo67b22152020-03-02 16:01:25 -0500259 var wg sync.WaitGroup
260 for _, device := range devices.Items {
261 wg.Add(1)
262 go func(wg *sync.WaitGroup, device *voltha.Device) {
263 // Wait until the device is in the right state
264 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
265 assert.Nil(t, err)
266
267 // Now, verify the details of the device. First get the latest update
268 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
269 assert.Nil(t, err)
270 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
271 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
272 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
273 assert.Equal(t, d.Type, d.Adapter)
274 assert.NotEqual(t, "", d.MacAddress)
275 assert.NotEqual(t, "", d.SerialNumber)
276
277 if d.Type == "olt_adapter_mock" {
278 assert.Equal(t, true, d.Root)
279 assert.NotEqual(t, "", d.Id)
280 assert.NotEqual(t, "", d.ParentId)
281 assert.Nil(t, d.ProxyAddress)
282 } else if d.Type == "onu_adapter_mock" {
283 assert.Equal(t, false, d.Root)
284 assert.NotEqual(t, uint32(0), d.Vlan)
285 assert.NotEqual(t, "", d.Id)
286 assert.NotEqual(t, "", d.ParentId)
287 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
288 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500289 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500290 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500291 }
khenaidoo67b22152020-03-02 16:01:25 -0500292 assert.Equal(t, 2, len(d.Ports))
293 for _, p := range d.Ports {
294 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
295 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
296 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
297 assert.Equal(t, 0, len(p.Peers))
298 } else if p.Type == voltha.Port_PON_OLT {
299 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
300 assert.Equal(t, uint32(1), p.PortNo)
301 } else if p.Type == voltha.Port_PON_ONU {
302 assert.Equal(t, 1, len(p.Peers))
303 assert.Equal(t, uint32(1), p.PortNo)
304 } else {
305 assert.Error(t, errors.New("invalid-port"))
306 }
307 }
308 wg.Done()
309 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500310 }
khenaidoo67b22152020-03-02 16:01:25 -0500311 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500312}
313
Kent Hagerman2b216042020-04-03 18:28:56 -0400314func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500315 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
316 if err != nil {
317 return nil, err
318 }
319 for _, d := range devices.Items {
320 if d.Root == rootDevice {
321 return d, nil
322 }
323 }
324 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
325}
326
Kent Hagerman2b216042020-04-03 18:28:56 -0400327func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500328 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
329 assert.Nil(t, err)
330 assert.NotNil(t, lds)
331 assert.Equal(t, 0, len(lds.Items))
332 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
333 assert.Nil(t, err)
334 assert.NotNil(t, devices)
335 assert.Equal(t, 0, len(devices.Items))
336 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400337 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500338 assert.Nil(t, err)
339 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500340}
341
Kent Hagerman2b216042020-04-03 18:28:56 -0400342func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500343 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
344 assert.Nil(t, err)
345 assert.NotNil(t, adapters)
346 assert.Equal(t, 2, len(adapters.Items))
347 for _, a := range adapters.Items {
348 switch a.Id {
349 case nb.oltAdapterName:
350 assert.Equal(t, "Voltha-olt", a.Vendor)
351 case nb.onuAdapterName:
352 assert.Equal(t, "Voltha-onu", a.Vendor)
353 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000354 logger.Fatal("unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500355 }
356 }
357 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
358 assert.Nil(t, err)
359 assert.NotNil(t, deviceTypes)
360 assert.Equal(t, 2, len(deviceTypes.Items))
361 for _, dt := range deviceTypes.Items {
362 switch dt.Id {
363 case nb.oltAdapterName:
364 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
365 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
366 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
367 case nb.onuAdapterName:
368 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
369 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
370 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
371 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000372 logger.Fatal("invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500373 }
374 }
375}
376
Kent Hagerman2b216042020-04-03 18:28:56 -0400377func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500378 // Create a valid device
379 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
380 assert.Nil(t, err)
381 assert.NotNil(t, oltDevice)
382 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
383 assert.Nil(t, err)
384 assert.NotNil(t, device)
385 assert.Equal(t, oltDevice.String(), device.String())
386
387 // Try to create the same device
388 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
389 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400390 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500391
392 // Try to create a device with invalid data
393 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
394 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530395 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500396
397 // Ensure we only have 1 device in the Core
398 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
399 assert.Nil(t, err)
400 assert.NotNil(t, devices)
401 assert.Equal(t, 1, len(devices.Items))
402 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
403
404 //Remove the device
405 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
406 assert.Nil(t, err)
407
408 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
409 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
410 return devices != nil && len(devices.Items) == 0
411 }
khenaidoo442e7c72020-03-10 16:13:48 -0400412 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500413 assert.Nil(t, err)
414}
415
Kent Hagerman2b216042020-04-03 18:28:56 -0400416func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500417 // Create a device that has no adapter registered
418 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
419 assert.Nil(t, err)
420 assert.NotNil(t, oltDeviceNoAdapter)
421
422 // Try to enable the oltDevice and check the error message
423 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
424 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400425 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500426
427 //Remove the device
428 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
429 assert.Nil(t, err)
430
431 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
432 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
433 return devices != nil && len(devices.Items) == 0
434 }
khenaidoo442e7c72020-03-10 16:13:48 -0400435 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500436 assert.Nil(t, err)
437
khenaidoo67b22152020-03-02 16:01:25 -0500438 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
439 var wg sync.WaitGroup
440 wg.Add(1)
441 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg)
442
khenaidoob64fc8a2019-11-27 15:08:19 -0500443 // Create the device with valid data
444 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
445 assert.Nil(t, err)
446 assert.NotNil(t, oltDevice)
447
448 // Verify oltDevice exist in the core
449 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
450 assert.Nil(t, err)
451 assert.Equal(t, 1, len(devices.Items))
452 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
453
454 // Enable the oltDevice
455 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
456 assert.Nil(t, err)
457
458 // Wait for the logical device to be in the ready state
459 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
460 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
461 }
462 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
463 assert.Nil(t, err)
464
465 // Verify that the devices have been setup correctly
466 nb.verifyDevices(t, nbi)
467
468 // Get latest oltDevice data
469 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
470 assert.Nil(t, err)
471
472 // Verify that the logical device has been setup correctly
473 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500474
475 // Wait until all flows has been sent to the devices successfully
476 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500477}
478
Kent Hagerman2b216042020-04-03 18:28:56 -0400479func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500480 //Get an OLT device
481 oltDevice, err := nb.getADevice(true, nbi)
482 assert.Nil(t, err)
483 assert.NotNil(t, oltDevice)
484
485 // Disable the oltDevice
486 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
487 assert.Nil(t, err)
488
489 // Wait for the old device to be disabled
490 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
491 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
492 }
493 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
494 assert.Nil(t, err)
495
496 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400497 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500498 assert.Nil(t, err)
499 for _, onu := range onuDevices.Items {
500 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
501 assert.Nil(t, err)
502 }
503
504 // Wait for the logical device to satisfy the expected condition
505 var vlFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500506 if ld == nil {
507 return false
508 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500509 for _, lp := range ld.Ports {
510 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
511 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
512 return false
513 }
514 }
515 return true
516 }
517 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
518 assert.Nil(t, err)
519
520 // Reenable the oltDevice
521 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
522 assert.Nil(t, err)
523
524 // Wait for the old device to be enabled
525 vdFunction = func(device *voltha.Device) bool {
526 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
527 }
528 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
529 assert.Nil(t, err)
530
531 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400532 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500533 assert.Nil(t, err)
534 for _, onu := range onuDevices.Items {
535 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
536 assert.Nil(t, err)
537 }
538
539 // Wait for the logical device to satisfy the expected condition
540 vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500541 if ld == nil {
542 return false
543 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500544 for _, lp := range ld.Ports {
545 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
546 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
547 return false
548 }
549 }
550 return true
551 }
552 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
553 assert.Nil(t, err)
554}
555
Kent Hagerman2b216042020-04-03 18:28:56 -0400556func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500557 //Get an OLT device
558 oltDevice, err := nb.getADevice(true, nbi)
559 assert.Nil(t, err)
560 assert.NotNil(t, oltDevice)
561
562 // Disable the oltDevice
563 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
564 assert.Nil(t, err)
565
566 // Wait for the olt device to be disabled
567 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
568 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
569 }
570 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
571 assert.Nil(t, err)
572
573 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400574 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500575 assert.Nil(t, err)
576 for _, onu := range onuDevices.Items {
577 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
578 assert.Nil(t, err)
579 }
580
581 // Delete the oltDevice
582 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
583 assert.Nil(t, err)
584
585 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
586 return devices != nil && len(devices.Items) == 0
587 }
588 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
589 assert.Nil(t, err)
590
591 // Wait for absence of logical device
592 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
593 return lds != nil && len(lds.Items) == 0
594 }
595
596 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
597 assert.Nil(t, err)
598}
Kent Hagerman2b216042020-04-03 18:28:56 -0400599func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500600 //Create the device with valid data
601 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
602 assert.Nil(t, err)
603 assert.NotNil(t, oltDevice)
604
605 //Get an OLT device
606 oltDevice, err = nb.getADevice(true, nbi)
607 assert.Nil(t, err)
608 assert.NotNil(t, oltDevice)
609
610 // Enable the oltDevice
611 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
612 assert.Nil(t, err)
613
614 // Wait for the logical device to be in the ready state
615 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
616 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
617 }
618 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
619 assert.Nil(t, err)
620
621 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400622 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500623 assert.Nil(t, err)
624
625 // Wait for the all onu devices to be enabled
626 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
627 return device.AdminState == voltha.AdminState_ENABLED
628 }
629 for _, onu := range onuDevices.Items {
630 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
631 assert.Nil(t, err)
632 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500633 // Wait for each onu device to get deleted
634 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
635 return device == nil
636 }
637
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500638 // Delete the onuDevice
639 for _, onu := range onuDevices.Items {
640 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
641 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500642 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
643 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500644 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500645
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500646 // Disable the oltDevice
647 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
648 assert.Nil(t, err)
649
650 // Wait for the olt device to be disabled
651 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
652 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
653 }
654 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
655 assert.Nil(t, err)
656
657 // Delete the oltDevice
658 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
659 assert.Nil(t, err)
660
661 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
662 return devices != nil && len(devices.Items) == 0
663 }
664 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
665 assert.Nil(t, err)
666}
Kent Hagerman2b216042020-04-03 18:28:56 -0400667func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500668 //Get an OLT device
669 var cp *voltha.Port
670 oltDevice, err := nb.getADevice(true, nbi)
671 assert.Nil(t, err)
672 assert.NotNil(t, oltDevice)
673
674 for _, cp = range oltDevice.Ports {
675 if cp.Type == voltha.Port_PON_OLT {
676 break
677 }
678
679 }
680 assert.NotNil(t, cp)
681 cp.DeviceId = oltDevice.Id
682
683 // Disable the NW Port of oltDevice
684 _, err = nbi.DisablePort(getContext(), cp)
685 assert.Nil(t, err)
686 // Wait for the olt device Port to be disabled
687 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
688 for _, port := range device.Ports {
689 if port.PortNo == cp.PortNo {
690 return port.AdminState == voltha.AdminState_DISABLED
691 }
692 }
693 return false
694 }
695 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
696 assert.Nil(t, err)
697 // Wait for the logical device to satisfy the expected condition
698 var vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500699 if ld == nil {
700 return false
701 }
kesavandbc2d1622020-01-21 00:42:01 -0500702 for _, lp := range ld.Ports {
703 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
704 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
705 return false
706 }
707 }
708 return true
709 }
710 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
711 assert.Nil(t, err)
712
713 // Enable the NW Port of oltDevice
714 _, err = nbi.EnablePort(getContext(), cp)
715 assert.Nil(t, err)
716
717 // Wait for the olt device Port to be enabled
718 vdFunction = func(device *voltha.Device) bool {
719 for _, port := range device.Ports {
720 if port.PortNo == cp.PortNo {
721 return port.AdminState == voltha.AdminState_ENABLED
722 }
723 }
724 return false
725 }
726 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
727 assert.Nil(t, err)
728 // Wait for the logical device to satisfy the expected condition
729 vlFunction = func(ld *voltha.LogicalDevice) bool {
khenaidoo67b22152020-03-02 16:01:25 -0500730 if ld == nil {
731 return false
732 }
kesavandbc2d1622020-01-21 00:42:01 -0500733 for _, lp := range ld.Ports {
734 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
735 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
736 return false
737 }
738 }
739 return true
740 }
741 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
742 assert.Nil(t, err)
743
744 // Disable a non-PON port
745 for _, cp = range oltDevice.Ports {
746 if cp.Type != voltha.Port_PON_OLT {
747 break
748 }
749
750 }
751 assert.NotNil(t, cp)
752 cp.DeviceId = oltDevice.Id
753
754 // Disable the NW Port of oltDevice
755 _, err = nbi.DisablePort(getContext(), cp)
756 assert.NotNil(t, err)
757
758}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500759
Kent Hagerman2b216042020-04-03 18:28:56 -0400760func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700761 //Get an OLT device
762 oltDevice, err := nb.getADevice(true, nbi)
763 assert.Nil(t, err)
764 assert.NotNil(t, oltDevice)
765 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
766 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
767
768 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400769 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700770 assert.Nil(t, err)
771 assert.NotNil(t, onuDevices)
772 assert.Greater(t, len(onuDevices.Items), 0)
773
774 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
775 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
776 assert.Nil(t, err)
777
778 var vlFunction0 = func(d *voltha.Device) bool {
779 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
780 }
781
782 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
783 assert.Nil(t, err)
784
785 // Wait for the logical device to satisfy the expected condition
786 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
787 return ld == nil
788 }
789
790 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
791 assert.Nil(t, err)
792
793 // Wait for the device to satisfy the expected condition (device does not have flows)
794 var vlFunction2 = func(d *voltha.Device) bool {
795 var deviceFlows *ofp.Flows
796 var err error
797 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
798 return false
799 }
800 return len(deviceFlows.Items) == 0
801 }
802
803 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
804 assert.Nil(t, err)
805
806 // Wait for the device to satisfy the expected condition (there are no child devices)
807 var vlFunction3 = func(d *voltha.Device) bool {
808 var devices *voltha.Devices
809 var err error
810 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
811 return false
812 }
813 for _, device := range devices.Items {
814 if device.ParentId == d.Id {
815 // We have a child device still left
816 return false
817 }
818 }
819 return true
820 }
821
822 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
823 assert.Nil(t, err)
824
825 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
826 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400827 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700828 assert.Nil(t, err)
829
830 // Verify the device connection and operation states
831 oltDevice, err = nb.getADevice(true, nbi)
832 assert.Nil(t, err)
833 assert.NotNil(t, oltDevice)
834 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
835 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
836
837 // Wait for the logical device to satisfy the expected condition
838 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
839 return ld != nil
840 }
841 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
842 assert.Nil(t, err)
843
844 // Verify that logical device is created again
845 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
846 assert.Nil(t, err)
847 assert.NotNil(t, logicalDevices)
848 assert.Equal(t, 1, len(logicalDevices.Items))
849
850 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400851 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700852 assert.Nil(t, err)
853 assert.NotNil(t, onuDevices)
854 assert.Equal(t, 0, len(onuDevices.Items))
855}
856
Kent Hagerman2b216042020-04-03 18:28:56 -0400857func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700858 // -----------------------------------------------------------------------
859 // SubTest 1: Omci test action should fail due to nonexistent device id
860
861 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
862 _, err := nbi.StartOmciTestAction(getContext(), request)
863 assert.NotNil(t, err)
864 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
865
866 // -----------------------------------------------------------------------
867 // SubTest 2: Error should be returned for device with no adapter registered
868
869 // Create a device that has no adapter registered
870 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
871 assert.Nil(t, err)
872 assert.NotNil(t, deviceNoAdapter)
873
874 // Omci test action should fail due to nonexistent adapter
875 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
876 _, err = nbi.StartOmciTestAction(getContext(), request)
877 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400878 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700879
880 //Remove the device
881 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
882 assert.Nil(t, err)
883
884 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
885 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
886 return devices != nil && len(devices.Items) == 0
887 }
888 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
889 assert.Nil(t, err)
890
891 // -----------------------------------------------------------------------
892 // SubTest 3: Omci test action should succeed on valid ONU
893
894 // Create the device with valid data
895 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
896 assert.Nil(t, err)
897 assert.NotNil(t, oltDevice)
898
899 // Verify oltDevice exist in the core
900 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
901 assert.Nil(t, err)
902 assert.Equal(t, 1, len(devices.Items))
903 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
904
905 // Enable the oltDevice
906 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
907 assert.Nil(t, err)
908
909 // Wait for the logical device to be in the ready state
910 var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool {
911 return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1
912 }
913 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
914 assert.Nil(t, err)
915
916 // Wait for the olt device to be enabled
917 vdFunction := func(device *voltha.Device) bool {
918 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
919 }
920 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
921 assert.Nil(t, err)
922
Kent Hagerman2b216042020-04-03 18:28:56 -0400923 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700924 assert.Nil(t, err)
925 assert.Greater(t, len(onuDevices.Items), 0)
926
927 onuDevice := onuDevices.Items[0]
928
929 // Omci test action should succeed
930 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
931 resp, err := nbi.StartOmciTestAction(getContext(), request)
932 assert.Nil(t, err)
933 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
934
935 //Remove the device
936 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
937 assert.Nil(t, err)
938 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
939 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
940 assert.Nil(t, err)
941}
942
khenaidoo67b22152020-03-02 16:01:25 -0500943func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
944 matchFields := make([]*ofp.OfpOxmField, 0)
945 for _, val := range fa.MatchFields {
946 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
947 }
948 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
949}
950
951func createMetadata(cTag int, techProfile int, port int) uint64 {
952 md := 0
953 md = (md | (cTag & 0xFFFF)) << 16
954 md = (md | (techProfile & 0xFFFF)) << 32
955 return uint64(md | (port & 0xFFFFFFFF))
956}
957
Kent Hagerman2b216042020-04-03 18:28:56 -0400958func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -0500959 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
960 // Wait for logical device to have all the flows
961 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
962 return lds != nil && len(lds.Items) == 1 && len(lds.Items[0].Flows.Items) == expectedNumFlows
963 }
964 // No timeout implies a success
965 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
966 assert.Nil(t, err)
967}
968
Kent Hagerman2b216042020-04-03 18:28:56 -0400969func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDevice *voltha.LogicalDevice, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -0500970 // Send flows for the parent device
971 var nniPorts []*voltha.LogicalPort
972 var uniPorts []*voltha.LogicalPort
973 for _, p := range logicalDevice.Ports {
974 if p.RootPort {
975 nniPorts = append(nniPorts, p)
976 } else {
977 uniPorts = append(uniPorts, p)
978 }
979 }
980 assert.Equal(t, 1, len(nniPorts))
981 //assert.Greater(t, len(uniPorts), 1 )
982 nniPort := nniPorts[0].OfpPort.PortNo
983 maxInt32 := uint64(0xFFFFFFFF)
984 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
985 var fa *flows.FlowArgs
986 fa = &flows.FlowArgs{
987 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
988 MatchFields: []*ofp.OfpOxmOfbField{
989 flows.InPort(nniPort),
990 flows.EthType(35020),
991 },
992 Actions: []*ofp.OfpAction{
993 flows.Output(controllerPortMask),
994 },
995 }
996 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
997 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
998 assert.Nil(t, err)
999
1000 fa = &flows.FlowArgs{
1001 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1002 MatchFields: []*ofp.OfpOxmOfbField{
1003 flows.InPort(nniPort),
1004 flows.EthType(2048),
1005 flows.IpProto(17),
1006 flows.UdpSrc(67),
1007 flows.UdpDst(68),
1008 },
1009 Actions: []*ofp.OfpAction{
1010 flows.Output(controllerPortMask),
1011 },
1012 }
1013 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1014 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1015 assert.Nil(t, err)
1016
1017 fa = &flows.FlowArgs{
1018 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1019 MatchFields: []*ofp.OfpOxmOfbField{
1020 flows.InPort(nniPort),
1021 flows.EthType(34525),
1022 flows.IpProto(17),
1023 flows.UdpSrc(546),
1024 flows.UdpDst(547),
1025 },
1026 Actions: []*ofp.OfpAction{
1027 flows.Output(controllerPortMask),
1028 },
1029 }
1030 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id}
1031 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1032 assert.Nil(t, err)
1033
1034 return len(nniPorts), len(uniPorts)
1035}
1036
Kent Hagerman2b216042020-04-03 18:28:56 -04001037func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001038 maxInt32 := uint64(0xFFFFFFFF)
1039 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1040 fa := &flows.FlowArgs{
1041 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},
1042 MatchFields: []*ofp.OfpOxmOfbField{
1043 flows.InPort(port.PortNo),
1044 flows.EthType(34958),
1045 flows.VlanVid(8187),
1046 },
1047 Actions: []*ofp.OfpAction{
1048 flows.Output(controllerPortMask),
1049 },
1050 }
1051 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1052 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP)
1053 assert.Nil(t, err)
1054}
1055
Kent Hagerman2b216042020-04-03 18:28:56 -04001056func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup) {
khenaidoo67b22152020-03-02 16:01:25 -05001057 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001058
1059 // Clear any existing flows on the adapters
1060 nb.oltAdapter.ClearFlows()
1061 nb.onuAdapter.ClearFlows()
1062
1063 // Wait until a logical device is ready
1064 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1065 if lds == nil || len(lds.Items) != 1 {
1066 return false
1067 }
1068 // Ensure there are both NNI ports and at least one UNI port on the logical device
1069 ld := lds.Items[0]
1070 nniPort := false
1071 uniPort := false
1072 for _, p := range ld.Ports {
1073 nniPort = nniPort || p.RootPort == true
1074 uniPort = uniPort || p.RootPort == false
1075 if nniPort && uniPort {
1076 return true
1077 }
1078 }
1079 return false
1080 }
1081 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1082 assert.Nil(t, err)
1083
1084 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1085 assert.Nil(t, err)
1086 assert.NotNil(t, logicalDevices)
1087 assert.Equal(t, 1, len(logicalDevices.Items))
1088
1089 logicalDevice := logicalDevices.Items[0]
1090 meterID := rand.Uint32()
1091
1092 // Add a meter to the logical device
1093 meterMod := &ofp.OfpMeterMod{
1094 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1095 Flags: rand.Uint32(),
1096 MeterId: meterID,
1097 Bands: []*ofp.OfpMeterBandHeader{
1098 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1099 Rate: rand.Uint32(),
1100 BurstSize: rand.Uint32(),
1101 Data: nil,
1102 },
1103 },
1104 }
1105 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDevice.Id, MeterMod: meterMod})
1106 assert.Nil(t, err)
1107
1108 // Send initial set of Trap flows
1109 startingVlan := 4091
1110 nb.sendTrapFlows(t, nbi, logicalDevice, uint64(meterID), startingVlan)
1111
1112 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001113 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001114 processedNniLogicalPorts := 0
1115 processedUniLogicalPorts := 0
1116
Kent Hagerman45a13e42020-04-13 12:23:50 -04001117 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001118 startingVlan++
1119 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1120 ps := portStatus.PortStatus
1121 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001122 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001123 processedUniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001124 nb.sendEAPFlows(t, nbi, logicalDevice.Id, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001125 } else {
1126 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001127 }
1128 }
1129 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001130
1131 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001132 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001133 break
1134 }
1135 }
1136 //Verify the flow count on the logical device
1137 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts)
1138
1139 // Wait until all flows have been sent to the OLT adapters
1140 var oltVFunc isConditionSatisfied = func() bool {
1141 return nb.oltAdapter.GetFlowCount() >= (numNNIPorts*3)+numNNIPorts*numUNIPorts
1142 }
1143 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1144 assert.Nil(t, err)
1145
1146 // Wait until all flows have been sent to the ONU adapters
1147 var onuVFunc isConditionSatisfied = func() bool {
1148 return nb.onuAdapter.GetFlowCount() == numUNIPorts
1149 }
1150 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1151 assert.Nil(t, err)
1152}
1153
Matteo Scandolod525ae32020-04-02 17:27:29 -07001154func TestSuiteNbiApiHandler(t *testing.T) {
Kent Hagerman2b216042020-04-03 18:28:56 -04001155 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001156 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001157 logger.Fatalf("could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001158 }
1159 defer f.Close()
1160 runtime.SetBlockProfileRate(1)
1161 runtime.SetMutexProfileFraction(-1)
1162 if err := pprof.StartCPUProfile(f); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001163 logger.Fatalf("could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001164 }
1165 defer pprof.StopCPUProfile()
1166
khenaidoo442e7c72020-03-10 16:13:48 -04001167 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1168
khenaidoob64fc8a2019-11-27 15:08:19 -05001169 nb := newNBTest()
1170 assert.NotNil(t, nb)
1171
1172 defer nb.stopAll()
1173
1174 // Start the Core
1175 nb.startCore(false)
1176
1177 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001178 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001179
1180 // 1. Basic test with no data in Core
1181 nb.testCoreWithoutData(t, nbi)
1182
1183 // Create/register the adapters
Thomas Lee Se5a44012019-11-07 20:32:24 +05301184 nb.createAndregisterAdapters(t)
khenaidoob64fc8a2019-11-27 15:08:19 -05001185
1186 // 2. Test adapter registration
1187 nb.testAdapterRegistration(t, nbi)
1188
Scott Baker8cb47bb2020-04-01 09:57:20 -07001189 numberOfDeviceTestRuns := 2
khenaidoo93d5a3d2020-01-15 12:37:05 -05001190 for i := 1; i <= numberOfDeviceTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001191 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001192 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001193
khenaidoo93d5a3d2020-01-15 12:37:05 -05001194 // 4. Test Enable a device
1195 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001196
khenaidoo93d5a3d2020-01-15 12:37:05 -05001197 // 5. Test disable and ReEnable a root device
1198 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001199
kesavandbc2d1622020-01-21 00:42:01 -05001200 // 6. Test disable and Enable pon port of OLT device
1201 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001202
Girish Gowdra408cd962020-03-11 14:31:31 -07001203 // 7.Test Device unreachable when OLT is enabled
1204 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1205
1206 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001207 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001208
Girish Gowdra408cd962020-03-11 14:31:31 -07001209 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001210 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001211
1212 // 10. Test omci test
1213 nb.testStartOmciTestAction(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001214 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001215
1216 //x. TODO - More tests to come
1217}