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 |
| 117 | TxCounters map[txMetricCounterType]*metricCounter |
| 118 | RxCounters map[rxMetricCounterType]*metricCounter |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | NewPonSimMetricCounter instantiates new metric counters for a PON device |
| 123 | */ |
| 124 | func NewPonSimMetricCounter(name string) *PonSimMetricCounter { |
| 125 | counter := &PonSimMetricCounter{Name: name} |
| 126 | |
| 127 | counter.TxCounters = map[txMetricCounterType]*metricCounter{ |
| 128 | tx_64_pkts: newTxMetricCounter(tx_64_pkts, 1, 64), |
| 129 | tx_65_127_pkts: newTxMetricCounter(tx_65_127_pkts, 65, 127), |
| 130 | tx_128_255_pkts: newTxMetricCounter(tx_128_255_pkts, 128, 255), |
| 131 | tx_256_511_pkts: newTxMetricCounter(tx_256_511_pkts, 256, 511), |
| 132 | tx_512_1023_pkts: newTxMetricCounter(tx_512_1023_pkts, 512, 1023), |
| 133 | tx_1024_1518_pkts: newTxMetricCounter(tx_1024_1518_pkts, 1024, 1518), |
| 134 | tx_1519_9k_pkts: newTxMetricCounter(tx_1519_9k_pkts, 1519, 9216), |
| 135 | } |
| 136 | counter.RxCounters = map[rxMetricCounterType]*metricCounter{ |
| 137 | rx_64_pkts: newRxMetricCounter(rx_64_pkts, 1, 64), |
| 138 | rx_65_127_pkts: newRxMetricCounter(rx_65_127_pkts, 65, 127), |
| 139 | rx_128_255_pkts: newRxMetricCounter(rx_128_255_pkts, 128, 255), |
| 140 | rx_256_511_pkts: newRxMetricCounter(rx_256_511_pkts, 256, 511), |
| 141 | rx_512_1023_pkts: newRxMetricCounter(rx_512_1023_pkts, 512, 1023), |
| 142 | rx_1024_1518_pkts: newRxMetricCounter(rx_1024_1518_pkts, 1024, 1518), |
| 143 | rx_1519_9k_pkts: newRxMetricCounter(rx_1519_9k_pkts, 1519, 9216), |
| 144 | } |
| 145 | |
| 146 | return counter |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | CountRxFrame increments the receive count for a specific packet size metric |
| 151 | */ |
| 152 | func (mc *PonSimMetricCounter) CountRxFrame(port int, size int) { |
| 153 | for k, v := range mc.RxCounters { |
| 154 | if size >= v.Min && size <= v.Max { |
| 155 | mc.RxCounters[k].Value[port-1] += 1 |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | CountTxFrame increments the transmit count for a specific packet size metric |
| 162 | */ |
| 163 | func (mc *PonSimMetricCounter) CountTxFrame(port int, size int) { |
| 164 | for k, v := range mc.TxCounters { |
| 165 | if size >= v.Min && size <= v.Max { |
| 166 | mc.TxCounters[k].Value[port-1] += 1 |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | LogCounts logs the current counts for all RX/TX packets |
| 173 | */ |
| 174 | func (mc *PonSimMetricCounter) LogCounts() { |
| 175 | common.Logger().WithFields(logrus.Fields{ |
| 176 | "counters": mc.RxCounters, |
| 177 | }).Info("RX Metrics") |
| 178 | common.Logger().WithFields(logrus.Fields{ |
| 179 | "counters": mc.TxCounters, |
| 180 | }).Info("TX Metrics") |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | MakeProto collects all RX/TX metrics with which it constructs a GRPC proto metrics structure |
| 185 | */ |
| 186 | func (mc *PonSimMetricCounter) MakeProto() *voltha.PonSimMetrics { |
| 187 | simMetrics := &voltha.PonSimMetrics{Device: mc.Name} |
| 188 | ponMetrics := &voltha.PonSimPortMetrics{PortName: "pon"} |
| 189 | nniMetrics := &voltha.PonSimPortMetrics{PortName: "nni"} |
| 190 | |
| 191 | // Collect RX metrics |
| 192 | for _, c := range mc.RxCounters { |
| 193 | // PON values |
| 194 | ponMetrics.Packets = append( |
| 195 | ponMetrics.Packets, |
| 196 | &voltha.PonSimPacketCounter{ |
| 197 | Name: c.Name, |
| 198 | Value: int64(c.Value[0]), |
| 199 | }, |
| 200 | ) |
| 201 | // NNI values |
| 202 | nniMetrics.Packets = append( |
| 203 | nniMetrics.Packets, |
| 204 | &voltha.PonSimPacketCounter{ |
| 205 | Name: c.Name, |
| 206 | Value: int64(c.Value[1]), |
| 207 | }, |
| 208 | ) |
| 209 | } |
| 210 | // Collect TX metrics |
| 211 | for _, c := range mc.TxCounters { |
| 212 | // PON values |
| 213 | ponMetrics.Packets = append( |
| 214 | ponMetrics.Packets, |
| 215 | &voltha.PonSimPacketCounter{ |
| 216 | Name: c.Name, |
| 217 | Value: int64(c.Value[0]), |
| 218 | }, |
| 219 | ) |
| 220 | // NNI values |
| 221 | nniMetrics.Packets = append( |
| 222 | nniMetrics.Packets, |
| 223 | &voltha.PonSimPacketCounter{ |
| 224 | Name: c.Name, |
| 225 | Value: int64(c.Value[1]), |
| 226 | }, |
| 227 | ) |
| 228 | } |
| 229 | |
| 230 | // Populate GRPC proto structure |
| 231 | simMetrics.Metrics = append(simMetrics.Metrics, ponMetrics) |
| 232 | simMetrics.Metrics = append(simMetrics.Metrics, nniMetrics) |
| 233 | |
| 234 | return simMetrics |
| 235 | } |