blob: b55ce70ee195b553d96f5da747425f2eeee77538 [file] [log] [blame]
Georgios Katsikasfda66742018-07-31 20:18:14 +02001/*
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 */
Georgios Katsikas740d3282020-03-18 12:05:03 +010016
Georgios Katsikasfda66742018-07-31 20:18:14 +020017package org.onosproject.drivers.server.stats;
18
19import java.util.Map;
20import java.util.HashMap;
21
Georgios Katsikas740d3282020-03-18 12:05:03 +010022import static org.onosproject.drivers.server.Constants.MSG_CONVERSION_TO_BITS;
23import static org.onosproject.drivers.server.Constants.MSG_CONVERSION_TO_BYTES;
24
Georgios Katsikasfda66742018-07-31 20:18:14 +020025/**
26 * Representation of a monitoring unit.
27 */
28public interface MonitoringUnit {
29
30 /**
31 * Throughput-related monitoring units.
32 */
33 public enum ThroughputUnit implements MonitoringUnit {
34
35 BPS("bps"),
36 KBPS("kbps"),
37 MBPS("mbps"),
38 GBPS("gbps");
39
40 private String throughputUnit;
41
42 // Statically maps throughput monitoring units to enum types
43 private static final Map<String, MonitoringUnit> MAP =
44 new HashMap<String, MonitoringUnit>();
45 static {
46 for (ThroughputUnit tu : ThroughputUnit.values()) {
47 MAP.put(tu.toString().toLowerCase(), (MonitoringUnit) tu);
48 }
49 }
50
51 private ThroughputUnit(String throughputUnit) {
52 this.throughputUnit = throughputUnit;
53 }
54
55 public static MonitoringUnit getByName(String tu) {
56 tu = tu.toLowerCase();
57 return MAP.get(tu);
58 }
59
Georgios Katsikas973a2652018-06-28 08:45:47 +020060 public static float toGbps(float value, ThroughputUnit fromUnit) {
61 if (value == 0) {
62 return value;
63 }
64
65 if (fromUnit == BPS) {
66 return value / 1000000000;
67 } else if (fromUnit == KBPS) {
68 return value / 1000000;
69 } else if (fromUnit == MBPS) {
70 return value / 1000;
71 }
72
73 return value;
74 }
75
Georgios Katsikasfda66742018-07-31 20:18:14 +020076 @Override
77 public String toString() {
78 return this.throughputUnit;
79 }
80
81 };
82
83 /**
84 * Latency-related monitoring units.
85 */
86 public enum LatencyUnit implements MonitoringUnit {
87
88 NANO_SECOND("ns"),
89 MICRO_SECOND("us"),
90 MILLI_SECOND("ms"),
91 SECOND("s");
92
93 private String latencyUnit;
94
95 // Statically maps latency monitoring units to enum types
96 private static final Map<String, MonitoringUnit> MAP =
97 new HashMap<String, MonitoringUnit>();
98 static {
99 for (LatencyUnit lu : LatencyUnit.values()) {
100 MAP.put(lu.toString().toLowerCase(), (MonitoringUnit) lu);
101 }
102 }
103
104 private LatencyUnit(String latencyUnit) {
105 this.latencyUnit = latencyUnit;
106 }
107
108 public static MonitoringUnit getByName(String lu) {
109 lu = lu.toLowerCase();
110 return MAP.get(lu);
111 }
112
Georgios Katsikas973a2652018-06-28 08:45:47 +0200113 public static float toNano(float value, LatencyUnit fromUnit) {
114 if (value == 0) {
115 return value;
116 }
117
118 if (fromUnit == MICRO_SECOND) {
119 return value * 1000;
120 } else if (fromUnit == MILLI_SECOND) {
121 return value * 1000000;
122 } else if (fromUnit == SECOND) {
123 return value * 1000000000;
124 }
125
126 return value;
127 }
128
Georgios Katsikasfda66742018-07-31 20:18:14 +0200129 @Override
130 public String toString() {
131 return this.latencyUnit;
132 }
133
134 };
135
Georgios Katsikas740d3282020-03-18 12:05:03 +0100136 /**
137 * Capacity-related monitoring units.
138 */
139 public enum CapacityUnit implements MonitoringUnit {
140
141 BITS("Bits"),
142 KILOBITS("kBits"),
143 MEGABITS("MBits"),
144 GIGABITS("GBits"),
145 BYTES("Bytes"),
146 KILOBYTES("kBytes"),
147 MEGABYTES("MBytes"),
148 GIGABYTES("GBytes");
149
150 private String capacityUnit;
151
152 // Statically maps capacity monitoring units to enum types
153 private static final Map<String, MonitoringUnit> MAP =
154 new HashMap<String, MonitoringUnit>();
155 static {
156 for (CapacityUnit cu : CapacityUnit.values()) {
157 MAP.put(cu.toString().toLowerCase(), (MonitoringUnit) cu);
158 }
159 }
160
161 private CapacityUnit(String capacityUnit) {
162 this.capacityUnit = capacityUnit;
163 }
164
165 public static MonitoringUnit getByName(String cu) {
166 cu = cu.toLowerCase();
167 return MAP.get(cu);
168 }
169
170 public static float toBits(float value, CapacityUnit fromUnit) {
171 if (value == 0) {
172 return value;
173 }
174
175 if (fromUnit == BITS) {
176 return value;
177 } else if (fromUnit == KILOBITS) {
178 return (value * 1000);
179 } else if (fromUnit == MEGABITS) {
180 return (value * 1000000);
181 } else if (fromUnit == GIGABITS) {
182 return (value * 1000000000);
183 } else if (fromUnit == BYTES) {
184 return value * 8;
185 } else if (fromUnit == KILOBYTES) {
186 return (value * 1000) * 8;
187 } else if (fromUnit == MEGABYTES) {
188 return (value * 1000000) * 8;
189 } else if (fromUnit == GIGABYTES) {
190 return (value * 1000000000) * 8;
191 }
192
193 throw new IllegalArgumentException(MSG_CONVERSION_TO_BITS);
194 }
195
196 public static float toKiloBits(float value, CapacityUnit fromUnit) {
197 return toBits(value, fromUnit) / 1000;
198 }
199
200 public static float toMegaBits(float value, CapacityUnit fromUnit) {
201 return toBits(value, fromUnit) / 1000000;
202 }
203
204 public static float toGigaBits(float value, CapacityUnit fromUnit) {
205 return toBits(value, fromUnit) / 1000000000;
206 }
207
208 public static float toBytes(float value, CapacityUnit fromUnit) {
209 if (value == 0) {
210 return value;
211 }
212
213 if (fromUnit == BITS) {
214 return value / 8;
215 } else if (fromUnit == KILOBITS) {
216 return (value * 1000) / 8;
217 } else if (fromUnit == MEGABITS) {
218 return (value * 1000000) / 8;
219 } else if (fromUnit == GIGABITS) {
220 return (value * 1000000000) / 8;
221 } else if (fromUnit == BYTES) {
222 return value;
223 } else if (fromUnit == KILOBYTES) {
224 return value * 1000;
225 } else if (fromUnit == MEGABYTES) {
226 return value * 1000000;
227 } else if (fromUnit == GIGABYTES) {
228 return value * 1000000000;
229 }
230
231 throw new IllegalArgumentException(MSG_CONVERSION_TO_BYTES);
232 }
233
234 public static float toKiloBytes(float value, CapacityUnit fromUnit) {
235 return toBytes(value, fromUnit) / 1000;
236 }
237
238 public static float toMegaBytes(float value, CapacityUnit fromUnit) {
239 return toBytes(value, fromUnit) / 1000000;
240 }
241
242 public static float toGigaBytes(float value, CapacityUnit fromUnit) {
243 return toBytes(value, fromUnit) / 1000000000;
244 }
245
246 @Override
247 public String toString() {
248 return this.capacityUnit;
249 }
250
251 };
252
253 /**
254 * Percentage-related monitoring unit.
255 */
256 public enum PercentageUnit implements MonitoringUnit {
257
258 PERCENTAGE("percentage");
259
260 private String percentageUnit;
261
262 // Statically maps percentage monitoring units to enum types
263 private static final Map<String, MonitoringUnit> MAP =
264 new HashMap<String, MonitoringUnit>();
265 static {
266 MAP.put(PERCENTAGE.toString().toLowerCase(), (MonitoringUnit) PERCENTAGE);
267 }
268
269 private PercentageUnit(String percentageUnit) {
270 this.percentageUnit = percentageUnit;
271 }
272
273 public static MonitoringUnit getByName(String pu) {
274 pu = pu.toLowerCase();
275 return MAP.get(pu);
276 }
277
278 @Override
279 public String toString() {
280 return this.percentageUnit;
281 }
282
283 };
284
Georgios Katsikasfda66742018-07-31 20:18:14 +0200285 String toString();
286
287}