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