blob: 8d6b3191a9f0bf55c214731d819cb20bbc404973 [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;
24import java.util.Map;
25
26import static org.onosproject.net.LinkKey.linkKey;
27
28/**
29 * Utility methods for helping out with the topology view.
30 */
31public final class TopoUtils {
32
33 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 }
158
159
160 /**
161 * Creates a new biLink with the supplied link (and adds it to the map),
162 * or attaches the link to an existing biLink (which already has the
163 * peer link).
164 *
165 * @param linkMap map of biLinks
166 * @param link the link to add
167 * @return the biLink to which the link was added
168 */
169 // creates a new biLink with supplied link, or attaches link to the
170 // existing biLink (which already has its peer link)
171 public static BiLink addLink(Map<LinkKey, BiLink> linkMap, Link link) {
172 LinkKey key = TopoUtils.canonicalLinkKey(link);
173 BiLink biLink = linkMap.get(key);
174 if (biLink != null) {
175 biLink.setOther(link);
176 } else {
177 biLink = new BiLink(key, link);
178 linkMap.put(key, biLink);
179 }
180 return biLink;
181 }
182
183
184}