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