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 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 28 | "github.com/golang/protobuf/ptypes" |
| 29 | "github.com/golang/protobuf/ptypes/any" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 31 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 33 | "github.com/opencord/voltha-lib-go/v3/pkg/pmmetrics" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 34 | ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager" |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 35 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 36 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager" |
| 37 | "github.com/opencord/voltha-openolt-adapter/pkg/mocks" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 38 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 39 | of "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 40 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 41 | oop "github.com/opencord/voltha-protos/v3/go/openolt" |
| 42 | "github.com/opencord/voltha-protos/v3/go/voltha" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 43 | ) |
| 44 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 45 | func newMockCoreProxy() *mocks.MockCoreProxy { |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 46 | mcp := mocks.MockCoreProxy{ |
| 47 | Devices: make(map[string]*voltha.Device), |
| 48 | DevicePorts: make(map[string][]*voltha.Port), |
| 49 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 50 | var pm []*voltha.PmConfig |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 51 | mcp.Devices["olt"] = &voltha.Device{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 52 | Id: "olt", |
| 53 | Root: true, |
| 54 | ParentId: "logical_device", |
| 55 | ParentPortNo: 1, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 56 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 57 | DeviceId: "olt", |
| 58 | DeviceType: "onu", |
| 59 | ChannelId: 1, |
| 60 | ChannelGroupId: 1, |
| 61 | }, |
| 62 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 63 | PmConfigs: &voltha.PmConfigs{ |
| 64 | DefaultFreq: 10, |
| 65 | Id: "olt", |
| 66 | FreqOverride: false, |
| 67 | Grouped: false, |
| 68 | Metrics: pm, |
| 69 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 70 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 71 | mcp.DevicePorts["olt"] = []*voltha.Port{ |
| 72 | {PortNo: 1, Label: "pon"}, |
| 73 | {PortNo: 2, Label: "nni"}, |
| 74 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 75 | |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 76 | mcp.Devices["onu1"] = &voltha.Device{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 77 | Id: "1", |
| 78 | Root: false, |
| 79 | ParentId: "olt", |
| 80 | ParentPortNo: 1, |
| 81 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 82 | OperStatus: 4, |
| 83 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 84 | OnuId: 1, |
| 85 | ChannelId: 1, |
| 86 | ChannelGroupId: 1, |
| 87 | }, |
| 88 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 89 | PmConfigs: &voltha.PmConfigs{ |
| 90 | DefaultFreq: 10, |
| 91 | Id: "olt", |
| 92 | FreqOverride: false, |
| 93 | Grouped: false, |
| 94 | Metrics: pm, |
| 95 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 96 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 97 | mcp.DevicePorts["onu1"] = []*voltha.Port{ |
| 98 | {PortNo: 1, Label: "pon"}, |
| 99 | {PortNo: 2, Label: "uni"}, |
| 100 | } |
| 101 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 102 | mcp.Devices["onu2"] = &voltha.Device{ |
| 103 | Id: "2", |
| 104 | Root: false, |
| 105 | ParentId: "olt", |
| 106 | OperStatus: 2, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 107 | |
| 108 | ParentPortNo: 1, |
| 109 | |
| 110 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 111 | OnuId: 2, |
| 112 | ChannelId: 1, |
| 113 | ChannelGroupId: 1, |
| 114 | }, |
| 115 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 116 | PmConfigs: &voltha.PmConfigs{ |
| 117 | DefaultFreq: 10, |
| 118 | Id: "olt", |
| 119 | FreqOverride: false, |
| 120 | Grouped: false, |
| 121 | Metrics: pm, |
| 122 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 123 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 124 | mcp.DevicePorts["onu2"] = []*voltha.Port{ |
| 125 | {PortNo: 1, Label: "pon"}, |
| 126 | {PortNo: 2, Label: "uni"}, |
| 127 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 135 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 136 | DeviceId: "olt", |
| 137 | DeviceType: "onu", |
| 138 | ChannelId: 1, |
| 139 | ChannelGroupId: 1, |
| 140 | }, |
| 141 | ConnectStatus: 1, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 142 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 143 | cp := newMockCoreProxy() |
| 144 | ap := &mocks.MockAdapterProxy{} |
| 145 | ep := &mocks.MockEventProxy{} |
| 146 | openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep} |
| 147 | dh := NewDeviceHandler(cp, ap, ep, device, openOLT) |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 148 | oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{ |
| 149 | IntfIds: []uint32{0, 1}, |
| 150 | Technology: "xgs-pon", |
| 151 | Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}}, |
| 152 | }} |
| 153 | |
Thomas Lee S | 985938d | 2020-05-04 11:40:41 +0530 | [diff] [blame] | 154 | deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: 2} |
| 155 | rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf, |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 156 | KVStore: &db.Backend{ |
| 157 | Client: &mocks.MockKVClient{}, |
| 158 | }} |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 159 | rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 160 | rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 161 | rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 162 | |
| 163 | dh.resourceMgr = &rsrMgr |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 164 | dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager) |
| 165 | ranges := make(map[string]interface{}) |
| 166 | sharedIdxByType := make(map[string]string) |
| 167 | sharedIdxByType["ALLOC_ID"] = "ALLOC_ID" |
| 168 | sharedIdxByType["ONU_ID"] = "ONU_ID" |
| 169 | sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID" |
| 170 | sharedIdxByType["FLOW_ID"] = "FLOW_ID" |
| 171 | ranges["ONU_ID"] = uint32(0) |
| 172 | ranges["GEMPORT_ID"] = uint32(0) |
| 173 | ranges["ALLOC_ID"] = uint32(0) |
| 174 | ranges["FLOW_ID"] = uint32(0) |
| 175 | ranges["onu_id_shared"] = uint32(0) |
| 176 | ranges["alloc_id_shared"] = uint32(0) |
| 177 | ranges["gemport_id_shared"] = uint32(0) |
| 178 | ranges["flow_id_shared"] = uint32(0) |
| 179 | |
| 180 | ponmgr := &ponrmgr.PONResourceManager{ |
| 181 | DeviceID: "onu-1", |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 182 | IntfIDs: []uint32{0, 1}, |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 183 | KVStore: &db.Backend{ |
| 184 | Client: &mocks.MockKVClient{}, |
| 185 | }, |
| 186 | PonResourceRanges: ranges, |
| 187 | SharedIdxByType: sharedIdxByType, |
| 188 | } |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 189 | dh.resourceMgr.ResourceMgrs[0] = ponmgr |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 190 | dh.resourceMgr.ResourceMgrs[1] = ponmgr |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 191 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 192 | defer cancel() |
| 193 | dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 194 | dh.Client = &mocks.MockOpenoltClient{} |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 195 | dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh} |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 196 | dh.transitionMap = &TransitionMap{} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 197 | dh.portStats = &OpenOltStatisticsMgr{} |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 198 | |
| 199 | var pmNames = []string{ |
| 200 | "rx_bytes", |
| 201 | "rx_packets", |
| 202 | "rx_mcast_packets", |
| 203 | "rx_bcast_packets", |
| 204 | "tx_bytes", |
| 205 | "tx_packets", |
| 206 | "tx_mcast_packets", |
| 207 | "tx_bcast_packets", |
| 208 | } |
| 209 | |
| 210 | 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] | 211 | return dh |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 212 | } |
| 213 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 214 | func negativeDeviceHandler() *DeviceHandler { |
| 215 | dh := newMockDeviceHandler() |
| 216 | device := dh.device |
| 217 | device.Id = "" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 218 | return dh |
| 219 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 220 | func Test_generateMacFromHost(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 221 | ctx := context.Background() |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 222 | type args struct { |
| 223 | host string |
| 224 | } |
| 225 | tests := []struct { |
| 226 | name string |
| 227 | args args |
| 228 | want string |
| 229 | wantErr bool |
| 230 | }{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 231 | {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false}, |
| 232 | {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false}, |
| 233 | //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false}, |
| 234 | {"generateMacFromHost-4", args{host: "testing3"}, "", true}, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 235 | } |
| 236 | for _, tt := range tests { |
| 237 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 238 | got, err := generateMacFromHost(ctx, tt.args.host) |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 239 | if (err != nil) != tt.wantErr { |
| 240 | t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr) |
| 241 | return |
| 242 | } |
| 243 | if got != tt.want { |
| 244 | t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want) |
| 245 | } |
| 246 | }) |
| 247 | } |
| 248 | } |
| 249 | func Test_macifyIP(t *testing.T) { |
| 250 | type args struct { |
| 251 | ip net.IP |
| 252 | } |
| 253 | tests := []struct { |
| 254 | name string |
| 255 | args args |
| 256 | want string |
| 257 | }{{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 258 | "macifyIP-1", |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 259 | args{ip: net.ParseIP("10.10.10.10")}, |
| 260 | "00:00:0a:0a:0a:0a", |
| 261 | }, |
| 262 | { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 263 | "macifyIP-2", |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 264 | args{ip: net.ParseIP("127.0.0.1")}, |
| 265 | "00:00:7f:00:00:01", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 266 | }, |
| 267 | { |
| 268 | "macifyIP-3", |
| 269 | args{ip: net.ParseIP("127.0.0.1/24")}, |
| 270 | "", |
| 271 | }, |
| 272 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 273 | for _, tt := range tests { |
| 274 | t.Run(tt.name, func(t *testing.T) { |
| 275 | if got := macifyIP(tt.args.ip); got != tt.want { |
| 276 | t.Errorf("macifyIP() = %v, want %v", got, tt.want) |
| 277 | } |
| 278 | }) |
| 279 | } |
| 280 | } |
| 281 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 282 | func sparseCompare(keys []string, spec, target interface{}) bool { |
| 283 | if spec == target { |
| 284 | return true |
| 285 | } |
| 286 | if spec == nil || target == nil { |
| 287 | return false |
| 288 | } |
| 289 | typeSpec := reflect.TypeOf(spec) |
| 290 | typeTarget := reflect.TypeOf(target) |
| 291 | if typeSpec != typeTarget { |
| 292 | return false |
| 293 | } |
| 294 | |
| 295 | vSpec := reflect.ValueOf(spec) |
| 296 | vTarget := reflect.ValueOf(target) |
| 297 | if vSpec.Kind() == reflect.Ptr { |
| 298 | vSpec = vSpec.Elem() |
| 299 | vTarget = vTarget.Elem() |
| 300 | } |
| 301 | |
| 302 | for _, key := range keys { |
| 303 | fSpec := vSpec.FieldByName(key) |
| 304 | fTarget := vTarget.FieldByName(key) |
| 305 | if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) { |
| 306 | return false |
| 307 | } |
| 308 | } |
| 309 | return true |
| 310 | } |
| 311 | |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 312 | func TestDeviceHandler_GetChildDevice(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 313 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 314 | dh1 := newMockDeviceHandler() |
| 315 | dh2 := negativeDeviceHandler() |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 316 | type args struct { |
| 317 | parentPort uint32 |
| 318 | onuID uint32 |
| 319 | } |
| 320 | tests := []struct { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 321 | name string |
| 322 | devicehandler *DeviceHandler |
| 323 | args args |
| 324 | want *voltha.Device |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 325 | errType reflect.Type |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 326 | }{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 327 | {"GetChildDevice-1", dh1, |
| 328 | args{parentPort: 1, |
| 329 | onuID: 1}, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 330 | &voltha.Device{ |
| 331 | Id: "1", |
| 332 | ParentId: "olt", |
| 333 | ParentPortNo: 1, |
| 334 | }, |
| 335 | nil, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 336 | }, |
| 337 | {"GetChildDevice-2", dh2, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 338 | args{parentPort: 1, |
| 339 | onuID: 1}, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 340 | nil, |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 341 | reflect.TypeOf(&olterrors.ErrNotFound{}), |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 342 | }, |
| 343 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s) |
| 347 | 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>) |
| 348 | --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s) |
| 349 | */ |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 350 | for _, tt := range tests { |
| 351 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 352 | got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 353 | if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) { |
| 354 | t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)", |
| 355 | tt.want, tt.errType, got, reflect.TypeOf(err)) |
| 356 | return |
| 357 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 358 | t.Log("onu device id", got) |
| 359 | }) |
| 360 | } |
| 361 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 362 | |
| 363 | func TestGetportLabel(t *testing.T) { |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 364 | invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 365 | type args struct { |
| 366 | portNum uint32 |
| 367 | portType voltha.Port_PortType |
| 368 | } |
| 369 | tests := []struct { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 370 | name string |
| 371 | args args |
| 372 | want string |
| 373 | errType reflect.Type |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 374 | }{ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 375 | {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid}, |
| 376 | {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil}, |
| 377 | {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid}, |
| 378 | {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil}, |
| 379 | {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid}, |
| 380 | {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid}, |
| 381 | {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 382 | } |
| 383 | for _, tt := range tests { |
| 384 | t.Run(tt.name, func(t *testing.T) { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 385 | got, err := GetportLabel(tt.args.portNum, tt.args.portType) |
| 386 | if reflect.TypeOf(err) != tt.errType || got != tt.want { |
| 387 | t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)", |
| 388 | tt.want, tt.errType, got, reflect.TypeOf(err)) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 389 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 390 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 391 | }) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 396 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 397 | dh := newMockDeviceHandler() |
| 398 | proxyAddr := dh.device.ProxyAddress |
| 399 | body := &ic.InterAdapterOmciMessage{ |
| 400 | Message: []byte("asdfasdfasdfasdfas"), |
| 401 | ProxyAddress: proxyAddr, |
| 402 | } |
| 403 | body2 := &ic.InterAdapterOmciMessage{ |
| 404 | Message: []byte("asdfasdfasdfasdfas"), |
| 405 | //ProxyAddress: &voltha.Device_ProxyAddress{}, |
| 406 | } |
| 407 | body3 := &ic.InterAdapterTechProfileDownloadMessage{} |
| 408 | var marshalledData *any.Any |
| 409 | var err error |
| 410 | |
| 411 | if marshalledData, err = ptypes.MarshalAny(body); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 412 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | var marshalledData1 *any.Any |
| 416 | |
| 417 | if marshalledData1, err = ptypes.MarshalAny(body2); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 418 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 419 | } |
| 420 | var marshalledData2 *any.Any |
| 421 | |
| 422 | if marshalledData2, err = ptypes.MarshalAny(body3); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 423 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 424 | } |
| 425 | type args struct { |
| 426 | msg *ic.InterAdapterMessage |
| 427 | } |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 428 | invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 429 | tests := []struct { |
| 430 | name string |
| 431 | args args |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 432 | wantErr reflect.Type |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 433 | }{ |
| 434 | {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{ |
| 435 | Header: &ic.InterAdapterHeader{ |
| 436 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 437 | Type: ic.InterAdapterMessageType_FLOW_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 438 | }, |
| 439 | Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 440 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 441 | {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{ |
| 442 | Header: &ic.InterAdapterHeader{ |
| 443 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 444 | Type: ic.InterAdapterMessageType_FLOW_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 445 | }, |
| 446 | Body: marshalledData1, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 447 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 448 | {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{ |
| 449 | Header: &ic.InterAdapterHeader{ |
| 450 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 451 | Type: ic.InterAdapterMessageType_OMCI_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 452 | }, |
| 453 | Body: marshalledData, |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 454 | }}, reflect.TypeOf(&olterrors.ErrCommunication{})}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 455 | {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{ |
| 456 | Header: &ic.InterAdapterHeader{ |
| 457 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 458 | Type: ic.InterAdapterMessageType_OMCI_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 459 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 460 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 461 | {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{ |
| 462 | Header: &ic.InterAdapterHeader{ |
| 463 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 464 | Type: ic.InterAdapterMessageType_METRICS_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 465 | }, Body: marshalledData1, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 466 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 467 | {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{ |
| 468 | Header: &ic.InterAdapterHeader{ |
| 469 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 470 | Type: ic.InterAdapterMessageType_METRICS_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 471 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 472 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 473 | {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{ |
| 474 | Header: &ic.InterAdapterHeader{ |
| 475 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 476 | Type: ic.InterAdapterMessageType_ONU_IND_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 477 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 478 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 479 | {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{ |
| 480 | Header: &ic.InterAdapterHeader{ |
| 481 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 482 | Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 483 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 484 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 485 | {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{ |
| 486 | Header: &ic.InterAdapterHeader{ |
| 487 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 488 | Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 489 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 490 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 491 | {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{ |
| 492 | Header: &ic.InterAdapterHeader{ |
| 493 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 494 | Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 495 | }, Body: marshalledData2, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 496 | }}, invalid}, |
| 497 | {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{ |
| 498 | Header: &ic.InterAdapterHeader{ |
| 499 | Id: "012345", |
| 500 | Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST, |
| 501 | }, Body: marshalledData2, |
| 502 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 503 | } |
| 504 | for _, tt := range tests { |
| 505 | t.Run(tt.name, func(t *testing.T) { |
| 506 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 507 | if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 508 | t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr) |
| 509 | } |
| 510 | }) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | func TestDeviceHandler_sendProxiedMessage(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 515 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 516 | dh1 := newMockDeviceHandler() |
| 517 | dh2 := negativeDeviceHandler() |
| 518 | device1 := &voltha.Device{ |
| 519 | Id: "onu1", |
| 520 | Root: false, |
| 521 | ParentId: "logical_device", |
| 522 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 523 | DeviceId: "onu1", |
| 524 | DeviceType: "onu", |
| 525 | ChannelId: 1, |
| 526 | ChannelGroupId: 1, |
| 527 | }, |
| 528 | ConnectStatus: 1, |
| 529 | } |
| 530 | device2 := device1 |
| 531 | device2.ConnectStatus = 2 |
| 532 | iaomciMsg1 := &ic.InterAdapterOmciMessage{ |
| 533 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 534 | DeviceId: "onu2", |
| 535 | DeviceType: "onu", |
| 536 | ChannelId: 1, |
| 537 | ChannelGroupId: 1, |
| 538 | //OnuId: 2, |
| 539 | }, |
| 540 | ConnectStatus: 1, |
| 541 | } |
| 542 | iaomciMsg2 := &ic.InterAdapterOmciMessage{ |
| 543 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 544 | DeviceId: "onu3", |
| 545 | DeviceType: "onu", |
| 546 | ChannelId: 1, |
| 547 | ChannelGroupId: 1, |
| 548 | }, |
| 549 | ConnectStatus: 1, |
| 550 | } |
| 551 | type args struct { |
| 552 | onuDevice *voltha.Device |
| 553 | omciMsg *ic.InterAdapterOmciMessage |
| 554 | } |
| 555 | tests := []struct { |
| 556 | name string |
| 557 | devicehandler *DeviceHandler |
| 558 | args args |
| 559 | }{ |
| 560 | {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 561 | {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 562 | {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}}, |
| 563 | {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}}, |
| 564 | {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}}, |
| 565 | {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 566 | } |
| 567 | for _, tt := range tests { |
| 568 | t.Run(tt.name, func(t *testing.T) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 569 | _ = tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg) |
| 570 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 594 | _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload) |
| 595 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 596 | }) |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | func TestDeviceHandler_DisableDevice(t *testing.T) { |
| 601 | dh1 := newMockDeviceHandler() |
| 602 | dh2 := negativeDeviceHandler() |
| 603 | type args struct { |
| 604 | device *voltha.Device |
| 605 | } |
| 606 | tests := []struct { |
| 607 | name string |
| 608 | devicehandler *DeviceHandler |
| 609 | args args |
| 610 | wantErr bool |
| 611 | }{ |
| 612 | {"DisableDevice-1", dh1, args{device: dh1.device}, false}, |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 613 | {"DisableDevice-2", dh1, args{device: dh2.device}, true}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 614 | } |
| 615 | for _, tt := range tests { |
| 616 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 617 | if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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 |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 643 | if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 670 | if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 787 | _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state) |
| 788 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 789 | }) |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | func Test_macAddressToUint32Array(t *testing.T) { |
| 794 | type args struct { |
| 795 | mac string |
| 796 | } |
| 797 | tests := []struct { |
| 798 | name string |
| 799 | args args |
| 800 | want []uint32 |
| 801 | }{ |
| 802 | // TODO: Add test cases. |
| 803 | {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}}, |
| 804 | {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}}, |
| 805 | {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}}, |
| 806 | } |
| 807 | for _, tt := range tests { |
| 808 | t.Run(tt.name, func(t *testing.T) { |
| 809 | if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) { |
| 810 | t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want) |
| 811 | } |
| 812 | }) |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | func TestDeviceHandler_handleOltIndication(t *testing.T) { |
| 817 | |
| 818 | type args struct { |
| 819 | oltIndication *oop.OltIndication |
| 820 | } |
| 821 | tests := []struct { |
| 822 | name string |
| 823 | args args |
| 824 | }{ |
| 825 | {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}}, |
| 826 | {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}}, |
| 827 | } |
| 828 | for _, tt := range tests { |
| 829 | t.Run(tt.name, func(t *testing.T) { |
| 830 | dh := newMockDeviceHandler() |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 831 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 832 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 833 | if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil { |
| 834 | t.Error(err) |
| 835 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 836 | }) |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | func TestDeviceHandler_AdoptDevice(t *testing.T) { |
| 841 | dh1 := newMockDeviceHandler() |
| 842 | dh2 := negativeDeviceHandler() |
| 843 | type args struct { |
| 844 | device *voltha.Device |
| 845 | } |
| 846 | tests := []struct { |
| 847 | name string |
| 848 | devicehandler *DeviceHandler |
| 849 | args args |
| 850 | }{ |
| 851 | // TODO: Add test cases. |
| 852 | {"AdoptDevice-1", dh1, args{device: dh1.device}}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 853 | {"AdoptDevice-2", dh2, args{device: dh2.device}}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 854 | } |
| 855 | for _, tt := range tests { |
| 856 | t.Run(tt.name, func(t *testing.T) { |
| 857 | //dh.doStateInit() |
| 858 | // context. |
| 859 | //dh.AdoptDevice(tt.args.device) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 860 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 861 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 862 | if err := tt.devicehandler.postInit(ctx); err != nil { |
| 863 | t.Error(err) |
| 864 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 865 | }) |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | func TestDeviceHandler_activateONU(t *testing.T) { |
| 870 | dh := newMockDeviceHandler() |
| 871 | dh1 := negativeDeviceHandler() |
| 872 | type args struct { |
| 873 | intfID uint32 |
| 874 | onuID int64 |
| 875 | serialNum *oop.SerialNumber |
| 876 | serialNumber string |
| 877 | } |
| 878 | tests := []struct { |
| 879 | name string |
| 880 | devicehandler *DeviceHandler |
| 881 | args args |
| 882 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 883 | {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}}, |
| 884 | {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}}, |
| 885 | {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}}, |
| 886 | {"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] | 887 | } |
| 888 | for _, tt := range tests { |
| 889 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 890 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 891 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 892 | _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber) |
| 893 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 894 | }) |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | func TestDeviceHandler_start(t *testing.T) { |
| 899 | dh := newMockDeviceHandler() |
| 900 | dh1 := negativeDeviceHandler() |
| 901 | dh.start(context.Background()) |
| 902 | dh.stop(context.Background()) |
| 903 | |
| 904 | dh1.start(context.Background()) |
| 905 | dh1.stop(context.Background()) |
| 906 | |
| 907 | } |
| 908 | |
| 909 | func TestDeviceHandler_PacketOut(t *testing.T) { |
| 910 | dh1 := newMockDeviceHandler() |
| 911 | dh2 := negativeDeviceHandler() |
| 912 | acts := []*ofp.OfpAction{ |
| 913 | fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))), |
| 914 | fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)), |
| 915 | fu.Output(1), |
| 916 | } |
| 917 | pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")} |
| 918 | type args struct { |
| 919 | egressPortNo int |
| 920 | packet *of.OfpPacketOut |
| 921 | } |
| 922 | tests := []struct { |
| 923 | name string |
| 924 | devicehandler *DeviceHandler |
| 925 | args args |
| 926 | wantErr bool |
| 927 | }{ |
| 928 | // TODO: Add test cases. |
| 929 | //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true}, |
| 930 | {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false}, |
| 931 | {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false}, |
Matteo Scandolo | 2c0d274 | 2020-06-10 11:28:42 -0700 | [diff] [blame] | 932 | {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false}, |
| 933 | {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false}, |
| 934 | {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 935 | } |
| 936 | for _, tt := range tests { |
| 937 | t.Run(tt.name, func(t *testing.T) { |
| 938 | dh := tt.devicehandler |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 939 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 940 | defer cancel() |
| 941 | 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] | 942 | t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr) |
| 943 | } |
| 944 | }) |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // |
| 949 | func TestDeviceHandler_doStateUp(t *testing.T) { |
| 950 | dh1 := newMockDeviceHandler() |
| 951 | dh2 := newMockDeviceHandler() |
| 952 | |
Thomas Lee S | 985938d | 2020-05-04 11:40:41 +0530 | [diff] [blame] | 953 | dh2.device.Id = "" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 954 | dh3 := negativeDeviceHandler() |
| 955 | |
| 956 | tests := []struct { |
| 957 | name string |
| 958 | devicehandler *DeviceHandler |
| 959 | wantErr bool |
| 960 | }{ |
| 961 | {"dostateup-1", dh1, false}, |
| 962 | {"dostateup-2", dh2, false}, |
| 963 | {"dostateup-3", dh3, true}, |
| 964 | } |
| 965 | for _, tt := range tests { |
| 966 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 967 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 968 | defer cancel() |
| 969 | if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 970 | t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr) |
| 971 | } |
Thomas Lee S | 85f3731 | 2020-04-03 17:06:12 +0530 | [diff] [blame] | 972 | tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 973 | }) |
| 974 | } |
| 975 | } |
| 976 | func TestDeviceHandler_doStateDown(t *testing.T) { |
| 977 | dh1 := newMockDeviceHandler() |
| 978 | dh2 := negativeDeviceHandler() |
| 979 | dh3 := newMockDeviceHandler() |
| 980 | dh3.device.OperStatus = voltha.OperStatus_UNKNOWN |
| 981 | tests := []struct { |
| 982 | name string |
| 983 | devicehandler *DeviceHandler |
| 984 | wantErr bool |
| 985 | }{ |
| 986 | {"dostatedown-1", dh1, false}, |
| 987 | {"dostatedown-2", dh2, true}, |
| 988 | {"dostatedown-2", dh3, true}, |
| 989 | } |
| 990 | for _, tt := range tests { |
| 991 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 992 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 993 | defer cancel() |
| 994 | if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 995 | t.Logf("DeviceHandler.doStateDown() error = %v", err) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 996 | //TODO: should fail this test case (Errorf) if result is not as expected |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 997 | } |
| 998 | }) |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) { |
| 1003 | dh1 := newMockDeviceHandler() |
| 1004 | dh2 := negativeDeviceHandler() |
| 1005 | type args struct { |
| 1006 | device *voltha.Device |
| 1007 | } |
| 1008 | tests := []struct { |
| 1009 | name string |
| 1010 | devicehandler *DeviceHandler |
| 1011 | args args |
| 1012 | wantErr bool |
| 1013 | }{ |
| 1014 | // TODO: Add test cases. |
| 1015 | {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false}, |
| 1016 | {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false}, |
| 1017 | {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false}, |
| 1018 | } |
| 1019 | for _, tt := range tests { |
| 1020 | t.Run(tt.name, func(t *testing.T) { |
| 1021 | dh := tt.devicehandler |
| 1022 | _, err := dh.GetOfpDeviceInfo(tt.args.device) |
| 1023 | if (err != nil) != tt.wantErr { |
| 1024 | t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1025 | return |
| 1026 | } |
| 1027 | }) |
| 1028 | } |
| 1029 | } |
| 1030 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1031 | func TestDeviceHandler_onuDiscIndication(t *testing.T) { |
| 1032 | |
| 1033 | dh1 := newMockDeviceHandler() |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1034 | dh1.discOnus = sync.Map{} |
| 1035 | dh1.discOnus.Store("onu1", true) |
| 1036 | dh1.discOnus.Store("onu2", false) |
Thiyagarajan Subramani | 34a0028 | 2020-03-10 20:19:31 +0530 | [diff] [blame] | 1037 | dh1.discOnus.Store("onu3", true) |
| 1038 | dh1.discOnus.Store("onu4", true) |
| 1039 | dh1.onus = sync.Map{} |
| 1040 | dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true)) |
| 1041 | dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true)) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1042 | dh2 := negativeDeviceHandler() |
| 1043 | type args struct { |
| 1044 | onuDiscInd *oop.OnuDiscIndication |
| 1045 | sn string |
| 1046 | } |
| 1047 | tests := []struct { |
| 1048 | name string |
| 1049 | devicehandler *DeviceHandler |
| 1050 | args args |
| 1051 | }{ |
| 1052 | // TODO: Add test cases. |
| 1053 | {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}, |
| 1054 | {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}}, |
| 1055 | {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}}, |
| 1056 | {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}}, |
| 1057 | {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}}, |
| 1058 | {"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] | 1059 | {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}}, |
| 1060 | {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}}, |
| 1061 | {"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] | 1062 | } |
| 1063 | for _, tt := range tests { |
| 1064 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1065 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1066 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1067 | _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn) |
| 1068 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1069 | }) |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | func TestDeviceHandler_populateDeviceInfo(t *testing.T) { |
| 1074 | dh1 := newMockDeviceHandler() |
| 1075 | dh2 := negativeDeviceHandler() |
| 1076 | tests := []struct { |
| 1077 | name string |
| 1078 | devicehandler *DeviceHandler |
| 1079 | |
| 1080 | wantErr bool |
| 1081 | }{ |
| 1082 | // TODO: Add test cases. |
| 1083 | {"populateDeviceInfo-1", dh1, false}, |
| 1084 | {"populateDeviceInfo-2", dh1, true}, |
| 1085 | {"populateDeviceInfo-3", dh1, true}, |
| 1086 | {"populateDeviceInfo-4", dh1, true}, |
| 1087 | {"populateDeviceInfo-5", dh2, true}, |
| 1088 | } |
| 1089 | for _, tt := range tests { |
| 1090 | t.Run(tt.name, func(t *testing.T) { |
| 1091 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1092 | _, err := tt.devicehandler.populateDeviceInfo(context.Background()) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1093 | if (err != nil) != tt.wantErr { |
| 1094 | t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1095 | return |
| 1096 | } |
| 1097 | |
| 1098 | }) |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | func TestDeviceHandler_readIndications(t *testing.T) { |
| 1103 | dh1 := newMockDeviceHandler() |
| 1104 | dh2 := newMockDeviceHandler() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1105 | dh3 := newMockDeviceHandler() |
| 1106 | dh3.device.AdminState = voltha.AdminState_DISABLED |
| 1107 | dh4 := negativeDeviceHandler() |
| 1108 | tests := []struct { |
| 1109 | name string |
| 1110 | devicehandler *DeviceHandler |
| 1111 | }{ |
| 1112 | // TODO: Add test cases. |
| 1113 | {"readIndications-1", dh1}, |
| 1114 | {"readIndications-2", dh2}, |
| 1115 | {"readIndications-3", dh2}, |
| 1116 | {"readIndications-4", dh2}, |
| 1117 | {"readIndications-5", dh2}, |
| 1118 | {"readIndications-6", dh3}, |
| 1119 | {"readIndications-7", dh3}, |
| 1120 | {"readIndications-8", dh4}, |
| 1121 | } |
| 1122 | for _, tt := range tests { |
| 1123 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1124 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1125 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1126 | _ = tt.devicehandler.readIndications(ctx) |
| 1127 | // TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1128 | }) |
| 1129 | } |
| 1130 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1131 | |
| 1132 | func Test_startCollector(t *testing.T) { |
| 1133 | type args struct { |
| 1134 | dh *DeviceHandler |
| 1135 | } |
| 1136 | dh := newMockDeviceHandler() |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 1137 | dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{ |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1138 | {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT}, |
| 1139 | {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI}, |
| 1140 | {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI}, |
| 1141 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1142 | dh.portStats.NorthBoundPort = make(map[uint32]*NniPort) |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1143 | dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"} |
| 1144 | dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1145 | dh.portStats.SouthBoundPort = make(map[uint32]*PonPort) |
| 1146 | dh.portStats.Device = dh |
| 1147 | for i := 0; i < 16; i++ { |
| 1148 | dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"} |
| 1149 | } |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1150 | dh1 := newMockDeviceHandler() |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 1151 | dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1152 | tests := []struct { |
| 1153 | name string |
| 1154 | args args |
| 1155 | }{ |
| 1156 | // TODO: Add test cases. |
| 1157 | {"StartCollector-1", args{dh}}, |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1158 | {"StartCollector-2", args{dh1}}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1159 | } |
| 1160 | for _, tt := range tests { |
| 1161 | t.Run(tt.name, func(t *testing.T) { |
| 1162 | go func() { |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1163 | time.Sleep(1 * time.Second) // simulated wait time to stop startCollector |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1164 | tt.args.dh.stopCollector <- true |
| 1165 | }() |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1166 | startCollector(context.Background(), tt.args.dh) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1167 | }) |
| 1168 | } |
| 1169 | } |