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