blob: 531c27b65c27e89c17f470c82e610b5c3d931524 [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 newMockCoreProxy() *mocks.MockCoreProxy {
47 mcp := mocks.MockCoreProxy{}
48 mcp.Devices = make(map[string]*voltha.Device)
Naga Manjunath7615e552019-10-11 22:35:47 +053049 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053050 mcp.Devices["olt"] = &voltha.Device{
51
52 Id: "olt",
53 Root: true,
54 ParentId: "logical_device",
55 ParentPortNo: 1,
56
57 Ports: []*voltha.Port{
58 {PortNo: 1, Label: "pon"},
59 {PortNo: 2, Label: "nni"},
60 },
61 ProxyAddress: &voltha.Device_ProxyAddress{
62 DeviceId: "olt",
63 DeviceType: "onu",
64 ChannelId: 1,
65 ChannelGroupId: 1,
66 },
67 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053068 PmConfigs: &voltha.PmConfigs{
69 DefaultFreq: 10,
70 Id: "olt",
71 FreqOverride: false,
72 Grouped: false,
73 Metrics: pm,
74 },
kdarapu891693b2019-09-16 12:33:49 +053075 }
76 mcp.Devices["onu1"] = &voltha.Device{
77
78 Id: "1",
79 Root: false,
80 ParentId: "olt",
81 ParentPortNo: 1,
82
83 Ports: []*voltha.Port{
84 {PortNo: 1, Label: "pon"},
85 {PortNo: 2, Label: "uni"},
86 },
87 OperStatus: 4,
88 ProxyAddress: &voltha.Device_ProxyAddress{
89 OnuId: 1,
90 ChannelId: 1,
91 ChannelGroupId: 1,
92 },
93 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053094 PmConfigs: &voltha.PmConfigs{
95 DefaultFreq: 10,
96 Id: "olt",
97 FreqOverride: false,
98 Grouped: false,
99 Metrics: pm,
100 },
kdarapu891693b2019-09-16 12:33:49 +0530101 }
102 mcp.Devices["onu2"] = &voltha.Device{
103 Id: "2",
104 Root: false,
105 ParentId: "olt",
106 OperStatus: 2,
107 Ports: []*voltha.Port{
108 {PortNo: 1, Label: "pon"},
109 {PortNo: 2, Label: "uni"},
110 },
111
112 ParentPortNo: 1,
113
114 ProxyAddress: &voltha.Device_ProxyAddress{
115 OnuId: 2,
116 ChannelId: 1,
117 ChannelGroupId: 1,
118 },
119 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530120 PmConfigs: &voltha.PmConfigs{
121 DefaultFreq: 10,
122 Id: "olt",
123 FreqOverride: false,
124 Grouped: false,
125 Metrics: pm,
126 },
kdarapu891693b2019-09-16 12:33:49 +0530127 }
128 return &mcp
129}
130func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530131 device := &voltha.Device{
132 Id: "olt",
133 Root: true,
134 ParentId: "logical_device",
135 Ports: []*voltha.Port{
kdarapu1afeceb2020-02-12 01:38:09 -0500136 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
137 {PortNo: 2, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
kdarapu381c6902019-07-31 18:23:16 +0530138 },
kdarapu891693b2019-09-16 12:33:49 +0530139 ProxyAddress: &voltha.Device_ProxyAddress{
140 DeviceId: "olt",
141 DeviceType: "onu",
142 ChannelId: 1,
143 ChannelGroupId: 1,
144 },
145 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530146 }
kdarapu891693b2019-09-16 12:33:49 +0530147 cp := newMockCoreProxy()
148 ap := &mocks.MockAdapterProxy{}
149 ep := &mocks.MockEventProxy{}
150 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep}
151 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700152 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
153 IntfIds: []uint32{0, 1},
154 Technology: "xgs-pon",
155 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
156 }}
157
Thomas Lee S985938d2020-05-04 11:40:41 +0530158 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: 2}
159 rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530160 KVStore: &db.Backend{
161 Client: &mocks.MockKVClient{},
162 }}
Girish Gowdra38d533d2020-03-30 20:38:51 -0700163 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
164 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
165 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
166
167 dh.resourceMgr = &rsrMgr
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530168 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
169 ranges := make(map[string]interface{})
170 sharedIdxByType := make(map[string]string)
171 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
172 sharedIdxByType["ONU_ID"] = "ONU_ID"
173 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
174 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
175 ranges["ONU_ID"] = uint32(0)
176 ranges["GEMPORT_ID"] = uint32(0)
177 ranges["ALLOC_ID"] = uint32(0)
178 ranges["FLOW_ID"] = uint32(0)
179 ranges["onu_id_shared"] = uint32(0)
180 ranges["alloc_id_shared"] = uint32(0)
181 ranges["gemport_id_shared"] = uint32(0)
182 ranges["flow_id_shared"] = uint32(0)
183
184 ponmgr := &ponrmgr.PONResourceManager{
185 DeviceID: "onu-1",
Girish Gowdra38d533d2020-03-30 20:38:51 -0700186 IntfIDs: []uint32{0, 1},
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530187 KVStore: &db.Backend{
188 Client: &mocks.MockKVClient{},
189 },
190 PonResourceRanges: ranges,
191 SharedIdxByType: sharedIdxByType,
192 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700193 dh.resourceMgr.ResourceMgrs[0] = ponmgr
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530194 dh.resourceMgr.ResourceMgrs[1] = ponmgr
npujarec5762e2020-01-01 14:08:48 +0530195 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
196 defer cancel()
197 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
kdarapu891693b2019-09-16 12:33:49 +0530198 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500199 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530200 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530201 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000202
203 var pmNames = []string{
204 "rx_bytes",
205 "rx_packets",
206 "rx_mcast_packets",
207 "rx_bcast_packets",
208 "tx_bytes",
209 "tx_packets",
210 "tx_mcast_packets",
211 "tx_bcast_packets",
212 }
213
214 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530215 return dh
kdarapu381c6902019-07-31 18:23:16 +0530216}
217
kdarapu891693b2019-09-16 12:33:49 +0530218func negativeDeviceHandler() *DeviceHandler {
219 dh := newMockDeviceHandler()
220 device := dh.device
221 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530222 return dh
223}
kdarapu381c6902019-07-31 18:23:16 +0530224func Test_generateMacFromHost(t *testing.T) {
225 type args struct {
226 host string
227 }
228 tests := []struct {
229 name string
230 args args
231 want string
232 wantErr bool
233 }{
kdarapu891693b2019-09-16 12:33:49 +0530234 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
235 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
236 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
237 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530238 }
239 for _, tt := range tests {
240 t.Run(tt.name, func(t *testing.T) {
241 got, err := generateMacFromHost(tt.args.host)
242 if (err != nil) != tt.wantErr {
243 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
244 return
245 }
246 if got != tt.want {
247 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
248 }
249 })
250 }
251}
252func Test_macifyIP(t *testing.T) {
253 type args struct {
254 ip net.IP
255 }
256 tests := []struct {
257 name string
258 args args
259 want string
260 }{{
kdarapu891693b2019-09-16 12:33:49 +0530261 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530262 args{ip: net.ParseIP("10.10.10.10")},
263 "00:00:0a:0a:0a:0a",
264 },
265 {
kdarapu891693b2019-09-16 12:33:49 +0530266 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530267 args{ip: net.ParseIP("127.0.0.1")},
268 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530269 },
270 {
271 "macifyIP-3",
272 args{ip: net.ParseIP("127.0.0.1/24")},
273 "",
274 },
275 }
kdarapu381c6902019-07-31 18:23:16 +0530276 for _, tt := range tests {
277 t.Run(tt.name, func(t *testing.T) {
278 if got := macifyIP(tt.args.ip); got != tt.want {
279 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
280 }
281 })
282 }
283}
284
David K. Bainbridge794735f2020-02-11 21:01:37 -0800285func sparseCompare(keys []string, spec, target interface{}) bool {
286 if spec == target {
287 return true
288 }
289 if spec == nil || target == nil {
290 return false
291 }
292 typeSpec := reflect.TypeOf(spec)
293 typeTarget := reflect.TypeOf(target)
294 if typeSpec != typeTarget {
295 return false
296 }
297
298 vSpec := reflect.ValueOf(spec)
299 vTarget := reflect.ValueOf(target)
300 if vSpec.Kind() == reflect.Ptr {
301 vSpec = vSpec.Elem()
302 vTarget = vTarget.Elem()
303 }
304
305 for _, key := range keys {
306 fSpec := vSpec.FieldByName(key)
307 fTarget := vTarget.FieldByName(key)
308 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
309 return false
310 }
311 }
312 return true
313}
314
kdarapu381c6902019-07-31 18:23:16 +0530315func TestDeviceHandler_GetChildDevice(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530316 dh1 := newMockDeviceHandler()
317 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530318 type args struct {
319 parentPort uint32
320 onuID uint32
321 }
322 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530323 name string
324 devicehandler *DeviceHandler
325 args args
326 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800327 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530328 }{
kdarapu891693b2019-09-16 12:33:49 +0530329 {"GetChildDevice-1", dh1,
330 args{parentPort: 1,
331 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800332 &voltha.Device{
333 Id: "1",
334 ParentId: "olt",
335 ParentPortNo: 1,
336 },
337 nil,
kdarapu891693b2019-09-16 12:33:49 +0530338 },
339 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530340 args{parentPort: 1,
341 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800342 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530343 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530344 },
345 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800346
347 /*
348 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
349 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>)
350 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
351 */
kdarapu381c6902019-07-31 18:23:16 +0530352 for _, tt := range tests {
353 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800354 got, err := tt.devicehandler.GetChildDevice(tt.args.parentPort, tt.args.onuID)
355 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
356 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
357 tt.want, tt.errType, got, reflect.TypeOf(err))
358 return
359 }
kdarapu381c6902019-07-31 18:23:16 +0530360 t.Log("onu device id", got)
361 })
362 }
363}
kdarapu891693b2019-09-16 12:33:49 +0530364
365func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530366 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530367 type args struct {
368 portNum uint32
369 portType voltha.Port_PortType
370 }
371 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800372 name string
373 args args
374 want string
375 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530376 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800377 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
378 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
379 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
380 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
381 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
382 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
383 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530384 }
385 for _, tt := range tests {
386 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800387 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
388 if reflect.TypeOf(err) != tt.errType || got != tt.want {
389 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
390 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530391 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800392
kdarapu891693b2019-09-16 12:33:49 +0530393 })
394 }
395}
396
397func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
398 dh := newMockDeviceHandler()
399 proxyAddr := dh.device.ProxyAddress
400 body := &ic.InterAdapterOmciMessage{
401 Message: []byte("asdfasdfasdfasdfas"),
402 ProxyAddress: proxyAddr,
403 }
404 body2 := &ic.InterAdapterOmciMessage{
405 Message: []byte("asdfasdfasdfasdfas"),
406 //ProxyAddress: &voltha.Device_ProxyAddress{},
407 }
408 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
409 var marshalledData *any.Any
410 var err error
411
412 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000413 logger.Errorw("cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530414 }
415
416 var marshalledData1 *any.Any
417
418 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000419 logger.Errorw("cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530420 }
421 var marshalledData2 *any.Any
422
423 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000424 logger.Errorw("cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530425 }
426 type args struct {
427 msg *ic.InterAdapterMessage
428 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530429 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530430 tests := []struct {
431 name string
432 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800433 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530434 }{
435 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
436 Header: &ic.InterAdapterHeader{
437 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800438 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530439 },
440 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800441 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530442 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
443 Header: &ic.InterAdapterHeader{
444 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800445 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530446 },
447 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800448 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530449 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
450 Header: &ic.InterAdapterHeader{
451 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800452 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530453 },
454 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530455 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530456 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
457 Header: &ic.InterAdapterHeader{
458 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800459 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530460 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800461 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530462 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
463 Header: &ic.InterAdapterHeader{
464 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800465 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530466 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800467 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530468 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
469 Header: &ic.InterAdapterHeader{
470 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800471 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
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-7", 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_REQUEST,
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-8", args{msg: &ic.InterAdapterMessage{
481 Header: &ic.InterAdapterHeader{
482 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800483 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
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-9", args{msg: &ic.InterAdapterMessage{
487 Header: &ic.InterAdapterHeader{
488 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800489 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530490 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800491 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530492 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
493 Header: &ic.InterAdapterHeader{
494 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800495 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530496 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800497 }}, invalid},
498 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
499 Header: &ic.InterAdapterHeader{
500 Id: "012345",
501 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
502 }, Body: marshalledData2,
503 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530504 }
505 for _, tt := range tests {
506 t.Run(tt.name, func(t *testing.T) {
507
David K. Bainbridge794735f2020-02-11 21:01:37 -0800508 if err := dh.ProcessInterAdapterMessage(tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530509 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
510 }
511 })
512 }
513}
514
515func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
516 dh1 := newMockDeviceHandler()
517 dh2 := negativeDeviceHandler()
518 device1 := &voltha.Device{
519 Id: "onu1",
520 Root: false,
521 ParentId: "logical_device",
522 ProxyAddress: &voltha.Device_ProxyAddress{
523 DeviceId: "onu1",
524 DeviceType: "onu",
525 ChannelId: 1,
526 ChannelGroupId: 1,
527 },
528 ConnectStatus: 1,
529 }
530 device2 := device1
531 device2.ConnectStatus = 2
532 iaomciMsg1 := &ic.InterAdapterOmciMessage{
533 ProxyAddress: &voltha.Device_ProxyAddress{
534 DeviceId: "onu2",
535 DeviceType: "onu",
536 ChannelId: 1,
537 ChannelGroupId: 1,
538 //OnuId: 2,
539 },
540 ConnectStatus: 1,
541 }
542 iaomciMsg2 := &ic.InterAdapterOmciMessage{
543 ProxyAddress: &voltha.Device_ProxyAddress{
544 DeviceId: "onu3",
545 DeviceType: "onu",
546 ChannelId: 1,
547 ChannelGroupId: 1,
548 },
549 ConnectStatus: 1,
550 }
551 type args struct {
552 onuDevice *voltha.Device
553 omciMsg *ic.InterAdapterOmciMessage
554 }
555 tests := []struct {
556 name string
557 devicehandler *DeviceHandler
558 args args
559 }{
560 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
561 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
562 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
563 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
564 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
565 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
566 }
567 for _, tt := range tests {
568 t.Run(tt.name, func(t *testing.T) {
569 tt.devicehandler.sendProxiedMessage(tt.args.onuDevice, tt.args.omciMsg)
570 })
571 }
572}
573
574func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
575 dh1 := newMockDeviceHandler()
576 dh2 := negativeDeviceHandler()
577
578 type args struct {
579 logicalPort uint32
580 packetPayload []byte
581 }
582 tests := []struct {
583 name string
584 devicehandler *DeviceHandler
585 args args
586 }{
587 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
588 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
589 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
590 }
591 for _, tt := range tests {
592 t.Run(tt.name, func(t *testing.T) {
593 tt.devicehandler.SendPacketInToCore(tt.args.logicalPort, tt.args.packetPayload)
594 })
595 }
596}
597
598func TestDeviceHandler_DisableDevice(t *testing.T) {
599 dh1 := newMockDeviceHandler()
600 dh2 := negativeDeviceHandler()
601 type args struct {
602 device *voltha.Device
603 }
604 tests := []struct {
605 name string
606 devicehandler *DeviceHandler
607 args args
608 wantErr bool
609 }{
610 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400611 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530612 }
613 for _, tt := range tests {
614 t.Run(tt.name, func(t *testing.T) {
615
616 if err := tt.devicehandler.DisableDevice(tt.args.device); (err != nil) != tt.wantErr {
617 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
618 }
619 })
620 }
621}
622
623func TestDeviceHandler_ReenableDevice(t *testing.T) {
624 dh1 := newMockDeviceHandler()
625 dh2 := negativeDeviceHandler()
626 type args struct {
627 device *voltha.Device
628 }
629 tests := []struct {
630 name string
631 devicehandler *DeviceHandler
632 args args
633 wantErr bool
634 }{
635 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
636 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
637 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
638 }
639 for _, tt := range tests {
640 t.Run(tt.name, func(t *testing.T) {
641 dh := tt.devicehandler
642 if err := dh.ReenableDevice(tt.args.device); (err != nil) != tt.wantErr {
643 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
644 }
645 })
646 }
647}
648
649func TestDeviceHandler_RebootDevice(t *testing.T) {
650 dh1 := newMockDeviceHandler()
651 dh2 := newMockDeviceHandler()
652 type args struct {
653 device *voltha.Device
654 }
655 tests := []struct {
656 name string
657 devicehandler *DeviceHandler
658 args args
659 wantErr bool
660 }{
661 // TODO: Add test cases.
662 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
663 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
664 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
665 }
666 for _, tt := range tests {
667 t.Run(tt.name, func(t *testing.T) {
668
669 if err := tt.devicehandler.RebootDevice(tt.args.device); (err != nil) != tt.wantErr {
670 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
671 }
672 })
673 }
674}
675
676func TestDeviceHandler_handleIndication(t *testing.T) {
677 dh1 := newMockDeviceHandler()
678 dh2 := negativeDeviceHandler()
679 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530680 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530681 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
682 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530683
kdarapu891693b2019-09-16 12:33:49 +0530684 type args struct {
685 indication *oop.Indication
686 }
687 tests := []struct {
688 name string
689 deviceHandler *DeviceHandler
690 args args
691 }{
692 // TODO: Add test cases.
693 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
694 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
695 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
696 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
697 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
698 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
699 {"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")}}}}}},
700 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
701 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
702 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
703 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
704 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
705 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
706 {"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}}}}},
707 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
708 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
709 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
710 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
711
712 // Negative testcases
713 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
714 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
715 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
716 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
717 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
718 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
719 {"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")}}}}}},
720 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
721 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
722 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
723 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
724 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
725 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
726 {"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}}}}},
727 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
728 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
729 //
730 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
731 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
732 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
733 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
734 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
735 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
736 }
737 for _, tt := range tests {
738 t.Run(tt.name, func(t *testing.T) {
739 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530740 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
741 defer cancel()
742 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530743 })
744 }
745}
746
747func TestDeviceHandler_addPort(t *testing.T) {
748 dh1 := newMockDeviceHandler()
749 dh2 := negativeDeviceHandler()
750 type args struct {
751 intfID uint32
752 portType voltha.Port_PortType
753 state string
754 }
755 tests := []struct {
756 name string
757 devicehandler *DeviceHandler
758 args args
759 }{
760 // State up
761 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
762 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
763 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
764 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
765 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
766 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
767 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
768 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
769 // state discovery
770 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
771 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
772 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
773 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
774 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
775 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
776 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
777 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
778
779 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
780 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
781 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
782 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
783 }
784 for _, tt := range tests {
785 t.Run(tt.name, func(t *testing.T) {
786 tt.devicehandler.addPort(tt.args.intfID, tt.args.portType, tt.args.state)
787 })
788 }
789}
790
791func Test_macAddressToUint32Array(t *testing.T) {
792 type args struct {
793 mac string
794 }
795 tests := []struct {
796 name string
797 args args
798 want []uint32
799 }{
800 // TODO: Add test cases.
801 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
802 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
803 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
804 }
805 for _, tt := range tests {
806 t.Run(tt.name, func(t *testing.T) {
807 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
808 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
809 }
810 })
811 }
812}
813
814func TestDeviceHandler_handleOltIndication(t *testing.T) {
815
816 type args struct {
817 oltIndication *oop.OltIndication
818 }
819 tests := []struct {
820 name string
821 args args
822 }{
823 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
824 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
825 }
826 for _, tt := range tests {
827 t.Run(tt.name, func(t *testing.T) {
828 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530829 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
830 defer cancel()
831 dh.handleOltIndication(ctx, tt.args.oltIndication)
kdarapu891693b2019-09-16 12:33:49 +0530832 })
833 }
834}
835
836func TestDeviceHandler_AdoptDevice(t *testing.T) {
837 dh1 := newMockDeviceHandler()
838 dh2 := negativeDeviceHandler()
839 type args struct {
840 device *voltha.Device
841 }
842 tests := []struct {
843 name string
844 devicehandler *DeviceHandler
845 args args
846 }{
847 // TODO: Add test cases.
848 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530849 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530850 }
851 for _, tt := range tests {
852 t.Run(tt.name, func(t *testing.T) {
853 //dh.doStateInit()
854 // context.
855 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530856 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
857 defer cancel()
858 tt.devicehandler.postInit(ctx)
kdarapu891693b2019-09-16 12:33:49 +0530859 })
860 }
861}
862
863func TestDeviceHandler_activateONU(t *testing.T) {
864 dh := newMockDeviceHandler()
865 dh1 := negativeDeviceHandler()
866 type args struct {
867 intfID uint32
868 onuID int64
869 serialNum *oop.SerialNumber
870 serialNumber string
871 }
872 tests := []struct {
873 name string
874 devicehandler *DeviceHandler
875 args args
876 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700877 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
878 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
879 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
880 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530881 }
882 for _, tt := range tests {
883 t.Run(tt.name, func(t *testing.T) {
884
npujarec5762e2020-01-01 14:08:48 +0530885 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
886 defer cancel()
887 tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID,
kdarapu891693b2019-09-16 12:33:49 +0530888 tt.args.serialNum, tt.args.serialNumber)
889 })
890 }
891}
892
893func TestDeviceHandler_start(t *testing.T) {
894 dh := newMockDeviceHandler()
895 dh1 := negativeDeviceHandler()
896 dh.start(context.Background())
897 dh.stop(context.Background())
898
899 dh1.start(context.Background())
900 dh1.stop(context.Background())
901
902}
903
904func TestDeviceHandler_PacketOut(t *testing.T) {
905 dh1 := newMockDeviceHandler()
906 dh2 := negativeDeviceHandler()
907 acts := []*ofp.OfpAction{
908 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
909 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
910 fu.Output(1),
911 }
912 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
913 type args struct {
914 egressPortNo int
915 packet *of.OfpPacketOut
916 }
917 tests := []struct {
918 name string
919 devicehandler *DeviceHandler
920 args args
921 wantErr bool
922 }{
923 // TODO: Add test cases.
924 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
925 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
926 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700927 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
928 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
929 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530930 }
931 for _, tt := range tests {
932 t.Run(tt.name, func(t *testing.T) {
933 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530934 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
935 defer cancel()
936 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530937 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
938 }
939 })
940 }
941}
942
943//
944func TestDeviceHandler_doStateUp(t *testing.T) {
945 dh1 := newMockDeviceHandler()
946 dh2 := newMockDeviceHandler()
947
Thomas Lee S985938d2020-05-04 11:40:41 +0530948 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530949 dh3 := negativeDeviceHandler()
950
951 tests := []struct {
952 name string
953 devicehandler *DeviceHandler
954 wantErr bool
955 }{
956 {"dostateup-1", dh1, false},
957 {"dostateup-2", dh2, false},
958 {"dostateup-3", dh3, true},
959 }
960 for _, tt := range tests {
961 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530962 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
963 defer cancel()
964 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530965 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
966 }
Thomas Lee S85f37312020-04-03 17:06:12 +0530967 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +0530968 })
969 }
970}
971func TestDeviceHandler_doStateDown(t *testing.T) {
972 dh1 := newMockDeviceHandler()
973 dh2 := negativeDeviceHandler()
974 dh3 := newMockDeviceHandler()
975 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
976 tests := []struct {
977 name string
978 devicehandler *DeviceHandler
979 wantErr bool
980 }{
981 {"dostatedown-1", dh1, false},
982 {"dostatedown-2", dh2, true},
983 {"dostatedown-2", dh3, true},
984 }
985 for _, tt := range tests {
986 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530987 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
988 defer cancel()
989 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530990 t.Logf("DeviceHandler.doStateDown() error = %v", err)
991 }
992 })
993 }
994}
995
996func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
997 dh1 := newMockDeviceHandler()
998 dh2 := negativeDeviceHandler()
999 type args struct {
1000 device *voltha.Device
1001 }
1002 tests := []struct {
1003 name string
1004 devicehandler *DeviceHandler
1005 args args
1006 wantErr bool
1007 }{
1008 // TODO: Add test cases.
1009 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1010 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1011 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1012 }
1013 for _, tt := range tests {
1014 t.Run(tt.name, func(t *testing.T) {
1015 dh := tt.devicehandler
1016 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1017 if (err != nil) != tt.wantErr {
1018 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1019 return
1020 }
1021 })
1022 }
1023}
1024
kdarapu891693b2019-09-16 12:33:49 +05301025func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1026
1027 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301028 dh1.discOnus = sync.Map{}
1029 dh1.discOnus.Store("onu1", true)
1030 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301031 dh1.discOnus.Store("onu3", true)
1032 dh1.discOnus.Store("onu4", true)
1033 dh1.onus = sync.Map{}
1034 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1035 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301036 dh2 := negativeDeviceHandler()
1037 type args struct {
1038 onuDiscInd *oop.OnuDiscIndication
1039 sn string
1040 }
1041 tests := []struct {
1042 name string
1043 devicehandler *DeviceHandler
1044 args args
1045 }{
1046 // TODO: Add test cases.
1047 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1048 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1049 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1050 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1051 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1052 {"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 +05301053 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1054 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1055 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301056 }
1057 for _, tt := range tests {
1058 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301059 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1060 defer cancel()
1061 tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
kdarapu891693b2019-09-16 12:33:49 +05301062 })
1063 }
1064}
1065
1066func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1067 dh1 := newMockDeviceHandler()
1068 dh2 := negativeDeviceHandler()
1069 tests := []struct {
1070 name string
1071 devicehandler *DeviceHandler
1072
1073 wantErr bool
1074 }{
1075 // TODO: Add test cases.
1076 {"populateDeviceInfo-1", dh1, false},
1077 {"populateDeviceInfo-2", dh1, true},
1078 {"populateDeviceInfo-3", dh1, true},
1079 {"populateDeviceInfo-4", dh1, true},
1080 {"populateDeviceInfo-5", dh2, true},
1081 }
1082 for _, tt := range tests {
1083 t.Run(tt.name, func(t *testing.T) {
1084
1085 _, err := tt.devicehandler.populateDeviceInfo()
1086 if (err != nil) != tt.wantErr {
1087 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1088 return
1089 }
1090
1091 })
1092 }
1093}
1094
1095func TestDeviceHandler_readIndications(t *testing.T) {
1096 dh1 := newMockDeviceHandler()
1097 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301098 dh3 := newMockDeviceHandler()
1099 dh3.device.AdminState = voltha.AdminState_DISABLED
1100 dh4 := negativeDeviceHandler()
1101 tests := []struct {
1102 name string
1103 devicehandler *DeviceHandler
1104 }{
1105 // TODO: Add test cases.
1106 {"readIndications-1", dh1},
1107 {"readIndications-2", dh2},
1108 {"readIndications-3", dh2},
1109 {"readIndications-4", dh2},
1110 {"readIndications-5", dh2},
1111 {"readIndications-6", dh3},
1112 {"readIndications-7", dh3},
1113 {"readIndications-8", dh4},
1114 }
1115 for _, tt := range tests {
1116 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301117 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1118 defer cancel()
1119 tt.devicehandler.readIndications(ctx)
kdarapu891693b2019-09-16 12:33:49 +05301120 })
1121 }
1122}
Naga Manjunath7615e552019-10-11 22:35:47 +05301123
1124func Test_startCollector(t *testing.T) {
1125 type args struct {
1126 dh *DeviceHandler
1127 }
1128 dh := newMockDeviceHandler()
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301129 ports := []*voltha.Port{
1130 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1131 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1132 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1133 }
1134 dh.device.Ports = ports
Naga Manjunath7615e552019-10-11 22:35:47 +05301135 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301136 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1137 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301138 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1139 dh.portStats.Device = dh
1140 for i := 0; i < 16; i++ {
1141 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1142 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301143 dh1 := newMockDeviceHandler()
1144 dh1.device.Ports = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301145 tests := []struct {
1146 name string
1147 args args
1148 }{
1149 // TODO: Add test cases.
1150 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301151 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301152 }
1153 for _, tt := range tests {
1154 t.Run(tt.name, func(t *testing.T) {
1155 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301156 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301157 tt.args.dh.stopCollector <- true
1158 }()
1159 startCollector(tt.args.dh)
1160 })
1161 }
1162}