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