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