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