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