blob: 1d1192e7ee44aa4b1a302d450b17e5fc2eaae3d7 [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"
Scott Bakerdbd960e2020-02-28 08:57:51 -080036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
37 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000038 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
39 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
40 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
41 oop "github.com/opencord/voltha-protos/v3/go/openolt"
42 "github.com/opencord/voltha-protos/v3/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053043)
44
kdarapu891693b2019-09-16 12:33:49 +053045func init() {
46 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
47}
48
49func newMockCoreProxy() *mocks.MockCoreProxy {
50 mcp := mocks.MockCoreProxy{}
51 mcp.Devices = make(map[string]*voltha.Device)
Naga Manjunath7615e552019-10-11 22:35:47 +053052 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053053 mcp.Devices["olt"] = &voltha.Device{
54
55 Id: "olt",
56 Root: true,
57 ParentId: "logical_device",
58 ParentPortNo: 1,
59
60 Ports: []*voltha.Port{
61 {PortNo: 1, Label: "pon"},
62 {PortNo: 2, Label: "nni"},
63 },
64 ProxyAddress: &voltha.Device_ProxyAddress{
65 DeviceId: "olt",
66 DeviceType: "onu",
67 ChannelId: 1,
68 ChannelGroupId: 1,
69 },
70 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053071 PmConfigs: &voltha.PmConfigs{
72 DefaultFreq: 10,
73 Id: "olt",
74 FreqOverride: false,
75 Grouped: false,
76 Metrics: pm,
77 },
kdarapu891693b2019-09-16 12:33:49 +053078 }
79 mcp.Devices["onu1"] = &voltha.Device{
80
81 Id: "1",
82 Root: false,
83 ParentId: "olt",
84 ParentPortNo: 1,
85
86 Ports: []*voltha.Port{
87 {PortNo: 1, Label: "pon"},
88 {PortNo: 2, Label: "uni"},
89 },
90 OperStatus: 4,
91 ProxyAddress: &voltha.Device_ProxyAddress{
92 OnuId: 1,
93 ChannelId: 1,
94 ChannelGroupId: 1,
95 },
96 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053097 PmConfigs: &voltha.PmConfigs{
98 DefaultFreq: 10,
99 Id: "olt",
100 FreqOverride: false,
101 Grouped: false,
102 Metrics: pm,
103 },
kdarapu891693b2019-09-16 12:33:49 +0530104 }
105 mcp.Devices["onu2"] = &voltha.Device{
106 Id: "2",
107 Root: false,
108 ParentId: "olt",
109 OperStatus: 2,
110 Ports: []*voltha.Port{
111 {PortNo: 1, Label: "pon"},
112 {PortNo: 2, Label: "uni"},
113 },
114
115 ParentPortNo: 1,
116
117 ProxyAddress: &voltha.Device_ProxyAddress{
118 OnuId: 2,
119 ChannelId: 1,
120 ChannelGroupId: 1,
121 },
122 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530123 PmConfigs: &voltha.PmConfigs{
124 DefaultFreq: 10,
125 Id: "olt",
126 FreqOverride: false,
127 Grouped: false,
128 Metrics: pm,
129 },
kdarapu891693b2019-09-16 12:33:49 +0530130 }
131 return &mcp
132}
133func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530134 device := &voltha.Device{
135 Id: "olt",
136 Root: true,
137 ParentId: "logical_device",
138 Ports: []*voltha.Port{
kdarapu1afeceb2020-02-12 01:38:09 -0500139 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
140 {PortNo: 2, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
kdarapu381c6902019-07-31 18:23:16 +0530141 },
kdarapu891693b2019-09-16 12:33:49 +0530142 ProxyAddress: &voltha.Device_ProxyAddress{
143 DeviceId: "olt",
144 DeviceType: "onu",
145 ChannelId: 1,
146 ChannelGroupId: 1,
147 },
148 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530149 }
kdarapu891693b2019-09-16 12:33:49 +0530150 cp := newMockCoreProxy()
151 ap := &mocks.MockAdapterProxy{}
152 ep := &mocks.MockEventProxy{}
153 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep}
154 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
155 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: nil, Model: "openolt", DeviceId: dh.deviceID}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530156 dh.resourceMgr = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.deviceID, DeviceType: dh.deviceType, DevInfo: deviceInf,
157 KVStore: &db.Backend{
158 Client: &mocks.MockKVClient{},
159 }}
160 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
161 ranges := make(map[string]interface{})
162 sharedIdxByType := make(map[string]string)
163 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
164 sharedIdxByType["ONU_ID"] = "ONU_ID"
165 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
166 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
167 ranges["ONU_ID"] = uint32(0)
168 ranges["GEMPORT_ID"] = uint32(0)
169 ranges["ALLOC_ID"] = uint32(0)
170 ranges["FLOW_ID"] = uint32(0)
171 ranges["onu_id_shared"] = uint32(0)
172 ranges["alloc_id_shared"] = uint32(0)
173 ranges["gemport_id_shared"] = uint32(0)
174 ranges["flow_id_shared"] = uint32(0)
175
176 ponmgr := &ponrmgr.PONResourceManager{
177 DeviceID: "onu-1",
178 IntfIDs: []uint32{1, 2},
179 KVStore: &db.Backend{
180 Client: &mocks.MockKVClient{},
181 },
182 PonResourceRanges: ranges,
183 SharedIdxByType: sharedIdxByType,
184 }
185 dh.resourceMgr.ResourceMgrs[1] = ponmgr
186 dh.resourceMgr.ResourceMgrs[2] = ponmgr
npujarec5762e2020-01-01 14:08:48 +0530187 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
188 defer cancel()
189 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
kdarapu891693b2019-09-16 12:33:49 +0530190 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500191 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530192 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530193 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000194
195 var pmNames = []string{
196 "rx_bytes",
197 "rx_packets",
198 "rx_mcast_packets",
199 "rx_bcast_packets",
200 "tx_bytes",
201 "tx_packets",
202 "tx_mcast_packets",
203 "tx_bcast_packets",
204 }
205
206 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530207 return dh
kdarapu381c6902019-07-31 18:23:16 +0530208}
209
kdarapu891693b2019-09-16 12:33:49 +0530210func negativeDeviceHandler() *DeviceHandler {
211 dh := newMockDeviceHandler()
212 device := dh.device
213 device.Id = ""
214 dh.adminState = "down"
215 return dh
216}
kdarapu381c6902019-07-31 18:23:16 +0530217func Test_generateMacFromHost(t *testing.T) {
218 type args struct {
219 host string
220 }
221 tests := []struct {
222 name string
223 args args
224 want string
225 wantErr bool
226 }{
kdarapu891693b2019-09-16 12:33:49 +0530227 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
228 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
229 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
230 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530231 }
232 for _, tt := range tests {
233 t.Run(tt.name, func(t *testing.T) {
234 got, err := generateMacFromHost(tt.args.host)
235 if (err != nil) != tt.wantErr {
236 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
237 return
238 }
239 if got != tt.want {
240 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
241 }
242 })
243 }
244}
245func Test_macifyIP(t *testing.T) {
246 type args struct {
247 ip net.IP
248 }
249 tests := []struct {
250 name string
251 args args
252 want string
253 }{{
kdarapu891693b2019-09-16 12:33:49 +0530254 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530255 args{ip: net.ParseIP("10.10.10.10")},
256 "00:00:0a:0a:0a:0a",
257 },
258 {
kdarapu891693b2019-09-16 12:33:49 +0530259 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530260 args{ip: net.ParseIP("127.0.0.1")},
261 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530262 },
263 {
264 "macifyIP-3",
265 args{ip: net.ParseIP("127.0.0.1/24")},
266 "",
267 },
268 }
kdarapu381c6902019-07-31 18:23:16 +0530269 for _, tt := range tests {
270 t.Run(tt.name, func(t *testing.T) {
271 if got := macifyIP(tt.args.ip); got != tt.want {
272 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
273 }
274 })
275 }
276}
277
David K. Bainbridge794735f2020-02-11 21:01:37 -0800278func sparseCompare(keys []string, spec, target interface{}) bool {
279 if spec == target {
280 return true
281 }
282 if spec == nil || target == nil {
283 return false
284 }
285 typeSpec := reflect.TypeOf(spec)
286 typeTarget := reflect.TypeOf(target)
287 if typeSpec != typeTarget {
288 return false
289 }
290
291 vSpec := reflect.ValueOf(spec)
292 vTarget := reflect.ValueOf(target)
293 if vSpec.Kind() == reflect.Ptr {
294 vSpec = vSpec.Elem()
295 vTarget = vTarget.Elem()
296 }
297
298 for _, key := range keys {
299 fSpec := vSpec.FieldByName(key)
300 fTarget := vTarget.FieldByName(key)
301 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
302 return false
303 }
304 }
305 return true
306}
307
kdarapu381c6902019-07-31 18:23:16 +0530308func TestDeviceHandler_GetChildDevice(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530309 dh1 := newMockDeviceHandler()
310 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530311 type args struct {
312 parentPort uint32
313 onuID uint32
314 }
315 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530316 name string
317 devicehandler *DeviceHandler
318 args args
319 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800320 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530321 }{
kdarapu891693b2019-09-16 12:33:49 +0530322 {"GetChildDevice-1", dh1,
323 args{parentPort: 1,
324 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800325 &voltha.Device{
326 Id: "1",
327 ParentId: "olt",
328 ParentPortNo: 1,
329 },
330 nil,
kdarapu891693b2019-09-16 12:33:49 +0530331 },
332 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530333 args{parentPort: 1,
334 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800335 nil,
336 reflect.TypeOf(&ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530337 },
338 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800339
340 /*
341 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
342 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>)
343 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
344 */
kdarapu381c6902019-07-31 18:23:16 +0530345 for _, tt := range tests {
346 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800347 got, err := tt.devicehandler.GetChildDevice(tt.args.parentPort, tt.args.onuID)
348 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
349 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
350 tt.want, tt.errType, got, reflect.TypeOf(err))
351 return
352 }
kdarapu381c6902019-07-31 18:23:16 +0530353 t.Log("onu device id", got)
354 })
355 }
356}
kdarapu891693b2019-09-16 12:33:49 +0530357
358func TestGetportLabel(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800359 invalid := reflect.TypeOf(&ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530360 type args struct {
361 portNum uint32
362 portType voltha.Port_PortType
363 }
364 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800365 name string
366 args args
367 want string
368 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530369 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800370 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
371 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
372 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
373 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
374 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
375 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
376 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530377 }
378 for _, tt := range tests {
379 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800380 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
381 if reflect.TypeOf(err) != tt.errType || got != tt.want {
382 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
383 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530384 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800385
kdarapu891693b2019-09-16 12:33:49 +0530386 })
387 }
388}
389
390func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
391 dh := newMockDeviceHandler()
392 proxyAddr := dh.device.ProxyAddress
393 body := &ic.InterAdapterOmciMessage{
394 Message: []byte("asdfasdfasdfasdfas"),
395 ProxyAddress: proxyAddr,
396 }
397 body2 := &ic.InterAdapterOmciMessage{
398 Message: []byte("asdfasdfasdfasdfas"),
399 //ProxyAddress: &voltha.Device_ProxyAddress{},
400 }
401 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
402 var marshalledData *any.Any
403 var err error
404
405 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
406 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
407 }
408
409 var marshalledData1 *any.Any
410
411 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
412 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
413 }
414 var marshalledData2 *any.Any
415
416 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
417 log.Errorw("cannot-marshal-request", log.Fields{"error": err})
418 }
419 type args struct {
420 msg *ic.InterAdapterMessage
421 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800422 invalid := reflect.TypeOf(&ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530423 tests := []struct {
424 name string
425 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800426 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530427 }{
428 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
429 Header: &ic.InterAdapterHeader{
430 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800431 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530432 },
433 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800434 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530435 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
436 Header: &ic.InterAdapterHeader{
437 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800438 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530439 },
440 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800441 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530442 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
443 Header: &ic.InterAdapterHeader{
444 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800445 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530446 },
447 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800448 }}, reflect.TypeOf(&ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530449 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
450 Header: &ic.InterAdapterHeader{
451 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800452 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530453 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800454 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530455 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
456 Header: &ic.InterAdapterHeader{
457 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800458 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530459 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800460 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530461 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
462 Header: &ic.InterAdapterHeader{
463 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800464 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530465 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800466 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530467 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
468 Header: &ic.InterAdapterHeader{
469 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800470 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530471 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800472 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530473 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
474 Header: &ic.InterAdapterHeader{
475 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800476 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530477 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800478 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530479 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
480 Header: &ic.InterAdapterHeader{
481 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800482 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530483 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800484 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530485 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
486 Header: &ic.InterAdapterHeader{
487 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800488 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530489 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800490 }}, invalid},
491 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
492 Header: &ic.InterAdapterHeader{
493 Id: "012345",
494 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
495 }, Body: marshalledData2,
496 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530497 }
498 for _, tt := range tests {
499 t.Run(tt.name, func(t *testing.T) {
500
David K. Bainbridge794735f2020-02-11 21:01:37 -0800501 if err := dh.ProcessInterAdapterMessage(tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530502 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
503 }
504 })
505 }
506}
507
508func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
509 dh1 := newMockDeviceHandler()
510 dh2 := negativeDeviceHandler()
511 device1 := &voltha.Device{
512 Id: "onu1",
513 Root: false,
514 ParentId: "logical_device",
515 ProxyAddress: &voltha.Device_ProxyAddress{
516 DeviceId: "onu1",
517 DeviceType: "onu",
518 ChannelId: 1,
519 ChannelGroupId: 1,
520 },
521 ConnectStatus: 1,
522 }
523 device2 := device1
524 device2.ConnectStatus = 2
525 iaomciMsg1 := &ic.InterAdapterOmciMessage{
526 ProxyAddress: &voltha.Device_ProxyAddress{
527 DeviceId: "onu2",
528 DeviceType: "onu",
529 ChannelId: 1,
530 ChannelGroupId: 1,
531 //OnuId: 2,
532 },
533 ConnectStatus: 1,
534 }
535 iaomciMsg2 := &ic.InterAdapterOmciMessage{
536 ProxyAddress: &voltha.Device_ProxyAddress{
537 DeviceId: "onu3",
538 DeviceType: "onu",
539 ChannelId: 1,
540 ChannelGroupId: 1,
541 },
542 ConnectStatus: 1,
543 }
544 type args struct {
545 onuDevice *voltha.Device
546 omciMsg *ic.InterAdapterOmciMessage
547 }
548 tests := []struct {
549 name string
550 devicehandler *DeviceHandler
551 args args
552 }{
553 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
554 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
555 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
556 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
557 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
558 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
559 }
560 for _, tt := range tests {
561 t.Run(tt.name, func(t *testing.T) {
562 tt.devicehandler.sendProxiedMessage(tt.args.onuDevice, tt.args.omciMsg)
563 })
564 }
565}
566
567func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
568 dh1 := newMockDeviceHandler()
569 dh2 := negativeDeviceHandler()
570
571 type args struct {
572 logicalPort uint32
573 packetPayload []byte
574 }
575 tests := []struct {
576 name string
577 devicehandler *DeviceHandler
578 args args
579 }{
580 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
581 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
582 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
583 }
584 for _, tt := range tests {
585 t.Run(tt.name, func(t *testing.T) {
586 tt.devicehandler.SendPacketInToCore(tt.args.logicalPort, tt.args.packetPayload)
587 })
588 }
589}
590
591func TestDeviceHandler_DisableDevice(t *testing.T) {
592 dh1 := newMockDeviceHandler()
593 dh2 := negativeDeviceHandler()
594 type args struct {
595 device *voltha.Device
596 }
597 tests := []struct {
598 name string
599 devicehandler *DeviceHandler
600 args args
601 wantErr bool
602 }{
603 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400604 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530605 }
606 for _, tt := range tests {
607 t.Run(tt.name, func(t *testing.T) {
608
609 if err := tt.devicehandler.DisableDevice(tt.args.device); (err != nil) != tt.wantErr {
610 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
611 }
612 })
613 }
614}
615
616func TestDeviceHandler_ReenableDevice(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 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
629 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
630 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
631 }
632 for _, tt := range tests {
633 t.Run(tt.name, func(t *testing.T) {
634 dh := tt.devicehandler
635 if err := dh.ReenableDevice(tt.args.device); (err != nil) != tt.wantErr {
636 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
637 }
638 })
639 }
640}
641
642func TestDeviceHandler_RebootDevice(t *testing.T) {
643 dh1 := newMockDeviceHandler()
644 dh2 := newMockDeviceHandler()
645 type args struct {
646 device *voltha.Device
647 }
648 tests := []struct {
649 name string
650 devicehandler *DeviceHandler
651 args args
652 wantErr bool
653 }{
654 // TODO: Add test cases.
655 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
656 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
657 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
658 }
659 for _, tt := range tests {
660 t.Run(tt.name, func(t *testing.T) {
661
662 if err := tt.devicehandler.RebootDevice(tt.args.device); (err != nil) != tt.wantErr {
663 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
664 }
665 })
666 }
667}
668
669func TestDeviceHandler_handleIndication(t *testing.T) {
670 dh1 := newMockDeviceHandler()
671 dh2 := negativeDeviceHandler()
672 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530673 dh3.onus = sync.Map{}
674 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1"))
675 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2"))
676
kdarapu891693b2019-09-16 12:33:49 +0530677 type args struct {
678 indication *oop.Indication
679 }
680 tests := []struct {
681 name string
682 deviceHandler *DeviceHandler
683 args args
684 }{
685 // TODO: Add test cases.
686 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
687 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
688 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
689 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
690 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
691 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
692 {"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")}}}}}},
693 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
694 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
695 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
696 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
697 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
698 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
699 {"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}}}}},
700 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
701 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
702 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
703 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
704
705 // Negative testcases
706 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
707 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
708 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
709 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
710 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
711 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
712 {"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")}}}}}},
713 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
714 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
715 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
716 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
717 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
718 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
719 {"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}}}}},
720 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
721 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
722 //
723 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
724 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
725 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
726 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
727 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
728 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
729 }
730 for _, tt := range tests {
731 t.Run(tt.name, func(t *testing.T) {
732 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530733 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
734 defer cancel()
735 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530736 })
737 }
738}
739
740func TestDeviceHandler_addPort(t *testing.T) {
741 dh1 := newMockDeviceHandler()
742 dh2 := negativeDeviceHandler()
743 type args struct {
744 intfID uint32
745 portType voltha.Port_PortType
746 state string
747 }
748 tests := []struct {
749 name string
750 devicehandler *DeviceHandler
751 args args
752 }{
753 // State up
754 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
755 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
756 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
757 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
758 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
759 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
760 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
761 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
762 // state discovery
763 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
764 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
765 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
766 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
767 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
768 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
769 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
770 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
771
772 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
773 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
774 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
775 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
776 }
777 for _, tt := range tests {
778 t.Run(tt.name, func(t *testing.T) {
779 tt.devicehandler.addPort(tt.args.intfID, tt.args.portType, tt.args.state)
780 })
781 }
782}
783
784func Test_macAddressToUint32Array(t *testing.T) {
785 type args struct {
786 mac string
787 }
788 tests := []struct {
789 name string
790 args args
791 want []uint32
792 }{
793 // TODO: Add test cases.
794 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
795 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
796 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
797 }
798 for _, tt := range tests {
799 t.Run(tt.name, func(t *testing.T) {
800 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
801 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
802 }
803 })
804 }
805}
806
807func TestDeviceHandler_handleOltIndication(t *testing.T) {
808
809 type args struct {
810 oltIndication *oop.OltIndication
811 }
812 tests := []struct {
813 name string
814 args args
815 }{
816 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
817 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
818 }
819 for _, tt := range tests {
820 t.Run(tt.name, func(t *testing.T) {
821 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530822 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
823 defer cancel()
824 dh.handleOltIndication(ctx, tt.args.oltIndication)
kdarapu891693b2019-09-16 12:33:49 +0530825 })
826 }
827}
828
829func TestDeviceHandler_AdoptDevice(t *testing.T) {
830 dh1 := newMockDeviceHandler()
831 dh2 := negativeDeviceHandler()
832 type args struct {
833 device *voltha.Device
834 }
835 tests := []struct {
836 name string
837 devicehandler *DeviceHandler
838 args args
839 }{
840 // TODO: Add test cases.
841 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530842 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530843 }
844 for _, tt := range tests {
845 t.Run(tt.name, func(t *testing.T) {
846 //dh.doStateInit()
847 // context.
848 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530849 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
850 defer cancel()
851 tt.devicehandler.postInit(ctx)
kdarapu891693b2019-09-16 12:33:49 +0530852 })
853 }
854}
855
856func TestDeviceHandler_activateONU(t *testing.T) {
857 dh := newMockDeviceHandler()
858 dh1 := negativeDeviceHandler()
859 type args struct {
860 intfID uint32
861 onuID int64
862 serialNum *oop.SerialNumber
863 serialNumber string
864 }
865 tests := []struct {
866 name string
867 devicehandler *DeviceHandler
868 args args
869 }{
870 {"activateONU-1", dh, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
871 {"activateONU-2", dh, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
872 {"activateONU-3", dh1, args{intfID: 1, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
873 {"activateONU-4", dh1, args{intfID: 2, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
874 }
875 for _, tt := range tests {
876 t.Run(tt.name, func(t *testing.T) {
877
npujarec5762e2020-01-01 14:08:48 +0530878 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
879 defer cancel()
880 tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID,
kdarapu891693b2019-09-16 12:33:49 +0530881 tt.args.serialNum, tt.args.serialNumber)
882 })
883 }
884}
885
886func TestDeviceHandler_start(t *testing.T) {
887 dh := newMockDeviceHandler()
888 dh1 := negativeDeviceHandler()
889 dh.start(context.Background())
890 dh.stop(context.Background())
891
892 dh1.start(context.Background())
893 dh1.stop(context.Background())
894
895}
896
897func TestDeviceHandler_PacketOut(t *testing.T) {
898 dh1 := newMockDeviceHandler()
899 dh2 := negativeDeviceHandler()
900 acts := []*ofp.OfpAction{
901 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
902 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
903 fu.Output(1),
904 }
905 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
906 type args struct {
907 egressPortNo int
908 packet *of.OfpPacketOut
909 }
910 tests := []struct {
911 name string
912 devicehandler *DeviceHandler
913 args args
914 wantErr bool
915 }{
916 // TODO: Add test cases.
917 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
918 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
919 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
920 {"PacketOut-2", dh2, args{egressPortNo: 115000, packet: pktout}, false},
921 {"PacketOut-3", dh1, args{egressPortNo: 65536, packet: pktout}, false},
922 {"PacketOut-4", dh2, args{egressPortNo: 65535, packet: pktout}, false},
923 }
924 for _, tt := range tests {
925 t.Run(tt.name, func(t *testing.T) {
926 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530927 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
928 defer cancel()
929 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530930 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
931 }
932 })
933 }
934}
935
936//
937func TestDeviceHandler_doStateUp(t *testing.T) {
938 dh1 := newMockDeviceHandler()
939 dh2 := newMockDeviceHandler()
940
941 dh2.deviceID = ""
942 dh3 := negativeDeviceHandler()
943
944 tests := []struct {
945 name string
946 devicehandler *DeviceHandler
947 wantErr bool
948 }{
949 {"dostateup-1", dh1, false},
950 {"dostateup-2", dh2, false},
951 {"dostateup-3", dh3, true},
952 }
953 for _, tt := range tests {
954 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530955 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
956 defer cancel()
957 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530958 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
959 }
960 })
961 }
962}
963func TestDeviceHandler_doStateDown(t *testing.T) {
964 dh1 := newMockDeviceHandler()
965 dh2 := negativeDeviceHandler()
966 dh3 := newMockDeviceHandler()
967 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
968 tests := []struct {
969 name string
970 devicehandler *DeviceHandler
971 wantErr bool
972 }{
973 {"dostatedown-1", dh1, false},
974 {"dostatedown-2", dh2, true},
975 {"dostatedown-2", dh3, true},
976 }
977 for _, tt := range tests {
978 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530979 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
980 defer cancel()
981 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530982 t.Logf("DeviceHandler.doStateDown() error = %v", err)
983 }
984 })
985 }
986}
987
988func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
989 dh1 := newMockDeviceHandler()
990 dh2 := negativeDeviceHandler()
991 type args struct {
992 device *voltha.Device
993 }
994 tests := []struct {
995 name string
996 devicehandler *DeviceHandler
997 args args
998 wantErr bool
999 }{
1000 // TODO: Add test cases.
1001 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1002 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1003 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1004 }
1005 for _, tt := range tests {
1006 t.Run(tt.name, func(t *testing.T) {
1007 dh := tt.devicehandler
1008 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1009 if (err != nil) != tt.wantErr {
1010 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1011 return
1012 }
1013 })
1014 }
1015}
1016
1017func TestDeviceHandler_GetOfpPortInfo(t *testing.T) {
1018 dh1 := newMockDeviceHandler()
1019 dh2 := negativeDeviceHandler()
1020 type args struct {
1021 device *voltha.Device
1022 portNo int64
1023 }
1024 tests := []struct {
1025 name string
1026 devicehandler *DeviceHandler
1027 args args
1028 wantErr bool
1029 }{
1030 {"GetOfpPortInfo-1", dh1, args{device: dh1.device, portNo: 1}, false},
1031 {"GetOfpPortInfo-2", dh2, args{device: dh2.device, portNo: 1}, false},
1032 {"GetOfpPortInfo-3", dh1, args{device: dh1.device, portNo: 0}, false},
1033 {"GetOfpPortInfo-4", dh2, args{device: dh2.device, portNo: 0}, false},
1034 {"GetOfpPortInfo-5", dh1, args{device: &voltha.Device{}, portNo: 1}, false},
1035 {"GetOfpPortInfo-6", dh2, args{device: &voltha.Device{}, portNo: 0}, false},
1036 // TODO: Add test cases.
1037 }
1038 for _, tt := range tests {
1039 t.Run(tt.name, func(t *testing.T) {
1040 dh := tt.devicehandler
1041 _, err := dh.GetOfpPortInfo(tt.args.device, tt.args.portNo)
1042 if (err != nil) != tt.wantErr {
1043 t.Errorf("DeviceHandler.GetOfpPortInfo() error = %v, wantErr %v", err, tt.wantErr)
1044 return
1045 }
1046 })
1047 }
1048}
1049
1050func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1051
1052 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301053 dh1.discOnus = sync.Map{}
1054 dh1.discOnus.Store("onu1", true)
1055 dh1.discOnus.Store("onu2", false)
kdarapu891693b2019-09-16 12:33:49 +05301056 dh2 := negativeDeviceHandler()
1057 type args struct {
1058 onuDiscInd *oop.OnuDiscIndication
1059 sn string
1060 }
1061 tests := []struct {
1062 name string
1063 devicehandler *DeviceHandler
1064 args args
1065 }{
1066 // TODO: Add test cases.
1067 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1068 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1069 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1070 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1071 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1072 {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}},
1073 {"onuDiscIndication-7", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1074 }
1075 for _, tt := range tests {
1076 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301077 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1078 defer cancel()
1079 tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
kdarapu891693b2019-09-16 12:33:49 +05301080 })
1081 }
1082}
1083
1084func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1085 dh1 := newMockDeviceHandler()
1086 dh2 := negativeDeviceHandler()
1087 tests := []struct {
1088 name string
1089 devicehandler *DeviceHandler
1090
1091 wantErr bool
1092 }{
1093 // TODO: Add test cases.
1094 {"populateDeviceInfo-1", dh1, false},
1095 {"populateDeviceInfo-2", dh1, true},
1096 {"populateDeviceInfo-3", dh1, true},
1097 {"populateDeviceInfo-4", dh1, true},
1098 {"populateDeviceInfo-5", dh2, true},
1099 }
1100 for _, tt := range tests {
1101 t.Run(tt.name, func(t *testing.T) {
1102
1103 _, err := tt.devicehandler.populateDeviceInfo()
1104 if (err != nil) != tt.wantErr {
1105 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1106 return
1107 }
1108
1109 })
1110 }
1111}
1112
1113func TestDeviceHandler_readIndications(t *testing.T) {
1114 dh1 := newMockDeviceHandler()
1115 dh2 := newMockDeviceHandler()
1116 dh2.adminState = "down"
1117 dh3 := newMockDeviceHandler()
1118 dh3.device.AdminState = voltha.AdminState_DISABLED
1119 dh4 := negativeDeviceHandler()
1120 tests := []struct {
1121 name string
1122 devicehandler *DeviceHandler
1123 }{
1124 // TODO: Add test cases.
1125 {"readIndications-1", dh1},
1126 {"readIndications-2", dh2},
1127 {"readIndications-3", dh2},
1128 {"readIndications-4", dh2},
1129 {"readIndications-5", dh2},
1130 {"readIndications-6", dh3},
1131 {"readIndications-7", dh3},
1132 {"readIndications-8", dh4},
1133 }
1134 for _, tt := range tests {
1135 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301136 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1137 defer cancel()
1138 tt.devicehandler.readIndications(ctx)
kdarapu891693b2019-09-16 12:33:49 +05301139 })
1140 }
1141}
Naga Manjunath7615e552019-10-11 22:35:47 +05301142
1143func Test_startCollector(t *testing.T) {
1144 type args struct {
1145 dh *DeviceHandler
1146 }
1147 dh := newMockDeviceHandler()
1148 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
1149 dh.portStats.NorthBoundPort[0] = &NniPort{Name: "OLT-1"}
1150 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1151 dh.portStats.Device = dh
1152 for i := 0; i < 16; i++ {
1153 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1154 }
1155 tests := []struct {
1156 name string
1157 args args
1158 }{
1159 // TODO: Add test cases.
1160 {"StartCollector-1", args{dh}},
1161 }
1162 for _, tt := range tests {
1163 t.Run(tt.name, func(t *testing.T) {
1164 go func() {
Amit Ghoshd4cbe482019-11-21 12:07:14 +00001165 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 +05301166 tt.args.dh.stopCollector <- true
1167 }()
1168 startCollector(tt.args.dh)
1169 })
1170 }
1171}