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