blob: 65953155da19c64b39165667b037262029c9696c [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
kdarapu381c6902019-07-31 18:23:16 +053022 "net"
kdarapu891693b2019-09-16 12:33:49 +053023 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053024 "sync"
kdarapu381c6902019-07-31 18:23:16 +053025 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053026 "time"
27
khenaidoo106c61a2021-08-11 18:05:46 -040028 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
29 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
30
31 "github.com/opencord/voltha-lib-go/v7/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
Mahir Gunyel85f61c12021-10-06 11:53:45 -070033 plt "github.com/opencord/voltha-lib-go/v7/pkg/platform"
khenaidoo106c61a2021-08-11 18:05:46 -040034 "github.com/opencord/voltha-lib-go/v7/pkg/pmmetrics"
35 ponrmgr "github.com/opencord/voltha-lib-go/v7/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"
khenaidoo106c61a2021-08-11 18:05:46 -040040 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
41 of "github.com/opencord/voltha-protos/v5/go/openflow_13"
42 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
43 oop "github.com/opencord/voltha-protos/v5/go/openolt"
44 "github.com/opencord/voltha-protos/v5/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
khenaidoo106c61a2021-08-11 18:05:46 -040059func newMockOnuInterAdapterService() *mocks.MockOnuInterAdapterService {
60 return &mocks.MockOnuInterAdapterService{}
61}
62
63func newMockCoreService() *mocks.MockCoreService {
64 mcp := mocks.MockCoreService{
Kent Hagermanf1db18b2020-07-08 13:38:15 -040065 Devices: make(map[string]*voltha.Device),
66 DevicePorts: make(map[string][]*voltha.Port),
67 }
Naga Manjunath7615e552019-10-11 22:35:47 +053068 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053069 mcp.Devices["olt"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040070 Id: "olt",
71 Root: true,
72 ParentId: "logical_device",
73 ParentPortNo: 1,
74 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +053075 ProxyAddress: &voltha.Device_ProxyAddress{
76 DeviceId: "olt",
77 DeviceType: "onu",
78 ChannelId: 1,
79 ChannelGroupId: 1,
80 },
81 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053082 PmConfigs: &voltha.PmConfigs{
83 DefaultFreq: 10,
84 Id: "olt",
85 FreqOverride: false,
86 Grouped: false,
87 Metrics: pm,
88 },
kdarapu891693b2019-09-16 12:33:49 +053089 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040090 mcp.DevicePorts["olt"] = []*voltha.Port{
91 {PortNo: 1, Label: "pon"},
92 {PortNo: 2, Label: "nni"},
93 }
kdarapu891693b2019-09-16 12:33:49 +053094
Kent Hagermanf1db18b2020-07-08 13:38:15 -040095 mcp.Devices["onu1"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040096 Id: "1",
97 Root: false,
98 ParentId: "olt",
99 ParentPortNo: 1,
100 AdapterEndpoint: "mock-onu-endpoint",
101 OperStatus: 4,
kdarapu891693b2019-09-16 12:33:49 +0530102 ProxyAddress: &voltha.Device_ProxyAddress{
103 OnuId: 1,
104 ChannelId: 1,
105 ChannelGroupId: 1,
106 },
107 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530108 PmConfigs: &voltha.PmConfigs{
109 DefaultFreq: 10,
110 Id: "olt",
111 FreqOverride: false,
112 Grouped: false,
113 Metrics: pm,
114 },
kdarapu891693b2019-09-16 12:33:49 +0530115 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400116 mcp.DevicePorts["onu1"] = []*voltha.Port{
117 {PortNo: 1, Label: "pon"},
118 {PortNo: 2, Label: "uni"},
119 }
120
kdarapu891693b2019-09-16 12:33:49 +0530121 mcp.Devices["onu2"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400122 Id: "2",
123 Root: false,
124 ParentId: "olt",
125 OperStatus: 2,
126 AdapterEndpoint: "mock-onu-endpoint",
127 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +0530128
129 ProxyAddress: &voltha.Device_ProxyAddress{
130 OnuId: 2,
131 ChannelId: 1,
132 ChannelGroupId: 1,
133 },
134 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530135 PmConfigs: &voltha.PmConfigs{
136 DefaultFreq: 10,
137 Id: "olt",
138 FreqOverride: false,
139 Grouped: false,
140 Metrics: pm,
141 },
kdarapu891693b2019-09-16 12:33:49 +0530142 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400143 mcp.DevicePorts["onu2"] = []*voltha.Port{
144 {PortNo: 1, Label: "pon"},
145 {PortNo: 2, Label: "uni"},
146 }
kdarapu891693b2019-09-16 12:33:49 +0530147 return &mcp
148}
149func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530150 device := &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400151 Id: "olt",
152 Root: true,
153 ParentId: "logical_device",
154 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +0530155 ProxyAddress: &voltha.Device_ProxyAddress{
156 DeviceId: "olt",
157 DeviceType: "onu",
158 ChannelId: 1,
159 ChannelGroupId: 1,
160 },
161 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530162 }
khenaidoo106c61a2021-08-11 18:05:46 -0400163 mcs := newMockCoreService()
164 cc := mocks.NewMockCoreClient(mcs)
kdarapu891693b2019-09-16 12:33:49 +0530165 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800166 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700167 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
kesavand494c2082020-08-31 11:16:12 +0530168 cfg := &config.AdapterFlags{OmccEncryption: true}
khenaidoo106c61a2021-08-11 18:05:46 -0400169 openOLT := &OpenOLT{eventProxy: ep, config: cfg}
170 dh := NewDeviceHandler(cc, ep, device, openOLT, cm, cfg)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700171 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700172 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 -0700173 Technology: "xgs-pon",
174 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
175 }}
176
Girish Gowdra9602eb42020-09-09 15:50:39 -0700177 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700178 dh.deviceInfo = deviceInf
khenaidoo106c61a2021-08-11 18:05:46 -0400179 dh.device = device
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700180 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
181 var i uint32
182 for i = 0; i < deviceInf.PonPorts; i++ {
183 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
184 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700185 StoreType: "etcd",
186 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700187 }}
188 dh.resourceMgr[i].InitLocalCache()
189 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700190
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530191 ranges := make(map[string]interface{})
192 sharedIdxByType := make(map[string]string)
193 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
194 sharedIdxByType["ONU_ID"] = "ONU_ID"
195 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
196 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
197 ranges["ONU_ID"] = uint32(0)
198 ranges["GEMPORT_ID"] = uint32(0)
199 ranges["ALLOC_ID"] = uint32(0)
200 ranges["FLOW_ID"] = uint32(0)
201 ranges["onu_id_shared"] = uint32(0)
202 ranges["alloc_id_shared"] = uint32(0)
203 ranges["gemport_id_shared"] = uint32(0)
204 ranges["flow_id_shared"] = uint32(0)
205
Matteo Scandolo84585372021-03-18 14:21:22 -0700206 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700207 ponmgr.DeviceID = "onu-1"
208 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
209 ponmgr.KVStore = &db.Backend{
210 Client: &mocks.MockKVClient{},
211 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700212 ponmgr.KVStoreForConfig = &db.Backend{
213 Client: &mocks.MockKVClient{},
214 }
215 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700216 ponmgr.PonResourceRanges = ranges
217 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700218 ponmgr.Technology = "XGS-PON"
219 for i = 0; i < deviceInf.PonPorts; i++ {
220 dh.resourceMgr[i].PonRsrMgr = ponmgr
221 }
222
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 dh.flowMgr[i] = &OpenOltFlowMgr{}
230 dh.flowMgr[i].deviceHandler = dh
231 dh.flowMgr[i].ponPortIdx = i
232 dh.flowMgr[i].grpMgr = dh.groupMgr
233 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700234 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
235 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
236 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
237 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
238
Girish Gowdra76a1b092021-07-28 10:07:04 -0700239 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
240
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700241 // Create a slice of buffered channels for handling concurrent flows per ONU.
242 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700243 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, plt.MaxOnusPerPon+1)
244 dh.flowMgr[i].stopFlowHandlerRoutine = make([]chan bool, plt.MaxOnusPerPon+1)
245 dh.flowMgr[i].flowHandlerRoutineActive = make([]bool, plt.MaxOnusPerPon+1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700246 for j := range dh.flowMgr[i].incomingFlows {
247 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700248 dh.flowMgr[i].stopFlowHandlerRoutine[j] = make(chan bool, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700249 // Spin up a go routine to handling incoming flows (add/remove).
250 // There will be on go routine per ONU.
251 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700252 dh.flowMgr[i].flowHandlerRoutineActive[j] = true
253 go dh.flowMgr[i].perOnuFlowHandlerRoutine(j, dh.flowMgr[i].incomingFlows[j], dh.flowMgr[i].stopFlowHandlerRoutine[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700254 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700255 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700256 }
kdarapu891693b2019-09-16 12:33:49 +0530257 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500258 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530259 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530260 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000261
262 var pmNames = []string{
263 "rx_bytes",
264 "rx_packets",
265 "rx_mcast_packets",
266 "rx_bcast_packets",
267 "tx_bytes",
268 "tx_packets",
269 "tx_mcast_packets",
270 "tx_bcast_packets",
271 }
272
273 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
khenaidoo106c61a2021-08-11 18:05:46 -0400274
275 // Set the children endpoints
276 dh.childAdapterClients = map[string]*vgrpc.Client{
277 "mock-onu-endpoint": mocks.NewMockChildAdapterClient(newMockOnuInterAdapterService()),
278 }
kdarapu891693b2019-09-16 12:33:49 +0530279 return dh
kdarapu381c6902019-07-31 18:23:16 +0530280}
281
kdarapu891693b2019-09-16 12:33:49 +0530282func negativeDeviceHandler() *DeviceHandler {
283 dh := newMockDeviceHandler()
284 device := dh.device
285 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530286 return dh
287}
Girish Gowdra0fb24a32021-10-27 15:15:27 -0700288
289func negativeDeviceHandlerNilFlowMgr() *DeviceHandler {
290 dh := newMockDeviceHandler()
291 dh.flowMgr = nil
292 return dh
293}
294
kdarapu381c6902019-07-31 18:23:16 +0530295func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000296 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530297 type args struct {
298 host string
299 }
300 tests := []struct {
301 name string
302 args args
303 want string
304 wantErr bool
305 }{
kdarapu891693b2019-09-16 12:33:49 +0530306 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
307 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
308 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
309 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530310 }
311 for _, tt := range tests {
312 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000313 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530314 if (err != nil) != tt.wantErr {
315 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
316 return
317 }
318 if got != tt.want {
319 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
320 }
321 })
322 }
323}
324func Test_macifyIP(t *testing.T) {
325 type args struct {
326 ip net.IP
327 }
328 tests := []struct {
329 name string
330 args args
331 want string
332 }{{
kdarapu891693b2019-09-16 12:33:49 +0530333 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530334 args{ip: net.ParseIP("10.10.10.10")},
335 "00:00:0a:0a:0a:0a",
336 },
337 {
kdarapu891693b2019-09-16 12:33:49 +0530338 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530339 args{ip: net.ParseIP("127.0.0.1")},
340 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530341 },
342 {
343 "macifyIP-3",
344 args{ip: net.ParseIP("127.0.0.1/24")},
345 "",
346 },
347 }
kdarapu381c6902019-07-31 18:23:16 +0530348 for _, tt := range tests {
349 t.Run(tt.name, func(t *testing.T) {
350 if got := macifyIP(tt.args.ip); got != tt.want {
351 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
352 }
353 })
354 }
355}
356
David K. Bainbridge794735f2020-02-11 21:01:37 -0800357func sparseCompare(keys []string, spec, target interface{}) bool {
358 if spec == target {
359 return true
360 }
361 if spec == nil || target == nil {
362 return false
363 }
364 typeSpec := reflect.TypeOf(spec)
365 typeTarget := reflect.TypeOf(target)
366 if typeSpec != typeTarget {
367 return false
368 }
369
370 vSpec := reflect.ValueOf(spec)
371 vTarget := reflect.ValueOf(target)
372 if vSpec.Kind() == reflect.Ptr {
373 vSpec = vSpec.Elem()
374 vTarget = vTarget.Elem()
375 }
376
377 for _, key := range keys {
378 fSpec := vSpec.FieldByName(key)
379 fTarget := vTarget.FieldByName(key)
380 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
381 return false
382 }
383 }
384 return true
385}
386
kdarapu381c6902019-07-31 18:23:16 +0530387func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000388 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530389 dh1 := newMockDeviceHandler()
390 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530391 type args struct {
392 parentPort uint32
393 onuID uint32
394 }
395 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530396 name string
397 devicehandler *DeviceHandler
398 args args
399 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800400 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530401 }{
kdarapu891693b2019-09-16 12:33:49 +0530402 {"GetChildDevice-1", dh1,
403 args{parentPort: 1,
404 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800405 &voltha.Device{
406 Id: "1",
407 ParentId: "olt",
408 ParentPortNo: 1,
409 },
410 nil,
kdarapu891693b2019-09-16 12:33:49 +0530411 },
412 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530413 args{parentPort: 1,
414 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800415 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530416 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530417 },
418 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800419
420 /*
421 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
422 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>)
423 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
424 */
kdarapu381c6902019-07-31 18:23:16 +0530425 for _, tt := range tests {
426 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000427 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800428 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
429 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
430 tt.want, tt.errType, got, reflect.TypeOf(err))
431 return
432 }
kdarapu381c6902019-07-31 18:23:16 +0530433 t.Log("onu device id", got)
434 })
435 }
436}
kdarapu891693b2019-09-16 12:33:49 +0530437
438func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530439 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530440 type args struct {
441 portNum uint32
442 portType voltha.Port_PortType
443 }
444 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800445 name string
446 args args
447 want string
448 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530449 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800450 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
451 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
452 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
453 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
454 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
455 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
456 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530457 }
458 for _, tt := range tests {
459 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800460 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
461 if reflect.TypeOf(err) != tt.errType || got != tt.want {
462 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
463 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530464 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800465
kdarapu891693b2019-09-16 12:33:49 +0530466 })
467 }
468}
469
khenaidoo106c61a2021-08-11 18:05:46 -0400470// func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
471// ctx := context.Background()
472// dh := newMockDeviceHandler()
473// proxyAddr := dh.device.ProxyAddress
474// body := &ic.InterAdapterOmciMessage{
475// Message: []byte("asdfasdfasdfasdfas"),
476// ProxyAddress: proxyAddr,
477// }
478// body2 := &ic.InterAdapterOmciMessage{
479// Message: []byte("asdfasdfasdfasdfas"),
480// //ProxyAddress: &voltha.Device_ProxyAddress{},
481// }
482// body3 := &ic.InterAdapterTechProfileDownloadMessage{}
483// var marshalledData *any.Any
484// var err error
kdarapu891693b2019-09-16 12:33:49 +0530485
khenaidoo106c61a2021-08-11 18:05:46 -0400486// if marshalledData, err = ptypes.MarshalAny(body); err != nil {
487// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
488// }
kdarapu891693b2019-09-16 12:33:49 +0530489
khenaidoo106c61a2021-08-11 18:05:46 -0400490// var marshalledData1 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530491
khenaidoo106c61a2021-08-11 18:05:46 -0400492// if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
493// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
494// }
495// var marshalledData2 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530496
khenaidoo106c61a2021-08-11 18:05:46 -0400497// if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
498// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
499// }
500// type args struct {
501// msg *ic.InterAdapterMessage
502// }
503// invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
504// tests := []struct {
505// name string
506// args args
507// wantErr reflect.Type
508// }{
509// {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
510// Header: &ic.InterAdapterHeader{
511// Id: "012345",
512// Type: ic.InterAdapterMessageType_FLOW_REQUEST,
513// },
514// Body: marshalledData,
515// }}, invalid},
516// {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
517// Header: &ic.InterAdapterHeader{
518// Id: "012345",
519// Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
520// },
521// Body: marshalledData1,
522// }}, invalid},
523// {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
524// Header: &ic.InterAdapterHeader{
525// Id: "012345",
526// Type: ic.InterAdapterMessageType_OMCI_REQUEST,
527// },
528// Body: marshalledData,
529// }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
530// {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
531// Header: &ic.InterAdapterHeader{
532// Id: "012345",
533// Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
534// }, Body: marshalledData,
535// }}, invalid},
536// {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
537// Header: &ic.InterAdapterHeader{
538// Id: "012345",
539// Type: ic.InterAdapterMessageType_METRICS_REQUEST,
540// }, Body: marshalledData1,
541// }}, invalid},
542// {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
543// Header: &ic.InterAdapterHeader{
544// Id: "012345",
545// Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
546// }, Body: marshalledData,
547// }}, invalid},
548// {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
549// Header: &ic.InterAdapterHeader{
550// Id: "012345",
551// Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
552// }, Body: marshalledData,
553// }}, invalid},
554// {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
555// Header: &ic.InterAdapterHeader{
556// Id: "012345",
557// Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
558// }, Body: marshalledData,
559// }}, invalid},
560// {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
561// Header: &ic.InterAdapterHeader{
562// Id: "012345",
563// Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
564// }, Body: marshalledData,
565// }}, invalid},
566// {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
567// Header: &ic.InterAdapterHeader{
568// Id: "012345",
569// Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
570// }, Body: marshalledData2,
571// }}, invalid},
572// {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
573// Header: &ic.InterAdapterHeader{
574// Id: "012345",
575// Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
576// }, Body: marshalledData2,
577// }}, invalid},
578// }
579// for _, tt := range tests {
580// t.Run(tt.name, func(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530581
khenaidoo106c61a2021-08-11 18:05:46 -0400582// if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
583// t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
584// }
585// })
586// }
587// }
kdarapu891693b2019-09-16 12:33:49 +0530588
khenaidoo106c61a2021-08-11 18:05:46 -0400589func TestDeviceHandler_ProxyOmciMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000590 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530591 dh1 := newMockDeviceHandler()
592 dh2 := negativeDeviceHandler()
593 device1 := &voltha.Device{
594 Id: "onu1",
595 Root: false,
596 ParentId: "logical_device",
597 ProxyAddress: &voltha.Device_ProxyAddress{
598 DeviceId: "onu1",
599 DeviceType: "onu",
600 ChannelId: 1,
601 ChannelGroupId: 1,
602 },
603 ConnectStatus: 1,
604 }
605 device2 := device1
606 device2.ConnectStatus = 2
khenaidoo106c61a2021-08-11 18:05:46 -0400607 iaomciMsg1 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530608 ProxyAddress: &voltha.Device_ProxyAddress{
609 DeviceId: "onu2",
610 DeviceType: "onu",
611 ChannelId: 1,
612 ChannelGroupId: 1,
kdarapu891693b2019-09-16 12:33:49 +0530613 },
614 ConnectStatus: 1,
615 }
khenaidoo106c61a2021-08-11 18:05:46 -0400616 iaomciMsg2 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530617 ProxyAddress: &voltha.Device_ProxyAddress{
618 DeviceId: "onu3",
619 DeviceType: "onu",
620 ChannelId: 1,
621 ChannelGroupId: 1,
622 },
623 ConnectStatus: 1,
624 }
625 type args struct {
626 onuDevice *voltha.Device
khenaidoo106c61a2021-08-11 18:05:46 -0400627 omciMsg *ic.OmciMessage
kdarapu891693b2019-09-16 12:33:49 +0530628 }
629 tests := []struct {
630 name string
631 devicehandler *DeviceHandler
632 args args
633 }{
khenaidoo106c61a2021-08-11 18:05:46 -0400634 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
635 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530636 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
637 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
638 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
khenaidoo106c61a2021-08-11 18:05:46 -0400639 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530640 }
641 for _, tt := range tests {
642 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400643 _ = tt.devicehandler.ProxyOmciMessage(ctx, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400644 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530645 })
646 }
647}
648
649func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
650 dh1 := newMockDeviceHandler()
651 dh2 := negativeDeviceHandler()
652
653 type args struct {
654 logicalPort uint32
655 packetPayload []byte
656 }
657 tests := []struct {
658 name string
659 devicehandler *DeviceHandler
660 args args
661 }{
662 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
663 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
664 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
665 }
666 for _, tt := range tests {
667 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400668 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
669 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530670 })
671 }
672}
673
674func TestDeviceHandler_DisableDevice(t *testing.T) {
675 dh1 := newMockDeviceHandler()
676 dh2 := negativeDeviceHandler()
677 type args struct {
678 device *voltha.Device
679 }
680 tests := []struct {
681 name string
682 devicehandler *DeviceHandler
683 args args
684 wantErr bool
685 }{
686 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400687 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530688 }
689 for _, tt := range tests {
690 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000691 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530692 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
693 }
694 })
695 }
696}
697
698func TestDeviceHandler_ReenableDevice(t *testing.T) {
699 dh1 := newMockDeviceHandler()
700 dh2 := negativeDeviceHandler()
701 type args struct {
702 device *voltha.Device
703 }
704 tests := []struct {
705 name string
706 devicehandler *DeviceHandler
707 args args
708 wantErr bool
709 }{
710 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
711 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
712 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
713 }
714 for _, tt := range tests {
715 t.Run(tt.name, func(t *testing.T) {
716 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000717 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530718 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
719 }
720 })
721 }
722}
723
724func TestDeviceHandler_RebootDevice(t *testing.T) {
725 dh1 := newMockDeviceHandler()
726 dh2 := newMockDeviceHandler()
727 type args struct {
728 device *voltha.Device
729 }
730 tests := []struct {
731 name string
732 devicehandler *DeviceHandler
733 args args
734 wantErr bool
735 }{
736 // TODO: Add test cases.
737 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
738 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
739 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
740 }
741 for _, tt := range tests {
742 t.Run(tt.name, func(t *testing.T) {
743
Neha Sharma96b7bf22020-06-15 10:37:32 +0000744 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530745 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
746 }
747 })
748 }
749}
750
751func TestDeviceHandler_handleIndication(t *testing.T) {
752 dh1 := newMockDeviceHandler()
753 dh2 := negativeDeviceHandler()
754 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530755 dh3.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -0400756 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false, "mock_endpoint"))
757 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false, "mock_endpoint"))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530758
kdarapu891693b2019-09-16 12:33:49 +0530759 type args struct {
760 indication *oop.Indication
761 }
762 tests := []struct {
763 name string
764 deviceHandler *DeviceHandler
765 args args
766 }{
767 // TODO: Add test cases.
768 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
769 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
770 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
771 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
772 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
773 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
774 {"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")}}}}}},
775 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
776 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
777 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
778 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
779 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
780 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
781 {"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}}}}},
782 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
783 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
784 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
785 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
786
787 // Negative testcases
788 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
789 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
790 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
791 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
792 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
793 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
794 {"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")}}}}}},
795 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
796 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
797 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
798 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
799 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
800 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
801 {"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}}}}},
802 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
803 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
804 //
805 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
806 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
807 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
808 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
809 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
810 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
811 }
812 for _, tt := range tests {
813 t.Run(tt.name, func(t *testing.T) {
814 dh := tt.deviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -0400815 time.Sleep(5 * time.Millisecond)
npujarec5762e2020-01-01 14:08:48 +0530816 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
817 defer cancel()
818 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530819 })
820 }
821}
822
823func TestDeviceHandler_addPort(t *testing.T) {
824 dh1 := newMockDeviceHandler()
825 dh2 := negativeDeviceHandler()
826 type args struct {
827 intfID uint32
828 portType voltha.Port_PortType
829 state string
830 }
831 tests := []struct {
832 name string
833 devicehandler *DeviceHandler
834 args args
835 }{
836 // State up
837 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
838 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
839 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
840 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
841 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
842 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
843 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
844 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
845 // state discovery
846 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
847 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
848 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
849 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
850 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
851 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
852 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
853 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
854
855 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
856 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
857 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
858 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
859 }
860 for _, tt := range tests {
861 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400862 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
863 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530864 })
865 }
866}
867
868func Test_macAddressToUint32Array(t *testing.T) {
869 type args struct {
870 mac string
871 }
872 tests := []struct {
873 name string
874 args args
875 want []uint32
876 }{
877 // TODO: Add test cases.
878 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
879 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
880 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
881 }
882 for _, tt := range tests {
883 t.Run(tt.name, func(t *testing.T) {
884 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
885 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
886 }
887 })
888 }
889}
890
891func TestDeviceHandler_handleOltIndication(t *testing.T) {
892
893 type args struct {
894 oltIndication *oop.OltIndication
895 }
896 tests := []struct {
897 name string
898 args args
899 }{
900 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
901 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
902 }
903 for _, tt := range tests {
904 t.Run(tt.name, func(t *testing.T) {
905 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530906 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
907 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400908 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
909 t.Error(err)
910 }
kdarapu891693b2019-09-16 12:33:49 +0530911 })
912 }
913}
914
915func TestDeviceHandler_AdoptDevice(t *testing.T) {
916 dh1 := newMockDeviceHandler()
917 dh2 := negativeDeviceHandler()
918 type args struct {
919 device *voltha.Device
920 }
921 tests := []struct {
922 name string
923 devicehandler *DeviceHandler
924 args args
925 }{
926 // TODO: Add test cases.
927 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530928 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530929 }
930 for _, tt := range tests {
931 t.Run(tt.name, func(t *testing.T) {
932 //dh.doStateInit()
933 // context.
934 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530935 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
936 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400937 if err := tt.devicehandler.postInit(ctx); err != nil {
938 t.Error(err)
939 }
kdarapu891693b2019-09-16 12:33:49 +0530940 })
941 }
942}
943
944func TestDeviceHandler_activateONU(t *testing.T) {
945 dh := newMockDeviceHandler()
946 dh1 := negativeDeviceHandler()
947 type args struct {
948 intfID uint32
949 onuID int64
950 serialNum *oop.SerialNumber
951 serialNumber string
952 }
953 tests := []struct {
954 name string
955 devicehandler *DeviceHandler
956 args args
957 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700958 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
959 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
960 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
961 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530962 }
963 for _, tt := range tests {
964 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530965 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
966 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400967 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
968 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530969 })
970 }
971}
972
973func TestDeviceHandler_start(t *testing.T) {
974 dh := newMockDeviceHandler()
975 dh1 := negativeDeviceHandler()
976 dh.start(context.Background())
977 dh.stop(context.Background())
978
979 dh1.start(context.Background())
980 dh1.stop(context.Background())
981
982}
983
984func TestDeviceHandler_PacketOut(t *testing.T) {
985 dh1 := newMockDeviceHandler()
986 dh2 := negativeDeviceHandler()
987 acts := []*ofp.OfpAction{
988 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
989 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
990 fu.Output(1),
991 }
992 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
993 type args struct {
994 egressPortNo int
995 packet *of.OfpPacketOut
996 }
997 tests := []struct {
998 name string
999 devicehandler *DeviceHandler
1000 args args
1001 wantErr bool
1002 }{
1003 // TODO: Add test cases.
1004 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
1005 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
1006 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -07001007 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001008 {"PacketOut-4", dh1, args{egressPortNo: 16777217, packet: pktout}, false},
1009 {"PacketOut-5", dh2, args{egressPortNo: 16777216, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +05301010 }
1011 for _, tt := range tests {
1012 t.Run(tt.name, func(t *testing.T) {
1013 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +05301014 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1015 defer cancel()
khenaidoo106c61a2021-08-11 18:05:46 -04001016 if err := dh.PacketOut(ctx, uint32(tt.args.egressPortNo), tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301017 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
1018 }
1019 })
1020 }
1021}
1022
1023//
1024func TestDeviceHandler_doStateUp(t *testing.T) {
1025 dh1 := newMockDeviceHandler()
1026 dh2 := newMockDeviceHandler()
1027
Thomas Lee S985938d2020-05-04 11:40:41 +05301028 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301029 dh3 := negativeDeviceHandler()
1030
1031 tests := []struct {
1032 name string
1033 devicehandler *DeviceHandler
1034 wantErr bool
1035 }{
1036 {"dostateup-1", dh1, false},
1037 {"dostateup-2", dh2, false},
1038 {"dostateup-3", dh3, true},
1039 }
1040 for _, tt := range tests {
1041 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301042 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1043 defer cancel()
1044 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301045 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1046 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301047 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301048 })
1049 }
1050}
1051func TestDeviceHandler_doStateDown(t *testing.T) {
1052 dh1 := newMockDeviceHandler()
1053 dh2 := negativeDeviceHandler()
1054 dh3 := newMockDeviceHandler()
1055 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1056 tests := []struct {
1057 name string
1058 devicehandler *DeviceHandler
1059 wantErr bool
1060 }{
1061 {"dostatedown-1", dh1, false},
1062 {"dostatedown-2", dh2, true},
1063 {"dostatedown-2", dh3, true},
1064 }
1065 for _, tt := range tests {
1066 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301067 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1068 defer cancel()
1069 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301070 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001071 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301072 }
1073 })
1074 }
1075}
1076
1077func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1078 dh1 := newMockDeviceHandler()
1079 dh2 := negativeDeviceHandler()
1080 type args struct {
1081 device *voltha.Device
1082 }
1083 tests := []struct {
1084 name string
1085 devicehandler *DeviceHandler
1086 args args
1087 wantErr bool
1088 }{
1089 // TODO: Add test cases.
1090 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1091 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1092 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1093 }
1094 for _, tt := range tests {
1095 t.Run(tt.name, func(t *testing.T) {
1096 dh := tt.devicehandler
1097 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1098 if (err != nil) != tt.wantErr {
1099 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1100 return
1101 }
1102 })
1103 }
1104}
1105
kdarapu891693b2019-09-16 12:33:49 +05301106func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1107
1108 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301109 dh1.discOnus = sync.Map{}
1110 dh1.discOnus.Store("onu1", true)
1111 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301112 dh1.discOnus.Store("onu3", true)
1113 dh1.discOnus.Store("onu4", true)
1114 dh1.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -04001115 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true, "mock_endpoint"))
1116 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true, "mock_endpoint"))
kdarapu891693b2019-09-16 12:33:49 +05301117 dh2 := negativeDeviceHandler()
1118 type args struct {
1119 onuDiscInd *oop.OnuDiscIndication
1120 sn string
1121 }
1122 tests := []struct {
1123 name string
1124 devicehandler *DeviceHandler
1125 args args
1126 }{
1127 // TODO: Add test cases.
1128 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1129 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1130 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1131 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1132 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1133 {"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 +05301134 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1135 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1136 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301137 }
1138 for _, tt := range tests {
1139 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301140 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1141 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001142 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001143 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301144 })
1145 }
1146}
1147
1148func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1149 dh1 := newMockDeviceHandler()
1150 dh2 := negativeDeviceHandler()
1151 tests := []struct {
1152 name string
1153 devicehandler *DeviceHandler
1154
1155 wantErr bool
1156 }{
1157 // TODO: Add test cases.
1158 {"populateDeviceInfo-1", dh1, false},
1159 {"populateDeviceInfo-2", dh1, true},
1160 {"populateDeviceInfo-3", dh1, true},
1161 {"populateDeviceInfo-4", dh1, true},
1162 {"populateDeviceInfo-5", dh2, true},
1163 }
1164 for _, tt := range tests {
1165 t.Run(tt.name, func(t *testing.T) {
1166
Neha Sharma96b7bf22020-06-15 10:37:32 +00001167 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301168 if (err != nil) != tt.wantErr {
1169 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1170 return
1171 }
1172
1173 })
1174 }
1175}
1176
1177func TestDeviceHandler_readIndications(t *testing.T) {
1178 dh1 := newMockDeviceHandler()
1179 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301180 dh3 := newMockDeviceHandler()
1181 dh3.device.AdminState = voltha.AdminState_DISABLED
1182 dh4 := negativeDeviceHandler()
1183 tests := []struct {
1184 name string
1185 devicehandler *DeviceHandler
1186 }{
1187 // TODO: Add test cases.
1188 {"readIndications-1", dh1},
1189 {"readIndications-2", dh2},
1190 {"readIndications-3", dh2},
1191 {"readIndications-4", dh2},
1192 {"readIndications-5", dh2},
1193 {"readIndications-6", dh3},
1194 {"readIndications-7", dh3},
1195 {"readIndications-8", dh4},
1196 }
1197 for _, tt := range tests {
1198 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301199 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1200 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001201 _ = tt.devicehandler.readIndications(ctx)
1202 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301203 })
1204 }
1205}
Naga Manjunath7615e552019-10-11 22:35:47 +05301206
1207func Test_startCollector(t *testing.T) {
1208 type args struct {
1209 dh *DeviceHandler
1210 }
1211 dh := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001212 mcs := newMockCoreService()
1213 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301214 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001215 {PortNo: 16777216, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1216 {PortNo: 16777218, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301217 }
khenaidoo106c61a2021-08-11 18:05:46 -04001218 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301219 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301220 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1221 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301222 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1223 dh.portStats.Device = dh
1224 for i := 0; i < 16; i++ {
1225 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1226 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301227 dh1 := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001228 mcs = newMockCoreService()
1229 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{}
1230 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301231 tests := []struct {
1232 name string
1233 args args
1234 }{
1235 // TODO: Add test cases.
1236 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301237 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301238 }
1239 for _, tt := range tests {
1240 t.Run(tt.name, func(t *testing.T) {
1241 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301242 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301243 tt.args.dh.stopCollector <- true
1244 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001245 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301246 })
1247 }
1248}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001249
1250func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1251
1252 // olt disconnect (not reboot)
1253 dh1 := newMockDeviceHandler()
1254 dh1.adapterPreviouslyConnected = false
1255 dh1.agentPreviouslyConnected = true
1256
1257 // adapter restart
1258 dh2 := newMockDeviceHandler()
1259 dh2.Client = &mocks.MockOpenoltClient{}
1260 dh2.adapterPreviouslyConnected = true
1261 dh2.agentPreviouslyConnected = true
1262
1263 // first connection or olt restart
1264 dh3 := newMockDeviceHandler()
1265 dh3.Client = &mocks.MockOpenoltClient{}
1266 dh3.adapterPreviouslyConnected = false
1267 dh3.agentPreviouslyConnected = false
1268
1269 // olt and adapter restart at the same time (first case)
1270 dh4 := newMockDeviceHandler()
1271 dh4.Client = &mocks.MockOpenoltClient{}
1272 dh4.adapterPreviouslyConnected = true
1273 dh4.agentPreviouslyConnected = false
1274
1275 // adapter restart and olt disconnect at the same time
1276 dh5 := newMockDeviceHandler()
1277 dh5.Client = &mocks.MockOpenoltClient{}
1278 dh5.adapterPreviouslyConnected = true
1279 dh5.agentPreviouslyConnected = true
1280
1281 tests := []struct {
1282 name string
1283 devicehandler *DeviceHandler
1284 expectedRestart bool
1285 wantErr bool
1286 }{
1287 {"dostateup-1", dh1, true, false},
1288 {"dostateup-2", dh2, false, false},
1289 {"dostateup-3", dh3, false, false},
1290 {"dostateup-4", dh4, true, false},
1291 {"dostateup-5", dh5, false, false},
1292 }
1293 for _, tt := range tests {
1294 t.Run(tt.name, func(t *testing.T) {
1295 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1296 defer cancel()
1297 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1298 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1299 }
1300 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1301 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1302 if tt.expectedRestart != isRestarted {
1303 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1304 }
1305 })
1306 }
1307}
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001308
1309func Test_UpdateFlowsIncrementallyNegativeTestCases(t *testing.T) {
1310 dh1 := negativeDeviceHandlerNilFlowMgr()
1311 tests := []struct {
1312 name string
1313 devicehandler *DeviceHandler
1314 wantErr bool
1315 }{
1316 {"update-flow-when-device-handler-is-nil", dh1, true},
1317 }
1318
1319 flowMetadata0 := voltha.FlowMetadata{Meters: []*voltha.OfpMeterConfig{
1320 {
1321 Flags: 5,
1322 MeterId: 1,
1323 Bands: []*voltha.OfpMeterBandHeader{
1324 {
1325 Type: voltha.OfpMeterBandType_OFPMBT_DROP,
1326 Rate: 16000,
1327 BurstSize: 0,
1328 },
1329 {
1330 Type: voltha.OfpMeterBandType_OFPMBT_DROP,
1331 Rate: 32000,
1332 BurstSize: 30,
1333 },
1334 {
1335 Type: voltha.OfpMeterBandType_OFPMBT_DROP,
1336 Rate: 64000,
1337 BurstSize: 30,
1338 },
1339 },
1340 },
1341 }}
1342
1343 kwTable0Meter1 := make(map[string]uint64)
1344 kwTable0Meter1["table_id"] = 0
1345 kwTable0Meter1["meter_id"] = 1
1346 kwTable0Meter1["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
1347
1348 // Upstream flow DHCP flow - ONU1 UNI0 PON0
1349 fa0 := &fu.FlowArgs{
1350 MatchFields: []*ofp.OfpOxmOfbField{
1351 fu.InPort(536870912),
1352 fu.Metadata_ofp(1),
1353 fu.IpProto(17), // dhcp
1354 fu.VlanPcp(0),
1355 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
1356 fu.TunnelId(256),
1357 },
1358 Actions: []*ofp.OfpAction{
1359 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
1360 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
1361 fu.Output(2147483645),
1362 fu.PushVlan(0x8100),
1363 },
1364 KV: kwTable0Meter1,
1365 }
1366
1367 flow0, _ := fu.MkFlowStat(fa0)
1368 flowAdd := of.Flows{Items: make([]*voltha.OfpFlowStats, 0)}
1369 flowAdd.Items = append(flowAdd.Items, flow0)
1370 flowRemove := of.Flows{Items: make([]*voltha.OfpFlowStats, 0)}
1371 flowChanges := &ofp.FlowChanges{ToAdd: &flowAdd, ToRemove: &flowRemove}
1372
1373 for _, tt := range tests {
1374 t.Run(tt.name, func(t *testing.T) {
1375 err := tt.devicehandler.UpdateFlowsIncrementally(context.Background(), tt.devicehandler.device, flowChanges, nil, &flowMetadata0)
1376 if (err != nil) != tt.wantErr {
1377 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1378 return
1379 }
1380 })
1381 }
1382}