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