Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 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; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 21 | import com.google.common.collect.Lists; |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 22 | import org.onosproject.incubator.net.PortStatisticsService.MetricType; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ElementId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Host; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 27 | import org.onosproject.net.HostId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Link; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 29 | import org.onosproject.net.PortNumber; |
Marc De Leenheer | 20913c6 | 2017-04-12 14:45:15 -0700 | [diff] [blame] | 30 | import org.onosproject.net.device.DeviceService; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.FlowEntry; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.flow.instructions.Instruction; |
| 34 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Thomas Vachuska | da0665b | 2016-03-02 19:06:17 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.FlowObjectiveIntent; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intent.FlowRuleIntent; |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.HostToHostIntent; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 40 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 41 | import org.onosproject.net.intent.OpticalPathIntent; |
| 42 | import org.onosproject.net.intent.PathIntent; |
Marc De Leenheer | 20913c6 | 2017-04-12 14:45:15 -0700 | [diff] [blame] | 43 | import org.onosproject.net.link.LinkService; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 44 | import org.onosproject.net.statistic.Load; |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.impl.topo.util.IntentSelection; |
| 46 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
| 47 | import org.onosproject.ui.impl.topo.util.TopoIntentFilter; |
| 48 | import org.onosproject.ui.impl.topo.util.TrafficLink; |
| 49 | import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType; |
| 50 | import org.onosproject.ui.impl.topo.util.TrafficLinkMap; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 51 | import org.onosproject.ui.topo.DeviceHighlight; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 52 | import org.onosproject.ui.topo.Highlights; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 53 | import org.onosproject.ui.topo.Highlights.Amount; |
| 54 | import org.onosproject.ui.topo.HostHighlight; |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 55 | import org.onosproject.ui.topo.LinkHighlight.Flavor; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 56 | import org.onosproject.ui.topo.NodeHighlight; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.topo.NodeSelection; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
| 61 | import java.util.ArrayList; |
| 62 | import java.util.Collection; |
| 63 | import java.util.Collections; |
| 64 | import java.util.HashMap; |
| 65 | import java.util.HashSet; |
| 66 | import java.util.List; |
| 67 | import java.util.Map; |
| 68 | import java.util.Set; |
Marc De Leenheer | 20913c6 | 2017-04-12 14:45:15 -0700 | [diff] [blame] | 69 | import java.util.stream.Collectors; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 71 | import static org.onosproject.incubator.net.PortStatisticsService.MetricType.BYTES; |
| 72 | import static org.onosproject.incubator.net.PortStatisticsService.MetricType.PACKETS; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 73 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 74 | import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.RELATED_INTENTS; |
| 75 | import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.SELECTED_INTENT; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Encapsulates the behavior of monitoring specific traffic patterns. |
| 79 | */ |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 80 | public class TrafficMonitor extends TrafficMonitorBase { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 81 | |
| 82 | private static final Logger log = |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 83 | LoggerFactory.getLogger(TrafficMonitor.class); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 84 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 85 | private final TopologyViewMessageHandler msgHandler; |
| 86 | private final TopoIntentFilter intentFilter; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 87 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 88 | private NodeSelection selectedNodes = null; |
| 89 | private IntentSelection selectedIntents = null; |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Constructs a traffic monitor. |
| 94 | * |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 95 | * @param trafficPeriod traffic task period in ms |
| 96 | * @param servicesBundle bundle of services |
| 97 | * @param msgHandler our message handler |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 98 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 99 | public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle, |
| 100 | TopologyViewMessageHandler msgHandler) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 101 | super(trafficPeriod, servicesBundle); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 102 | this.msgHandler = msgHandler; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 103 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 104 | intentFilter = new TopoIntentFilter(servicesBundle); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // ======================================================================= |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 108 | // === API === |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 109 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 110 | // monitor(Mode) is implemented in the super class |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 111 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Monitor for traffic data to be sent back to the web client, under |
| 114 | * the given mode, using the given selection of devices and hosts. |
| 115 | * In the case of "device link flows", this causes a background traffic |
| 116 | * task to be scheduled to repeatedly compute and transmit the appropriate |
| 117 | * traffic data to the client. In the case of "related intents", no |
| 118 | * repeating task is scheduled. |
| 119 | * <p> |
| 120 | * The monitoring mode is expected to be one of: |
| 121 | * <ul> |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 122 | * <li>DEV_LINK_FLOWS</li> |
| 123 | * <li>RELATED_INTENTS</li> |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 124 | * </ul> |
| 125 | * |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 126 | * @param mode monitoring mode |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 127 | * @param nodeSelection how to select a node |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 128 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 129 | public synchronized void monitor(Mode mode, NodeSelection nodeSelection) { |
| 130 | log.debug("monitor: {} -- {}", mode, nodeSelection); |
| 131 | this.mode = mode; |
| 132 | this.selectedNodes = nodeSelection; |
| 133 | |
| 134 | switch (mode) { |
| 135 | case DEV_LINK_FLOWS: |
| 136 | // only care about devices (not hosts) |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 137 | if (selectedNodes.devicesWithHover().isEmpty()) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 138 | clearAll(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 139 | } else { |
| 140 | scheduleTask(); |
| 141 | sendDeviceLinkFlows(); |
| 142 | } |
| 143 | break; |
| 144 | |
| 145 | case RELATED_INTENTS: |
| 146 | if (selectedNodes.none()) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 147 | clearAll(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 148 | } else { |
| 149 | selectedIntents = new IntentSelection(selectedNodes, intentFilter); |
| 150 | if (selectedIntents.none()) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 151 | clearAll(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 152 | } else { |
| 153 | sendSelectedIntents(); |
| 154 | } |
| 155 | } |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection); |
| 160 | clearAll(); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 165 | // TODO: move this out to the "h2h/multi-intent app" |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 166 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Monitor for traffic data to be sent back to the web client, for the |
| 169 | * given intent. |
| 170 | * |
| 171 | * @param intent the intent to monitor |
| 172 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 173 | public synchronized void monitor(Intent intent) { |
| 174 | log.debug("monitor intent: {}", intent.id()); |
| 175 | selectedNodes = null; |
| 176 | selectedIntents = new IntentSelection(intent); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 177 | mode = SELECTED_INTENT; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 178 | scheduleTask(); |
| 179 | sendSelectedIntentTraffic(); |
| 180 | } |
| 181 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Selects the next intent in the select group (if there is one), |
| 184 | * and sends highlighting data back to the web client to display |
| 185 | * which path is selected. |
| 186 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 187 | public synchronized void selectNextIntent() { |
| 188 | if (selectedIntents != null) { |
| 189 | selectedIntents.next(); |
| 190 | sendSelectedIntents(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 191 | if (mode == SELECTED_INTENT) { |
| 192 | mode = RELATED_INTENTS; |
| 193 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Selects the previous intent in the select group (if there is one), |
| 199 | * and sends highlighting data back to the web client to display |
| 200 | * which path is selected. |
| 201 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 202 | public synchronized void selectPreviousIntent() { |
| 203 | if (selectedIntents != null) { |
| 204 | selectedIntents.prev(); |
| 205 | sendSelectedIntents(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 206 | if (mode == SELECTED_INTENT) { |
| 207 | mode = RELATED_INTENTS; |
| 208 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Resends selected intent traffic data. This is called, for example, |
| 214 | * when the system detects an intent update happened. |
| 215 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 216 | public synchronized void pokeIntent() { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 217 | if (mode == SELECTED_INTENT) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 218 | sendSelectedIntentTraffic(); |
| 219 | } |
| 220 | } |
| 221 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 222 | // ======================================================================= |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 223 | // === Abstract method implementations === |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 224 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 225 | @Override |
| 226 | protected void sendAllFlowTraffic() { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 227 | log.debug("sendAllFlowTraffic"); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 228 | msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 231 | @Override |
| 232 | protected void sendAllPortTrafficBits() { |
| 233 | log.debug("sendAllPortTrafficBits"); |
| 234 | msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 237 | @Override |
| 238 | protected void sendAllPortTrafficPackets() { |
| 239 | log.debug("sendAllPortTrafficPackets"); |
| 240 | msgHandler.sendHighlights(trafficSummary(StatsType.PORT_PACKET_STATS)); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | protected void sendDeviceLinkFlows() { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 245 | log.debug("sendDeviceLinkFlows: {}", selectedNodes); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 246 | msgHandler.sendHighlights(deviceLinkFlows()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 249 | @Override |
| 250 | protected void sendSelectedIntentTraffic() { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 251 | log.debug("sendSelectedIntentTraffic: {}", selectedIntents); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 252 | msgHandler.sendHighlights(intentTraffic()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 255 | @Override |
| 256 | protected void sendClearHighlights() { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 257 | log.debug("sendClearHighlights"); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 258 | msgHandler.sendHighlights(new Highlights()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 261 | @Override |
| 262 | protected void clearSelection() { |
| 263 | selectedNodes = null; |
| 264 | selectedIntents = null; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | private void sendSelectedIntents() { |
| 269 | log.debug("sendSelectedIntents: {}", selectedIntents); |
| 270 | msgHandler.sendHighlights(intentGroup()); |
| 271 | } |
| 272 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 273 | // ======================================================================= |
| 274 | // === Generate messages in JSON object node format |
| 275 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 276 | private Highlights trafficSummary(StatsType type) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 277 | Highlights highlights = new Highlights(); |
| 278 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 279 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 280 | compileLinks(linkMap); |
| 281 | addEdgeLinks(linkMap); |
| 282 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 283 | for (TrafficLink tlink : linkMap.biLinks()) { |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 284 | if (type == StatsType.FLOW_STATS) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 285 | attachFlowLoad(tlink); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 286 | } else if (type == StatsType.PORT_STATS) { |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 287 | attachPortLoad(tlink, BYTES); |
| 288 | } else if (type == StatsType.PORT_PACKET_STATS) { |
| 289 | attachPortLoad(tlink, PACKETS); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // we only want to report on links deemed to have traffic |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 293 | if (tlink.hasTraffic()) { |
| 294 | highlights.add(tlink.highlight(type)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | return highlights; |
| 298 | } |
| 299 | |
| 300 | // create highlights for links, showing flows for selected devices. |
| 301 | private Highlights deviceLinkFlows() { |
| 302 | Highlights highlights = new Highlights(); |
| 303 | |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 304 | if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 305 | // capture flow counts on bilinks |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 306 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 307 | |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 308 | for (Device device : selectedNodes.devicesWithHover()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 309 | Map<Link, Integer> counts = getLinkFlowCounts(device.id()); |
| 310 | for (Link link : counts.keySet()) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 311 | TrafficLink tlink = linkMap.add(link); |
| 312 | tlink.addFlows(counts.get(link)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | // now report on our collated links |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 317 | for (TrafficLink tlink : linkMap.biLinks()) { |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 318 | highlights.add(tlink.highlight(StatsType.FLOW_COUNT)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | } |
| 322 | return highlights; |
| 323 | } |
| 324 | |
| 325 | private Highlights intentGroup() { |
| 326 | Highlights highlights = new Highlights(); |
| 327 | |
| 328 | if (selectedIntents != null && !selectedIntents.none()) { |
| 329 | // If 'all' intents are selected, they will all have primary |
| 330 | // highlighting; otherwise, the specifically selected intent will |
| 331 | // have primary highlighting, and the remainder will have secondary |
| 332 | // highlighting. |
| 333 | Set<Intent> primary; |
| 334 | Set<Intent> secondary; |
| 335 | int count = selectedIntents.size(); |
| 336 | |
| 337 | Set<Intent> allBut = new HashSet<>(selectedIntents.intents()); |
| 338 | Intent current; |
| 339 | |
| 340 | if (selectedIntents.all()) { |
| 341 | primary = allBut; |
| 342 | secondary = Collections.emptySet(); |
| 343 | log.debug("Highlight all intents ({})", count); |
| 344 | } else { |
| 345 | current = selectedIntents.current(); |
| 346 | primary = new HashSet<>(); |
| 347 | primary.add(current); |
| 348 | allBut.remove(current); |
| 349 | secondary = allBut; |
| 350 | log.debug("Highlight intent: {} ([{}] of {})", |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 351 | current.id(), selectedIntents.index(), count); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 352 | } |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 353 | |
| 354 | highlightIntentLinks(highlights, primary, secondary); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 355 | } |
| 356 | return highlights; |
| 357 | } |
| 358 | |
| 359 | private Highlights intentTraffic() { |
| 360 | Highlights highlights = new Highlights(); |
| 361 | |
| 362 | if (selectedIntents != null && selectedIntents.single()) { |
| 363 | Intent current = selectedIntents.current(); |
| 364 | Set<Intent> primary = new HashSet<>(); |
| 365 | primary.add(current); |
| 366 | log.debug("Highlight traffic for intent: {} ([{}] of {})", |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 367 | current.id(), selectedIntents.index(), selectedIntents.size()); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 368 | |
| 369 | highlightIntentLinksWithTraffic(highlights, primary); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 370 | highlights.subdueAllElse(Amount.MINIMALLY); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 371 | } |
| 372 | return highlights; |
| 373 | } |
| 374 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 375 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 376 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 377 | private void compileLinks(TrafficLinkMap linkMap) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 378 | services.link().getLinks().forEach(linkMap::add); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 381 | private void addEdgeLinks(TrafficLinkMap linkMap) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 382 | services.host().getHosts().forEach(host -> { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 383 | linkMap.add(createEdgeLink(host, true)); |
| 384 | linkMap.add(createEdgeLink(host, false)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 385 | }); |
| 386 | } |
| 387 | |
| 388 | private Load getLinkFlowLoad(Link link) { |
| 389 | if (link != null && link.src().elementId() instanceof DeviceId) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 390 | return services.flowStats().load(link); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 391 | } |
| 392 | return null; |
| 393 | } |
| 394 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 395 | private void attachFlowLoad(TrafficLink link) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 396 | link.addLoad(getLinkFlowLoad(link.one())); |
| 397 | link.addLoad(getLinkFlowLoad(link.two())); |
| 398 | } |
| 399 | |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 400 | private void attachPortLoad(TrafficLink link, MetricType metricType) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 401 | // For bi-directional traffic links, use |
| 402 | // the max link rate of either direction |
| 403 | // (we choose 'one' since we know that is never null) |
| 404 | Link one = link.one(); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 405 | Load egressSrc = services.portStats().load(one.src(), metricType); |
| 406 | Load egressDst = services.portStats().load(one.dst(), metricType); |
Thomas Vachuska | 0932ac5 | 2017-03-30 13:28:49 -0700 | [diff] [blame] | 407 | link.addLoad(maxLoad(egressSrc, egressDst), metricType == BYTES ? BPS_THRESHOLD : 0); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | private Load maxLoad(Load a, Load b) { |
| 411 | if (a == null) { |
| 412 | return b; |
| 413 | } |
| 414 | if (b == null) { |
| 415 | return a; |
| 416 | } |
| 417 | return a.rate() > b.rate() ? a : b; |
| 418 | } |
| 419 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 420 | // Counts all flow entries that egress on the links of the given device. |
| 421 | private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) { |
| 422 | // get the flows for the device |
| 423 | List<FlowEntry> entries = new ArrayList<>(); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 424 | for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 425 | entries.add(flowEntry); |
| 426 | } |
| 427 | |
| 428 | // get egress links from device, and include edge links |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 429 | Set<Link> links = new HashSet<>(services.link() |
| 430 | .getDeviceEgressLinks(deviceId)); |
| 431 | Set<Host> hosts = services.host().getConnectedHosts(deviceId); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 432 | if (hosts != null) { |
| 433 | for (Host host : hosts) { |
| 434 | links.add(createEdgeLink(host, false)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // compile flow counts per link |
| 439 | Map<Link, Integer> counts = new HashMap<>(); |
| 440 | for (Link link : links) { |
| 441 | counts.put(link, getEgressFlows(link, entries)); |
| 442 | } |
| 443 | return counts; |
| 444 | } |
| 445 | |
| 446 | // Counts all entries that egress on the link source port. |
| 447 | private int getEgressFlows(Link link, List<FlowEntry> entries) { |
| 448 | int count = 0; |
| 449 | PortNumber out = link.src().port(); |
| 450 | for (FlowEntry entry : entries) { |
| 451 | TrafficTreatment treatment = entry.treatment(); |
| 452 | for (Instruction instruction : treatment.allInstructions()) { |
| 453 | if (instruction.type() == Instruction.Type.OUTPUT && |
| 454 | ((OutputInstruction) instruction).port().equals(out)) { |
| 455 | count++; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | return count; |
| 460 | } |
| 461 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 462 | private void highlightIntentLinks(Highlights highlights, |
| 463 | Set<Intent> primary, Set<Intent> secondary) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 464 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 465 | // NOTE: highlight secondary first, then primary, so that links shared |
| 466 | // by intents are colored correctly ("last man wins") |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 467 | createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false); |
| 468 | createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 469 | colorLinks(highlights, linkMap); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 472 | private void highlightIntentLinksWithTraffic(Highlights highlights, |
| 473 | Set<Intent> primary) { |
| 474 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 475 | createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 476 | colorLinks(highlights, linkMap); |
| 477 | } |
| 478 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 479 | private void createTrafficLinks(Highlights highlights, |
| 480 | TrafficLinkMap linkMap, Set<Intent> intents, |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 481 | Flavor flavor, boolean showTraffic) { |
| 482 | for (Intent intent : intents) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 483 | List<Intent> installables = services.intent() |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 484 | .getInstallableIntents(intent.key()); |
| 485 | Iterable<Link> links = null; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 486 | if (installables != null) { |
| 487 | for (Intent installable : installables) { |
| 488 | |
| 489 | if (installable instanceof PathIntent) { |
| 490 | links = ((PathIntent) installable).path().links(); |
| 491 | } else if (installable instanceof FlowRuleIntent) { |
Marc De Leenheer | 20913c6 | 2017-04-12 14:45:15 -0700 | [diff] [blame] | 492 | Collection<Link> l = new ArrayList<>(); |
| 493 | l.addAll(linkResources(installable)); |
| 494 | // Add cross connect links |
| 495 | if (intent instanceof OpticalConnectivityIntent) { |
| 496 | OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 497 | LinkService linkService = services.link(); |
| 498 | DeviceService deviceService = services.device(); |
Marc De Leenheer | 20913c6 | 2017-04-12 14:45:15 -0700 | [diff] [blame] | 499 | l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream() |
| 500 | .filter(i -> |
| 501 | deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH) |
| 502 | .collect(Collectors.toList())); |
| 503 | l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream() |
| 504 | .filter(e -> |
| 505 | deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH) |
| 506 | .collect(Collectors.toList())); |
| 507 | } |
| 508 | links = l; |
Thomas Vachuska | da0665b | 2016-03-02 19:06:17 -0800 | [diff] [blame] | 509 | } else if (installable instanceof FlowObjectiveIntent) { |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 510 | links = linkResources(installable); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 511 | } else if (installable instanceof LinkCollectionIntent) { |
| 512 | links = ((LinkCollectionIntent) installable).links(); |
| 513 | } else if (installable instanceof OpticalPathIntent) { |
| 514 | links = ((OpticalPathIntent) installable).path().links(); |
| 515 | } |
| 516 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 517 | if (links == null) { |
| 518 | links = Lists.newArrayList(); |
| 519 | } |
| 520 | |
| 521 | links = addEdgeLinksIfNeeded(intent, Lists.newArrayList(links)); |
| 522 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 523 | boolean isOptical = intent instanceof OpticalConnectivityIntent; |
| 524 | processLinks(linkMap, links, flavor, isOptical, showTraffic); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 525 | updateHighlights(highlights, links); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 531 | private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent, |
| 532 | Collection<Link> links) { |
| 533 | if (parentIntent instanceof HostToHostIntent) { |
| 534 | links = new HashSet<>(links); |
| 535 | HostToHostIntent h2h = (HostToHostIntent) parentIntent; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame^] | 536 | Host h1 = services.host().getHost(h2h.one()); |
| 537 | Host h2 = services.host().getHost(h2h.two()); |
Simon Hunt | 5328f79 | 2017-01-11 17:43:31 -0800 | [diff] [blame] | 538 | links.add(createEdgeLink(h1, true)); |
| 539 | links.add(createEdgeLink(h2, true)); |
| 540 | } |
| 541 | return links; |
| 542 | } |
| 543 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 544 | private void updateHighlights(Highlights highlights, Iterable<Link> links) { |
| 545 | for (Link link : links) { |
| 546 | ensureNodePresent(highlights, link.src().elementId()); |
| 547 | ensureNodePresent(highlights, link.dst().elementId()); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | private void ensureNodePresent(Highlights highlights, ElementId eid) { |
| 552 | String id = eid.toString(); |
| 553 | NodeHighlight nh = highlights.getNode(id); |
| 554 | if (nh == null) { |
| 555 | if (eid instanceof DeviceId) { |
| 556 | nh = new DeviceHighlight(id); |
| 557 | highlights.add((DeviceHighlight) nh); |
| 558 | } else if (eid instanceof HostId) { |
| 559 | nh = new HostHighlight(id); |
| 560 | highlights.add((HostHighlight) nh); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 565 | // Extracts links from the specified flow rule intent resources |
| 566 | private Collection<Link> linkResources(Intent installable) { |
| 567 | ImmutableList.Builder<Link> builder = ImmutableList.builder(); |
| 568 | installable.resources().stream().filter(r -> r instanceof Link) |
| 569 | .forEach(r -> builder.add((Link) r)); |
| 570 | return builder.build(); |
| 571 | } |
| 572 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 573 | private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links, |
| 574 | Flavor flavor, boolean isOptical, |
| 575 | boolean showTraffic) { |
| 576 | if (links != null) { |
| 577 | for (Link link : links) { |
| 578 | TrafficLink tlink = linkMap.add(link); |
| 579 | tlink.tagFlavor(flavor); |
| 580 | tlink.optical(isOptical); |
| 581 | if (showTraffic) { |
| 582 | tlink.addLoad(getLinkFlowLoad(link)); |
| 583 | tlink.antMarch(true); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) { |
| 590 | for (TrafficLink tlink : linkMap.biLinks()) { |
| 591 | highlights.add(tlink.highlight(StatsType.TAGGED)); |
| 592 | } |
| 593 | } |
| 594 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 595 | } |