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