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