blob: d43b376720ea3867411cc50c15bc9920a1854783 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 *
16 */
17
18package org.onosproject.ui.impl.topo;
19
20import org.onosproject.net.Link;
21import org.onosproject.net.LinkKey;
22
23import java.text.DecimalFormat;
Simon Hunta17fa672015-08-19 18:42:22 -070024
25import static org.onosproject.net.LinkKey.linkKey;
26
27/**
Simon Hunt4fc86852015-08-20 17:57:52 -070028 * Utility methods for helping out with formatting data for the Topology View
29 * in the web client.
Simon Hunta17fa672015-08-19 18:42:22 -070030 */
31public final class TopoUtils {
32
Simon Hunt4fc86852015-08-20 17:57:52 -070033 // explicit decision made to not 'javadoc' these self explanatory constants
Simon Hunta17fa672015-08-19 18:42:22 -070034 public static final double KILO = 1024;
35 public static final double MEGA = 1024 * KILO;
36 public static final double GIGA = 1024 * MEGA;
37
38 public static final String GBITS_UNIT = "Gb";
39 public static final String MBITS_UNIT = "Mb";
40 public static final String KBITS_UNIT = "Kb";
41 public static final String BITS_UNIT = "b";
42 public static final String GBYTES_UNIT = "GB";
43 public static final String MBYTES_UNIT = "MB";
44 public static final String KBYTES_UNIT = "KB";
45 public static final String BYTES_UNIT = "B";
46
47
48 private static final DecimalFormat DF2 = new DecimalFormat("#,###.##");
49
50 private static final String COMPACT = "%s/%s-%s/%s";
51 private static final String EMPTY = "";
52 private static final String SPACE = " ";
53 private static final String PER_SEC = "ps";
54 private static final String FLOW = "flow";
55 private static final String FLOWS = "flows";
56
57 // non-instantiable
58 private TopoUtils() { }
59
60 /**
61 * Returns a compact identity for the given link, in the form
62 * used to identify links in the Topology View on the client.
63 *
64 * @param link link
65 * @return compact link identity
66 */
67 public static String compactLinkString(Link link) {
68 return String.format(COMPACT, link.src().elementId(), link.src().port(),
69 link.dst().elementId(), link.dst().port());
70 }
71
72 /**
73 * Produces a canonical link key, that is, one that will match both a link
74 * and its inverse.
75 *
76 * @param link the link
77 * @return canonical key
78 */
79 public static LinkKey canonicalLinkKey(Link link) {
80 String sn = link.src().elementId().toString();
81 String dn = link.dst().elementId().toString();
82 return sn.compareTo(dn) < 0 ?
83 linkKey(link.src(), link.dst()) : linkKey(link.dst(), link.src());
84 }
85
86 /**
87 * Returns human readable count of bytes, to be displayed as a label.
88 *
89 * @param bytes number of bytes
90 * @return formatted byte count
91 */
92 public static String formatBytes(long bytes) {
93 String unit;
94 double value;
95 if (bytes > GIGA) {
96 value = bytes / GIGA;
97 unit = GBYTES_UNIT;
98 } else if (bytes > MEGA) {
99 value = bytes / MEGA;
100 unit = MBYTES_UNIT;
101 } else if (bytes > KILO) {
102 value = bytes / KILO;
103 unit = KBYTES_UNIT;
104 } else {
105 value = bytes;
106 unit = BYTES_UNIT;
107 }
108 return DF2.format(value) + SPACE + unit;
109 }
110
111 /**
112 * Returns human readable bit rate, to be displayed as a label.
113 *
114 * @param bytes bytes per second
115 * @return formatted bits per second
116 */
117 public static String formatBitRate(long bytes) {
118 String unit;
119 double value;
120
121 //Convert to bits
122 long bits = bytes * 8;
123 if (bits > GIGA) {
124 value = bits / GIGA;
125 unit = GBITS_UNIT;
126
127 // NOTE: temporary hack to clip rate at 10.0 Gbps
128 // Added for the CORD Fabric demo at ONS 2015
129 // TODO: provide a more elegant solution to this issue
130 if (value > 10.0) {
131 value = 10.0;
132 }
133
134 } else if (bits > MEGA) {
135 value = bits / MEGA;
136 unit = MBITS_UNIT;
137 } else if (bits > KILO) {
138 value = bits / KILO;
139 unit = KBITS_UNIT;
140 } else {
141 value = bits;
142 unit = BITS_UNIT;
143 }
144 return DF2.format(value) + SPACE + unit + PER_SEC;
145 }
146
147 /**
148 * Returns human readable flow count, to be displayed as a label.
149 *
150 * @param flows number of flows
151 * @return formatted flow count
152 */
153 public static String formatFlows(long flows) {
154 if (flows < 1) {
155 return EMPTY;
156 }
157 return String.valueOf(flows) + SPACE + (flows > 1 ? FLOWS : FLOW);
158 }
Simon Hunta17fa672015-08-19 18:42:22 -0700159}