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