blob: 70b92bbc857521e349268d95b171031588608852 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
2 * Copyright 2020-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 core provides the utility for olt devices, flows and statistics
18package core
19
20import (
21 "context"
22 "net"
23 "reflect"
24 "sync"
25 "testing"
26 "time"
27
28 "github.com/golang/protobuf/ptypes"
29 "github.com/golang/protobuf/ptypes/any"
30 "github.com/opencord/voltha-lib-go/v3/pkg/db"
31 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 "github.com/opencord/voltha-lib-go/v3/pkg/pmmetrics"
34 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
35 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
36 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
37 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
38 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
39 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
40 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
41 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
42 oop "github.com/opencord/voltha-protos/v3/go/openolt"
43 "github.com/opencord/voltha-protos/v3/go/voltha"
44)
45
46func newMockCoreProxy() *mocks.MockCoreProxy {
47 mcp := mocks.MockCoreProxy{
48 Devices: make(map[string]*voltha.Device),
49 DevicePorts: make(map[string][]*voltha.Port),
50 }
51 var pm []*voltha.PmConfig
52 mcp.Devices["olt"] = &voltha.Device{
53 Id: "olt",
54 Root: true,
55 ParentId: "logical_device",
56 ParentPortNo: 1,
57 ProxyAddress: &voltha.Device_ProxyAddress{
58 DeviceId: "olt",
59 DeviceType: "onu",
60 ChannelId: 1,
61 ChannelGroupId: 1,
62 },
63 ConnectStatus: 1,
64 PmConfigs: &voltha.PmConfigs{
65 DefaultFreq: 10,
66 Id: "olt",
67 FreqOverride: false,
68 Grouped: false,
69 Metrics: pm,
70 },
71 }
72 mcp.DevicePorts["olt"] = []*voltha.Port{
73 {PortNo: 1, Label: "pon"},
74 {PortNo: 2, Label: "nni"},
75 }
76
77 mcp.Devices["onu1"] = &voltha.Device{
78 Id: "1",
79 Root: false,
80 ParentId: "olt",
81 ParentPortNo: 1,
82
83 OperStatus: 4,
84 ProxyAddress: &voltha.Device_ProxyAddress{
85 OnuId: 1,
86 ChannelId: 1,
87 ChannelGroupId: 1,
88 },
89 ConnectStatus: 1,
90 PmConfigs: &voltha.PmConfigs{
91 DefaultFreq: 10,
92 Id: "olt",
93 FreqOverride: false,
94 Grouped: false,
95 Metrics: pm,
96 },
97 }
98 mcp.DevicePorts["onu1"] = []*voltha.Port{
99 {PortNo: 1, Label: "pon"},
100 {PortNo: 2, Label: "uni"},
101 }
102
103 mcp.Devices["onu2"] = &voltha.Device{
104 Id: "2",
105 Root: false,
106 ParentId: "olt",
107 OperStatus: 2,
108
109 ParentPortNo: 1,
110
111 ProxyAddress: &voltha.Device_ProxyAddress{
112 OnuId: 2,
113 ChannelId: 1,
114 ChannelGroupId: 1,
115 },
116 ConnectStatus: 1,
117 PmConfigs: &voltha.PmConfigs{
118 DefaultFreq: 10,
119 Id: "olt",
120 FreqOverride: false,
121 Grouped: false,
122 Metrics: pm,
123 },
124 }
125 mcp.DevicePorts["onu2"] = []*voltha.Port{
126 {PortNo: 1, Label: "pon"},
127 {PortNo: 2, Label: "uni"},
128 }
129 return &mcp
130}
131
132func newMockDeviceHandler() *DeviceHandler {
133 device := &voltha.Device{
134 Id: "olt",
135 Root: true,
136 ParentId: "logical_device",
137 ProxyAddress: &voltha.Device_ProxyAddress{
138 DeviceId: "olt",
139 DeviceType: "onu",
140 ChannelId: 1,
141 ChannelGroupId: 1,
142 },
143 ConnectStatus: 1,
144 MacAddress: "08:00:27:1c:18:55",
145 }
146 cp := newMockCoreProxy()
147 ap := &mocks.MockAdapterProxy{}
148 ep := &mocks.MockEventProxy{}
149 cnf := &config.AdapterFlags{
150 InterfaceName: "enp0s3",
151 SrcMac: "08:00:27:1c:18:9a",
152 }
153 openOLT := &OpenOLT{
154 coreProxy: cp,
155 adapterProxy: ap,
156 eventProxy: ep,
157 config: cnf,
158 }
159 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
160 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
161 IntfIds: []uint32{0, 1},
162 Technology: "xgs-pon",
163 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
164 }}
165
166 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: 2}
167 rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
168 KVStore: &db.Backend{
169 Client: &mocks.MockKVClient{},
170 }}
171 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
172 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
173 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
174
175 dh.resourceMgr = &rsrMgr
176 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
177 ranges := make(map[string]interface{})
178 sharedIdxByType := make(map[string]string)
179 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
180 sharedIdxByType["ONU_ID"] = "ONU_ID"
181 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
182 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
183 ranges["ONU_ID"] = uint32(0)
184 ranges["GEMPORT_ID"] = uint32(0)
185 ranges["ALLOC_ID"] = uint32(0)
186 ranges["FLOW_ID"] = uint32(0)
187 ranges["onu_id_shared"] = uint32(0)
188 ranges["alloc_id_shared"] = uint32(0)
189 ranges["gemport_id_shared"] = uint32(0)
190 ranges["flow_id_shared"] = uint32(0)
191
192 ponmgr := &ponrmgr.PONResourceManager{
193 DeviceID: "onu-1",
194 IntfIDs: []uint32{0, 1},
195 KVStore: &db.Backend{
196 Client: &mocks.MockKVClient{},
197 },
198 PonResourceRanges: ranges,
199 SharedIdxByType: sharedIdxByType,
200 }
201 dh.resourceMgr.ResourceMgrs[0] = ponmgr
202 dh.resourceMgr.ResourceMgrs[1] = ponmgr
203 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
204 defer cancel()
205 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
206 dh.Client = &mocks.MockOpenoltClient{}
207 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
208 dh.transitionMap = &TransitionMap{}
209 dh.portStats = &OpenOltStatisticsMgr{}
210
211 var pmNames = []string{
212 "rx_bytes",
213 "rx_packets",
214 "rx_mcast_packets",
215 "rx_bcast_packets",
216 "tx_bytes",
217 "tx_packets",
218 "tx_mcast_packets",
219 "tx_bcast_packets",
220 }
221
222 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
223 return dh
224}
225
226func negativeDeviceHandler() *DeviceHandler {
227 dh := newMockDeviceHandler()
228 device := dh.device
229 device.Id = ""
230 return dh
231}
232
233func Test_generateMacFromHost(t *testing.T) {
234
235 ctx := context.Background()
236 type args struct {
237 host string
238 }
239 tests := []struct {
240 name string
241 args args
242 want string
243 wantErr bool
244 }{
245 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
246 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
247 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
248 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
249 }
250 for _, tt := range tests {
251 t.Run(tt.name, func(t *testing.T) {
252 got, err := generateMacFromHost(ctx, tt.args.host)
253 if (err != nil) != tt.wantErr {
254 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
255 return
256 }
257 if got != tt.want {
258 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
259 }
260 })
261 }
262}
263func Test_macifyIP(t *testing.T) {
264 type args struct {
265 ip net.IP
266 }
267 tests := []struct {
268 name string
269 args args
270 want string
271 }{{
272 "macifyIP-1",
273 args{ip: net.ParseIP("10.10.10.10")},
274 "00:00:0a:0a:0a:0a",
275 },
276 {
277 "macifyIP-2",
278 args{ip: net.ParseIP("127.0.0.1")},
279 "00:00:7f:00:00:01",
280 },
281 {
282 "macifyIP-3",
283 args{ip: net.ParseIP("127.0.0.1/24")},
284 "",
285 },
286 }
287 for _, tt := range tests {
288 t.Run(tt.name, func(t *testing.T) {
289 if got := macifyIP(tt.args.ip); got != tt.want {
290 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
291 }
292 })
293 }
294}
295
296func sparseCompare(keys []string, spec, target interface{}) bool {
297 if spec == target {
298 return true
299 }
300 if spec == nil || target == nil {
301 return false
302 }
303 typeSpec := reflect.TypeOf(spec)
304 typeTarget := reflect.TypeOf(target)
305 if typeSpec != typeTarget {
306 return false
307 }
308
309 vSpec := reflect.ValueOf(spec)
310 vTarget := reflect.ValueOf(target)
311 if vSpec.Kind() == reflect.Ptr {
312 vSpec = vSpec.Elem()
313 vTarget = vTarget.Elem()
314 }
315
316 for _, key := range keys {
317 fSpec := vSpec.FieldByName(key)
318 fTarget := vTarget.FieldByName(key)
319 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
320 return false
321 }
322 }
323 return true
324}
325
326func TestDeviceHandler_GetChildDevice(t *testing.T) {
327 ctx := context.Background()
328 dh1 := newMockDeviceHandler()
329 dh2 := negativeDeviceHandler()
330 type args struct {
331 parentPort uint32
332 onuID uint32
333 }
334 tests := []struct {
335 name string
336 devicehandler *DeviceHandler
337 args args
338 want *voltha.Device
339 errType reflect.Type
340 }{
341 {"GetChildDevice-1", dh1,
342 args{parentPort: 1,
343 onuID: 1},
344 &voltha.Device{
345 Id: "1",
346 ParentId: "olt",
347 ParentPortNo: 1,
348 },
349 nil,
350 },
351 {"GetChildDevice-2", dh2,
352 args{parentPort: 1,
353 onuID: 1},
354 nil,
355 reflect.TypeOf(&olterrors.ErrNotFound{}),
356 },
357 }
358
359 /*
360 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
361 device_handler_test.go:309: GetportLabel() => want=(, <nil>) got=(id:"1" parent_id:"olt" parent_port_no:1 proxy_address:<channel_id:1 channel_group_id:1 onu_id:1 > oper_status:ACTIVE connect_status:UNREACHABLE ports:<port_no:1 label:"pon" > ports:<port_no:2 label:"uni" > pm_configs:<id:"olt" default_freq:10 > , <nil>)
362 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
363 */
364 for _, tt := range tests {
365 t.Run(tt.name, func(t *testing.T) {
366 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
367 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
368 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
369 tt.want, tt.errType, got, reflect.TypeOf(err))
370 return
371 }
372 t.Log("onu device id", got)
373 })
374 }
375}
376
377func TestGetportLabel(t *testing.T) {
378 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
379 type args struct {
380 portNum uint32
381 portType voltha.Port_PortType
382 }
383 tests := []struct {
384 name string
385 args args
386 want string
387 errType reflect.Type
388 }{
389 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
390 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
391 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
392 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
393 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
394 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
395 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
396 }
397 for _, tt := range tests {
398 t.Run(tt.name, func(t *testing.T) {
399 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
400 if reflect.TypeOf(err) != tt.errType || got != tt.want {
401 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
402 tt.want, tt.errType, got, reflect.TypeOf(err))
403 }
404
405 })
406 }
407}
408
409func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
410 ctx := context.Background()
411 dh := newMockDeviceHandler()
412 proxyAddr := dh.device.ProxyAddress
413 body := &ic.InterAdapterOmciMessage{
414 Message: []byte("asdfasdfasdfasdfas"),
415 ProxyAddress: proxyAddr,
416 }
417 body2 := &ic.InterAdapterOmciMessage{
418 Message: []byte("asdfasdfasdfasdfas"),
419 //ProxyAddress: &voltha.Device_ProxyAddress{},
420 }
421 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
422 var marshalledData *any.Any
423 var err error
424
425 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
426 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
427 }
428
429 var marshalledData1 *any.Any
430
431 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
432 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
433 }
434 var marshalledData2 *any.Any
435
436 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
437 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
438 }
439 type args struct {
440 msg *ic.InterAdapterMessage
441 }
442 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
443 tests := []struct {
444 name string
445 args args
446 wantErr reflect.Type
447 }{
448 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
449 Header: &ic.InterAdapterHeader{
450 Id: "012345",
451 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
452 },
453 Body: marshalledData,
454 }}, invalid},
455 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
456 Header: &ic.InterAdapterHeader{
457 Id: "012345",
458 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
459 },
460 Body: marshalledData1,
461 }}, invalid},
462 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
463 Header: &ic.InterAdapterHeader{
464 Id: "012345",
465 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
466 },
467 Body: marshalledData,
468 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
469 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
470 Header: &ic.InterAdapterHeader{
471 Id: "012345",
472 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
473 }, Body: marshalledData,
474 }}, invalid},
475 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
476 Header: &ic.InterAdapterHeader{
477 Id: "012345",
478 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
479 }, Body: marshalledData1,
480 }}, invalid},
481 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
482 Header: &ic.InterAdapterHeader{
483 Id: "012345",
484 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
485 }, Body: marshalledData,
486 }}, invalid},
487 /*
488 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
489 Header: &ic.InterAdapterHeader{
490 Id: "012345",
491 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
492 }, Body: marshalledData,
493 }}, nil},
494 */
495 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
496 Header: &ic.InterAdapterHeader{
497 Id: "012345",
498 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
499 }, Body: marshalledData,
500 }}, invalid},
501 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
502 Header: &ic.InterAdapterHeader{
503 Id: "012345",
504 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
505 }, Body: marshalledData,
506 }}, invalid},
507 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
508 Header: &ic.InterAdapterHeader{
509 Id: "012345",
510 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
511 }, Body: marshalledData2,
512 }}, invalid},
513 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
514 Header: &ic.InterAdapterHeader{
515 Id: "012345",
516 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
517 }, Body: marshalledData2,
518 }}, invalid},
519 }
520 for _, tt := range tests {
521 t.Run(tt.name, func(t *testing.T) {
522
523 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
524 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
525 }
526 })
527 }
528}
529
530func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
531 ctx := context.Background()
532 dh1 := newMockDeviceHandler()
533 dh2 := negativeDeviceHandler()
534 device1 := &voltha.Device{
535 Id: "onu1",
536 Root: false,
537 ParentId: "logical_device",
538 ProxyAddress: &voltha.Device_ProxyAddress{
539 DeviceId: "onu1",
540 DeviceType: "onu",
541 ChannelId: 1,
542 ChannelGroupId: 1,
543 },
544 ConnectStatus: 1,
545 }
546 device2 := device1
547 device2.ConnectStatus = 2
548 iaomciMsg1 := &ic.InterAdapterOmciMessage{
549 ProxyAddress: &voltha.Device_ProxyAddress{
550 DeviceId: "onu2",
551 DeviceType: "onu",
552 ChannelId: 1,
553 ChannelGroupId: 1,
554 //OnuId: 2,
555 },
556 ConnectStatus: 1,
557 }
558 iaomciMsg2 := &ic.InterAdapterOmciMessage{
559 ProxyAddress: &voltha.Device_ProxyAddress{
560 DeviceId: "onu3",
561 DeviceType: "onu",
562 ChannelId: 1,
563 ChannelGroupId: 1,
564 },
565 ConnectStatus: 1,
566 }
567 type args struct {
568 onuDevice *voltha.Device
569 omciMsg *ic.InterAdapterOmciMessage
570 }
571 tests := []struct {
572 name string
573 devicehandler *DeviceHandler
574 args args
575 }{
576 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
577 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
578 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
579 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
580 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
581 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
582 }
583 for _, tt := range tests {
584 t.Run(tt.name, func(t *testing.T) {
585 _ = tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
586 //TODO: actually verify test cases
587 })
588 }
589}
590
591func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
592 dh1 := newMockDeviceHandler()
593 dh2 := negativeDeviceHandler()
594
595 type args struct {
596 logicalPort uint32
597 packetPayload []byte
598 }
599 tests := []struct {
600 name string
601 devicehandler *DeviceHandler
602 args args
603 }{
604 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
605 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
606 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
607 }
608 for _, tt := range tests {
609 t.Run(tt.name, func(t *testing.T) {
610 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
611 //TODO: actually verify test cases
612 })
613 }
614}
615
616func TestDeviceHandler_DisableDevice(t *testing.T) {
617 dh1 := newMockDeviceHandler()
618 dh2 := negativeDeviceHandler()
619 type args struct {
620 device *voltha.Device
621 }
622 tests := []struct {
623 name string
624 devicehandler *DeviceHandler
625 args args
626 wantErr bool
627 }{
628 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
629 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
630 }
631 for _, tt := range tests {
632 t.Run(tt.name, func(t *testing.T) {
633 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
634 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
635 }
636 })
637 }
638}
639
640func TestDeviceHandler_ReenableDevice(t *testing.T) {
641 dh1 := newMockDeviceHandler()
642 dh2 := negativeDeviceHandler()
643 type args struct {
644 device *voltha.Device
645 }
646 tests := []struct {
647 name string
648 devicehandler *DeviceHandler
649 args args
650 wantErr bool
651 }{
652 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
653 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
654 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
655 }
656 for _, tt := range tests {
657 t.Run(tt.name, func(t *testing.T) {
658 dh := tt.devicehandler
659 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
660 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
661 }
662 })
663 }
664}
665
666func TestDeviceHandler_RebootDevice(t *testing.T) {
667 dh1 := newMockDeviceHandler()
668 dh2 := newMockDeviceHandler()
669 type args struct {
670 device *voltha.Device
671 }
672 tests := []struct {
673 name string
674 devicehandler *DeviceHandler
675 args args
676 }{
677 // TODO: Add test cases.
678 {"RebootDevice-1", dh1, args{device: dh1.device}},
679 {"RebootDevice-2", dh1, args{device: dh2.device}},
680 {"RebootDevice-3", dh2, args{device: dh2.device}},
681 }
682 for _, tt := range tests {
683 t.Run(tt.name, func(t *testing.T) {
684
685 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); err != nil {
686 t.Errorf("DeviceHandler.RebootDevice() error = %v", err)
687 }
688 })
689 }
690}
691
692func TestDeviceHandler_handleIndication(t *testing.T) {
693 dh1 := newMockDeviceHandler()
694 dh2 := negativeDeviceHandler()
695 dh3 := newMockDeviceHandler()
696 dh3.onus = sync.Map{}
697 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
698 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
699
700 type args struct {
701 indication *oop.Indication
702 }
703 tests := []struct {
704 name string
705 deviceHandler *DeviceHandler
706 args args
707 }{
708 // TODO: Add test cases.
709 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
710 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
711 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
712 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
713 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
714 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
715 {"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")}}}}}},
716 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
717 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
718 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
719 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
720 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
721 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
722 {"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}}}}},
723 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
724 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
725 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
726 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
727
728 // Negative testcases
729 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
730 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
731 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
732 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
733 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
734 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
735 {"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")}}}}}},
736 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
737 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
738 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
739 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
740 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
741 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
742 {"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}}}}},
743 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
744 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
745 //
746 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
747 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
748 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
749 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
750 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
751 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
752 }
753 for _, tt := range tests {
754 t.Run(tt.name, func(t *testing.T) {
755 dh := tt.deviceHandler
756 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
757 defer cancel()
758 dh.handleIndication(ctx, tt.args.indication)
759 })
760 }
761}
762
763func TestDeviceHandler_addPort(t *testing.T) {
764 dh1 := newMockDeviceHandler()
765 dh2 := negativeDeviceHandler()
766 type args struct {
767 intfID uint32
768 portType voltha.Port_PortType
769 state string
770 }
771 tests := []struct {
772 name string
773 devicehandler *DeviceHandler
774 args args
775 }{
776 // State up
777 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
778 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
779 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
780 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
781 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
782 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
783 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
784 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
785 // state discovery
786 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
787 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
788 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
789 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
790 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
791 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
792 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
793 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
794
795 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
796 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
797 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
798 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
799 }
800 for _, tt := range tests {
801 t.Run(tt.name, func(t *testing.T) {
802 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
803 //TODO: actually verify test cases
804 })
805 }
806}
807
808func Test_macAddressToUint32Array(t *testing.T) {
809 type args struct {
810 mac string
811 }
812 tests := []struct {
813 name string
814 args args
815 want []uint32
816 }{
817 // TODO: Add test cases.
818 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
819 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
820 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
821 }
822 for _, tt := range tests {
823 t.Run(tt.name, func(t *testing.T) {
824 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
825 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
826 }
827 })
828 }
829}
830
831func TestDeviceHandler_handleOltIndication(t *testing.T) {
832
833 type args struct {
834 oltIndication *oop.OltIndication
835 }
836 tests := []struct {
837 name string
838 args args
839 }{
840 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
841 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
842 }
843 for _, tt := range tests {
844 t.Run(tt.name, func(t *testing.T) {
845 dh := newMockDeviceHandler()
846 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
847 defer cancel()
848 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
849 t.Error(err)
850 }
851 })
852 }
853}
854
855func TestDeviceHandler_AdoptDevice(t *testing.T) {
856 dh1 := newMockDeviceHandler()
857 dh2 := negativeDeviceHandler()
858 type args struct {
859 device *voltha.Device
860 }
861 tests := []struct {
862 name string
863 devicehandler *DeviceHandler
864 args args
865 }{
866 // TODO: Add test cases.
867 {"AdoptDevice-1", dh1, args{device: dh1.device}},
868 {"AdoptDevice-2", dh2, args{device: dh2.device}},
869 }
870 for _, tt := range tests {
871 t.Run(tt.name, func(t *testing.T) {
872 //dh.doStateInit()
873 // context.
874 //dh.AdoptDevice(tt.args.device)
875 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
876 defer cancel()
877 if err := tt.devicehandler.postInit(ctx); err != nil {
878 t.Error(err)
879 }
880 })
881 }
882}
883
884func TestDeviceHandler_activateONU(t *testing.T) {
885 dh := newMockDeviceHandler()
886 dh1 := negativeDeviceHandler()
887 type args struct {
888 intfID uint32
889 onuID int64
890 serialNum *oop.SerialNumber
891 serialNumber string
892 }
893 tests := []struct {
894 name string
895 devicehandler *DeviceHandler
896 args args
897 }{
898 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
899 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
900 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
901 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
902 }
903 for _, tt := range tests {
904 t.Run(tt.name, func(t *testing.T) {
905 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
906 defer cancel()
907 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
908 //TODO: actually verify test cases
909 })
910 }
911}
912
913func TestDeviceHandler_start(t *testing.T) {
914 dh := newMockDeviceHandler()
915 dh1 := negativeDeviceHandler()
916 dh.start(context.Background())
917 dh.stop(context.Background())
918
919 dh1.start(context.Background())
920 dh1.stop(context.Background())
921
922}
923
924func TestDeviceHandler_PacketOut(t *testing.T) {
925 dh1 := newMockDeviceHandler()
926 dh2 := negativeDeviceHandler()
927 acts := []*ofp.OfpAction{
928 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
929 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
930 fu.Output(1),
931 }
932 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
933 type args struct {
934 egressPortNo int
935 packet *of.OfpPacketOut
936 }
937 tests := []struct {
938 name string
939 devicehandler *DeviceHandler
940 args args
941 wantErr bool
942 }{
943 // TODO: Add test cases.
944 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
945 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
946 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
947 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
948 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
949 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
950 }
951 for _, tt := range tests {
952 t.Run(tt.name, func(t *testing.T) {
953 dh := tt.devicehandler
954 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
955 defer cancel()
956 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
957 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
958 }
959 })
960 }
961}
962
963//
964func TestDeviceHandler_doStateUp(t *testing.T) {
965 dh1 := newMockDeviceHandler()
966 dh2 := newMockDeviceHandler()
967
968 dh2.device.Id = ""
969 dh3 := negativeDeviceHandler()
970
971 tests := []struct {
972 name string
973 devicehandler *DeviceHandler
974 wantErr bool
975 }{
976 {"dostateup-1", dh1, false},
977 {"dostateup-2", dh2, false},
978 {"dostateup-3", dh3, true},
979 }
980 for _, tt := range tests {
981 t.Run(tt.name, func(t *testing.T) {
982 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
983 defer cancel()
984 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
985 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
986 }
987 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
988 })
989 }
990}
991func TestDeviceHandler_doStateDown(t *testing.T) {
992 dh1 := newMockDeviceHandler()
993 dh2 := negativeDeviceHandler()
994 dh3 := newMockDeviceHandler()
995 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
996 tests := []struct {
997 name string
998 devicehandler *DeviceHandler
999 wantErr bool
1000 }{
1001 {"dostatedown-1", dh1, false},
1002 {"dostatedown-2", dh2, true},
1003 {"dostatedown-2", dh3, true},
1004 }
1005 for _, tt := range tests {
1006 t.Run(tt.name, func(t *testing.T) {
1007 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1008 defer cancel()
1009 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
1010 t.Logf("DeviceHandler.doStateDown() error = %v", err)
1011 //TODO: should fail this test case (Errorf) if result is not as expected
1012 }
1013 })
1014 }
1015}
1016
1017func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1018 dh1 := newMockDeviceHandler()
1019 dh2 := negativeDeviceHandler()
1020 type args struct {
1021 device *voltha.Device
1022 }
1023 tests := []struct {
1024 name string
1025 devicehandler *DeviceHandler
1026 args args
1027 wantErr bool
1028 }{
1029 // TODO: Add test cases.
1030 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1031 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1032 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1033 }
1034 for _, tt := range tests {
1035 t.Run(tt.name, func(t *testing.T) {
1036 dh := tt.devicehandler
1037 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1038 if (err != nil) != tt.wantErr {
1039 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1040 return
1041 }
1042 })
1043 }
1044}
1045
1046func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1047
1048 dh1 := newMockDeviceHandler()
1049 dh1.discOnus = sync.Map{}
1050 dh1.discOnus.Store("onu1", true)
1051 dh1.discOnus.Store("onu2", false)
1052 dh1.discOnus.Store("onu3", true)
1053 dh1.discOnus.Store("onu4", true)
1054 dh1.onus = sync.Map{}
1055 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1056 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
1057 dh2 := negativeDeviceHandler()
1058 type args struct {
1059 onuDiscInd *oop.OnuDiscIndication
1060 sn string
1061 }
1062 tests := []struct {
1063 name string
1064 devicehandler *DeviceHandler
1065 args args
1066 }{
1067 // TODO: Add test cases.
1068 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1069 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1070 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1071 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1072 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1073 {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}},
1074 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1075 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1076 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1077 }
1078 for _, tt := range tests {
1079 t.Run(tt.name, func(t *testing.T) {
1080 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1081 defer cancel()
1082 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
1083 //TODO: actually verify test cases
1084 })
1085 }
1086}
1087
1088func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1089 dh1 := newMockDeviceHandler()
1090 dh2 := negativeDeviceHandler()
1091 tests := []struct {
1092 name string
1093 devicehandler *DeviceHandler
1094
1095 wantErr bool
1096 }{
1097 // TODO: Add test cases.
1098 {"populateDeviceInfo-1", dh1, true},
1099 {"populateDeviceInfo-2", dh1, true},
1100 {"populateDeviceInfo-3", dh1, true},
1101 {"populateDeviceInfo-4", dh1, true},
1102 {"populateDeviceInfo-5", dh2, true},
1103 }
1104 for _, tt := range tests {
1105 t.Run(tt.name, func(t *testing.T) {
1106
1107 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
1108 if (err != nil) != tt.wantErr {
1109 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1110 return
1111 }
1112
1113 })
1114 }
1115}
1116
1117func TestDeviceHandler_readIndications(t *testing.T) {
1118 dh1 := newMockDeviceHandler()
1119 dh2 := newMockDeviceHandler()
1120 dh3 := newMockDeviceHandler()
1121 dh3.device.AdminState = voltha.AdminState_DISABLED
1122 dh4 := negativeDeviceHandler()
1123 tests := []struct {
1124 name string
1125 devicehandler *DeviceHandler
1126 }{
1127 // TODO: Add test cases.
1128 {"readIndications-1", dh1},
1129 {"readIndications-2", dh2},
1130 {"readIndications-3", dh2},
1131 {"readIndications-4", dh2},
1132 {"readIndications-5", dh2},
1133 {"readIndications-6", dh3},
1134 {"readIndications-7", dh3},
1135 {"readIndications-8", dh4},
1136 }
1137 for _, tt := range tests {
1138 t.Run(tt.name, func(t *testing.T) {
1139 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1140 defer cancel()
1141 _ = tt.devicehandler.readIndications(ctx)
1142 // TODO: actually verify test cases
1143 })
1144 }
1145}
1146
1147func Test_startCollector(t *testing.T) {
1148 type args struct {
1149 dh *DeviceHandler
1150 }
1151 dh := newMockDeviceHandler()
1152 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
1153 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1154 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1155 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1156 }
1157 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
1158 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1159 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
1160 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1161 dh.portStats.Device = dh
1162 for i := 0; i < 16; i++ {
1163 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1164 }
1165 dh1 := newMockDeviceHandler()
1166 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
1167 tests := []struct {
1168 name string
1169 args args
1170 }{
1171 // TODO: Add test cases.
1172 {"StartCollector-1", args{dh}},
1173 {"StartCollector-2", args{dh1}},
1174 }
1175 for _, tt := range tests {
1176 t.Run(tt.name, func(t *testing.T) {
1177 go func() {
1178 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
1179 tt.args.dh.stopCollector <- true
1180 }()
1181 startCollector(context.Background(), tt.args.dh)
1182 })
1183 }
1184}