kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | a6af152 | 2023-01-17 16:06:16 -0500 | [diff] [blame] | 2 | * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 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 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 17 | // Package core provides the utility for olt devices, flows and statistics |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 18 | package core |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 19 | |
| 20 | import ( |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 21 | "context" |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 22 | "fmt" |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 23 | "reflect" |
| 24 | "testing" |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 25 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/openolt" |
| 27 | "github.com/opencord/voltha-protos/v5/go/voltha" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 30 | func TestOpenOltStatisticsMgr_PortStatisticsIndication(t *testing.T) { |
| 31 | device := &voltha.Device{ |
| 32 | Id: "olt", |
| 33 | Root: true, |
| 34 | ParentId: "logical_device", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 35 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 36 | DeviceId: "olt", |
| 37 | DeviceType: "onu", |
| 38 | ChannelId: 1, |
| 39 | ChannelGroupId: 1, |
| 40 | }, |
| 41 | ConnectStatus: 1, |
| 42 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 43 | dh := newMockDeviceHandler() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 44 | dh.device = device |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 45 | StatMgr := NewOpenOltStatsMgr(context.Background(), dh) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 46 | |
| 47 | type args struct { |
| 48 | PortStats *openolt.PortStatistics |
| 49 | } |
| 50 | tests := []struct { |
| 51 | name string |
| 52 | args args |
| 53 | }{ |
| 54 | // TODO: Add test cases. |
| 55 | {"PortStatisticsIndication", args{PortStats: &openolt.PortStatistics{}}}, |
| 56 | } |
| 57 | for _, tt := range tests { |
| 58 | t.Run(tt.name, func(t *testing.T) { |
| 59 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 60 | StatMgr.PortStatisticsIndication(context.Background(), tt.args.PortStats, 16) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestOpenOltStatisticsMgr_publishMetrics(t *testing.T) { |
| 66 | type fields struct { |
| 67 | Device *DeviceHandler |
| 68 | NorthBoundPort map[uint32]*NniPort |
| 69 | SouthBoundPort map[uint32]*PonPort |
| 70 | } |
| 71 | type args struct { |
Gamze Abaka | fcbd6e7 | 2020-12-17 13:25:16 +0000 | [diff] [blame] | 72 | val map[string]float32 |
| 73 | port *voltha.Port |
| 74 | statType string |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 75 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 76 | ponmap := map[uint32]*PonPort{} |
| 77 | ponmap[0] = &PonPort{ |
| 78 | PONID: 0, |
| 79 | DeviceID: "onu1", |
| 80 | IntfID: 0, |
| 81 | PortNum: 0, |
| 82 | PortID: 0, |
| 83 | Label: "", |
| 84 | ONUs: nil, |
| 85 | ONUsByID: nil, |
| 86 | RxBytes: 0, |
| 87 | RxPackets: 0, |
Dileep Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 88 | RxUcastPackets: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 89 | RxMcastPackets: 0, |
| 90 | RxBcastPackets: 0, |
| 91 | RxErrorPackets: 0, |
| 92 | TxBytes: 0, |
| 93 | TxPackets: 0, |
| 94 | TxUcastPackets: 0, |
| 95 | TxMcastPackets: 0, |
| 96 | TxBcastPackets: 0, |
| 97 | TxErrorPackets: 0, |
Dileep Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 98 | RxCrcErrors: 0, |
| 99 | BipErrors: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 100 | } |
| 101 | nnimap := map[uint32]*NniPort{} |
| 102 | nnimap[0] = &NniPort{ |
| 103 | PortNum: 0, |
| 104 | Name: "olt1", |
| 105 | LogicalPort: 0, |
| 106 | IntfID: 0, |
| 107 | RxBytes: 0, |
| 108 | RxPackets: 0, |
Dileep Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 109 | RxUcastPackets: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 110 | RxMcastPackets: uint64(1111), |
| 111 | RxBcastPackets: 0, |
| 112 | RxErrorPackets: 0, |
| 113 | TxBytes: 0, |
| 114 | TxPackets: 0, |
| 115 | TxUcastPackets: 0, |
| 116 | TxMcastPackets: 0, |
| 117 | TxBcastPackets: 0, |
| 118 | TxErrorPackets: 0, |
Dileep Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 119 | RxCrcErrors: 0, |
| 120 | BipErrors: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 121 | } |
| 122 | pval := make(map[string]float32) |
| 123 | pval["rx_bytes"] = float32(111) |
| 124 | nval := make(map[string]float32) |
| 125 | nval["rx_bytes"] = float32(111) |
| 126 | dhandlerNNI := newMockDeviceHandler() |
| 127 | dhandlerNNI.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: nil, NorthBoundPort: nnimap} |
| 128 | dhandlerPON := newMockDeviceHandler() |
| 129 | dhandlerPON.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: ponmap, NorthBoundPort: nil} |
Gamze Abaka | fcbd6e7 | 2020-12-17 13:25:16 +0000 | [diff] [blame] | 130 | dhandlerONU := newMockDeviceHandler() |
| 131 | dhandlerGEM := newMockDeviceHandler() |
| 132 | |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 133 | tests := []struct { |
| 134 | name string |
| 135 | fields fields |
| 136 | args args |
| 137 | }{ |
| 138 | { |
| 139 | name: "PublishNNIMetrics-1", |
| 140 | fields: fields{ |
| 141 | Device: dhandlerNNI, |
| 142 | NorthBoundPort: nnimap, |
| 143 | SouthBoundPort: nil, |
| 144 | }, |
| 145 | args: args{ |
Gamze Abaka | fcbd6e7 | 2020-12-17 13:25:16 +0000 | [diff] [blame] | 146 | val: nval, |
| 147 | port: &voltha.Port{PortNo: 0, Label: fmt.Sprintf("%s%d", "nni-", 0), Type: voltha.Port_ETHERNET_NNI}, |
| 148 | statType: NNIStats, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 149 | }, |
| 150 | }, |
| 151 | { |
| 152 | name: "PublishPONMetrics-1", |
| 153 | fields: fields{ |
| 154 | Device: dhandlerPON, |
| 155 | NorthBoundPort: nil, |
| 156 | SouthBoundPort: ponmap, |
| 157 | }, |
| 158 | args: args{ |
Gamze Abaka | fcbd6e7 | 2020-12-17 13:25:16 +0000 | [diff] [blame] | 159 | val: pval, |
| 160 | port: &voltha.Port{PortNo: 1, Label: fmt.Sprintf("%s%d", "pon-", 1), Type: voltha.Port_PON_OLT}, |
| 161 | statType: PONStats, |
| 162 | }, |
| 163 | }, |
| 164 | { |
| 165 | name: "PublishONUMetrics-1", |
| 166 | fields: fields{ |
| 167 | Device: dhandlerONU, |
| 168 | NorthBoundPort: nil, |
| 169 | SouthBoundPort: nil, |
| 170 | }, |
| 171 | args: args{ |
| 172 | port: &voltha.Port{Label: "ONU"}, |
| 173 | statType: ONUStats, |
| 174 | }, |
| 175 | }, |
| 176 | { |
| 177 | name: "PublishGEMMetrics-1", |
| 178 | fields: fields{ |
| 179 | Device: dhandlerGEM, |
| 180 | NorthBoundPort: nil, |
| 181 | SouthBoundPort: nil, |
| 182 | }, |
| 183 | args: args{ |
| 184 | port: &voltha.Port{Label: "GEM"}, |
| 185 | statType: GEMStats, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 186 | }, |
| 187 | }, |
| 188 | // TODO: Add test cases. |
| 189 | } |
| 190 | for _, tt := range tests { |
| 191 | t.Run(tt.name, func(t *testing.T) { |
| 192 | StatMgr := &OpenOltStatisticsMgr{ |
| 193 | Device: tt.fields.Device, |
| 194 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 195 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 196 | } |
Gamze Abaka | fcbd6e7 | 2020-12-17 13:25:16 +0000 | [diff] [blame] | 197 | if tt.args.statType == ONUStats { |
| 198 | tt.args.val = StatMgr.convertONUStats(&openolt.OnuStatistics{IntfId: 1, OnuId: 1, PositiveDrift: 123, BipErrors: 22}) |
| 199 | } else if tt.args.statType == GEMStats { |
| 200 | tt.args.val = StatMgr.convertGemStats(&openolt.GemPortStatistics{IntfId: 1, GemportId: 1024, RxPackets: 12, TxBytes: 12}) |
| 201 | } |
| 202 | StatMgr.publishMetrics(context.Background(), tt.args.statType, tt.args.val, tt.args.port, "onu1", "openolt") |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 203 | }) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestOpenOltStatisticsMgr_collectNNIMetrics(t *testing.T) { |
| 208 | type fields struct { |
| 209 | Device *DeviceHandler |
| 210 | NorthBoundPort map[uint32]*NniPort |
| 211 | SouthBoundPort map[uint32]*PonPort |
| 212 | } |
| 213 | type args struct { |
| 214 | nniID uint32 |
| 215 | } |
| 216 | dhandler := newMockDeviceHandler() |
| 217 | pmconfig := make(map[string]*voltha.PmConfig) |
| 218 | pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"} |
| 219 | |
| 220 | var res map[string]float32 |
| 221 | nnimap := map[uint32]*NniPort{} |
| 222 | nnimap[0] = &NniPort{Name: "olt"} |
| 223 | nnimap[1] = &NniPort{Name: "olt"} |
| 224 | dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: nil, NorthBoundPort: nnimap}} |
| 225 | tests := []struct { |
| 226 | name string |
| 227 | fields fields |
| 228 | args args |
| 229 | want map[string]float32 |
| 230 | }{ |
| 231 | {"CollectNNIMetrics-1", fields{ |
| 232 | Device: dh, |
| 233 | NorthBoundPort: nnimap, |
| 234 | SouthBoundPort: nil, |
| 235 | }, args{0}, res}, |
| 236 | {"CollectNNIMetrics-2", fields{ |
| 237 | Device: dh, |
| 238 | NorthBoundPort: nnimap, |
| 239 | SouthBoundPort: nil, |
| 240 | }, args{1}, res}, |
| 241 | // TODO: Add test cases. |
| 242 | } |
| 243 | for _, tt := range tests { |
| 244 | t.Run(tt.name, func(t *testing.T) { |
| 245 | StatMgr := &OpenOltStatisticsMgr{ |
| 246 | Device: tt.fields.Device, |
| 247 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 248 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 249 | } |
| 250 | got := StatMgr.collectNNIMetrics(tt.args.nniID) |
| 251 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 252 | t.Errorf("collectNNIMetrics() = %v, want %v", got, tt.want) |
| 253 | } |
| 254 | }) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func TestOpenOltStatisticsMgr_collectPONMetrics(t *testing.T) { |
| 259 | type fields struct { |
| 260 | Device *DeviceHandler |
| 261 | NorthBoundPort map[uint32]*NniPort |
| 262 | SouthBoundPort map[uint32]*PonPort |
| 263 | } |
| 264 | type args struct { |
| 265 | pID uint32 |
| 266 | } |
| 267 | dhandler := newMockDeviceHandler() |
| 268 | pmconfig := make(map[string]*voltha.PmConfig) |
| 269 | pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"} |
| 270 | |
| 271 | var res map[string]float32 |
| 272 | ponmap := map[uint32]*PonPort{} |
| 273 | ponmap[0] = &PonPort{DeviceID: "olt"} |
| 274 | ponmap[1] = &PonPort{DeviceID: "olt"} |
| 275 | dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: ponmap, NorthBoundPort: nil}} |
| 276 | |
| 277 | tests := []struct { |
| 278 | name string |
| 279 | fields fields |
| 280 | args args |
| 281 | want map[string]float32 |
| 282 | }{ |
| 283 | {"CollectPONMetrics-1", fields{ |
| 284 | Device: dh, |
| 285 | NorthBoundPort: nil, |
| 286 | SouthBoundPort: ponmap, |
| 287 | }, args{0}, res}, |
| 288 | // TODO: Add test cases. |
| 289 | } |
| 290 | for _, tt := range tests { |
| 291 | t.Run(tt.name, func(t *testing.T) { |
| 292 | StatMgr := &OpenOltStatisticsMgr{ |
| 293 | Device: tt.fields.Device, |
| 294 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 295 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 296 | } |
| 297 | got := StatMgr.collectPONMetrics(tt.args.pID) |
| 298 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 299 | t.Errorf("collectPONMetrics() = %v, want %v", got, tt.want) |
| 300 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 301 | }) |
| 302 | } |
| 303 | } |