blob: 6f84ba2ff0e901855edd15025bb599ff5ba3722e [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
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
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"
Thomas Lee S94109f12020-03-03 16:39:29 +053036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
38 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000039 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"
kdarapu381c6902019-07-31 18:23:16 +053044)
45
kdarapu891693b2019-09-16 12:33:49 +053046func init() {
47 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
48}
49
50func newMockCoreProxy() *mocks.MockCoreProxy {
51 mcp := mocks.MockCoreProxy{}
52 mcp.Devices = make(map[string]*voltha.Device)
Naga Manjunath7615e552019-10-11 22:35:47 +053053 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053054 mcp.Devices["olt"] = &voltha.Device{
55
56 Id: "olt",
57 Root: true,
58 ParentId: "logical_device",
59 ParentPortNo: 1,
60
61 Ports: []*voltha.Port{
62 {PortNo: 1, Label: "pon"},
63 {PortNo: 2, Label: "nni"},
64 },
65 ProxyAddress: &voltha.Device_ProxyAddress{
66 DeviceId: "olt",
67 DeviceType: "onu",
68 ChannelId: 1,
69 ChannelGroupId: 1,
70 },
71 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053072 PmConfigs: &voltha.PmConfigs{
73 DefaultFreq: 10,
74 Id: "olt",
75 FreqOverride: false,
76 Grouped: false,
77 Metrics: pm,
78 },
kdarapu891693b2019-09-16 12:33:49 +053079 }
80 mcp.Devices["onu1"] = &voltha.Device{
81
82 Id: "1",
83 Root: false,
84 ParentId: "olt",
85 ParentPortNo: 1,
86
87 Ports: []*voltha.Port{
88 {PortNo: 1, Label: "pon"},
89 {PortNo: 2, Label: "uni"},
90 },
91 OperStatus: 4,
92 ProxyAddress: &voltha.Device_ProxyAddress{
93 OnuId: 1,
94 ChannelId: 1,
95 ChannelGroupId: 1,
96 },
97 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053098 PmConfigs: &voltha.PmConfigs{
99 DefaultFreq: 10,
100 Id: "olt",
101 FreqOverride: false,
102 Grouped: false,
103 Metrics: pm,
104 },
kdarapu891693b2019-09-16 12:33:49 +0530105 }
106 mcp.Devices["onu2"] = &voltha.Device{
107 Id: "2",
108 Root: false,
109 ParentId: "olt",
110 OperStatus: 2,
111 Ports: []*voltha.Port{
112 {PortNo: 1, Label: "pon"},
113 {PortNo: 2, Label: "uni"},
114 },
115
116 ParentPortNo: 1,
117
118 ProxyAddress: &voltha.Device_ProxyAddress{
119 OnuId: 2,
120 ChannelId: 1,
121 ChannelGroupId: 1,
122 },
123 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530124 PmConfigs: &voltha.PmConfigs{
125 DefaultFreq: 10,
126 Id: "olt",
127 FreqOverride: false,
128 Grouped: false,
129 Metrics: pm,
130 },
kdarapu891693b2019-09-16 12:33:49 +0530131 }
132 return &mcp
133}
134func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530135 device := &voltha.Device{
136 Id: "olt",
137 Root: true,
138 ParentId: "logical_device",
139 Ports: []*voltha.Port{
kdarapu1afeceb2020-02-12 01:38:09 -0500140 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
141 {PortNo: 2, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
kdarapu381c6902019-07-31 18:23:16 +0530142 },
kdarapu891693b2019-09-16 12:33:49 +0530143 ProxyAddress: &voltha.Device_ProxyAddress{
144 DeviceId: "olt",
145 DeviceType: "onu",
146 ChannelId: 1,
147 ChannelGroupId: 1,
148 },
149 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530150 }
kdarapu891693b2019-09-16 12:33:49 +0530151 cp := newMockCoreProxy()
152 ap := &mocks.MockAdapterProxy{}
153 ep := &mocks.MockEventProxy{}
154 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep}
155 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
156 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: nil, Model: "openolt", DeviceId: dh.deviceID}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530157 dh.resourceMgr = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.deviceID, DeviceType: dh.deviceType, DevInfo: deviceInf,
158 KVStore: &db.Backend{
159 Client: &mocks.MockKVClient{},
160 }}
161 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
162 ranges := make(map[string]interface{})
163 sharedIdxByType := make(map[string]string)
164 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
165 sharedIdxByType["ONU_ID"] = "ONU_ID"
166 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
167 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
168 ranges["ONU_ID"] = uint32(0)
169 ranges["GEMPORT_ID"] = uint32(0)
170 ranges["ALLOC_ID"] = uint32(0)
171 ranges["FLOW_ID"] = uint32(0)
172 ranges["onu_id_shared"] = uint32(0)
173 ranges["alloc_id_shared"] = uint32(0)
174 ranges["gemport_id_shared"] = uint32(0)
175 ranges["flow_id_shared"] = uint32(0)
176
177 ponmgr := &ponrmgr.PONResourceManager{
178 DeviceID: "onu-1",
179 IntfIDs: []uint32{1, 2},
180 KVStore: &db.Backend{
181 Client: &mocks.MockKVClient{},
182 },
183 PonResourceRanges: ranges,
184 SharedIdxByType: sharedIdxByType,
185 }
186 dh.resourceMgr.ResourceMgrs[1] = ponmgr
187 dh.resourceMgr.ResourceMgrs[2] = ponmgr
npujarec5762e2020-01-01 14:08:48 +0530188 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
189 defer cancel()
190 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
kdarapu891693b2019-09-16 12:33:49 +0530191 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500192 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530193 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530194 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000195
196 var pmNames = []string{
197 "rx_bytes",
198 "rx_packets",
199 "rx_mcast_packets",
200 "rx_bcast_packets",
201 "tx_bytes",
202 "tx_packets",
203 "tx_mcast_packets",
204 "tx_bcast_packets",
205 }
206
207 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530208 return dh
kdarapu381c6902019-07-31 18:23:16 +0530209}
210
kdarapu891693b2019-09-16 12:33:49 +0530211func negativeDeviceHandler() *DeviceHandler {
212 dh := newMockDeviceHandler()
213 device := dh.device
214 device.Id = ""
215 dh.adminState = "down"
216 return dh
217}
kdarapu381c6902019-07-31 18:23:16 +0530218func Test_generateMacFromHost(t *testing.T) {
219 type args struct {
220 host string
221 }
222 tests := []struct {
223 name string
224 args args
225 want string
226 wantErr bool
227 }{
kdarapu891693b2019-09-16 12:33:49 +0530228 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
229 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
230 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
231 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530232 }
233 for _, tt := range tests {
234 t.Run(tt.name, func(t *testing.T) {
235 got, err := generateMacFromHost(tt.args.host)
236 if (err != nil) != tt.wantErr {
237 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
238 return
239 }
240 if got != tt.want {
241 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
242 }
243 })
244 }
245}
246func Test_macifyIP(t *testing.T) {
247 type args struct {
248 ip net.IP
249 }
250 tests := []struct {
251 name string
252 args args
253 want string
254 }{{
kdarapu891693b2019-09-16 12:33:49 +0530255 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530256 args{ip: net.ParseIP("10.10.10.10")},
257 "00:00:0a:0a:0a:0a",
258 },
259 {
kdarapu891693b2019-09-16 12:33:49 +0530260 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530261 args{ip: net.ParseIP("127.0.0.1")},
262 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530263 },
264 {
265 "macifyIP-3",
266 args{ip: net.ParseIP("127.0.0.1/24")},
267 "",
268 },
269 }
kdarapu381c6902019-07-31 18:23:16 +0530270 for _, tt := range tests {
271 t.Run(tt.name, func(t *testing.T) {
272 if got := macifyIP(tt.args.ip); got != tt.want {
273 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
274 }
275 })
276 }
277}
278
David K. Bainbridge794735f2020-02-11 21:01:37 -0800279func sparseCompare(keys []string, spec, target interface{}) bool {
280 if spec == target {
281 return true
282 }
283 if spec == nil || target == nil {
284 return false
285 }
286 typeSpec := reflect.TypeOf(spec)
287 typeTarget := reflect.TypeOf(target)
288 if typeSpec != typeTarget {
289 return false
290 }
291
292 vSpec := reflect.ValueOf(spec)
293 vTarget := reflect.ValueOf(target)
294 if vSpec.Kind() == reflect.Ptr {
295 vSpec = vSpec.Elem()
296 vTarget = vTarget.Elem()
297 }
298
299 for _, key := range keys {
300 fSpec := vSpec.FieldByName(key)
301 fTarget := vTarget.FieldByName(key)
302 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
303 return false
304 }
305 }
306 return true
307}
308
kdarapu381c6902019-07-31 18:23:16 +0530309func TestDeviceHandler_GetChildDevice(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530310 dh1 := newMockDeviceHandler()
311 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530312 type args struct {
313 parentPort uint32
314 onuID uint32
315 }
316 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530317 name string
318 devicehandler *DeviceHandler
319 args args
320 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800321 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530322 }{
kdarapu891693b2019-09-16 12:33:49 +0530323 {"GetChildDevice-1", dh1,
324 args{parentPort: 1,
325 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800326 &voltha.Device{
327 Id: "1",
328 ParentId: "olt",
329 ParentPortNo: 1,
330 },
331 nil,
kdarapu891693b2019-09-16 12:33:49 +0530332 },
333 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530334 args{parentPort: 1,
335 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800336 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530337 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530338 },
339 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800340
341 /*
342 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
343 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>)
344 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
345 */
kdarapu381c6902019-07-31 18:23:16 +0530346 for _, tt := range tests {
347 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800348 got, err := tt.devicehandler.GetChildDevice(tt.args.parentPort, tt.args.onuID)
349 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
350 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
351 tt.want, tt.errType, got, reflect.TypeOf(err))
352 return
353 }
kdarapu381c6902019-07-31 18:23:16 +0530354 t.Log("onu device id", got)
355 })
356 }
357}
kdarapu891693b2019-09-16 12:33:49 +0530358
359func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530360 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530361 type args struct {
362 portNum uint32
363 portType voltha.Port_PortType
364 }
365 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800366 name string
367 args args
368 want string
369 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530370 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800371 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
372 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
373 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
374 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
375 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
376 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
377 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530378 }
379 for _, tt := range tests {
380 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800381 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
382 if reflect.TypeOf(err) != tt.errType || got != tt.want {
383 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
384 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530385 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800386
kdarapu891693b2019-09-16 12:33:49 +0530387 })
388 }
389}
390
391func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
392 dh := newMockDeviceHandler()
393 proxyAddr := dh.device.ProxyAddress
394 body := &ic.InterAdapterOmciMessage{
395 Message: []byte("asdfasdfasdfasdfas"),
396 ProxyAddress: proxyAddr,
397 }
398 body2 := &ic.InterAdapterOmciMessage{
399 Message: []byte("asdfasdfasdfasdfas"),
400 //ProxyAddress: &voltha.Device_ProxyAddress{},
401 }
402 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
403 var marshalledData *any.Any
404 var err error
405
406 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
407 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
408 }
409
410 var marshalledData1 *any.Any
411
412 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
413 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
414 }
415 var marshalledData2 *any.Any
416
417 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
418 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
419 }
420 type args struct {
421 msg *ic.InterAdapterMessage
422 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530423 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530424 tests := []struct {
425 name string
426 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800427 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530428 }{
429 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
430 Header: &ic.InterAdapterHeader{
431 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800432 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530433 },
434 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800435 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530436 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
437 Header: &ic.InterAdapterHeader{
438 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800439 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530440 },
441 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800442 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530443 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
444 Header: &ic.InterAdapterHeader{
445 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800446 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530447 },
448 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530449 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530450 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
451 Header: &ic.InterAdapterHeader{
452 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800453 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530454 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800455 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530456 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
457 Header: &ic.InterAdapterHeader{
458 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800459 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530460 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800461 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530462 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
463 Header: &ic.InterAdapterHeader{
464 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800465 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530466 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800467 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530468 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
469 Header: &ic.InterAdapterHeader{
470 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800471 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530472 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800473 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530474 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
475 Header: &ic.InterAdapterHeader{
476 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800477 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530478 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800479 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530480 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
481 Header: &ic.InterAdapterHeader{
482 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800483 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530484 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800485 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530486 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
487 Header: &ic.InterAdapterHeader{
488 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800489 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530490 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800491 }}, invalid},
492 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
493 Header: &ic.InterAdapterHeader{
494 Id: "012345",
495 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
496 }, Body: marshalledData2,
497 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530498 }
499 for _, tt := range tests {
500 t.Run(tt.name, func(t *testing.T) {
501
David K. Bainbridge794735f2020-02-11 21:01:37 -0800502 if err := dh.ProcessInterAdapterMessage(tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530503 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
504 }
505 })
506 }
507}
508
509func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
510 dh1 := newMockDeviceHandler()
511 dh2 := negativeDeviceHandler()
512 device1 := &voltha.Device{
513 Id: "onu1",
514 Root: false,
515 ParentId: "logical_device",
516 ProxyAddress: &voltha.Device_ProxyAddress{
517 DeviceId: "onu1",
518 DeviceType: "onu",
519 ChannelId: 1,
520 ChannelGroupId: 1,
521 },
522 ConnectStatus: 1,
523 }
524 device2 := device1
525 device2.ConnectStatus = 2
526 iaomciMsg1 := &ic.InterAdapterOmciMessage{
527 ProxyAddress: &voltha.Device_ProxyAddress{
528 DeviceId: "onu2",
529 DeviceType: "onu",
530 ChannelId: 1,
531 ChannelGroupId: 1,
532 //OnuId: 2,
533 },
534 ConnectStatus: 1,
535 }
536 iaomciMsg2 := &ic.InterAdapterOmciMessage{
537 ProxyAddress: &voltha.Device_ProxyAddress{
538 DeviceId: "onu3",
539 DeviceType: "onu",
540 ChannelId: 1,
541 ChannelGroupId: 1,
542 },
543 ConnectStatus: 1,
544 }
545 type args struct {
546 onuDevice *voltha.Device
547 omciMsg *ic.InterAdapterOmciMessage
548 }
549 tests := []struct {
550 name string
551 devicehandler *DeviceHandler
552 args args
553 }{
554 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
555 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
556 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
557 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
558 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
559 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
560 }
561 for _, tt := range tests {
562 t.Run(tt.name, func(t *testing.T) {
563 tt.devicehandler.sendProxiedMessage(tt.args.onuDevice, tt.args.omciMsg)
564 })
565 }
566}
567
568func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
569 dh1 := newMockDeviceHandler()
570 dh2 := negativeDeviceHandler()
571
572 type args struct {
573 logicalPort uint32
574 packetPayload []byte
575 }
576 tests := []struct {
577 name string
578 devicehandler *DeviceHandler
579 args args
580 }{
581 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
582 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
583 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
584 }
585 for _, tt := range tests {
586 t.Run(tt.name, func(t *testing.T) {
587 tt.devicehandler.SendPacketInToCore(tt.args.logicalPort, tt.args.packetPayload)
588 })
589 }
590}
591
592func TestDeviceHandler_DisableDevice(t *testing.T) {
593 dh1 := newMockDeviceHandler()
594 dh2 := negativeDeviceHandler()
595 type args struct {
596 device *voltha.Device
597 }
598 tests := []struct {
599 name string
600 devicehandler *DeviceHandler
601 args args
602 wantErr bool
603 }{
604 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400605 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530606 }
607 for _, tt := range tests {
608 t.Run(tt.name, func(t *testing.T) {
609
610 if err := tt.devicehandler.DisableDevice(tt.args.device); (err != nil) != tt.wantErr {
611 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
612 }
613 })
614 }
615}
616
617func TestDeviceHandler_ReenableDevice(t *testing.T) {
618 dh1 := newMockDeviceHandler()
619 dh2 := negativeDeviceHandler()
620 type args struct {
621 device *voltha.Device
622 }
623 tests := []struct {
624 name string
625 devicehandler *DeviceHandler
626 args args
627 wantErr bool
628 }{
629 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
630 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
631 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
632 }
633 for _, tt := range tests {
634 t.Run(tt.name, func(t *testing.T) {
635 dh := tt.devicehandler
636 if err := dh.ReenableDevice(tt.args.device); (err != nil) != tt.wantErr {
637 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
638 }
639 })
640 }
641}
642
643func TestDeviceHandler_RebootDevice(t *testing.T) {
644 dh1 := newMockDeviceHandler()
645 dh2 := newMockDeviceHandler()
646 type args struct {
647 device *voltha.Device
648 }
649 tests := []struct {
650 name string
651 devicehandler *DeviceHandler
652 args args
653 wantErr bool
654 }{
655 // TODO: Add test cases.
656 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
657 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
658 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
659 }
660 for _, tt := range tests {
661 t.Run(tt.name, func(t *testing.T) {
662
663 if err := tt.devicehandler.RebootDevice(tt.args.device); (err != nil) != tt.wantErr {
664 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
665 }
666 })
667 }
668}
669
670func TestDeviceHandler_handleIndication(t *testing.T) {
671 dh1 := newMockDeviceHandler()
672 dh2 := negativeDeviceHandler()
673 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530674 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530675 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
676 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530677
kdarapu891693b2019-09-16 12:33:49 +0530678 type args struct {
679 indication *oop.Indication
680 }
681 tests := []struct {
682 name string
683 deviceHandler *DeviceHandler
684 args args
685 }{
686 // TODO: Add test cases.
687 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
688 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
689 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
690 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
691 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
692 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
693 {"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")}}}}}},
694 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
695 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
696 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
697 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
698 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
699 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
700 {"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}}}}},
701 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
702 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
703 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
704 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
705
706 // Negative testcases
707 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
708 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
709 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
710 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
711 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
712 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
713 {"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")}}}}}},
714 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
715 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
716 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
717 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
718 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
719 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
720 {"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}}}}},
721 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
722 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
723 //
724 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
725 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
726 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
727 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
728 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
729 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
730 }
731 for _, tt := range tests {
732 t.Run(tt.name, func(t *testing.T) {
733 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530734 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
735 defer cancel()
736 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530737 })
738 }
739}
740
741func TestDeviceHandler_addPort(t *testing.T) {
742 dh1 := newMockDeviceHandler()
743 dh2 := negativeDeviceHandler()
744 type args struct {
745 intfID uint32
746 portType voltha.Port_PortType
747 state string
748 }
749 tests := []struct {
750 name string
751 devicehandler *DeviceHandler
752 args args
753 }{
754 // State up
755 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
756 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
757 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
758 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
759 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
760 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
761 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
762 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
763 // state discovery
764 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
765 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
766 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
767 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
768 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
769 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
770 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
771 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
772
773 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
774 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
775 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
776 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
777 }
778 for _, tt := range tests {
779 t.Run(tt.name, func(t *testing.T) {
780 tt.devicehandler.addPort(tt.args.intfID, tt.args.portType, tt.args.state)
781 })
782 }
783}
784
785func Test_macAddressToUint32Array(t *testing.T) {
786 type args struct {
787 mac string
788 }
789 tests := []struct {
790 name string
791 args args
792 want []uint32
793 }{
794 // TODO: Add test cases.
795 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
796 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
797 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
798 }
799 for _, tt := range tests {
800 t.Run(tt.name, func(t *testing.T) {
801 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
802 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
803 }
804 })
805 }
806}
807
808func TestDeviceHandler_handleOltIndication(t *testing.T) {
809
810 type args struct {
811 oltIndication *oop.OltIndication
812 }
813 tests := []struct {
814 name string
815 args args
816 }{
817 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
818 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
819 }
820 for _, tt := range tests {
821 t.Run(tt.name, func(t *testing.T) {
822 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530823 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
824 defer cancel()
825 dh.handleOltIndication(ctx, tt.args.oltIndication)
kdarapu891693b2019-09-16 12:33:49 +0530826 })
827 }
828}
829
830func TestDeviceHandler_AdoptDevice(t *testing.T) {
831 dh1 := newMockDeviceHandler()
832 dh2 := negativeDeviceHandler()
833 type args struct {
834 device *voltha.Device
835 }
836 tests := []struct {
837 name string
838 devicehandler *DeviceHandler
839 args args
840 }{
841 // TODO: Add test cases.
842 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530843 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530844 }
845 for _, tt := range tests {
846 t.Run(tt.name, func(t *testing.T) {
847 //dh.doStateInit()
848 // context.
849 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530850 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
851 defer cancel()
852 tt.devicehandler.postInit(ctx)
kdarapu891693b2019-09-16 12:33:49 +0530853 })
854 }
855}
856
857func TestDeviceHandler_activateONU(t *testing.T) {
858 dh := newMockDeviceHandler()
859 dh1 := negativeDeviceHandler()
860 type args struct {
861 intfID uint32
862 onuID int64
863 serialNum *oop.SerialNumber
864 serialNumber string
865 }
866 tests := []struct {
867 name string
868 devicehandler *DeviceHandler
869 args args
870 }{
871 {"activateONU-1", dh, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
872 {"activateONU-2", dh, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
873 {"activateONU-3", dh1, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
874 {"activateONU-4", dh1, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
875 }
876 for _, tt := range tests {
877 t.Run(tt.name, func(t *testing.T) {
878
npujarec5762e2020-01-01 14:08:48 +0530879 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
880 defer cancel()
881 tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID,
kdarapu891693b2019-09-16 12:33:49 +0530882 tt.args.serialNum, tt.args.serialNumber)
883 })
884 }
885}
886
887func TestDeviceHandler_start(t *testing.T) {
888 dh := newMockDeviceHandler()
889 dh1 := negativeDeviceHandler()
890 dh.start(context.Background())
891 dh.stop(context.Background())
892
893 dh1.start(context.Background())
894 dh1.stop(context.Background())
895
896}
897
898func TestDeviceHandler_PacketOut(t *testing.T) {
899 dh1 := newMockDeviceHandler()
900 dh2 := negativeDeviceHandler()
901 acts := []*ofp.OfpAction{
902 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
903 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
904 fu.Output(1),
905 }
906 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
907 type args struct {
908 egressPortNo int
909 packet *of.OfpPacketOut
910 }
911 tests := []struct {
912 name string
913 devicehandler *DeviceHandler
914 args args
915 wantErr bool
916 }{
917 // TODO: Add test cases.
918 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
919 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
920 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
921 {"PacketOut-2", dh2, args{egressPortNo: 115000, packet: pktout}, false},
922 {"PacketOut-3", dh1, args{egressPortNo: 65536, packet: pktout}, false},
923 {"PacketOut-4", dh2, args{egressPortNo: 65535, packet: pktout}, false},
924 }
925 for _, tt := range tests {
926 t.Run(tt.name, func(t *testing.T) {
927 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530928 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
929 defer cancel()
930 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530931 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
932 }
933 })
934 }
935}
936
937//
938func TestDeviceHandler_doStateUp(t *testing.T) {
939 dh1 := newMockDeviceHandler()
940 dh2 := newMockDeviceHandler()
941
942 dh2.deviceID = ""
943 dh3 := negativeDeviceHandler()
944
945 tests := []struct {
946 name string
947 devicehandler *DeviceHandler
948 wantErr bool
949 }{
950 {"dostateup-1", dh1, false},
951 {"dostateup-2", dh2, false},
952 {"dostateup-3", dh3, true},
953 }
954 for _, tt := range tests {
955 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530956 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
957 defer cancel()
958 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530959 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
960 }
961 })
962 }
963}
964func TestDeviceHandler_doStateDown(t *testing.T) {
965 dh1 := newMockDeviceHandler()
966 dh2 := negativeDeviceHandler()
967 dh3 := newMockDeviceHandler()
968 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
969 tests := []struct {
970 name string
971 devicehandler *DeviceHandler
972 wantErr bool
973 }{
974 {"dostatedown-1", dh1, false},
975 {"dostatedown-2", dh2, true},
976 {"dostatedown-2", dh3, true},
977 }
978 for _, tt := range tests {
979 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530980 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
981 defer cancel()
982 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530983 t.Logf("DeviceHandler.doStateDown() error = %v", err)
984 }
985 })
986 }
987}
988
989func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
990 dh1 := newMockDeviceHandler()
991 dh2 := negativeDeviceHandler()
992 type args struct {
993 device *voltha.Device
994 }
995 tests := []struct {
996 name string
997 devicehandler *DeviceHandler
998 args args
999 wantErr bool
1000 }{
1001 // TODO: Add test cases.
1002 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1003 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1004 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1005 }
1006 for _, tt := range tests {
1007 t.Run(tt.name, func(t *testing.T) {
1008 dh := tt.devicehandler
1009 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1010 if (err != nil) != tt.wantErr {
1011 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1012 return
1013 }
1014 })
1015 }
1016}
1017
1018func TestDeviceHandler_GetOfpPortInfo(t *testing.T) {
1019 dh1 := newMockDeviceHandler()
1020 dh2 := negativeDeviceHandler()
1021 type args struct {
1022 device *voltha.Device
1023 portNo int64
1024 }
1025 tests := []struct {
1026 name string
1027 devicehandler *DeviceHandler
1028 args args
1029 wantErr bool
1030 }{
1031 {"GetOfpPortInfo-1", dh1, args{device: dh1.device, portNo: 1}, false},
1032 {"GetOfpPortInfo-2", dh2, args{device: dh2.device, portNo: 1}, false},
1033 {"GetOfpPortInfo-3", dh1, args{device: dh1.device, portNo: 0}, false},
1034 {"GetOfpPortInfo-4", dh2, args{device: dh2.device, portNo: 0}, false},
1035 {"GetOfpPortInfo-5", dh1, args{device: &voltha.Device{}, portNo: 1}, false},
1036 {"GetOfpPortInfo-6", dh2, args{device: &voltha.Device{}, portNo: 0}, false},
1037 // TODO: Add test cases.
1038 }
1039 for _, tt := range tests {
1040 t.Run(tt.name, func(t *testing.T) {
1041 dh := tt.devicehandler
1042 _, err := dh.GetOfpPortInfo(tt.args.device, tt.args.portNo)
1043 if (err != nil) != tt.wantErr {
1044 t.Errorf("DeviceHandler.GetOfpPortInfo() error = %v, wantErr %v", err, tt.wantErr)
1045 return
1046 }
1047 })
1048 }
1049}
1050
1051func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1052
1053 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301054 dh1.discOnus = sync.Map{}
1055 dh1.discOnus.Store("onu1", true)
1056 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301057 dh1.discOnus.Store("onu3", true)
1058 dh1.discOnus.Store("onu4", true)
1059 dh1.onus = sync.Map{}
1060 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1061 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301062 dh2 := negativeDeviceHandler()
1063 type args struct {
1064 onuDiscInd *oop.OnuDiscIndication
1065 sn string
1066 }
1067 tests := []struct {
1068 name string
1069 devicehandler *DeviceHandler
1070 args args
1071 }{
1072 // TODO: Add test cases.
1073 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1074 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1075 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1076 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1077 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1078 {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}},
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301079 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1080 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1081 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301082 }
1083 for _, tt := range tests {
1084 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301085 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1086 defer cancel()
1087 tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
kdarapu891693b2019-09-16 12:33:49 +05301088 })
1089 }
1090}
1091
1092func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1093 dh1 := newMockDeviceHandler()
1094 dh2 := negativeDeviceHandler()
1095 tests := []struct {
1096 name string
1097 devicehandler *DeviceHandler
1098
1099 wantErr bool
1100 }{
1101 // TODO: Add test cases.
1102 {"populateDeviceInfo-1", dh1, false},
1103 {"populateDeviceInfo-2", dh1, true},
1104 {"populateDeviceInfo-3", dh1, true},
1105 {"populateDeviceInfo-4", dh1, true},
1106 {"populateDeviceInfo-5", dh2, true},
1107 }
1108 for _, tt := range tests {
1109 t.Run(tt.name, func(t *testing.T) {
1110
1111 _, err := tt.devicehandler.populateDeviceInfo()
1112 if (err != nil) != tt.wantErr {
1113 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1114 return
1115 }
1116
1117 })
1118 }
1119}
1120
1121func TestDeviceHandler_readIndications(t *testing.T) {
1122 dh1 := newMockDeviceHandler()
1123 dh2 := newMockDeviceHandler()
1124 dh2.adminState = "down"
1125 dh3 := newMockDeviceHandler()
1126 dh3.device.AdminState = voltha.AdminState_DISABLED
1127 dh4 := negativeDeviceHandler()
1128 tests := []struct {
1129 name string
1130 devicehandler *DeviceHandler
1131 }{
1132 // TODO: Add test cases.
1133 {"readIndications-1", dh1},
1134 {"readIndications-2", dh2},
1135 {"readIndications-3", dh2},
1136 {"readIndications-4", dh2},
1137 {"readIndications-5", dh2},
1138 {"readIndications-6", dh3},
1139 {"readIndications-7", dh3},
1140 {"readIndications-8", dh4},
1141 }
1142 for _, tt := range tests {
1143 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301144 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1145 defer cancel()
1146 tt.devicehandler.readIndications(ctx)
kdarapu891693b2019-09-16 12:33:49 +05301147 })
1148 }
1149}
Naga Manjunath7615e552019-10-11 22:35:47 +05301150
1151func Test_startCollector(t *testing.T) {
1152 type args struct {
1153 dh *DeviceHandler
1154 }
1155 dh := newMockDeviceHandler()
1156 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
1157 dh.portStats.NorthBoundPort[0] = &NniPort{Name: "OLT-1"}
1158 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1159 dh.portStats.Device = dh
1160 for i := 0; i < 16; i++ {
1161 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1162 }
1163 tests := []struct {
1164 name string
1165 args args
1166 }{
1167 // TODO: Add test cases.
1168 {"StartCollector-1", args{dh}},
1169 }
1170 for _, tt := range tests {
1171 t.Run(tt.name, func(t *testing.T) {
1172 go func() {
Amit Ghoshd4cbe482019-11-21 12:07:14 +00001173 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 +05301174 tt.args.dh.stopCollector <- true
1175 }()
1176 startCollector(tt.args.dh)
1177 })
1178 }
1179}