Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -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 | |
| 25 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.NO_HIGHLIGHT; |
| 26 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.PRIMARY_HIGHLIGHT; |
| 27 | import static org.onosproject.ui.topo.LinkHighlight.Flavor.SECONDARY_HIGHLIGHT; |
| 28 | |
| 29 | /** |
| 30 | * Representation of a link and its inverse, and any associated traffic data. |
| 31 | * This class understands how to generate {@link LinkHighlight}s for sending |
| 32 | * back to the topology view. |
| 33 | */ |
| 34 | public class BiLink { |
| 35 | |
| 36 | private static final String EMPTY = ""; |
| 37 | |
| 38 | private final LinkKey key; |
| 39 | private final Link one; |
| 40 | private Link two; |
| 41 | |
| 42 | private boolean hasTraffic = false; |
| 43 | private long bytes = 0; |
| 44 | private long rate = 0; |
| 45 | private long flows = 0; |
| 46 | private boolean isOptical = false; |
| 47 | private LinkHighlight.Flavor taggedFlavor = NO_HIGHLIGHT; |
| 48 | private boolean antMarch = false; |
| 49 | |
| 50 | /** |
| 51 | * Constructs a bilink for the given key and initial link. |
| 52 | * |
| 53 | * @param key canonical key for this bilink |
| 54 | * @param link first link |
| 55 | */ |
| 56 | public BiLink(LinkKey key, Link link) { |
| 57 | this.key = key; |
| 58 | this.one = link; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets the second link for this bilink. |
| 63 | * |
| 64 | * @param link second link |
| 65 | */ |
| 66 | public void setOther(Link link) { |
| 67 | this.two = link; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets the optical flag to the given value. |
| 72 | * |
| 73 | * @param b true if an optical link |
| 74 | */ |
| 75 | public void setOptical(boolean b) { |
| 76 | isOptical = b; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets the ant march flag to the given value. |
| 81 | * |
| 82 | * @param b true if marching ants required |
| 83 | */ |
| 84 | public void setAntMarch(boolean b) { |
| 85 | antMarch = b; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tags this bilink with a link flavor to be used in visual rendering. |
| 90 | * |
| 91 | * @param flavor the flavor to tag |
| 92 | */ |
| 93 | public void tagFlavor(LinkHighlight.Flavor flavor) { |
| 94 | this.taggedFlavor = flavor; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Adds load statistics, marks the bilink as having traffic. |
| 99 | * |
| 100 | * @param load load to add |
| 101 | */ |
| 102 | public void addLoad(Load load) { |
| 103 | addLoad(load, 0); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Adds load statistics, marks the bilink as having traffic, if the |
| 108 | * load rate is greater than the given threshold. |
| 109 | * |
| 110 | * @param load load to add |
| 111 | * @param threshold threshold to register traffic |
| 112 | */ |
| 113 | public void addLoad(Load load, double threshold) { |
| 114 | if (load != null) { |
| 115 | this.hasTraffic = hasTraffic || load.rate() > threshold; |
| 116 | this.bytes += load.latest(); |
| 117 | this.rate += load.rate(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Adds the given count of flows to this bilink. |
| 123 | * |
| 124 | * @param count count of flows |
| 125 | */ |
| 126 | public void addFlows(int count) { |
| 127 | this.flows += count; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Generates a link highlight entity, based on state of this bilink. |
| 132 | * |
| 133 | * @param type the type of statistics to use to interpret the data |
| 134 | * @return link highlight data for this bilink |
| 135 | */ |
| 136 | public LinkHighlight generateHighlight(LinkStatsType type) { |
| 137 | switch (type) { |
| 138 | case FLOW_COUNT: |
| 139 | return highlightForFlowCount(type); |
| 140 | |
| 141 | case FLOW_STATS: |
| 142 | case PORT_STATS: |
| 143 | return highlightForStats(type); |
| 144 | |
| 145 | case TAGGED: |
| 146 | return highlightForTagging(type); |
| 147 | |
| 148 | default: |
| 149 | throw new IllegalStateException("unexpected case: " + type); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private LinkHighlight highlightForStats(LinkStatsType type) { |
| 154 | return new LinkHighlight(linkId(), SECONDARY_HIGHLIGHT) |
| 155 | .setLabel(generateLabel(type)); |
| 156 | } |
| 157 | |
| 158 | private LinkHighlight highlightForFlowCount(LinkStatsType type) { |
| 159 | LinkHighlight.Flavor flavor = flows() > 0 ? |
| 160 | PRIMARY_HIGHLIGHT : SECONDARY_HIGHLIGHT; |
| 161 | return new LinkHighlight(linkId(), flavor) |
| 162 | .setLabel(generateLabel(type)); |
| 163 | } |
| 164 | |
| 165 | private LinkHighlight highlightForTagging(LinkStatsType type) { |
| 166 | LinkHighlight hlite = new LinkHighlight(linkId(), flavor()) |
| 167 | .setLabel(generateLabel(type)); |
| 168 | if (isOptical()) { |
| 169 | hlite.addMod(LinkHighlight.MOD_OPTICAL); |
| 170 | } |
| 171 | if (isAntMarch()) { |
| 172 | hlite.addMod(LinkHighlight.MOD_ANIMATED); |
| 173 | } |
| 174 | return hlite; |
| 175 | } |
| 176 | |
| 177 | // Generates a link identifier in the form that the Topology View on the |
| 178 | private String linkId() { |
| 179 | return TopoUtils.compactLinkString(one); |
| 180 | } |
| 181 | |
| 182 | // Generates a string representation of the load, to be used as a label |
| 183 | private String generateLabel(LinkStatsType type) { |
| 184 | switch (type) { |
| 185 | case FLOW_COUNT: |
| 186 | return TopoUtils.formatFlows(flows()); |
| 187 | |
| 188 | case FLOW_STATS: |
| 189 | return TopoUtils.formatBytes(bytes()); |
| 190 | |
| 191 | case PORT_STATS: |
| 192 | return TopoUtils.formatBitRate(rate()); |
| 193 | |
| 194 | case TAGGED: |
| 195 | return hasTraffic() ? TopoUtils.formatBytes(bytes()) : EMPTY; |
| 196 | |
| 197 | default: |
| 198 | return "?"; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // === ---------------------------------------------------------------- |
| 203 | // accessors |
| 204 | |
| 205 | public LinkKey key() { |
| 206 | return key; |
| 207 | } |
| 208 | |
| 209 | public Link one() { |
| 210 | return one; |
| 211 | } |
| 212 | |
| 213 | public Link two() { |
| 214 | return two; |
| 215 | } |
| 216 | |
| 217 | public boolean hasTraffic() { |
| 218 | return hasTraffic; |
| 219 | } |
| 220 | |
| 221 | public boolean isOptical() { |
| 222 | return isOptical; |
| 223 | } |
| 224 | |
| 225 | public boolean isAntMarch() { |
| 226 | return antMarch; |
| 227 | } |
| 228 | |
| 229 | public LinkHighlight.Flavor flavor() { |
| 230 | return taggedFlavor; |
| 231 | } |
| 232 | |
| 233 | public long bytes() { |
| 234 | return bytes; |
| 235 | } |
| 236 | |
| 237 | public long rate() { |
| 238 | return rate; |
| 239 | } |
| 240 | |
| 241 | public long flows() { |
| 242 | return flows; |
| 243 | } |
| 244 | } |