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