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