blob: 33d87637940bbbb9411b8f8e8d4dffa1b226bede [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 */
16package org.onosproject.drivers.server.stats;
17
18import java.util.Map;
19import java.util.HashMap;
20
21/**
22 * Representation of a monitoring unit.
23 */
24public interface MonitoringUnit {
25
26 /**
27 * Throughput-related monitoring units.
28 */
29 public enum ThroughputUnit implements MonitoringUnit {
30
31 BPS("bps"),
32 KBPS("kbps"),
33 MBPS("mbps"),
34 GBPS("gbps");
35
36 private String throughputUnit;
37
38 // Statically maps throughput monitoring units to enum types
39 private static final Map<String, MonitoringUnit> MAP =
40 new HashMap<String, MonitoringUnit>();
41 static {
42 for (ThroughputUnit tu : ThroughputUnit.values()) {
43 MAP.put(tu.toString().toLowerCase(), (MonitoringUnit) tu);
44 }
45 }
46
47 private ThroughputUnit(String throughputUnit) {
48 this.throughputUnit = throughputUnit;
49 }
50
51 public static MonitoringUnit getByName(String tu) {
52 tu = tu.toLowerCase();
53 return MAP.get(tu);
54 }
55
Georgios Katsikas973a2652018-06-28 08:45:47 +020056 public static float toGbps(float value, ThroughputUnit fromUnit) {
57 if (value == 0) {
58 return value;
59 }
60
61 if (fromUnit == BPS) {
62 return value / 1000000000;
63 } else if (fromUnit == KBPS) {
64 return value / 1000000;
65 } else if (fromUnit == MBPS) {
66 return value / 1000;
67 }
68
69 return value;
70 }
71
Georgios Katsikasfda66742018-07-31 20:18:14 +020072 @Override
73 public String toString() {
74 return this.throughputUnit;
75 }
76
77 };
78
79 /**
80 * Latency-related monitoring units.
81 */
82 public enum LatencyUnit implements MonitoringUnit {
83
84 NANO_SECOND("ns"),
85 MICRO_SECOND("us"),
86 MILLI_SECOND("ms"),
87 SECOND("s");
88
89 private String latencyUnit;
90
91 // Statically maps latency monitoring units to enum types
92 private static final Map<String, MonitoringUnit> MAP =
93 new HashMap<String, MonitoringUnit>();
94 static {
95 for (LatencyUnit lu : LatencyUnit.values()) {
96 MAP.put(lu.toString().toLowerCase(), (MonitoringUnit) lu);
97 }
98 }
99
100 private LatencyUnit(String latencyUnit) {
101 this.latencyUnit = latencyUnit;
102 }
103
104 public static MonitoringUnit getByName(String lu) {
105 lu = lu.toLowerCase();
106 return MAP.get(lu);
107 }
108
Georgios Katsikas973a2652018-06-28 08:45:47 +0200109 public static float toNano(float value, LatencyUnit fromUnit) {
110 if (value == 0) {
111 return value;
112 }
113
114 if (fromUnit == MICRO_SECOND) {
115 return value * 1000;
116 } else if (fromUnit == MILLI_SECOND) {
117 return value * 1000000;
118 } else if (fromUnit == SECOND) {
119 return value * 1000000000;
120 }
121
122 return value;
123 }
124
Georgios Katsikasfda66742018-07-31 20:18:14 +0200125 @Override
126 public String toString() {
127 return this.latencyUnit;
128 }
129
130 };
131
132 String toString();
133
134}