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