blob: e9e1585c10c5dfb8b7fb395bd7bb46cd31b9f41a [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001/*
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 Barbarie35595062018-02-08 08:34:39 -050016package core
17
18import (
19 "github.com/opencord/voltha/ponsim/v2/common"
20 "github.com/opencord/voltha/protos/go/voltha"
21 "github.com/sirupsen/logrus"
22)
23
24/*
25metricCounter holds details for a specific metric
26*/
27type metricCounter struct {
28 Name string
29 Value [2]int // [PON,NNI] values
30 Min int
31 Max int
32}
33
34/*
35Create a new MetricCounter instance for TX packets
36*/
37func newTxMetricCounter(name txMetricCounterType, min int, max int) *metricCounter {
38 return &metricCounter{Name: name.String(), Min: min, Max: max}
39}
40
41/*
42Create a new MetricCounter instance for RX packets
43*/
44func newRxMetricCounter(name rxMetricCounterType, min int, max int) *metricCounter {
45 return &metricCounter{Name: name.String(), Min: min, Max: max}
46}
47
48/*
49Define TX constants
50*/
51type txMetricCounterType uint8
52
53const (
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/*
64TX packet constants string equivalents
65*/
66var 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
76func (t txMetricCounterType) String() string {
77 return txMetricCounterEnum[t]
78}
79
80/*
81Define RX constants
82*/
83type rxMetricCounterType uint8
84
85const (
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/*
96RX packet constants string equivalents
97*/
98var 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
108func (t rxMetricCounterType) String() string {
109 return rxMetricCounterEnum[t]
110}
111
112/*
113
114 */
115type PonSimMetricCounter struct {
116 Name string
Scott Bakerb5be94d2018-10-09 16:13:32 -0700117 Device_Type string
Stephane Barbarie35595062018-02-08 08:34:39 -0500118 TxCounters map[txMetricCounterType]*metricCounter
119 RxCounters map[rxMetricCounterType]*metricCounter
120}
121
122/*
123NewPonSimMetricCounter instantiates new metric counters for a PON device
124*/
Scott Bakerb5be94d2018-10-09 16:13:32 -0700125func NewPonSimMetricCounter(name string, device_type string) *PonSimMetricCounter {
126 counter := &PonSimMetricCounter{Name: name, Device_Type: device_type}
Stephane Barbarie35595062018-02-08 08:34:39 -0500127
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/*
151CountRxFrame increments the receive count for a specific packet size metric
152*/
153func (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/*
162CountTxFrame increments the transmit count for a specific packet size metric
163*/
164func (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/*
173LogCounts logs the current counts for all RX/TX packets
174*/
175func (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/*
185MakeProto collects all RX/TX metrics with which it constructs a GRPC proto metrics structure
186*/
187func (mc *PonSimMetricCounter) MakeProto() *voltha.PonSimMetrics {
188 simMetrics := &voltha.PonSimMetrics{Device: mc.Name}
189 ponMetrics := &voltha.PonSimPortMetrics{PortName: "pon"}
Scott Bakerb5be94d2018-10-09 16:13:32 -0700190 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 Barbarie35595062018-02-08 08:34:39 -0500202
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 Bakerb5be94d2018-10-09 16:13:32 -0700213 // NNI/UNI values
214 portMetrics.Packets = append(
215 portMetrics.Packets,
Stephane Barbarie35595062018-02-08 08:34:39 -0500216 &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 Bakerb5be94d2018-10-09 16:13:32 -0700232 // NNI/UNI values
233 portMetrics.Packets = append(
234 portMetrics.Packets,
Stephane Barbarie35595062018-02-08 08:34:39 -0500235 &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 Bakerb5be94d2018-10-09 16:13:32 -0700244 simMetrics.Metrics = append(simMetrics.Metrics, portMetrics)
Stephane Barbarie35595062018-02-08 08:34:39 -0500245
246 return simMetrics
247}