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