Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltha/ponsim/v2/common" |
| 20 | "github.com/opencord/voltha/protos/go/voltha" |
| 21 | "github.com/sirupsen/logrus" |
| 22 | ) |
| 23 | |
| 24 | /* |
| 25 | metricCounter holds details for a specific metric |
| 26 | */ |
| 27 | type metricCounter struct { |
| 28 | Name string |
| 29 | Value [2]int // [PON,NNI] values |
| 30 | Min int |
| 31 | Max int |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | Create a new MetricCounter instance for TX packets |
| 36 | */ |
| 37 | func newTxMetricCounter(name txMetricCounterType, min int, max int) *metricCounter { |
| 38 | return &metricCounter{Name: name.String(), Min: min, Max: max} |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | Create a new MetricCounter instance for RX packets |
| 43 | */ |
| 44 | func newRxMetricCounter(name rxMetricCounterType, min int, max int) *metricCounter { |
| 45 | return &metricCounter{Name: name.String(), Min: min, Max: max} |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | Define TX constants |
| 50 | */ |
| 51 | type txMetricCounterType uint8 |
| 52 | |
| 53 | const ( |
| 54 | tx_64_pkts txMetricCounterType = iota |
| 55 | tx_65_127_pkts |
| 56 | tx_128_255_pkts |
| 57 | tx_256_511_pkts |
| 58 | tx_512_1023_pkts |
| 59 | tx_1024_1518_pkts |
| 60 | tx_1519_9k_pkts |
| 61 | ) |
| 62 | |
| 63 | /* |
| 64 | TX packet constants string equivalents |
| 65 | */ |
| 66 | var txMetricCounterEnum = []string{ |
| 67 | "tx_64_pkts", |
| 68 | "tx_65_127_pkts", |
| 69 | "tx_128_255_pkts", |
| 70 | "tx_256_511_pkts", |
| 71 | "tx_512_1023_pkts", |
| 72 | "tx_1024_1518_pkts", |
| 73 | "tx_1519_9k_pkts", |
| 74 | } |
| 75 | |
| 76 | func (t txMetricCounterType) String() string { |
| 77 | return txMetricCounterEnum[t] |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | Define RX constants |
| 82 | */ |
| 83 | type rxMetricCounterType uint8 |
| 84 | |
| 85 | const ( |
| 86 | rx_64_pkts rxMetricCounterType = iota |
| 87 | rx_65_127_pkts |
| 88 | rx_128_255_pkts |
| 89 | rx_256_511_pkts |
| 90 | rx_512_1023_pkts |
| 91 | rx_1024_1518_pkts |
| 92 | rx_1519_9k_pkts |
| 93 | ) |
| 94 | |
| 95 | /* |
| 96 | RX packet constants string equivalents |
| 97 | */ |
| 98 | var rxMetricCounterEnum = []string{ |
| 99 | "rx_64_pkts", |
| 100 | "rx_65_127_pkts", |
| 101 | "rx_128_255_pkts", |
| 102 | "rx_256_511_pkts", |
| 103 | "rx_512_1023_pkts", |
| 104 | "rx_1024_1518_pkts", |
| 105 | "rx_1519_9k_pkts", |
| 106 | } |
| 107 | |
| 108 | func (t rxMetricCounterType) String() string { |
| 109 | return rxMetricCounterEnum[t] |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | |
| 114 | */ |
| 115 | type PonSimMetricCounter struct { |
| 116 | Name string |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 117 | Device_Type string |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 118 | TxCounters map[txMetricCounterType]*metricCounter |
| 119 | RxCounters map[rxMetricCounterType]*metricCounter |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | NewPonSimMetricCounter instantiates new metric counters for a PON device |
| 124 | */ |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 125 | func NewPonSimMetricCounter(name string, device_type string) *PonSimMetricCounter { |
| 126 | counter := &PonSimMetricCounter{Name: name, Device_Type: device_type} |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 127 | |
| 128 | counter.TxCounters = map[txMetricCounterType]*metricCounter{ |
| 129 | tx_64_pkts: newTxMetricCounter(tx_64_pkts, 1, 64), |
| 130 | tx_65_127_pkts: newTxMetricCounter(tx_65_127_pkts, 65, 127), |
| 131 | tx_128_255_pkts: newTxMetricCounter(tx_128_255_pkts, 128, 255), |
| 132 | tx_256_511_pkts: newTxMetricCounter(tx_256_511_pkts, 256, 511), |
| 133 | tx_512_1023_pkts: newTxMetricCounter(tx_512_1023_pkts, 512, 1023), |
| 134 | tx_1024_1518_pkts: newTxMetricCounter(tx_1024_1518_pkts, 1024, 1518), |
| 135 | tx_1519_9k_pkts: newTxMetricCounter(tx_1519_9k_pkts, 1519, 9216), |
| 136 | } |
| 137 | counter.RxCounters = map[rxMetricCounterType]*metricCounter{ |
| 138 | rx_64_pkts: newRxMetricCounter(rx_64_pkts, 1, 64), |
| 139 | rx_65_127_pkts: newRxMetricCounter(rx_65_127_pkts, 65, 127), |
| 140 | rx_128_255_pkts: newRxMetricCounter(rx_128_255_pkts, 128, 255), |
| 141 | rx_256_511_pkts: newRxMetricCounter(rx_256_511_pkts, 256, 511), |
| 142 | rx_512_1023_pkts: newRxMetricCounter(rx_512_1023_pkts, 512, 1023), |
| 143 | rx_1024_1518_pkts: newRxMetricCounter(rx_1024_1518_pkts, 1024, 1518), |
| 144 | rx_1519_9k_pkts: newRxMetricCounter(rx_1519_9k_pkts, 1519, 9216), |
| 145 | } |
| 146 | |
| 147 | return counter |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | CountRxFrame increments the receive count for a specific packet size metric |
| 152 | */ |
| 153 | func (mc *PonSimMetricCounter) CountRxFrame(port int, size int) { |
| 154 | for k, v := range mc.RxCounters { |
| 155 | if size >= v.Min && size <= v.Max { |
| 156 | mc.RxCounters[k].Value[port-1] += 1 |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | CountTxFrame increments the transmit count for a specific packet size metric |
| 163 | */ |
| 164 | func (mc *PonSimMetricCounter) CountTxFrame(port int, size int) { |
| 165 | for k, v := range mc.TxCounters { |
| 166 | if size >= v.Min && size <= v.Max { |
| 167 | mc.TxCounters[k].Value[port-1] += 1 |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | LogCounts logs the current counts for all RX/TX packets |
| 174 | */ |
| 175 | func (mc *PonSimMetricCounter) LogCounts() { |
| 176 | common.Logger().WithFields(logrus.Fields{ |
| 177 | "counters": mc.RxCounters, |
| 178 | }).Info("RX Metrics") |
| 179 | common.Logger().WithFields(logrus.Fields{ |
| 180 | "counters": mc.TxCounters, |
| 181 | }).Info("TX Metrics") |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | MakeProto collects all RX/TX metrics with which it constructs a GRPC proto metrics structure |
| 186 | */ |
| 187 | func (mc *PonSimMetricCounter) MakeProto() *voltha.PonSimMetrics { |
| 188 | simMetrics := &voltha.PonSimMetrics{Device: mc.Name} |
| 189 | ponMetrics := &voltha.PonSimPortMetrics{PortName: "pon"} |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 190 | portMetrics := &voltha.PonSimPortMetrics{} |
| 191 | |
| 192 | if (mc.Device_Type == "ONU") { |
| 193 | portMetrics.PortName = "uni" |
| 194 | } else if (mc.Device_Type == "OLT") { |
| 195 | portMetrics.PortName = "nni" |
| 196 | } else { |
| 197 | common.Logger().WithFields(logrus.Fields{ |
| 198 | "counters": mc.RxCounters, |
| 199 | }).Error("Unknown Device_Type in PonSimMetricCounter") |
| 200 | portMetrics.PortName = "unknown" |
| 201 | } |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 202 | |
| 203 | // Collect RX metrics |
| 204 | for _, c := range mc.RxCounters { |
| 205 | // PON values |
| 206 | ponMetrics.Packets = append( |
| 207 | ponMetrics.Packets, |
| 208 | &voltha.PonSimPacketCounter{ |
| 209 | Name: c.Name, |
| 210 | Value: int64(c.Value[0]), |
| 211 | }, |
| 212 | ) |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 213 | // NNI/UNI values |
| 214 | portMetrics.Packets = append( |
| 215 | portMetrics.Packets, |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 216 | &voltha.PonSimPacketCounter{ |
| 217 | Name: c.Name, |
| 218 | Value: int64(c.Value[1]), |
| 219 | }, |
| 220 | ) |
| 221 | } |
| 222 | // Collect TX metrics |
| 223 | for _, c := range mc.TxCounters { |
| 224 | // PON values |
| 225 | ponMetrics.Packets = append( |
| 226 | ponMetrics.Packets, |
| 227 | &voltha.PonSimPacketCounter{ |
| 228 | Name: c.Name, |
| 229 | Value: int64(c.Value[0]), |
| 230 | }, |
| 231 | ) |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 232 | // NNI/UNI values |
| 233 | portMetrics.Packets = append( |
| 234 | portMetrics.Packets, |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 235 | &voltha.PonSimPacketCounter{ |
| 236 | Name: c.Name, |
| 237 | Value: int64(c.Value[1]), |
| 238 | }, |
| 239 | ) |
| 240 | } |
| 241 | |
| 242 | // Populate GRPC proto structure |
| 243 | simMetrics.Metrics = append(simMetrics.Metrics, ponMetrics) |
Scott Baker | b5be94d | 2018-10-09 16:13:32 -0700 | [diff] [blame] | 244 | simMetrics.Metrics = append(simMetrics.Metrics, portMetrics) |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 245 | |
| 246 | return simMetrics |
| 247 | } |