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