blob: dcb1f1706162eb3c2f20cc69ae786432773d9974 [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-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.
15 */
16
17//Package adaptercore provides the utility for olt devices, flows and statistics
18package adaptercore
19
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
kdarapu381c6902019-07-31 18:23:16 +053022 "net"
kdarapu891693b2019-09-16 12:33:49 +053023 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053024 "sync"
kdarapu381c6902019-07-31 18:23:16 +053025 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053026 "time"
27
Esin Karamanccb714b2019-11-29 15:02:06 +000028 "github.com/opencord/voltha-lib-go/v3/pkg/pmmetrics"
kdarapu381c6902019-07-31 18:23:16 +053029
kdarapu891693b2019-09-16 12:33:49 +053030 "github.com/golang/protobuf/ptypes"
31 "github.com/golang/protobuf/ptypes/any"
Esin Karamanccb714b2019-11-29 15:02:06 +000032 "github.com/opencord/voltha-lib-go/v3/pkg/db"
33 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
34 "github.com/opencord/voltha-lib-go/v3/pkg/log"
35 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
kdarapu891693b2019-09-16 12:33:49 +053036 "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
kdarapu381c6902019-07-31 18:23:16 +053037 "github.com/opencord/voltha-openolt-adapter/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000038 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
39 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
40 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
41 oop "github.com/opencord/voltha-protos/v3/go/openolt"
42 "github.com/opencord/voltha-protos/v3/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053043)
44
kdarapu891693b2019-09-16 12:33:49 +053045func init() {
46 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
47}
48
49func newMockCoreProxy() *mocks.MockCoreProxy {
50 mcp := mocks.MockCoreProxy{}
51 mcp.Devices = make(map[string]*voltha.Device)
Naga Manjunath7615e552019-10-11 22:35:47 +053052 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053053 mcp.Devices["olt"] = &voltha.Device{
54
55 Id: "olt",
56 Root: true,
57 ParentId: "logical_device",
58 ParentPortNo: 1,
59
60 Ports: []*voltha.Port{
61 {PortNo: 1, Label: "pon"},
62 {PortNo: 2, Label: "nni"},
63 },
64 ProxyAddress: &voltha.Device_ProxyAddress{
65 DeviceId: "olt",
66 DeviceType: "onu",
67 ChannelId: 1,
68 ChannelGroupId: 1,
69 },
70 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053071 PmConfigs: &voltha.PmConfigs{
72 DefaultFreq: 10,
73 Id: "olt",
74 FreqOverride: false,
75 Grouped: false,
76 Metrics: pm,
77 },
kdarapu891693b2019-09-16 12:33:49 +053078 }
79 mcp.Devices["onu1"] = &voltha.Device{
80
81 Id: "1",
82 Root: false,
83 ParentId: "olt",
84 ParentPortNo: 1,
85
86 Ports: []*voltha.Port{
87 {PortNo: 1, Label: "pon"},
88 {PortNo: 2, Label: "uni"},
89 },
90 OperStatus: 4,
91 ProxyAddress: &voltha.Device_ProxyAddress{
92 OnuId: 1,
93 ChannelId: 1,
94 ChannelGroupId: 1,
95 },
96 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053097 PmConfigs: &voltha.PmConfigs{
98 DefaultFreq: 10,
99 Id: "olt",
100 FreqOverride: false,
101 Grouped: false,
102 Metrics: pm,
103 },
kdarapu891693b2019-09-16 12:33:49 +0530104 }
105 mcp.Devices["onu2"] = &voltha.Device{
106 Id: "2",
107 Root: false,
108 ParentId: "olt",
109 OperStatus: 2,
110 Ports: []*voltha.Port{
111 {PortNo: 1, Label: "pon"},
112 {PortNo: 2, Label: "uni"},
113 },
114
115 ParentPortNo: 1,
116
117 ProxyAddress: &voltha.Device_ProxyAddress{
118 OnuId: 2,
119 ChannelId: 1,
120 ChannelGroupId: 1,
121 },
122 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530123 PmConfigs: &voltha.PmConfigs{
124 DefaultFreq: 10,
125 Id: "olt",
126 FreqOverride: false,
127 Grouped: false,
128 Metrics: pm,
129 },
kdarapu891693b2019-09-16 12:33:49 +0530130 }
131 return &mcp
132}
133func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530134 device := &voltha.Device{
135 Id: "olt",
136 Root: true,
137 ParentId: "logical_device",
138 Ports: []*voltha.Port{
139 {PortNo: 1, Label: "pon"},
140 {PortNo: 2, Label: "nni"},
141 },
kdarapu891693b2019-09-16 12:33:49 +0530142 ProxyAddress: &voltha.Device_ProxyAddress{
143 DeviceId: "olt",
144 DeviceType: "onu",
145 ChannelId: 1,
146 ChannelGroupId: 1,
147 },
148 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530149 }
kdarapu891693b2019-09-16 12:33:49 +0530150 cp := newMockCoreProxy()
151 ap := &mocks.MockAdapterProxy{}
152 ep := &mocks.MockEventProxy{}
153 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep}
154 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
155 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: nil, Model: "openolt", DeviceId: dh.deviceID}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530156 dh.resourceMgr = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.deviceID, DeviceType: dh.deviceType, DevInfo: deviceInf,
157 KVStore: &db.Backend{
158 Client: &mocks.MockKVClient{},
159 }}
160 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
161 ranges := make(map[string]interface{})
162 sharedIdxByType := make(map[string]string)
163 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
164 sharedIdxByType["ONU_ID"] = "ONU_ID"
165 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
166 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
167 ranges["ONU_ID"] = uint32(0)
168 ranges["GEMPORT_ID"] = uint32(0)
169 ranges["ALLOC_ID"] = uint32(0)
170 ranges["FLOW_ID"] = uint32(0)
171 ranges["onu_id_shared"] = uint32(0)
172 ranges["alloc_id_shared"] = uint32(0)
173 ranges["gemport_id_shared"] = uint32(0)
174 ranges["flow_id_shared"] = uint32(0)
175
176 ponmgr := &ponrmgr.PONResourceManager{
177 DeviceID: "onu-1",
178 IntfIDs: []uint32{1, 2},
179 KVStore: &db.Backend{
180 Client: &mocks.MockKVClient{},
181 },
182 PonResourceRanges: ranges,
183 SharedIdxByType: sharedIdxByType,
184 }
185 dh.resourceMgr.ResourceMgrs[1] = ponmgr
186 dh.resourceMgr.ResourceMgrs[2] = ponmgr
npujarec5762e2020-01-01 14:08:48 +0530187 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
188 defer cancel()
189 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
kdarapu891693b2019-09-16 12:33:49 +0530190 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500191 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530192 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530193 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000194
195 var pmNames = []string{
196 "rx_bytes",
197 "rx_packets",
198 "rx_mcast_packets",
199 "rx_bcast_packets",
200 "tx_bytes",
201 "tx_packets",
202 "tx_mcast_packets",
203 "tx_bcast_packets",
204 }
205
206 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530207 return dh
kdarapu381c6902019-07-31 18:23:16 +0530208}
209
kdarapu891693b2019-09-16 12:33:49 +0530210func negativeDeviceHandler() *DeviceHandler {
211 dh := newMockDeviceHandler()
212 device := dh.device
213 device.Id = ""
214 dh.adminState = "down"
215 return dh
216}
kdarapu381c6902019-07-31 18:23:16 +0530217func Test_generateMacFromHost(t *testing.T) {
218 type args struct {
219 host string
220 }
221 tests := []struct {
222 name string
223 args args
224 want string
225 wantErr bool
226 }{
kdarapu891693b2019-09-16 12:33:49 +0530227 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
228 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
229 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
230 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530231 }
232 for _, tt := range tests {
233 t.Run(tt.name, func(t *testing.T) {
234 got, err := generateMacFromHost(tt.args.host)
235 if (err != nil) != tt.wantErr {
236 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
237 return
238 }
239 if got != tt.want {
240 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
241 }
242 })
243 }
244}
245func Test_macifyIP(t *testing.T) {
246 type args struct {
247 ip net.IP
248 }
249 tests := []struct {
250 name string
251 args args
252 want string
253 }{{
kdarapu891693b2019-09-16 12:33:49 +0530254 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530255 args{ip: net.ParseIP("10.10.10.10")},
256 "00:00:0a:0a:0a:0a",
257 },
258 {
kdarapu891693b2019-09-16 12:33:49 +0530259 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530260 args{ip: net.ParseIP("127.0.0.1")},
261 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530262 },
263 {
264 "macifyIP-3",
265 args{ip: net.ParseIP("127.0.0.1/24")},
266 "",
267 },
268 }
kdarapu381c6902019-07-31 18:23:16 +0530269 for _, tt := range tests {
270 t.Run(tt.name, func(t *testing.T) {
271 if got := macifyIP(tt.args.ip); got != tt.want {
272 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
273 }
274 })
275 }
276}
277
kdarapu381c6902019-07-31 18:23:16 +0530278func TestDeviceHandler_GetChildDevice(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530279 dh1 := newMockDeviceHandler()
280 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530281 type args struct {
282 parentPort uint32
283 onuID uint32
284 }
285 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530286 name string
287 devicehandler *DeviceHandler
288 args args
289 want *voltha.Device
kdarapu381c6902019-07-31 18:23:16 +0530290 }{
kdarapu891693b2019-09-16 12:33:49 +0530291 {"GetChildDevice-1", dh1,
292 args{parentPort: 1,
293 onuID: 1},
294 &voltha.Device{},
295 },
296 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530297 args{parentPort: 1,
298 onuID: 1},
299 &voltha.Device{},
300 },
301 }
302 for _, tt := range tests {
303 t.Run(tt.name, func(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530304 got := tt.devicehandler.GetChildDevice(tt.args.parentPort, tt.args.onuID)
kdarapu381c6902019-07-31 18:23:16 +0530305 t.Log("onu device id", got)
306 })
307 }
308}
kdarapu891693b2019-09-16 12:33:49 +0530309
310func TestGetportLabel(t *testing.T) {
311 type args struct {
312 portNum uint32
313 portType voltha.Port_PortType
314 }
315 tests := []struct {
316 name string
317 args args
318 want string
319 }{
320 {"GetportLabel-1", args{portNum: 0, portType: 0}, ""},
321 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1"},
322 {"GetportLabel-3", args{portNum: 2, portType: 2}, ""},
323 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3"},
324 {"GetportLabel-5", args{portNum: 4, portType: 4}, ""},
325 {"GetportLabel-6", args{portNum: 5, portType: 5}, ""},
326 {"GetportLabel-7", args{portNum: 6, portType: 6}, ""},
327 }
328 for _, tt := range tests {
329 t.Run(tt.name, func(t *testing.T) {
330 if got := GetportLabel(tt.args.portNum, tt.args.portType); got != tt.want {
331 t.Errorf("GetportLabel() = %v, want %v", got, tt.want)
332 }
333 })
334 }
335}
336
337func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
338 dh := newMockDeviceHandler()
339 proxyAddr := dh.device.ProxyAddress
340 body := &ic.InterAdapterOmciMessage{
341 Message: []byte("asdfasdfasdfasdfas"),
342 ProxyAddress: proxyAddr,
343 }
344 body2 := &ic.InterAdapterOmciMessage{
345 Message: []byte("asdfasdfasdfasdfas"),
346 //ProxyAddress: &voltha.Device_ProxyAddress{},
347 }
348 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
349 var marshalledData *any.Any
350 var err error
351
352 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
353 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
354 }
355
356 var marshalledData1 *any.Any
357
358 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
359 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
360 }
361 var marshalledData2 *any.Any
362
363 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
364 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
365 }
366 type args struct {
367 msg *ic.InterAdapterMessage
368 }
369 tests := []struct {
370 name string
371 args args
372 wantErr bool
373 }{
374 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
375 Header: &ic.InterAdapterHeader{
376 Id: "012345",
377 Type: 0,
378 },
379 Body: marshalledData,
380 }}, false},
381 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
382 Header: &ic.InterAdapterHeader{
383 Id: "012345",
384 Type: 1,
385 },
386 Body: marshalledData1,
387 }}, false},
388 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
389 Header: &ic.InterAdapterHeader{
390 Id: "012345",
391 Type: 2,
392 },
393 Body: marshalledData,
394 }}, false},
395 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
396 Header: &ic.InterAdapterHeader{
397 Id: "012345",
398 Type: 3,
399 }, Body: marshalledData,
400 }}, false},
401 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
402 Header: &ic.InterAdapterHeader{
403 Id: "012345",
404 Type: 4,
405 }, Body: marshalledData1,
406 }}, false},
407 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
408 Header: &ic.InterAdapterHeader{
409 Id: "012345",
410 Type: 4,
411 }, Body: marshalledData,
412 }}, false},
413 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
414 Header: &ic.InterAdapterHeader{
415 Id: "012345",
416 Type: 5,
417 }, Body: marshalledData,
418 }}, false},
419 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
420 Header: &ic.InterAdapterHeader{
421 Id: "012345",
422 Type: 6,
423 }, Body: marshalledData,
424 }}, false},
425 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
426 Header: &ic.InterAdapterHeader{
427 Id: "012345",
428 Type: 7,
429 }, Body: marshalledData,
430 }}, false},
431 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
432 Header: &ic.InterAdapterHeader{
433 Id: "012345",
434 Type: 7,
435 }, Body: marshalledData2,
436 }}, false},
437 //marshalledData2
438 }
439 for _, tt := range tests {
440 t.Run(tt.name, func(t *testing.T) {
441
442 if err := dh.ProcessInterAdapterMessage(tt.args.msg); (err != nil) != tt.wantErr {
443 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
444 }
445 })
446 }
447}
448
449func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
450 dh1 := newMockDeviceHandler()
451 dh2 := negativeDeviceHandler()
452 device1 := &voltha.Device{
453 Id: "onu1",
454 Root: false,
455 ParentId: "logical_device",
456 ProxyAddress: &voltha.Device_ProxyAddress{
457 DeviceId: "onu1",
458 DeviceType: "onu",
459 ChannelId: 1,
460 ChannelGroupId: 1,
461 },
462 ConnectStatus: 1,
463 }
464 device2 := device1
465 device2.ConnectStatus = 2
466 iaomciMsg1 := &ic.InterAdapterOmciMessage{
467 ProxyAddress: &voltha.Device_ProxyAddress{
468 DeviceId: "onu2",
469 DeviceType: "onu",
470 ChannelId: 1,
471 ChannelGroupId: 1,
472 //OnuId: 2,
473 },
474 ConnectStatus: 1,
475 }
476 iaomciMsg2 := &ic.InterAdapterOmciMessage{
477 ProxyAddress: &voltha.Device_ProxyAddress{
478 DeviceId: "onu3",
479 DeviceType: "onu",
480 ChannelId: 1,
481 ChannelGroupId: 1,
482 },
483 ConnectStatus: 1,
484 }
485 type args struct {
486 onuDevice *voltha.Device
487 omciMsg *ic.InterAdapterOmciMessage
488 }
489 tests := []struct {
490 name string
491 devicehandler *DeviceHandler
492 args args
493 }{
494 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
495 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
496 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
497 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
498 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
499 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
500 }
501 for _, tt := range tests {
502 t.Run(tt.name, func(t *testing.T) {
503 tt.devicehandler.sendProxiedMessage(tt.args.onuDevice, tt.args.omciMsg)
504 })
505 }
506}
507
508func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
509 dh1 := newMockDeviceHandler()
510 dh2 := negativeDeviceHandler()
511
512 type args struct {
513 logicalPort uint32
514 packetPayload []byte
515 }
516 tests := []struct {
517 name string
518 devicehandler *DeviceHandler
519 args args
520 }{
521 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
522 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
523 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
524 }
525 for _, tt := range tests {
526 t.Run(tt.name, func(t *testing.T) {
527 tt.devicehandler.SendPacketInToCore(tt.args.logicalPort, tt.args.packetPayload)
528 })
529 }
530}
531
532func TestDeviceHandler_DisableDevice(t *testing.T) {
533 dh1 := newMockDeviceHandler()
534 dh2 := negativeDeviceHandler()
535 type args struct {
536 device *voltha.Device
537 }
538 tests := []struct {
539 name string
540 devicehandler *DeviceHandler
541 args args
542 wantErr bool
543 }{
544 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400545 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530546 }
547 for _, tt := range tests {
548 t.Run(tt.name, func(t *testing.T) {
549
550 if err := tt.devicehandler.DisableDevice(tt.args.device); (err != nil) != tt.wantErr {
551 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
552 }
553 })
554 }
555}
556
557func TestDeviceHandler_ReenableDevice(t *testing.T) {
558 dh1 := newMockDeviceHandler()
559 dh2 := negativeDeviceHandler()
560 type args struct {
561 device *voltha.Device
562 }
563 tests := []struct {
564 name string
565 devicehandler *DeviceHandler
566 args args
567 wantErr bool
568 }{
569 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
570 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
571 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
572 }
573 for _, tt := range tests {
574 t.Run(tt.name, func(t *testing.T) {
575 dh := tt.devicehandler
576 if err := dh.ReenableDevice(tt.args.device); (err != nil) != tt.wantErr {
577 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
578 }
579 })
580 }
581}
582
583func TestDeviceHandler_RebootDevice(t *testing.T) {
584 dh1 := newMockDeviceHandler()
585 dh2 := newMockDeviceHandler()
586 type args struct {
587 device *voltha.Device
588 }
589 tests := []struct {
590 name string
591 devicehandler *DeviceHandler
592 args args
593 wantErr bool
594 }{
595 // TODO: Add test cases.
596 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
597 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
598 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
599 }
600 for _, tt := range tests {
601 t.Run(tt.name, func(t *testing.T) {
602
603 if err := tt.devicehandler.RebootDevice(tt.args.device); (err != nil) != tt.wantErr {
604 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
605 }
606 })
607 }
608}
609
610func TestDeviceHandler_handleIndication(t *testing.T) {
611 dh1 := newMockDeviceHandler()
612 dh2 := negativeDeviceHandler()
613 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530614 dh3.onus = sync.Map{}
615 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1"))
616 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2"))
617
kdarapu891693b2019-09-16 12:33:49 +0530618 type args struct {
619 indication *oop.Indication
620 }
621 tests := []struct {
622 name string
623 deviceHandler *DeviceHandler
624 args args
625 }{
626 // TODO: Add test cases.
627 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
628 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
629 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
630 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
631 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
632 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
633 {"handleIndication-7", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}},
634 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
635 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
636 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
637 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
638 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
639 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
640 {"handleIndication-14", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}},
641 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
642 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
643 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
644 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
645
646 // Negative testcases
647 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
648 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
649 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
650 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
651 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
652 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
653 {"handleIndication-25", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}},
654 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
655 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
656 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
657 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
658 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
659 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
660 {"handleIndication-32", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}},
661 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
662 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
663 //
664 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
665 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
666 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
667 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
668 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
669 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
670 }
671 for _, tt := range tests {
672 t.Run(tt.name, func(t *testing.T) {
673 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530674 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
675 defer cancel()
676 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530677 })
678 }
679}
680
681func TestDeviceHandler_addPort(t *testing.T) {
682 dh1 := newMockDeviceHandler()
683 dh2 := negativeDeviceHandler()
684 type args struct {
685 intfID uint32
686 portType voltha.Port_PortType
687 state string
688 }
689 tests := []struct {
690 name string
691 devicehandler *DeviceHandler
692 args args
693 }{
694 // State up
695 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
696 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
697 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
698 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
699 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
700 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
701 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
702 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
703 // state discovery
704 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
705 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
706 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
707 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
708 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
709 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
710 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
711 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
712
713 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
714 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
715 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
716 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
717 }
718 for _, tt := range tests {
719 t.Run(tt.name, func(t *testing.T) {
720 tt.devicehandler.addPort(tt.args.intfID, tt.args.portType, tt.args.state)
721 })
722 }
723}
724
725func Test_macAddressToUint32Array(t *testing.T) {
726 type args struct {
727 mac string
728 }
729 tests := []struct {
730 name string
731 args args
732 want []uint32
733 }{
734 // TODO: Add test cases.
735 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
736 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
737 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
738 }
739 for _, tt := range tests {
740 t.Run(tt.name, func(t *testing.T) {
741 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
742 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
743 }
744 })
745 }
746}
747
748func TestDeviceHandler_handleOltIndication(t *testing.T) {
749
750 type args struct {
751 oltIndication *oop.OltIndication
752 }
753 tests := []struct {
754 name string
755 args args
756 }{
757 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
758 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
759 }
760 for _, tt := range tests {
761 t.Run(tt.name, func(t *testing.T) {
762 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530763 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
764 defer cancel()
765 dh.handleOltIndication(ctx, tt.args.oltIndication)
kdarapu891693b2019-09-16 12:33:49 +0530766 })
767 }
768}
769
770func TestDeviceHandler_AdoptDevice(t *testing.T) {
771 dh1 := newMockDeviceHandler()
772 dh2 := negativeDeviceHandler()
773 type args struct {
774 device *voltha.Device
775 }
776 tests := []struct {
777 name string
778 devicehandler *DeviceHandler
779 args args
780 }{
781 // TODO: Add test cases.
782 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530783 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530784 }
785 for _, tt := range tests {
786 t.Run(tt.name, func(t *testing.T) {
787 //dh.doStateInit()
788 // context.
789 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530790 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
791 defer cancel()
792 tt.devicehandler.postInit(ctx)
kdarapu891693b2019-09-16 12:33:49 +0530793 })
794 }
795}
796
797func TestDeviceHandler_activateONU(t *testing.T) {
798 dh := newMockDeviceHandler()
799 dh1 := negativeDeviceHandler()
800 type args struct {
801 intfID uint32
802 onuID int64
803 serialNum *oop.SerialNumber
804 serialNumber string
805 }
806 tests := []struct {
807 name string
808 devicehandler *DeviceHandler
809 args args
810 }{
811 {"activateONU-1", dh, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
812 {"activateONU-2", dh, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
813 {"activateONU-3", dh1, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
814 {"activateONU-4", dh1, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
815 }
816 for _, tt := range tests {
817 t.Run(tt.name, func(t *testing.T) {
818
npujarec5762e2020-01-01 14:08:48 +0530819 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
820 defer cancel()
821 tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID,
kdarapu891693b2019-09-16 12:33:49 +0530822 tt.args.serialNum, tt.args.serialNumber)
823 })
824 }
825}
826
827func TestDeviceHandler_start(t *testing.T) {
828 dh := newMockDeviceHandler()
829 dh1 := negativeDeviceHandler()
830 dh.start(context.Background())
831 dh.stop(context.Background())
832
833 dh1.start(context.Background())
834 dh1.stop(context.Background())
835
836}
837
838func TestDeviceHandler_PacketOut(t *testing.T) {
839 dh1 := newMockDeviceHandler()
840 dh2 := negativeDeviceHandler()
841 acts := []*ofp.OfpAction{
842 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
843 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
844 fu.Output(1),
845 }
846 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
847 type args struct {
848 egressPortNo int
849 packet *of.OfpPacketOut
850 }
851 tests := []struct {
852 name string
853 devicehandler *DeviceHandler
854 args args
855 wantErr bool
856 }{
857 // TODO: Add test cases.
858 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
859 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
860 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
861 {"PacketOut-2", dh2, args{egressPortNo: 115000, packet: pktout}, false},
862 {"PacketOut-3", dh1, args{egressPortNo: 65536, packet: pktout}, false},
863 {"PacketOut-4", dh2, args{egressPortNo: 65535, packet: pktout}, false},
864 }
865 for _, tt := range tests {
866 t.Run(tt.name, func(t *testing.T) {
867 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530868 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
869 defer cancel()
870 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530871 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
872 }
873 })
874 }
875}
876
877//
878func TestDeviceHandler_doStateUp(t *testing.T) {
879 dh1 := newMockDeviceHandler()
880 dh2 := newMockDeviceHandler()
881
882 dh2.deviceID = ""
883 dh3 := negativeDeviceHandler()
884
885 tests := []struct {
886 name string
887 devicehandler *DeviceHandler
888 wantErr bool
889 }{
890 {"dostateup-1", dh1, false},
891 {"dostateup-2", dh2, false},
892 {"dostateup-3", dh3, true},
893 }
894 for _, tt := range tests {
895 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530896 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
897 defer cancel()
898 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530899 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
900 }
901 })
902 }
903}
904func TestDeviceHandler_doStateDown(t *testing.T) {
905 dh1 := newMockDeviceHandler()
906 dh2 := negativeDeviceHandler()
907 dh3 := newMockDeviceHandler()
908 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
909 tests := []struct {
910 name string
911 devicehandler *DeviceHandler
912 wantErr bool
913 }{
914 {"dostatedown-1", dh1, false},
915 {"dostatedown-2", dh2, true},
916 {"dostatedown-2", dh3, true},
917 }
918 for _, tt := range tests {
919 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530920 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
921 defer cancel()
922 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530923 t.Logf("DeviceHandler.doStateDown() error = %v", err)
924 }
925 })
926 }
927}
928
929func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
930 dh1 := newMockDeviceHandler()
931 dh2 := negativeDeviceHandler()
932 type args struct {
933 device *voltha.Device
934 }
935 tests := []struct {
936 name string
937 devicehandler *DeviceHandler
938 args args
939 wantErr bool
940 }{
941 // TODO: Add test cases.
942 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
943 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
944 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
945 }
946 for _, tt := range tests {
947 t.Run(tt.name, func(t *testing.T) {
948 dh := tt.devicehandler
949 _, err := dh.GetOfpDeviceInfo(tt.args.device)
950 if (err != nil) != tt.wantErr {
951 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
952 return
953 }
954 })
955 }
956}
957
958func TestDeviceHandler_GetOfpPortInfo(t *testing.T) {
959 dh1 := newMockDeviceHandler()
960 dh2 := negativeDeviceHandler()
961 type args struct {
962 device *voltha.Device
963 portNo int64
964 }
965 tests := []struct {
966 name string
967 devicehandler *DeviceHandler
968 args args
969 wantErr bool
970 }{
971 {"GetOfpPortInfo-1", dh1, args{device: dh1.device, portNo: 1}, false},
972 {"GetOfpPortInfo-2", dh2, args{device: dh2.device, portNo: 1}, false},
973 {"GetOfpPortInfo-3", dh1, args{device: dh1.device, portNo: 0}, false},
974 {"GetOfpPortInfo-4", dh2, args{device: dh2.device, portNo: 0}, false},
975 {"GetOfpPortInfo-5", dh1, args{device: &voltha.Device{}, portNo: 1}, false},
976 {"GetOfpPortInfo-6", dh2, args{device: &voltha.Device{}, portNo: 0}, false},
977 // TODO: Add test cases.
978 }
979 for _, tt := range tests {
980 t.Run(tt.name, func(t *testing.T) {
981 dh := tt.devicehandler
982 _, err := dh.GetOfpPortInfo(tt.args.device, tt.args.portNo)
983 if (err != nil) != tt.wantErr {
984 t.Errorf("DeviceHandler.GetOfpPortInfo() error = %v, wantErr %v", err, tt.wantErr)
985 return
986 }
987 })
988 }
989}
990
991func TestDeviceHandler_onuDiscIndication(t *testing.T) {
992
993 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530994 dh1.discOnus = sync.Map{}
995 dh1.discOnus.Store("onu1", true)
996 dh1.discOnus.Store("onu2", false)
kdarapu891693b2019-09-16 12:33:49 +0530997 dh2 := negativeDeviceHandler()
998 type args struct {
999 onuDiscInd *oop.OnuDiscIndication
1000 sn string
1001 }
1002 tests := []struct {
1003 name string
1004 devicehandler *DeviceHandler
1005 args args
1006 }{
1007 // TODO: Add test cases.
1008 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1009 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1010 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1011 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1012 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1013 {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}},
1014 {"onuDiscIndication-7", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1015 }
1016 for _, tt := range tests {
1017 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301018 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1019 defer cancel()
1020 tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
kdarapu891693b2019-09-16 12:33:49 +05301021 })
1022 }
1023}
1024
1025func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1026 dh1 := newMockDeviceHandler()
1027 dh2 := negativeDeviceHandler()
1028 tests := []struct {
1029 name string
1030 devicehandler *DeviceHandler
1031
1032 wantErr bool
1033 }{
1034 // TODO: Add test cases.
1035 {"populateDeviceInfo-1", dh1, false},
1036 {"populateDeviceInfo-2", dh1, true},
1037 {"populateDeviceInfo-3", dh1, true},
1038 {"populateDeviceInfo-4", dh1, true},
1039 {"populateDeviceInfo-5", dh2, true},
1040 }
1041 for _, tt := range tests {
1042 t.Run(tt.name, func(t *testing.T) {
1043
1044 _, err := tt.devicehandler.populateDeviceInfo()
1045 if (err != nil) != tt.wantErr {
1046 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1047 return
1048 }
1049
1050 })
1051 }
1052}
1053
1054func TestDeviceHandler_readIndications(t *testing.T) {
1055 dh1 := newMockDeviceHandler()
1056 dh2 := newMockDeviceHandler()
1057 dh2.adminState = "down"
1058 dh3 := newMockDeviceHandler()
1059 dh3.device.AdminState = voltha.AdminState_DISABLED
1060 dh4 := negativeDeviceHandler()
1061 tests := []struct {
1062 name string
1063 devicehandler *DeviceHandler
1064 }{
1065 // TODO: Add test cases.
1066 {"readIndications-1", dh1},
1067 {"readIndications-2", dh2},
1068 {"readIndications-3", dh2},
1069 {"readIndications-4", dh2},
1070 {"readIndications-5", dh2},
1071 {"readIndications-6", dh3},
1072 {"readIndications-7", dh3},
1073 {"readIndications-8", dh4},
1074 }
1075 for _, tt := range tests {
1076 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301077 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1078 defer cancel()
1079 tt.devicehandler.readIndications(ctx)
kdarapu891693b2019-09-16 12:33:49 +05301080 })
1081 }
1082}
Naga Manjunath7615e552019-10-11 22:35:47 +05301083
1084func Test_startCollector(t *testing.T) {
1085 type args struct {
1086 dh *DeviceHandler
1087 }
1088 dh := newMockDeviceHandler()
1089 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
1090 dh.portStats.NorthBoundPort[0] = &NniPort{Name: "OLT-1"}
1091 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1092 dh.portStats.Device = dh
1093 for i := 0; i < 16; i++ {
1094 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1095 }
1096 tests := []struct {
1097 name string
1098 args args
1099 }{
1100 // TODO: Add test cases.
1101 {"StartCollector-1", args{dh}},
1102 }
1103 for _, tt := range tests {
1104 t.Run(tt.name, func(t *testing.T) {
1105 go func() {
Amit Ghoshd4cbe482019-11-21 12:07:14 +00001106 time.Sleep(66 * time.Second) // startCollector inside waits for 1 min, so we stop it after 6 secs of running
Naga Manjunath7615e552019-10-11 22:35:47 +05301107 tt.args.dh.stopCollector <- true
1108 }()
1109 startCollector(tt.args.dh)
1110 })
1111 }
1112}