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