blob: f04124992a9c185b91cd9c2974d1b8a8a7d89e61 [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"
Girish Gowdra4c3d4602021-07-22 16:33:37 -070022 conf "github.com/opencord/voltha-lib-go/v6/pkg/config"
kdarapu381c6902019-07-31 18:23:16 +053023 "net"
kdarapu891693b2019-09-16 12:33:49 +053024 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053025 "sync"
kdarapu381c6902019-07-31 18:23:16 +053026 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053027 "time"
28
kdarapu891693b2019-09-16 12:33:49 +053029 "github.com/golang/protobuf/ptypes"
30 "github.com/golang/protobuf/ptypes/any"
Girish Gowdra4c3d4602021-07-22 16:33:37 -070031 "github.com/opencord/voltha-lib-go/v6/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v6/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v6/pkg/log"
34 "github.com/opencord/voltha-lib-go/v6/pkg/pmmetrics"
35 ponrmgr "github.com/opencord/voltha-lib-go/v6/pkg/ponresourcemanager"
kesavand494c2082020-08-31 11:16:12 +053036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
39 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070040 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
41 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
42 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
43 oop "github.com/opencord/voltha-protos/v4/go/openolt"
44 "github.com/opencord/voltha-protos/v4/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053045)
46
Girish Gowdra9602eb42020-09-09 15:50:39 -070047const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070048 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070049 OnuIDStart = 1
50 OnuIDEnd = 32
51 AllocIDStart = 1
52 AllocIDEnd = 10
53 GemIDStart = 1
54 GemIDEnd = 10
55 FlowIDStart = 1
56 FlowIDEnd = 10
57)
58
kdarapu891693b2019-09-16 12:33:49 +053059func newMockCoreProxy() *mocks.MockCoreProxy {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040060 mcp := mocks.MockCoreProxy{
61 Devices: make(map[string]*voltha.Device),
62 DevicePorts: make(map[string][]*voltha.Port),
63 }
Naga Manjunath7615e552019-10-11 22:35:47 +053064 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053065 mcp.Devices["olt"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053066 Id: "olt",
67 Root: true,
68 ParentId: "logical_device",
69 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +053070 ProxyAddress: &voltha.Device_ProxyAddress{
71 DeviceId: "olt",
72 DeviceType: "onu",
73 ChannelId: 1,
74 ChannelGroupId: 1,
75 },
76 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053077 PmConfigs: &voltha.PmConfigs{
78 DefaultFreq: 10,
79 Id: "olt",
80 FreqOverride: false,
81 Grouped: false,
82 Metrics: pm,
83 },
kdarapu891693b2019-09-16 12:33:49 +053084 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040085 mcp.DevicePorts["olt"] = []*voltha.Port{
86 {PortNo: 1, Label: "pon"},
87 {PortNo: 2, Label: "nni"},
88 }
kdarapu891693b2019-09-16 12:33:49 +053089
Kent Hagermanf1db18b2020-07-08 13:38:15 -040090 mcp.Devices["onu1"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053091 Id: "1",
92 Root: false,
93 ParentId: "olt",
94 ParentPortNo: 1,
95
kdarapu891693b2019-09-16 12:33:49 +053096 OperStatus: 4,
97 ProxyAddress: &voltha.Device_ProxyAddress{
98 OnuId: 1,
99 ChannelId: 1,
100 ChannelGroupId: 1,
101 },
102 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530103 PmConfigs: &voltha.PmConfigs{
104 DefaultFreq: 10,
105 Id: "olt",
106 FreqOverride: false,
107 Grouped: false,
108 Metrics: pm,
109 },
kdarapu891693b2019-09-16 12:33:49 +0530110 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400111 mcp.DevicePorts["onu1"] = []*voltha.Port{
112 {PortNo: 1, Label: "pon"},
113 {PortNo: 2, Label: "uni"},
114 }
115
kdarapu891693b2019-09-16 12:33:49 +0530116 mcp.Devices["onu2"] = &voltha.Device{
117 Id: "2",
118 Root: false,
119 ParentId: "olt",
120 OperStatus: 2,
kdarapu891693b2019-09-16 12:33:49 +0530121
122 ParentPortNo: 1,
123
124 ProxyAddress: &voltha.Device_ProxyAddress{
125 OnuId: 2,
126 ChannelId: 1,
127 ChannelGroupId: 1,
128 },
129 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530130 PmConfigs: &voltha.PmConfigs{
131 DefaultFreq: 10,
132 Id: "olt",
133 FreqOverride: false,
134 Grouped: false,
135 Metrics: pm,
136 },
kdarapu891693b2019-09-16 12:33:49 +0530137 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400138 mcp.DevicePorts["onu2"] = []*voltha.Port{
139 {PortNo: 1, Label: "pon"},
140 {PortNo: 2, Label: "uni"},
141 }
kdarapu891693b2019-09-16 12:33:49 +0530142 return &mcp
143}
144func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530145 device := &voltha.Device{
146 Id: "olt",
147 Root: true,
148 ParentId: "logical_device",
kdarapu891693b2019-09-16 12:33:49 +0530149 ProxyAddress: &voltha.Device_ProxyAddress{
150 DeviceId: "olt",
151 DeviceType: "onu",
152 ChannelId: 1,
153 ChannelGroupId: 1,
154 },
155 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530156 }
kdarapu891693b2019-09-16 12:33:49 +0530157 cp := newMockCoreProxy()
158 ap := &mocks.MockAdapterProxy{}
159 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800160 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700161 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
kesavand494c2082020-08-31 11:16:12 +0530162 cfg := &config.AdapterFlags{OmccEncryption: true}
163 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep, config: cfg}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800164 dh := NewDeviceHandler(cp, ap, ep, device, openOLT, cm)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700165 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700166 IntfIds: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700167 Technology: "xgs-pon",
168 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
169 }}
170
Girish Gowdra9602eb42020-09-09 15:50:39 -0700171 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700172 dh.deviceInfo = deviceInf
173 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
174 var i uint32
175 for i = 0; i < deviceInf.PonPorts; i++ {
176 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
177 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700178 StoreType: "etcd",
179 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700180 }}
181 dh.resourceMgr[i].InitLocalCache()
182 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700183
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530184 ranges := make(map[string]interface{})
185 sharedIdxByType := make(map[string]string)
186 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
187 sharedIdxByType["ONU_ID"] = "ONU_ID"
188 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
189 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
190 ranges["ONU_ID"] = uint32(0)
191 ranges["GEMPORT_ID"] = uint32(0)
192 ranges["ALLOC_ID"] = uint32(0)
193 ranges["FLOW_ID"] = uint32(0)
194 ranges["onu_id_shared"] = uint32(0)
195 ranges["alloc_id_shared"] = uint32(0)
196 ranges["gemport_id_shared"] = uint32(0)
197 ranges["flow_id_shared"] = uint32(0)
198
Matteo Scandolo84585372021-03-18 14:21:22 -0700199 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700200 ponmgr.DeviceID = "onu-1"
201 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
202 ponmgr.KVStore = &db.Backend{
203 Client: &mocks.MockKVClient{},
204 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700205 ponmgr.KVStoreForConfig = &db.Backend{
206 Client: &mocks.MockKVClient{},
207 }
208 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700209 ponmgr.PonResourceRanges = ranges
210 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700211 ponmgr.Technology = "XGS-PON"
212 for i = 0; i < deviceInf.PonPorts; i++ {
213 dh.resourceMgr[i].PonRsrMgr = ponmgr
214 }
215
216 /*
217 tpMgr, err := tp.NewTechProfile(ctx, ponmgr, "etcd", "127.0.0.1", "/")
218 if err != nil {
219 logger.Fatal(ctx, err.Error())
220 }
221 */
Matteo Scandolo84585372021-03-18 14:21:22 -0700222
npujarec5762e2020-01-01 14:08:48 +0530223 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
224 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700225 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700226 dh.totalPonPorts = NumPonPorts
227 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700228 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700229 /*
230 // Instantiate flow manager
231 if dh.flowMgr[i] = NewFlowManager(ctx, dh, dh.resourceMgr[i], dh.groupMgr, uint32(i)); dh.flowMgr[i] == nil {
232 return nil
233 }
234
235 */
236 dh.flowMgr[i] = &OpenOltFlowMgr{}
237 dh.flowMgr[i].deviceHandler = dh
238 dh.flowMgr[i].ponPortIdx = i
239 dh.flowMgr[i].grpMgr = dh.groupMgr
240 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
241 /*
242 if err = flowMgr.populateTechProfilePerPonPort(ctx); err != nil {
243 logger.Errorw(ctx, "error-while-populating-tech-profile-mgr", log.Fields{"err": err})
244 return nil
245 }
246 */
247 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
248 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
249 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
250 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
251
252 // Create a slice of buffered channels for handling concurrent flows per ONU.
253 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
254 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, MaxOnusPerPon+1)
255 for j := range dh.flowMgr[i].incomingFlows {
256 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
257 // Spin up a go routine to handling incoming flows (add/remove).
258 // There will be on go routine per ONU.
259 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
260 go dh.flowMgr[i].perOnuFlowHandlerRoutine(dh.flowMgr[i].incomingFlows[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700261 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700262 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700263 }
kdarapu891693b2019-09-16 12:33:49 +0530264 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500265 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530266 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530267 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000268
269 var pmNames = []string{
270 "rx_bytes",
271 "rx_packets",
272 "rx_mcast_packets",
273 "rx_bcast_packets",
274 "tx_bytes",
275 "tx_packets",
276 "tx_mcast_packets",
277 "tx_bcast_packets",
278 }
279
280 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530281 return dh
kdarapu381c6902019-07-31 18:23:16 +0530282}
283
kdarapu891693b2019-09-16 12:33:49 +0530284func negativeDeviceHandler() *DeviceHandler {
285 dh := newMockDeviceHandler()
286 device := dh.device
287 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530288 return dh
289}
kdarapu381c6902019-07-31 18:23:16 +0530290func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000291 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530292 type args struct {
293 host string
294 }
295 tests := []struct {
296 name string
297 args args
298 want string
299 wantErr bool
300 }{
kdarapu891693b2019-09-16 12:33:49 +0530301 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
302 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
303 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
304 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530305 }
306 for _, tt := range tests {
307 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000308 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530309 if (err != nil) != tt.wantErr {
310 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
311 return
312 }
313 if got != tt.want {
314 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
315 }
316 })
317 }
318}
319func Test_macifyIP(t *testing.T) {
320 type args struct {
321 ip net.IP
322 }
323 tests := []struct {
324 name string
325 args args
326 want string
327 }{{
kdarapu891693b2019-09-16 12:33:49 +0530328 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530329 args{ip: net.ParseIP("10.10.10.10")},
330 "00:00:0a:0a:0a:0a",
331 },
332 {
kdarapu891693b2019-09-16 12:33:49 +0530333 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530334 args{ip: net.ParseIP("127.0.0.1")},
335 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530336 },
337 {
338 "macifyIP-3",
339 args{ip: net.ParseIP("127.0.0.1/24")},
340 "",
341 },
342 }
kdarapu381c6902019-07-31 18:23:16 +0530343 for _, tt := range tests {
344 t.Run(tt.name, func(t *testing.T) {
345 if got := macifyIP(tt.args.ip); got != tt.want {
346 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
347 }
348 })
349 }
350}
351
David K. Bainbridge794735f2020-02-11 21:01:37 -0800352func sparseCompare(keys []string, spec, target interface{}) bool {
353 if spec == target {
354 return true
355 }
356 if spec == nil || target == nil {
357 return false
358 }
359 typeSpec := reflect.TypeOf(spec)
360 typeTarget := reflect.TypeOf(target)
361 if typeSpec != typeTarget {
362 return false
363 }
364
365 vSpec := reflect.ValueOf(spec)
366 vTarget := reflect.ValueOf(target)
367 if vSpec.Kind() == reflect.Ptr {
368 vSpec = vSpec.Elem()
369 vTarget = vTarget.Elem()
370 }
371
372 for _, key := range keys {
373 fSpec := vSpec.FieldByName(key)
374 fTarget := vTarget.FieldByName(key)
375 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
376 return false
377 }
378 }
379 return true
380}
381
kdarapu381c6902019-07-31 18:23:16 +0530382func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000383 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530384 dh1 := newMockDeviceHandler()
385 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530386 type args struct {
387 parentPort uint32
388 onuID uint32
389 }
390 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530391 name string
392 devicehandler *DeviceHandler
393 args args
394 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800395 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530396 }{
kdarapu891693b2019-09-16 12:33:49 +0530397 {"GetChildDevice-1", dh1,
398 args{parentPort: 1,
399 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800400 &voltha.Device{
401 Id: "1",
402 ParentId: "olt",
403 ParentPortNo: 1,
404 },
405 nil,
kdarapu891693b2019-09-16 12:33:49 +0530406 },
407 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530408 args{parentPort: 1,
409 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800410 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530411 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530412 },
413 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800414
415 /*
416 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
417 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>)
418 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
419 */
kdarapu381c6902019-07-31 18:23:16 +0530420 for _, tt := range tests {
421 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000422 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800423 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
424 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
425 tt.want, tt.errType, got, reflect.TypeOf(err))
426 return
427 }
kdarapu381c6902019-07-31 18:23:16 +0530428 t.Log("onu device id", got)
429 })
430 }
431}
kdarapu891693b2019-09-16 12:33:49 +0530432
433func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530434 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530435 type args struct {
436 portNum uint32
437 portType voltha.Port_PortType
438 }
439 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800440 name string
441 args args
442 want string
443 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530444 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800445 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
446 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
447 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
448 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
449 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
450 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
451 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530452 }
453 for _, tt := range tests {
454 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800455 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
456 if reflect.TypeOf(err) != tt.errType || got != tt.want {
457 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
458 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530459 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800460
kdarapu891693b2019-09-16 12:33:49 +0530461 })
462 }
463}
464
465func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000466 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530467 dh := newMockDeviceHandler()
468 proxyAddr := dh.device.ProxyAddress
469 body := &ic.InterAdapterOmciMessage{
470 Message: []byte("asdfasdfasdfasdfas"),
471 ProxyAddress: proxyAddr,
472 }
473 body2 := &ic.InterAdapterOmciMessage{
474 Message: []byte("asdfasdfasdfasdfas"),
475 //ProxyAddress: &voltha.Device_ProxyAddress{},
476 }
477 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
478 var marshalledData *any.Any
479 var err error
480
481 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700482 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530483 }
484
485 var marshalledData1 *any.Any
486
487 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700488 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530489 }
490 var marshalledData2 *any.Any
491
492 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700493 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530494 }
495 type args struct {
496 msg *ic.InterAdapterMessage
497 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530498 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530499 tests := []struct {
500 name string
501 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800502 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530503 }{
504 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
505 Header: &ic.InterAdapterHeader{
506 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800507 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530508 },
509 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800510 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530511 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
512 Header: &ic.InterAdapterHeader{
513 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800514 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530515 },
516 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800517 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530518 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
519 Header: &ic.InterAdapterHeader{
520 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800521 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530522 },
523 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530524 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530525 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
526 Header: &ic.InterAdapterHeader{
527 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800528 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530529 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800530 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530531 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
532 Header: &ic.InterAdapterHeader{
533 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800534 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530535 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800536 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530537 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
538 Header: &ic.InterAdapterHeader{
539 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800540 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530541 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800542 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530543 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
544 Header: &ic.InterAdapterHeader{
545 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800546 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530547 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800548 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530549 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
550 Header: &ic.InterAdapterHeader{
551 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800552 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530553 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800554 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530555 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
556 Header: &ic.InterAdapterHeader{
557 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800558 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530559 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800560 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530561 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
562 Header: &ic.InterAdapterHeader{
563 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800564 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530565 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800566 }}, invalid},
567 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
568 Header: &ic.InterAdapterHeader{
569 Id: "012345",
570 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
571 }, Body: marshalledData2,
572 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530573 }
574 for _, tt := range tests {
575 t.Run(tt.name, func(t *testing.T) {
576
Neha Sharma96b7bf22020-06-15 10:37:32 +0000577 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530578 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
579 }
580 })
581 }
582}
583
584func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000585 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530586 dh1 := newMockDeviceHandler()
587 dh2 := negativeDeviceHandler()
588 device1 := &voltha.Device{
589 Id: "onu1",
590 Root: false,
591 ParentId: "logical_device",
592 ProxyAddress: &voltha.Device_ProxyAddress{
593 DeviceId: "onu1",
594 DeviceType: "onu",
595 ChannelId: 1,
596 ChannelGroupId: 1,
597 },
598 ConnectStatus: 1,
599 }
600 device2 := device1
601 device2.ConnectStatus = 2
602 iaomciMsg1 := &ic.InterAdapterOmciMessage{
603 ProxyAddress: &voltha.Device_ProxyAddress{
604 DeviceId: "onu2",
605 DeviceType: "onu",
606 ChannelId: 1,
607 ChannelGroupId: 1,
608 //OnuId: 2,
609 },
610 ConnectStatus: 1,
611 }
612 iaomciMsg2 := &ic.InterAdapterOmciMessage{
613 ProxyAddress: &voltha.Device_ProxyAddress{
614 DeviceId: "onu3",
615 DeviceType: "onu",
616 ChannelId: 1,
617 ChannelGroupId: 1,
618 },
619 ConnectStatus: 1,
620 }
621 type args struct {
622 onuDevice *voltha.Device
623 omciMsg *ic.InterAdapterOmciMessage
624 }
625 tests := []struct {
626 name string
627 devicehandler *DeviceHandler
628 args args
629 }{
630 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
631 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
632 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
633 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
634 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
635 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
636 }
637 for _, tt := range tests {
638 t.Run(tt.name, func(t *testing.T) {
Girish Gowdra7de58ac2021-07-20 15:09:25 -0700639 _ = tt.devicehandler.sendProxiedOmciMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400640 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530641 })
642 }
643}
644
645func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
646 dh1 := newMockDeviceHandler()
647 dh2 := negativeDeviceHandler()
648
649 type args struct {
650 logicalPort uint32
651 packetPayload []byte
652 }
653 tests := []struct {
654 name string
655 devicehandler *DeviceHandler
656 args args
657 }{
658 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
659 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
660 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
661 }
662 for _, tt := range tests {
663 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400664 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
665 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530666 })
667 }
668}
669
670func TestDeviceHandler_DisableDevice(t *testing.T) {
671 dh1 := newMockDeviceHandler()
672 dh2 := negativeDeviceHandler()
673 type args struct {
674 device *voltha.Device
675 }
676 tests := []struct {
677 name string
678 devicehandler *DeviceHandler
679 args args
680 wantErr bool
681 }{
682 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400683 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530684 }
685 for _, tt := range tests {
686 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000687 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530688 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
689 }
690 })
691 }
692}
693
694func TestDeviceHandler_ReenableDevice(t *testing.T) {
695 dh1 := newMockDeviceHandler()
696 dh2 := negativeDeviceHandler()
697 type args struct {
698 device *voltha.Device
699 }
700 tests := []struct {
701 name string
702 devicehandler *DeviceHandler
703 args args
704 wantErr bool
705 }{
706 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
707 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
708 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
709 }
710 for _, tt := range tests {
711 t.Run(tt.name, func(t *testing.T) {
712 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000713 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530714 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
715 }
716 })
717 }
718}
719
720func TestDeviceHandler_RebootDevice(t *testing.T) {
721 dh1 := newMockDeviceHandler()
722 dh2 := newMockDeviceHandler()
723 type args struct {
724 device *voltha.Device
725 }
726 tests := []struct {
727 name string
728 devicehandler *DeviceHandler
729 args args
730 wantErr bool
731 }{
732 // TODO: Add test cases.
733 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
734 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
735 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
736 }
737 for _, tt := range tests {
738 t.Run(tt.name, func(t *testing.T) {
739
Neha Sharma96b7bf22020-06-15 10:37:32 +0000740 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530741 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
742 }
743 })
744 }
745}
746
747func TestDeviceHandler_handleIndication(t *testing.T) {
748 dh1 := newMockDeviceHandler()
749 dh2 := negativeDeviceHandler()
750 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530751 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530752 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
753 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530754
kdarapu891693b2019-09-16 12:33:49 +0530755 type args struct {
756 indication *oop.Indication
757 }
758 tests := []struct {
759 name string
760 deviceHandler *DeviceHandler
761 args args
762 }{
763 // TODO: Add test cases.
764 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
765 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
766 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
767 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
768 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
769 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
770 {"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")}}}}}},
771 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
772 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
773 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
774 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
775 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
776 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
777 {"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}}}}},
778 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
779 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
780 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
781 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
782
783 // Negative testcases
784 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
785 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
786 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
787 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
788 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
789 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
790 {"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")}}}}}},
791 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
792 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
793 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
794 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
795 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
796 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
797 {"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}}}}},
798 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
799 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
800 //
801 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
802 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
803 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
804 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
805 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
806 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
807 }
808 for _, tt := range tests {
809 t.Run(tt.name, func(t *testing.T) {
810 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530811 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
812 defer cancel()
813 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530814 })
815 }
816}
817
818func TestDeviceHandler_addPort(t *testing.T) {
819 dh1 := newMockDeviceHandler()
820 dh2 := negativeDeviceHandler()
821 type args struct {
822 intfID uint32
823 portType voltha.Port_PortType
824 state string
825 }
826 tests := []struct {
827 name string
828 devicehandler *DeviceHandler
829 args args
830 }{
831 // State up
832 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
833 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
834 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
835 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
836 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
837 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
838 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
839 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
840 // state discovery
841 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
842 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
843 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
844 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
845 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
846 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
847 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
848 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
849
850 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
851 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
852 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
853 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
854 }
855 for _, tt := range tests {
856 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400857 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
858 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530859 })
860 }
861}
862
863func Test_macAddressToUint32Array(t *testing.T) {
864 type args struct {
865 mac string
866 }
867 tests := []struct {
868 name string
869 args args
870 want []uint32
871 }{
872 // TODO: Add test cases.
873 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
874 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
875 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
876 }
877 for _, tt := range tests {
878 t.Run(tt.name, func(t *testing.T) {
879 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
880 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
881 }
882 })
883 }
884}
885
886func TestDeviceHandler_handleOltIndication(t *testing.T) {
887
888 type args struct {
889 oltIndication *oop.OltIndication
890 }
891 tests := []struct {
892 name string
893 args args
894 }{
895 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
896 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
897 }
898 for _, tt := range tests {
899 t.Run(tt.name, func(t *testing.T) {
900 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530901 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
902 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400903 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
904 t.Error(err)
905 }
kdarapu891693b2019-09-16 12:33:49 +0530906 })
907 }
908}
909
910func TestDeviceHandler_AdoptDevice(t *testing.T) {
911 dh1 := newMockDeviceHandler()
912 dh2 := negativeDeviceHandler()
913 type args struct {
914 device *voltha.Device
915 }
916 tests := []struct {
917 name string
918 devicehandler *DeviceHandler
919 args args
920 }{
921 // TODO: Add test cases.
922 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530923 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530924 }
925 for _, tt := range tests {
926 t.Run(tt.name, func(t *testing.T) {
927 //dh.doStateInit()
928 // context.
929 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530930 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
931 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400932 if err := tt.devicehandler.postInit(ctx); err != nil {
933 t.Error(err)
934 }
kdarapu891693b2019-09-16 12:33:49 +0530935 })
936 }
937}
938
939func TestDeviceHandler_activateONU(t *testing.T) {
940 dh := newMockDeviceHandler()
941 dh1 := negativeDeviceHandler()
942 type args struct {
943 intfID uint32
944 onuID int64
945 serialNum *oop.SerialNumber
946 serialNumber string
947 }
948 tests := []struct {
949 name string
950 devicehandler *DeviceHandler
951 args args
952 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700953 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
954 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
955 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
956 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530957 }
958 for _, tt := range tests {
959 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530960 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
961 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400962 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
963 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530964 })
965 }
966}
967
968func TestDeviceHandler_start(t *testing.T) {
969 dh := newMockDeviceHandler()
970 dh1 := negativeDeviceHandler()
971 dh.start(context.Background())
972 dh.stop(context.Background())
973
974 dh1.start(context.Background())
975 dh1.stop(context.Background())
976
977}
978
979func TestDeviceHandler_PacketOut(t *testing.T) {
980 dh1 := newMockDeviceHandler()
981 dh2 := negativeDeviceHandler()
982 acts := []*ofp.OfpAction{
983 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
984 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
985 fu.Output(1),
986 }
987 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
988 type args struct {
989 egressPortNo int
990 packet *of.OfpPacketOut
991 }
992 tests := []struct {
993 name string
994 devicehandler *DeviceHandler
995 args args
996 wantErr bool
997 }{
998 // TODO: Add test cases.
999 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
1000 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
1001 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -07001002 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
1003 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
1004 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +05301005 }
1006 for _, tt := range tests {
1007 t.Run(tt.name, func(t *testing.T) {
1008 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +05301009 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1010 defer cancel()
1011 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301012 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
1013 }
1014 })
1015 }
1016}
1017
1018//
1019func TestDeviceHandler_doStateUp(t *testing.T) {
1020 dh1 := newMockDeviceHandler()
1021 dh2 := newMockDeviceHandler()
1022
Thomas Lee S985938d2020-05-04 11:40:41 +05301023 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301024 dh3 := negativeDeviceHandler()
1025
1026 tests := []struct {
1027 name string
1028 devicehandler *DeviceHandler
1029 wantErr bool
1030 }{
1031 {"dostateup-1", dh1, false},
1032 {"dostateup-2", dh2, false},
1033 {"dostateup-3", dh3, true},
1034 }
1035 for _, tt := range tests {
1036 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301037 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1038 defer cancel()
1039 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301040 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1041 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301042 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301043 })
1044 }
1045}
1046func TestDeviceHandler_doStateDown(t *testing.T) {
1047 dh1 := newMockDeviceHandler()
1048 dh2 := negativeDeviceHandler()
1049 dh3 := newMockDeviceHandler()
1050 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1051 tests := []struct {
1052 name string
1053 devicehandler *DeviceHandler
1054 wantErr bool
1055 }{
1056 {"dostatedown-1", dh1, false},
1057 {"dostatedown-2", dh2, true},
1058 {"dostatedown-2", dh3, true},
1059 }
1060 for _, tt := range tests {
1061 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301062 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1063 defer cancel()
1064 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301065 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001066 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301067 }
1068 })
1069 }
1070}
1071
1072func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1073 dh1 := newMockDeviceHandler()
1074 dh2 := negativeDeviceHandler()
1075 type args struct {
1076 device *voltha.Device
1077 }
1078 tests := []struct {
1079 name string
1080 devicehandler *DeviceHandler
1081 args args
1082 wantErr bool
1083 }{
1084 // TODO: Add test cases.
1085 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1086 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1087 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1088 }
1089 for _, tt := range tests {
1090 t.Run(tt.name, func(t *testing.T) {
1091 dh := tt.devicehandler
1092 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1093 if (err != nil) != tt.wantErr {
1094 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1095 return
1096 }
1097 })
1098 }
1099}
1100
kdarapu891693b2019-09-16 12:33:49 +05301101func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1102
1103 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301104 dh1.discOnus = sync.Map{}
1105 dh1.discOnus.Store("onu1", true)
1106 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301107 dh1.discOnus.Store("onu3", true)
1108 dh1.discOnus.Store("onu4", true)
1109 dh1.onus = sync.Map{}
1110 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1111 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301112 dh2 := negativeDeviceHandler()
1113 type args struct {
1114 onuDiscInd *oop.OnuDiscIndication
1115 sn string
1116 }
1117 tests := []struct {
1118 name string
1119 devicehandler *DeviceHandler
1120 args args
1121 }{
1122 // TODO: Add test cases.
1123 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1124 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1125 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1126 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1127 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1128 {"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 +05301129 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1130 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1131 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301132 }
1133 for _, tt := range tests {
1134 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301135 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1136 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001137 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001138 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301139 })
1140 }
1141}
1142
1143func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1144 dh1 := newMockDeviceHandler()
1145 dh2 := negativeDeviceHandler()
1146 tests := []struct {
1147 name string
1148 devicehandler *DeviceHandler
1149
1150 wantErr bool
1151 }{
1152 // TODO: Add test cases.
1153 {"populateDeviceInfo-1", dh1, false},
1154 {"populateDeviceInfo-2", dh1, true},
1155 {"populateDeviceInfo-3", dh1, true},
1156 {"populateDeviceInfo-4", dh1, true},
1157 {"populateDeviceInfo-5", dh2, true},
1158 }
1159 for _, tt := range tests {
1160 t.Run(tt.name, func(t *testing.T) {
1161
Neha Sharma96b7bf22020-06-15 10:37:32 +00001162 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301163 if (err != nil) != tt.wantErr {
1164 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1165 return
1166 }
1167
1168 })
1169 }
1170}
1171
1172func TestDeviceHandler_readIndications(t *testing.T) {
1173 dh1 := newMockDeviceHandler()
1174 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301175 dh3 := newMockDeviceHandler()
1176 dh3.device.AdminState = voltha.AdminState_DISABLED
1177 dh4 := negativeDeviceHandler()
1178 tests := []struct {
1179 name string
1180 devicehandler *DeviceHandler
1181 }{
1182 // TODO: Add test cases.
1183 {"readIndications-1", dh1},
1184 {"readIndications-2", dh2},
1185 {"readIndications-3", dh2},
1186 {"readIndications-4", dh2},
1187 {"readIndications-5", dh2},
1188 {"readIndications-6", dh3},
1189 {"readIndications-7", dh3},
1190 {"readIndications-8", dh4},
1191 }
1192 for _, tt := range tests {
1193 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301194 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1195 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001196 _ = tt.devicehandler.readIndications(ctx)
1197 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301198 })
1199 }
1200}
Naga Manjunath7615e552019-10-11 22:35:47 +05301201
1202func Test_startCollector(t *testing.T) {
1203 type args struct {
1204 dh *DeviceHandler
1205 }
1206 dh := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001207 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301208 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1209 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1210 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1211 }
Naga Manjunath7615e552019-10-11 22:35:47 +05301212 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301213 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1214 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301215 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1216 dh.portStats.Device = dh
1217 for i := 0; i < 16; i++ {
1218 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1219 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301220 dh1 := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001221 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301222 tests := []struct {
1223 name string
1224 args args
1225 }{
1226 // TODO: Add test cases.
1227 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301228 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301229 }
1230 for _, tt := range tests {
1231 t.Run(tt.name, func(t *testing.T) {
1232 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301233 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301234 tt.args.dh.stopCollector <- true
1235 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001236 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301237 })
1238 }
1239}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001240
1241func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1242
1243 // olt disconnect (not reboot)
1244 dh1 := newMockDeviceHandler()
1245 dh1.adapterPreviouslyConnected = false
1246 dh1.agentPreviouslyConnected = true
1247
1248 // adapter restart
1249 dh2 := newMockDeviceHandler()
1250 dh2.Client = &mocks.MockOpenoltClient{}
1251 dh2.adapterPreviouslyConnected = true
1252 dh2.agentPreviouslyConnected = true
1253
1254 // first connection or olt restart
1255 dh3 := newMockDeviceHandler()
1256 dh3.Client = &mocks.MockOpenoltClient{}
1257 dh3.adapterPreviouslyConnected = false
1258 dh3.agentPreviouslyConnected = false
1259
1260 // olt and adapter restart at the same time (first case)
1261 dh4 := newMockDeviceHandler()
1262 dh4.Client = &mocks.MockOpenoltClient{}
1263 dh4.adapterPreviouslyConnected = true
1264 dh4.agentPreviouslyConnected = false
1265
1266 // adapter restart and olt disconnect at the same time
1267 dh5 := newMockDeviceHandler()
1268 dh5.Client = &mocks.MockOpenoltClient{}
1269 dh5.adapterPreviouslyConnected = true
1270 dh5.agentPreviouslyConnected = true
1271
1272 tests := []struct {
1273 name string
1274 devicehandler *DeviceHandler
1275 expectedRestart bool
1276 wantErr bool
1277 }{
1278 {"dostateup-1", dh1, true, false},
1279 {"dostateup-2", dh2, false, false},
1280 {"dostateup-3", dh3, false, false},
1281 {"dostateup-4", dh4, true, false},
1282 {"dostateup-5", dh5, false, false},
1283 }
1284 for _, tt := range tests {
1285 t.Run(tt.name, func(t *testing.T) {
1286 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1287 defer cancel()
1288 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1289 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1290 }
1291 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1292 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1293 if tt.expectedRestart != isRestarted {
1294 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1295 }
1296 })
1297 }
1298}