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