Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
| 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.LinkKey; |
| 22 | import org.onosproject.net.statistic.Load; |
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; |
Simon Hunt | 191c84a | 2015-08-21 08:24:48 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.topo.TopoUtils; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 27 | |
| 28 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.NO_HIGHLIGHT; |
| 29 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.PRIMARY_HIGHLIGHT; |
| 30 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.SECONDARY_HIGHLIGHT; |
| 31 | |
| 32 | /** |
| 33 | * Representation of a link and its inverse, and associated traffic data. |
| 34 | * This class understands how to generate the appropriate |
| 35 | * {@link LinkHighlight}s for showing traffic data on the topology view. |
| 36 | */ |
| 37 | public class TrafficLink extends BiLink { |
| 38 | |
| 39 | private static final String EMPTY = ""; |
| 40 | private static final String QUE = "?"; |
| 41 | |
| 42 | private long bytes = 0; |
| 43 | private long rate = 0; |
| 44 | private long flows = 0; |
| 45 | private Flavor taggedFlavor = NO_HIGHLIGHT; |
| 46 | private boolean hasTraffic = false; |
| 47 | private boolean isOptical = false; |
| 48 | private boolean antMarch = false; |
| 49 | |
| 50 | /** |
| 51 | * Constructs a traffic link for the given key and initial link. |
| 52 | * |
| 53 | * @param key canonical key for this traffic link |
| 54 | * @param link first link |
| 55 | */ |
| 56 | public TrafficLink(LinkKey key, Link link) { |
| 57 | super(key, link); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Sets the optical flag to the given value. |
| 62 | * |
| 63 | * @param b true if an optical link |
| 64 | * @return self, for chaining |
| 65 | */ |
| 66 | public TrafficLink optical(boolean b) { |
| 67 | isOptical = b; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets the ant march flag to the given value. |
| 73 | * |
| 74 | * @param b true if marching ants required |
| 75 | * @return self, for chaining |
| 76 | */ |
| 77 | public TrafficLink antMarch(boolean b) { |
| 78 | antMarch = b; |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tags this traffic link with the flavor to be used in visual rendering. |
| 84 | * |
| 85 | * @param flavor the flavor to tag |
| 86 | * @return self, for chaining |
| 87 | */ |
| 88 | public TrafficLink tagFlavor(Flavor flavor) { |
| 89 | this.taggedFlavor = flavor; |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Adds load statistics, marks the traffic link as having traffic. |
| 95 | * |
| 96 | * @param load load to add |
| 97 | */ |
| 98 | public void addLoad(Load load) { |
| 99 | addLoad(load, 0); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Adds load statistics, marks the traffic link as having traffic, if the |
| 104 | * load {@link Load#rate rate} is greater than the given threshold |
| 105 | * (expressed in bytes per second). |
| 106 | * |
| 107 | * @param load load to add |
| 108 | * @param threshold threshold to register traffic |
| 109 | */ |
| 110 | public void addLoad(Load load, double threshold) { |
| 111 | if (load != null) { |
| 112 | this.hasTraffic = hasTraffic || load.rate() > threshold; |
| 113 | this.bytes += load.latest(); |
| 114 | this.rate += load.rate(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adds the given count of flows to this traffic link. |
| 120 | * |
| 121 | * @param count count of flows |
| 122 | */ |
| 123 | public void addFlows(int count) { |
| 124 | this.flows += count; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public LinkHighlight highlight(Enum<?> type) { |
| 129 | StatsType statsType = (StatsType) type; |
| 130 | switch (statsType) { |
| 131 | case FLOW_COUNT: |
| 132 | return highlightForFlowCount(statsType); |
| 133 | |
| 134 | case FLOW_STATS: |
| 135 | case PORT_STATS: |
| 136 | return highlightForStats(statsType); |
| 137 | |
| 138 | case TAGGED: |
| 139 | return highlightForTagging(statsType); |
| 140 | |
| 141 | default: |
| 142 | throw new IllegalStateException("unexpected case: " + statsType); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private LinkHighlight highlightForStats(StatsType type) { |
| 147 | return new LinkHighlight(linkId(), SECONDARY_HIGHLIGHT) |
| 148 | .setLabel(generateLabel(type)); |
| 149 | } |
| 150 | |
| 151 | private LinkHighlight highlightForFlowCount(StatsType type) { |
| 152 | Flavor flavor = flows > 0 ? PRIMARY_HIGHLIGHT : SECONDARY_HIGHLIGHT; |
| 153 | return new LinkHighlight(linkId(), flavor) |
| 154 | .setLabel(generateLabel(type)); |
| 155 | } |
| 156 | |
| 157 | private LinkHighlight highlightForTagging(StatsType type) { |
| 158 | LinkHighlight hlite = new LinkHighlight(linkId(), taggedFlavor) |
| 159 | .setLabel(generateLabel(type)); |
| 160 | if (isOptical) { |
| 161 | hlite.addMod(LinkHighlight.MOD_OPTICAL); |
| 162 | } |
| 163 | if (antMarch) { |
| 164 | hlite.addMod(LinkHighlight.MOD_ANIMATED); |
| 165 | } |
| 166 | return hlite; |
| 167 | } |
| 168 | |
| 169 | // Generates a string representation of the load, to be used as a label |
| 170 | private String generateLabel(StatsType type) { |
| 171 | switch (type) { |
| 172 | case FLOW_COUNT: |
| 173 | return TopoUtils.formatFlows(flows); |
| 174 | |
| 175 | case FLOW_STATS: |
| 176 | return TopoUtils.formatBytes(bytes); |
| 177 | |
| 178 | case PORT_STATS: |
| 179 | return TopoUtils.formatBitRate(rate); |
| 180 | |
| 181 | case TAGGED: |
| 182 | return hasTraffic ? TopoUtils.formatBytes(bytes) : EMPTY; |
| 183 | |
| 184 | default: |
| 185 | return QUE; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns true if this link has been deemed to have enough traffic |
| 191 | * to register on the topology view in the web UI. |
| 192 | * |
| 193 | * @return true if this link has displayable traffic |
| 194 | */ |
| 195 | public boolean hasTraffic() { |
| 196 | return hasTraffic; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Designates type of traffic statistics to report on a highlighted link. |
| 201 | */ |
| 202 | public enum StatsType { |
| 203 | /** |
| 204 | * Number of flows. |
| 205 | */ |
| 206 | FLOW_COUNT, |
| 207 | |
| 208 | /** |
| 209 | * Number of bytes. |
| 210 | */ |
| 211 | FLOW_STATS, |
| 212 | |
| 213 | /** |
| 214 | * Number of bits per second. |
| 215 | */ |
| 216 | PORT_STATS, |
| 217 | |
| 218 | /** |
| 219 | * Custom tagged information. |
| 220 | */ |
| 221 | TAGGED |
| 222 | } |
| 223 | } |