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