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