blob: f021312922b58be87cf15cdec2ca7225bdf8fc34 [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
npujarec5762e2020-01-01 14:08:48 +0530216 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
217 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700218 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700219 dh.totalPonPorts = NumPonPorts
220 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700221 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700222 dh.flowMgr[i] = &OpenOltFlowMgr{}
223 dh.flowMgr[i].deviceHandler = dh
224 dh.flowMgr[i].ponPortIdx = i
225 dh.flowMgr[i].grpMgr = dh.groupMgr
226 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700227 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
228 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
229 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
230 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
231
Girish Gowdra76a1b092021-07-28 10:07:04 -0700232 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
233
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700234 // Create a slice of buffered channels for handling concurrent flows per ONU.
235 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
236 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, MaxOnusPerPon+1)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700237 dh.flowMgr[i].stopFlowHandlerRoutine = make([]chan bool, MaxOnusPerPon+1)
238 dh.flowMgr[i].flowHandlerRoutineActive = make([]bool, MaxOnusPerPon+1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700239 for j := range dh.flowMgr[i].incomingFlows {
240 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700241 dh.flowMgr[i].stopFlowHandlerRoutine[j] = make(chan bool, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700242 // Spin up a go routine to handling incoming flows (add/remove).
243 // There will be on go routine per ONU.
244 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700245 dh.flowMgr[i].flowHandlerRoutineActive[j] = true
246 go dh.flowMgr[i].perOnuFlowHandlerRoutine(j, dh.flowMgr[i].incomingFlows[j], dh.flowMgr[i].stopFlowHandlerRoutine[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700247 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700248 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700249 }
kdarapu891693b2019-09-16 12:33:49 +0530250 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500251 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530252 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530253 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000254
255 var pmNames = []string{
256 "rx_bytes",
257 "rx_packets",
258 "rx_mcast_packets",
259 "rx_bcast_packets",
260 "tx_bytes",
261 "tx_packets",
262 "tx_mcast_packets",
263 "tx_bcast_packets",
264 }
265
266 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530267 return dh
kdarapu381c6902019-07-31 18:23:16 +0530268}
269
kdarapu891693b2019-09-16 12:33:49 +0530270func negativeDeviceHandler() *DeviceHandler {
271 dh := newMockDeviceHandler()
272 device := dh.device
273 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530274 return dh
275}
kdarapu381c6902019-07-31 18:23:16 +0530276func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000277 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530278 type args struct {
279 host string
280 }
281 tests := []struct {
282 name string
283 args args
284 want string
285 wantErr bool
286 }{
kdarapu891693b2019-09-16 12:33:49 +0530287 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
288 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
289 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
290 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530291 }
292 for _, tt := range tests {
293 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000294 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530295 if (err != nil) != tt.wantErr {
296 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
297 return
298 }
299 if got != tt.want {
300 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
301 }
302 })
303 }
304}
305func Test_macifyIP(t *testing.T) {
306 type args struct {
307 ip net.IP
308 }
309 tests := []struct {
310 name string
311 args args
312 want string
313 }{{
kdarapu891693b2019-09-16 12:33:49 +0530314 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530315 args{ip: net.ParseIP("10.10.10.10")},
316 "00:00:0a:0a:0a:0a",
317 },
318 {
kdarapu891693b2019-09-16 12:33:49 +0530319 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530320 args{ip: net.ParseIP("127.0.0.1")},
321 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530322 },
323 {
324 "macifyIP-3",
325 args{ip: net.ParseIP("127.0.0.1/24")},
326 "",
327 },
328 }
kdarapu381c6902019-07-31 18:23:16 +0530329 for _, tt := range tests {
330 t.Run(tt.name, func(t *testing.T) {
331 if got := macifyIP(tt.args.ip); got != tt.want {
332 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
333 }
334 })
335 }
336}
337
David K. Bainbridge794735f2020-02-11 21:01:37 -0800338func sparseCompare(keys []string, spec, target interface{}) bool {
339 if spec == target {
340 return true
341 }
342 if spec == nil || target == nil {
343 return false
344 }
345 typeSpec := reflect.TypeOf(spec)
346 typeTarget := reflect.TypeOf(target)
347 if typeSpec != typeTarget {
348 return false
349 }
350
351 vSpec := reflect.ValueOf(spec)
352 vTarget := reflect.ValueOf(target)
353 if vSpec.Kind() == reflect.Ptr {
354 vSpec = vSpec.Elem()
355 vTarget = vTarget.Elem()
356 }
357
358 for _, key := range keys {
359 fSpec := vSpec.FieldByName(key)
360 fTarget := vTarget.FieldByName(key)
361 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
362 return false
363 }
364 }
365 return true
366}
367
kdarapu381c6902019-07-31 18:23:16 +0530368func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000369 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530370 dh1 := newMockDeviceHandler()
371 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530372 type args struct {
373 parentPort uint32
374 onuID uint32
375 }
376 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530377 name string
378 devicehandler *DeviceHandler
379 args args
380 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800381 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530382 }{
kdarapu891693b2019-09-16 12:33:49 +0530383 {"GetChildDevice-1", dh1,
384 args{parentPort: 1,
385 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800386 &voltha.Device{
387 Id: "1",
388 ParentId: "olt",
389 ParentPortNo: 1,
390 },
391 nil,
kdarapu891693b2019-09-16 12:33:49 +0530392 },
393 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530394 args{parentPort: 1,
395 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800396 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530397 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530398 },
399 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800400
401 /*
402 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
403 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>)
404 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
405 */
kdarapu381c6902019-07-31 18:23:16 +0530406 for _, tt := range tests {
407 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000408 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800409 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
410 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
411 tt.want, tt.errType, got, reflect.TypeOf(err))
412 return
413 }
kdarapu381c6902019-07-31 18:23:16 +0530414 t.Log("onu device id", got)
415 })
416 }
417}
kdarapu891693b2019-09-16 12:33:49 +0530418
419func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530420 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530421 type args struct {
422 portNum uint32
423 portType voltha.Port_PortType
424 }
425 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800426 name string
427 args args
428 want string
429 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530430 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800431 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
432 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
433 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
434 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
435 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
436 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
437 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530438 }
439 for _, tt := range tests {
440 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800441 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
442 if reflect.TypeOf(err) != tt.errType || got != tt.want {
443 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
444 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530445 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800446
kdarapu891693b2019-09-16 12:33:49 +0530447 })
448 }
449}
450
451func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000452 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530453 dh := newMockDeviceHandler()
454 proxyAddr := dh.device.ProxyAddress
455 body := &ic.InterAdapterOmciMessage{
456 Message: []byte("asdfasdfasdfasdfas"),
457 ProxyAddress: proxyAddr,
458 }
459 body2 := &ic.InterAdapterOmciMessage{
460 Message: []byte("asdfasdfasdfasdfas"),
461 //ProxyAddress: &voltha.Device_ProxyAddress{},
462 }
463 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
464 var marshalledData *any.Any
465 var err error
466
467 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700468 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530469 }
470
471 var marshalledData1 *any.Any
472
473 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700474 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530475 }
476 var marshalledData2 *any.Any
477
478 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700479 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530480 }
481 type args struct {
482 msg *ic.InterAdapterMessage
483 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530484 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530485 tests := []struct {
486 name string
487 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800488 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530489 }{
490 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
491 Header: &ic.InterAdapterHeader{
492 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800493 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530494 },
495 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800496 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530497 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
498 Header: &ic.InterAdapterHeader{
499 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800500 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530501 },
502 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800503 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530504 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
505 Header: &ic.InterAdapterHeader{
506 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800507 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530508 },
509 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530510 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530511 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
512 Header: &ic.InterAdapterHeader{
513 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800514 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530515 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800516 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530517 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
518 Header: &ic.InterAdapterHeader{
519 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800520 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530521 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800522 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530523 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
524 Header: &ic.InterAdapterHeader{
525 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800526 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530527 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800528 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530529 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
530 Header: &ic.InterAdapterHeader{
531 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800532 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530533 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800534 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530535 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
536 Header: &ic.InterAdapterHeader{
537 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800538 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530539 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800540 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530541 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
542 Header: &ic.InterAdapterHeader{
543 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800544 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530545 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800546 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530547 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
548 Header: &ic.InterAdapterHeader{
549 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800550 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530551 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800552 }}, invalid},
553 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
554 Header: &ic.InterAdapterHeader{
555 Id: "012345",
556 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
557 }, Body: marshalledData2,
558 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530559 }
560 for _, tt := range tests {
561 t.Run(tt.name, func(t *testing.T) {
562
Neha Sharma96b7bf22020-06-15 10:37:32 +0000563 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530564 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
565 }
566 })
567 }
568}
569
570func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000571 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530572 dh1 := newMockDeviceHandler()
573 dh2 := negativeDeviceHandler()
574 device1 := &voltha.Device{
575 Id: "onu1",
576 Root: false,
577 ParentId: "logical_device",
578 ProxyAddress: &voltha.Device_ProxyAddress{
579 DeviceId: "onu1",
580 DeviceType: "onu",
581 ChannelId: 1,
582 ChannelGroupId: 1,
583 },
584 ConnectStatus: 1,
585 }
586 device2 := device1
587 device2.ConnectStatus = 2
588 iaomciMsg1 := &ic.InterAdapterOmciMessage{
589 ProxyAddress: &voltha.Device_ProxyAddress{
590 DeviceId: "onu2",
591 DeviceType: "onu",
592 ChannelId: 1,
593 ChannelGroupId: 1,
594 //OnuId: 2,
595 },
596 ConnectStatus: 1,
597 }
598 iaomciMsg2 := &ic.InterAdapterOmciMessage{
599 ProxyAddress: &voltha.Device_ProxyAddress{
600 DeviceId: "onu3",
601 DeviceType: "onu",
602 ChannelId: 1,
603 ChannelGroupId: 1,
604 },
605 ConnectStatus: 1,
606 }
607 type args struct {
608 onuDevice *voltha.Device
609 omciMsg *ic.InterAdapterOmciMessage
610 }
611 tests := []struct {
612 name string
613 devicehandler *DeviceHandler
614 args args
615 }{
616 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
617 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
618 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
619 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
620 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
621 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
622 }
623 for _, tt := range tests {
624 t.Run(tt.name, func(t *testing.T) {
Girish Gowdra7de58ac2021-07-20 15:09:25 -0700625 _ = tt.devicehandler.sendProxiedOmciMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400626 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530627 })
628 }
629}
630
631func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
632 dh1 := newMockDeviceHandler()
633 dh2 := negativeDeviceHandler()
634
635 type args struct {
636 logicalPort uint32
637 packetPayload []byte
638 }
639 tests := []struct {
640 name string
641 devicehandler *DeviceHandler
642 args args
643 }{
644 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
645 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
646 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
647 }
648 for _, tt := range tests {
649 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400650 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
651 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530652 })
653 }
654}
655
656func TestDeviceHandler_DisableDevice(t *testing.T) {
657 dh1 := newMockDeviceHandler()
658 dh2 := negativeDeviceHandler()
659 type args struct {
660 device *voltha.Device
661 }
662 tests := []struct {
663 name string
664 devicehandler *DeviceHandler
665 args args
666 wantErr bool
667 }{
668 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400669 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530670 }
671 for _, tt := range tests {
672 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000673 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530674 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
675 }
676 })
677 }
678}
679
680func TestDeviceHandler_ReenableDevice(t *testing.T) {
681 dh1 := newMockDeviceHandler()
682 dh2 := negativeDeviceHandler()
683 type args struct {
684 device *voltha.Device
685 }
686 tests := []struct {
687 name string
688 devicehandler *DeviceHandler
689 args args
690 wantErr bool
691 }{
692 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
693 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
694 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
695 }
696 for _, tt := range tests {
697 t.Run(tt.name, func(t *testing.T) {
698 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000699 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530700 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
701 }
702 })
703 }
704}
705
706func TestDeviceHandler_RebootDevice(t *testing.T) {
707 dh1 := newMockDeviceHandler()
708 dh2 := newMockDeviceHandler()
709 type args struct {
710 device *voltha.Device
711 }
712 tests := []struct {
713 name string
714 devicehandler *DeviceHandler
715 args args
716 wantErr bool
717 }{
718 // TODO: Add test cases.
719 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
720 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
721 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
722 }
723 for _, tt := range tests {
724 t.Run(tt.name, func(t *testing.T) {
725
Neha Sharma96b7bf22020-06-15 10:37:32 +0000726 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530727 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
728 }
729 })
730 }
731}
732
733func TestDeviceHandler_handleIndication(t *testing.T) {
734 dh1 := newMockDeviceHandler()
735 dh2 := negativeDeviceHandler()
736 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530737 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530738 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
739 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530740
kdarapu891693b2019-09-16 12:33:49 +0530741 type args struct {
742 indication *oop.Indication
743 }
744 tests := []struct {
745 name string
746 deviceHandler *DeviceHandler
747 args args
748 }{
749 // TODO: Add test cases.
750 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
751 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
752 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
753 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
754 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
755 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
756 {"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")}}}}}},
757 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
758 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
759 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
760 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
761 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
762 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
763 {"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}}}}},
764 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
765 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
766 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
767 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
768
769 // Negative testcases
770 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
771 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
772 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
773 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
774 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
775 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
776 {"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")}}}}}},
777 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
778 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
779 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
780 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
781 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
782 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
783 {"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}}}}},
784 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
785 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
786 //
787 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
788 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
789 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
790 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
791 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
792 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
793 }
794 for _, tt := range tests {
795 t.Run(tt.name, func(t *testing.T) {
796 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530797 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
798 defer cancel()
799 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530800 })
801 }
802}
803
804func TestDeviceHandler_addPort(t *testing.T) {
805 dh1 := newMockDeviceHandler()
806 dh2 := negativeDeviceHandler()
807 type args struct {
808 intfID uint32
809 portType voltha.Port_PortType
810 state string
811 }
812 tests := []struct {
813 name string
814 devicehandler *DeviceHandler
815 args args
816 }{
817 // State up
818 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
819 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
820 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
821 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
822 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
823 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
824 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
825 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
826 // state discovery
827 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
828 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
829 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
830 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
831 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
832 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
833 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
834 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
835
836 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
837 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
838 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
839 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
840 }
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400843 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
844 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530845 })
846 }
847}
848
849func Test_macAddressToUint32Array(t *testing.T) {
850 type args struct {
851 mac string
852 }
853 tests := []struct {
854 name string
855 args args
856 want []uint32
857 }{
858 // TODO: Add test cases.
859 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
860 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
861 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
862 }
863 for _, tt := range tests {
864 t.Run(tt.name, func(t *testing.T) {
865 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
866 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
867 }
868 })
869 }
870}
871
872func TestDeviceHandler_handleOltIndication(t *testing.T) {
873
874 type args struct {
875 oltIndication *oop.OltIndication
876 }
877 tests := []struct {
878 name string
879 args args
880 }{
881 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
882 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
883 }
884 for _, tt := range tests {
885 t.Run(tt.name, func(t *testing.T) {
886 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530887 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
888 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400889 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
890 t.Error(err)
891 }
kdarapu891693b2019-09-16 12:33:49 +0530892 })
893 }
894}
895
896func TestDeviceHandler_AdoptDevice(t *testing.T) {
897 dh1 := newMockDeviceHandler()
898 dh2 := negativeDeviceHandler()
899 type args struct {
900 device *voltha.Device
901 }
902 tests := []struct {
903 name string
904 devicehandler *DeviceHandler
905 args args
906 }{
907 // TODO: Add test cases.
908 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530909 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530910 }
911 for _, tt := range tests {
912 t.Run(tt.name, func(t *testing.T) {
913 //dh.doStateInit()
914 // context.
915 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530916 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
917 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400918 if err := tt.devicehandler.postInit(ctx); err != nil {
919 t.Error(err)
920 }
kdarapu891693b2019-09-16 12:33:49 +0530921 })
922 }
923}
924
925func TestDeviceHandler_activateONU(t *testing.T) {
926 dh := newMockDeviceHandler()
927 dh1 := negativeDeviceHandler()
928 type args struct {
929 intfID uint32
930 onuID int64
931 serialNum *oop.SerialNumber
932 serialNumber string
933 }
934 tests := []struct {
935 name string
936 devicehandler *DeviceHandler
937 args args
938 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700939 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
940 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
941 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
942 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530943 }
944 for _, tt := range tests {
945 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530946 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
947 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400948 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
949 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530950 })
951 }
952}
953
954func TestDeviceHandler_start(t *testing.T) {
955 dh := newMockDeviceHandler()
956 dh1 := negativeDeviceHandler()
957 dh.start(context.Background())
958 dh.stop(context.Background())
959
960 dh1.start(context.Background())
961 dh1.stop(context.Background())
962
963}
964
965func TestDeviceHandler_PacketOut(t *testing.T) {
966 dh1 := newMockDeviceHandler()
967 dh2 := negativeDeviceHandler()
968 acts := []*ofp.OfpAction{
969 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
970 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
971 fu.Output(1),
972 }
973 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
974 type args struct {
975 egressPortNo int
976 packet *of.OfpPacketOut
977 }
978 tests := []struct {
979 name string
980 devicehandler *DeviceHandler
981 args args
982 wantErr bool
983 }{
984 // TODO: Add test cases.
985 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
986 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
987 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700988 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
989 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
990 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530991 }
992 for _, tt := range tests {
993 t.Run(tt.name, func(t *testing.T) {
994 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530995 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
996 defer cancel()
997 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530998 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
999 }
1000 })
1001 }
1002}
1003
1004//
1005func TestDeviceHandler_doStateUp(t *testing.T) {
1006 dh1 := newMockDeviceHandler()
1007 dh2 := newMockDeviceHandler()
1008
Thomas Lee S985938d2020-05-04 11:40:41 +05301009 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301010 dh3 := negativeDeviceHandler()
1011
1012 tests := []struct {
1013 name string
1014 devicehandler *DeviceHandler
1015 wantErr bool
1016 }{
1017 {"dostateup-1", dh1, false},
1018 {"dostateup-2", dh2, false},
1019 {"dostateup-3", dh3, true},
1020 }
1021 for _, tt := range tests {
1022 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301023 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1024 defer cancel()
1025 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301026 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1027 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301028 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301029 })
1030 }
1031}
1032func TestDeviceHandler_doStateDown(t *testing.T) {
1033 dh1 := newMockDeviceHandler()
1034 dh2 := negativeDeviceHandler()
1035 dh3 := newMockDeviceHandler()
1036 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1037 tests := []struct {
1038 name string
1039 devicehandler *DeviceHandler
1040 wantErr bool
1041 }{
1042 {"dostatedown-1", dh1, false},
1043 {"dostatedown-2", dh2, true},
1044 {"dostatedown-2", dh3, true},
1045 }
1046 for _, tt := range tests {
1047 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301048 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1049 defer cancel()
1050 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301051 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001052 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301053 }
1054 })
1055 }
1056}
1057
1058func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1059 dh1 := newMockDeviceHandler()
1060 dh2 := negativeDeviceHandler()
1061 type args struct {
1062 device *voltha.Device
1063 }
1064 tests := []struct {
1065 name string
1066 devicehandler *DeviceHandler
1067 args args
1068 wantErr bool
1069 }{
1070 // TODO: Add test cases.
1071 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1072 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1073 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1074 }
1075 for _, tt := range tests {
1076 t.Run(tt.name, func(t *testing.T) {
1077 dh := tt.devicehandler
1078 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1079 if (err != nil) != tt.wantErr {
1080 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1081 return
1082 }
1083 })
1084 }
1085}
1086
kdarapu891693b2019-09-16 12:33:49 +05301087func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1088
1089 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301090 dh1.discOnus = sync.Map{}
1091 dh1.discOnus.Store("onu1", true)
1092 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301093 dh1.discOnus.Store("onu3", true)
1094 dh1.discOnus.Store("onu4", true)
1095 dh1.onus = sync.Map{}
1096 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1097 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301098 dh2 := negativeDeviceHandler()
1099 type args struct {
1100 onuDiscInd *oop.OnuDiscIndication
1101 sn string
1102 }
1103 tests := []struct {
1104 name string
1105 devicehandler *DeviceHandler
1106 args args
1107 }{
1108 // TODO: Add test cases.
1109 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1110 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1111 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1112 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1113 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1114 {"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 +05301115 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1116 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1117 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301118 }
1119 for _, tt := range tests {
1120 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301121 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1122 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001123 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001124 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301125 })
1126 }
1127}
1128
1129func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1130 dh1 := newMockDeviceHandler()
1131 dh2 := negativeDeviceHandler()
1132 tests := []struct {
1133 name string
1134 devicehandler *DeviceHandler
1135
1136 wantErr bool
1137 }{
1138 // TODO: Add test cases.
1139 {"populateDeviceInfo-1", dh1, false},
1140 {"populateDeviceInfo-2", dh1, true},
1141 {"populateDeviceInfo-3", dh1, true},
1142 {"populateDeviceInfo-4", dh1, true},
1143 {"populateDeviceInfo-5", dh2, true},
1144 }
1145 for _, tt := range tests {
1146 t.Run(tt.name, func(t *testing.T) {
1147
Neha Sharma96b7bf22020-06-15 10:37:32 +00001148 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301149 if (err != nil) != tt.wantErr {
1150 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1151 return
1152 }
1153
1154 })
1155 }
1156}
1157
1158func TestDeviceHandler_readIndications(t *testing.T) {
1159 dh1 := newMockDeviceHandler()
1160 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301161 dh3 := newMockDeviceHandler()
1162 dh3.device.AdminState = voltha.AdminState_DISABLED
1163 dh4 := negativeDeviceHandler()
1164 tests := []struct {
1165 name string
1166 devicehandler *DeviceHandler
1167 }{
1168 // TODO: Add test cases.
1169 {"readIndications-1", dh1},
1170 {"readIndications-2", dh2},
1171 {"readIndications-3", dh2},
1172 {"readIndications-4", dh2},
1173 {"readIndications-5", dh2},
1174 {"readIndications-6", dh3},
1175 {"readIndications-7", dh3},
1176 {"readIndications-8", dh4},
1177 }
1178 for _, tt := range tests {
1179 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301180 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1181 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001182 _ = tt.devicehandler.readIndications(ctx)
1183 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301184 })
1185 }
1186}
Naga Manjunath7615e552019-10-11 22:35:47 +05301187
1188func Test_startCollector(t *testing.T) {
1189 type args struct {
1190 dh *DeviceHandler
1191 }
1192 dh := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001193 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301194 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1195 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1196 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1197 }
Naga Manjunath7615e552019-10-11 22:35:47 +05301198 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301199 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1200 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301201 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1202 dh.portStats.Device = dh
1203 for i := 0; i < 16; i++ {
1204 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1205 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301206 dh1 := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001207 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301208 tests := []struct {
1209 name string
1210 args args
1211 }{
1212 // TODO: Add test cases.
1213 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301214 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301215 }
1216 for _, tt := range tests {
1217 t.Run(tt.name, func(t *testing.T) {
1218 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301219 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301220 tt.args.dh.stopCollector <- true
1221 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001222 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301223 })
1224 }
1225}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001226
1227func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1228
1229 // olt disconnect (not reboot)
1230 dh1 := newMockDeviceHandler()
1231 dh1.adapterPreviouslyConnected = false
1232 dh1.agentPreviouslyConnected = true
1233
1234 // adapter restart
1235 dh2 := newMockDeviceHandler()
1236 dh2.Client = &mocks.MockOpenoltClient{}
1237 dh2.adapterPreviouslyConnected = true
1238 dh2.agentPreviouslyConnected = true
1239
1240 // first connection or olt restart
1241 dh3 := newMockDeviceHandler()
1242 dh3.Client = &mocks.MockOpenoltClient{}
1243 dh3.adapterPreviouslyConnected = false
1244 dh3.agentPreviouslyConnected = false
1245
1246 // olt and adapter restart at the same time (first case)
1247 dh4 := newMockDeviceHandler()
1248 dh4.Client = &mocks.MockOpenoltClient{}
1249 dh4.adapterPreviouslyConnected = true
1250 dh4.agentPreviouslyConnected = false
1251
1252 // adapter restart and olt disconnect at the same time
1253 dh5 := newMockDeviceHandler()
1254 dh5.Client = &mocks.MockOpenoltClient{}
1255 dh5.adapterPreviouslyConnected = true
1256 dh5.agentPreviouslyConnected = true
1257
1258 tests := []struct {
1259 name string
1260 devicehandler *DeviceHandler
1261 expectedRestart bool
1262 wantErr bool
1263 }{
1264 {"dostateup-1", dh1, true, false},
1265 {"dostateup-2", dh2, false, false},
1266 {"dostateup-3", dh3, false, false},
1267 {"dostateup-4", dh4, true, false},
1268 {"dostateup-5", dh5, false, false},
1269 }
1270 for _, tt := range tests {
1271 t.Run(tt.name, func(t *testing.T) {
1272 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1273 defer cancel()
1274 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1275 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1276 }
1277 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1278 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1279 if tt.expectedRestart != isRestarted {
1280 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1281 }
1282 })
1283 }
1284}