Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 3 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 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 |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 7 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 9 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 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 Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.impl.topo.util; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.LinkKey; |
| 21 | import org.onosproject.net.statistic.Load; |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 22 | import org.onosproject.ui.model.topo.UiLinkId; |
Simon Hunt | 191c84a | 2015-08-21 08:24:48 -0700 | [diff] [blame] | 23 | import org.onosproject.ui.topo.BiLink; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.topo.LinkHighlight; |
| 25 | import org.onosproject.ui.topo.LinkHighlight.Flavor; |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 26 | import org.onosproject.ui.topo.Mod; |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.topo.TopoUtils.Magnitude; |
| 28 | import org.onosproject.ui.topo.TopoUtils.ValueLabel; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 29 | |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 30 | import java.util.HashSet; |
| 31 | import java.util.Set; |
| 32 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 33 | import static com.google.common.base.MoreObjects.toStringHelper; |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 34 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.NO_HIGHLIGHT; |
| 35 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.PRIMARY_HIGHLIGHT; |
| 36 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.SECONDARY_HIGHLIGHT; |
| 37 | import static org.onosproject.ui.topo.TopoUtils.formatBytes; |
| 38 | import static org.onosproject.ui.topo.TopoUtils.formatClippedBitRate; |
| 39 | import static org.onosproject.ui.topo.TopoUtils.formatFlows; |
| 40 | import static org.onosproject.ui.topo.TopoUtils.formatPacketRate; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Representation of a link and its inverse, and associated traffic data. |
| 44 | * This class understands how to generate the appropriate |
| 45 | * {@link LinkHighlight}s for showing traffic data on the topology view. |
| 46 | */ |
| 47 | public class TrafficLink extends BiLink { |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 48 | private static final Mod PORT_TRAFFIC_GREEN = new Mod("port-traffic-green"); |
| 49 | private static final Mod PORT_TRAFFIC_YELLOW = new Mod("port-traffic-yellow"); |
| 50 | private static final Mod PORT_TRAFFIC_ORANGE = new Mod("port-traffic-orange"); |
| 51 | private static final Mod PORT_TRAFFIC_RED = new Mod("port-traffic-red"); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 52 | |
| 53 | private static final String EMPTY = ""; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 54 | |
| 55 | private long bytes = 0; |
| 56 | private long rate = 0; |
| 57 | private long flows = 0; |
| 58 | private Flavor taggedFlavor = NO_HIGHLIGHT; |
| 59 | private boolean hasTraffic = false; |
| 60 | private boolean isOptical = false; |
| 61 | private boolean antMarch = false; |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 62 | private Set<Mod> mods = new HashSet<>(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Constructs a traffic link for the given key and initial link. |
| 66 | * |
| 67 | * @param key canonical key for this traffic link |
| 68 | * @param link first link |
| 69 | */ |
| 70 | public TrafficLink(LinkKey key, Link link) { |
| 71 | super(key, link); |
| 72 | } |
| 73 | |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 74 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 75 | /** |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 76 | * Returns an "empty" traffic link (one with no underlying links or stats) |
| 77 | * with the given identifier. This is useful when we want to aggregate |
| 78 | * stats from other links into a single entity (such as a region-region |
| 79 | * link reporting the stats for the links that compose it). |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 80 | * |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 81 | * @param id the link identifier |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 82 | */ |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 83 | public TrafficLink(UiLinkId id) { |
| 84 | super(id); |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object o) { |
| 89 | if (this == o) { |
| 90 | return true; |
| 91 | } |
| 92 | if (o == null || getClass() != o.getClass()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | TrafficLink that = (TrafficLink) o; |
| 97 | |
| 98 | return bytes == that.bytes && rate == that.rate && |
| 99 | flows == that.flows && hasTraffic == that.hasTraffic && |
| 100 | isOptical == that.isOptical && antMarch == that.antMarch && |
| 101 | taggedFlavor == that.taggedFlavor && mods.equals(that.mods); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | int result = (int) (bytes ^ (bytes >>> 32)); |
| 107 | result = 31 * result + (int) (rate ^ (rate >>> 32)); |
| 108 | result = 31 * result + (int) (flows ^ (flows >>> 32)); |
| 109 | result = 31 * result + taggedFlavor.hashCode(); |
| 110 | result = 31 * result + (hasTraffic ? 1 : 0); |
| 111 | result = 31 * result + (isOptical ? 1 : 0); |
| 112 | result = 31 * result + (antMarch ? 1 : 0); |
| 113 | result = 31 * result + mods.hashCode(); |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return toStringHelper(this) |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 120 | .add("linkId", linkId()) |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 121 | .add("bytes", bytes) |
| 122 | .add("rate", rate) |
| 123 | .add("flows", flows) |
| 124 | .toString(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the count of bytes. |
| 129 | * |
| 130 | * @return the byte count |
| 131 | */ |
| 132 | public long bytes() { |
| 133 | return bytes; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the rate. |
| 138 | * |
| 139 | * @return the rate |
| 140 | */ |
| 141 | public long rate() { |
| 142 | return rate; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the flows. |
| 147 | * |
| 148 | * @return flow count |
| 149 | */ |
| 150 | public long flows() { |
| 151 | return flows; |
| 152 | } |
| 153 | |
| 154 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 155 | * Sets the optical flag to the given value. |
| 156 | * |
| 157 | * @param b true if an optical link |
| 158 | * @return self, for chaining |
| 159 | */ |
| 160 | public TrafficLink optical(boolean b) { |
| 161 | isOptical = b; |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets the ant march flag to the given value. |
| 167 | * |
| 168 | * @param b true if marching ants required |
| 169 | * @return self, for chaining |
| 170 | */ |
| 171 | public TrafficLink antMarch(boolean b) { |
| 172 | antMarch = b; |
| 173 | return this; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Tags this traffic link with the flavor to be used in visual rendering. |
| 178 | * |
| 179 | * @param flavor the flavor to tag |
| 180 | * @return self, for chaining |
| 181 | */ |
| 182 | public TrafficLink tagFlavor(Flavor flavor) { |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 183 | taggedFlavor = flavor; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 184 | return this; |
| 185 | } |
| 186 | |
| 187 | /** |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 188 | * Tags this traffic link with the mods to be used in visual rendering. |
| 189 | * |
| 190 | * @param mods the mods to tag on this link |
| 191 | * @return self, for chaining |
| 192 | */ |
| 193 | public TrafficLink tagMods(Set<Mod> mods) { |
| 194 | if (mods != null) { |
| 195 | this.mods.addAll(mods); |
| 196 | } |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 201 | * Adds load statistics, marks the traffic link as having traffic. |
| 202 | * |
| 203 | * @param load load to add |
| 204 | */ |
| 205 | public void addLoad(Load load) { |
| 206 | addLoad(load, 0); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Adds load statistics, marks the traffic link as having traffic, if the |
| 211 | * load {@link Load#rate rate} is greater than the given threshold |
| 212 | * (expressed in bytes per second). |
| 213 | * |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 214 | * @param load load to add |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 215 | * @param threshold threshold to register traffic |
| 216 | */ |
| 217 | public void addLoad(Load load, double threshold) { |
| 218 | if (load != null) { |
| 219 | this.hasTraffic = hasTraffic || load.rate() > threshold; |
| 220 | this.bytes += load.latest(); |
| 221 | this.rate += load.rate(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Adds the given count of flows to this traffic link. |
| 227 | * |
| 228 | * @param count count of flows |
| 229 | */ |
| 230 | public void addFlows(int count) { |
| 231 | this.flows += count; |
| 232 | } |
| 233 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Merges the load recorded on the given traffic link into this one. |
| 236 | * |
| 237 | * @param other the other traffic link |
| 238 | */ |
| 239 | public void mergeStats(TrafficLink other) { |
| 240 | this.bytes += other.bytes; |
| 241 | this.rate += other.rate; |
| 242 | this.flows += other.flows; |
| 243 | } |
| 244 | |
| 245 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 246 | @Override |
| 247 | public LinkHighlight highlight(Enum<?> type) { |
| 248 | StatsType statsType = (StatsType) type; |
| 249 | switch (statsType) { |
| 250 | case FLOW_COUNT: |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 251 | return highlightForFlowCount(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 252 | |
| 253 | case FLOW_STATS: |
| 254 | case PORT_STATS: |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 255 | case PORT_PACKET_STATS: |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 256 | return highlightForStats(statsType); |
| 257 | |
| 258 | case TAGGED: |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 259 | return highlightForTagging(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 260 | |
| 261 | default: |
| 262 | throw new IllegalStateException("unexpected case: " + statsType); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private LinkHighlight highlightForStats(StatsType type) { |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 267 | ValueLabel vl = null; |
| 268 | Mod m = null; |
| 269 | |
| 270 | // based on the type of stats, need to determine the label and "color"... |
| 271 | switch (type) { |
| 272 | case FLOW_STATS: |
| 273 | vl = formatBytes(bytes); |
| 274 | // default to "secondary highlighting" of link |
| 275 | break; |
| 276 | |
| 277 | case PORT_STATS: |
| 278 | vl = formatClippedBitRate(rate); |
| 279 | |
| 280 | // set color based on bits per second... |
| 281 | if (vl.magnitude() == Magnitude.ONE || |
| 282 | vl.magnitude() == Magnitude.KILO) { |
| 283 | m = PORT_TRAFFIC_GREEN; |
| 284 | |
| 285 | } else if (vl.magnitude() == Magnitude.MEGA) { |
| 286 | m = PORT_TRAFFIC_YELLOW; |
| 287 | |
| 288 | } else if (vl.magnitude() == Magnitude.GIGA) { |
| 289 | m = vl.clipped() ? PORT_TRAFFIC_RED : PORT_TRAFFIC_ORANGE; |
| 290 | } |
| 291 | break; |
| 292 | |
| 293 | case PORT_PACKET_STATS: |
| 294 | vl = formatPacketRate(rate); |
| 295 | |
Thomas Vachuska | 605134d | 2017-03-31 11:15:39 -0700 | [diff] [blame] | 296 | // FIXME: Provisional color threshold parameters for packets |
| 297 | // set color based on bits per second... |
| 298 | if (rate < 10) { |
| 299 | m = PORT_TRAFFIC_GREEN; |
| 300 | |
| 301 | } else if (rate < 1000) { |
| 302 | m = PORT_TRAFFIC_YELLOW; |
| 303 | |
| 304 | } else if (rate < 100000) { |
| 305 | m = PORT_TRAFFIC_ORANGE; |
| 306 | } else { |
| 307 | m = PORT_TRAFFIC_RED; |
| 308 | } |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | default: |
| 312 | break; |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 313 | } |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 314 | |
| 315 | LinkHighlight hlite = new LinkHighlight(linkId(), SECONDARY_HIGHLIGHT); |
| 316 | if (vl != null) { |
| 317 | hlite.setLabel(vl.toString()); |
| 318 | } |
| 319 | if (m != null) { |
| 320 | hlite.addMod(m); |
| 321 | } |
| 322 | |
| 323 | return addCustomMods(hlite); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 326 | private LinkHighlight highlightForFlowCount() { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 327 | Flavor flavor = flows > 0 ? PRIMARY_HIGHLIGHT : SECONDARY_HIGHLIGHT; |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 328 | LinkHighlight hlite = new LinkHighlight(linkId(), flavor) |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 329 | .setLabel(formatFlows(flows)); |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 330 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 331 | return addCustomMods(hlite); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 334 | private LinkHighlight highlightForTagging() { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 335 | LinkHighlight hlite = new LinkHighlight(linkId(), taggedFlavor) |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 336 | .setLabel(hasTraffic ? formatBytes(bytes).toString() : EMPTY); |
| 337 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 338 | if (isOptical) { |
| 339 | hlite.addMod(LinkHighlight.MOD_OPTICAL); |
| 340 | } |
| 341 | if (antMarch) { |
| 342 | hlite.addMod(LinkHighlight.MOD_ANIMATED); |
| 343 | } |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 344 | return addCustomMods(hlite); |
| 345 | } |
| 346 | |
| 347 | private LinkHighlight addCustomMods(LinkHighlight hlite) { |
Andrea Campanella | af93468 | 2017-03-05 11:06:40 +0100 | [diff] [blame] | 348 | if (!mods.isEmpty()) { |
| 349 | mods.forEach(hlite::addMod); |
| 350 | } |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 351 | return hlite; |
| 352 | } |
| 353 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 354 | /** |
| 355 | * Returns true if this link has been deemed to have enough traffic |
| 356 | * to register on the topology view in the web UI. |
| 357 | * |
| 358 | * @return true if this link has displayable traffic |
| 359 | */ |
| 360 | public boolean hasTraffic() { |
| 361 | return hasTraffic; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Designates type of traffic statistics to report on a highlighted link. |
| 366 | */ |
| 367 | public enum StatsType { |
| 368 | /** |
| 369 | * Number of flows. |
| 370 | */ |
| 371 | FLOW_COUNT, |
| 372 | |
| 373 | /** |
| 374 | * Number of bytes. |
| 375 | */ |
| 376 | FLOW_STATS, |
| 377 | |
| 378 | /** |
| 379 | * Number of bits per second. |
| 380 | */ |
| 381 | PORT_STATS, |
| 382 | |
| 383 | /** |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 384 | * Number of packets per second. |
| 385 | */ |
| 386 | PORT_PACKET_STATS, |
| 387 | |
| 388 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 389 | * Custom tagged information. |
| 390 | */ |
| 391 | TAGGED |
| 392 | } |
| 393 | } |