Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | package org.onlab.onos.gui; |
| 17 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 21 | import org.eclipse.jetty.websocket.WebSocket; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 22 | import org.onlab.onos.cluster.ClusterEvent; |
| 23 | import org.onlab.onos.cluster.ClusterEventListener; |
| 24 | import org.onlab.onos.cluster.ControllerNode; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 25 | import org.onlab.onos.core.ApplicationId; |
| 26 | import org.onlab.onos.core.CoreService; |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 27 | import org.onlab.onos.event.AbstractEventAccumulator; |
| 28 | import org.onlab.onos.event.Event; |
| 29 | import org.onlab.onos.event.EventAccumulator; |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame^] | 30 | import org.onlab.onos.mastership.MastershipAdminService; |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 31 | import org.onlab.onos.mastership.MastershipEvent; |
| 32 | import org.onlab.onos.mastership.MastershipListener; |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 33 | import org.onlab.onos.net.ConnectPoint; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 34 | import org.onlab.onos.net.Device; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 35 | import org.onlab.onos.net.Host; |
| 36 | import org.onlab.onos.net.HostId; |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 37 | import org.onlab.onos.net.HostLocation; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 38 | import org.onlab.onos.net.Link; |
| 39 | import org.onlab.onos.net.device.DeviceEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 40 | import org.onlab.onos.net.device.DeviceListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 42 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 43 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 44 | import org.onlab.onos.net.flow.FlowRuleListener; |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 45 | import org.onlab.onos.net.flow.TrafficSelector; |
| 46 | import org.onlab.onos.net.flow.TrafficTreatment; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 47 | import org.onlab.onos.net.host.HostEvent; |
| 48 | import org.onlab.onos.net.host.HostListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 49 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 50 | import org.onlab.onos.net.intent.Intent; |
| 51 | import org.onlab.onos.net.intent.IntentEvent; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 52 | import org.onlab.onos.net.intent.IntentListener; |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 53 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 54 | import org.onlab.onos.net.link.LinkEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 55 | import org.onlab.onos.net.link.LinkListener; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 56 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 57 | import org.onlab.packet.Ethernet; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 58 | |
| 59 | import java.io.IOException; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 60 | import java.util.ArrayList; |
| 61 | import java.util.Collections; |
| 62 | import java.util.Comparator; |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 63 | import java.util.HashSet; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 64 | import java.util.List; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 65 | import java.util.Set; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 66 | import java.util.Timer; |
| 67 | import java.util.TimerTask; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 68 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 69 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 70 | import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 71 | import static org.onlab.onos.net.DeviceId.deviceId; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 72 | import static org.onlab.onos.net.HostId.hostId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 73 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 74 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_UPDATED; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 75 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 76 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 77 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Web socket capable of interacting with the GUI topology view. |
| 80 | */ |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 81 | public class TopologyViewWebSocket |
| 82 | extends TopologyViewMessages |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 83 | implements WebSocket.OnTextMessage, WebSocket.OnControl { |
| 84 | |
| 85 | private static final long MAX_AGE_MS = 15000; |
| 86 | |
| 87 | private static final byte PING = 0x9; |
| 88 | private static final byte PONG = 0xA; |
| 89 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 90 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 91 | private static final String APP_ID = "org.onlab.onos.gui"; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 92 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 93 | private static final long TRAFFIC_FREQUENCY_SEC = 2000; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 94 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 95 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
| 96 | new Comparator<ControllerNode>() { |
| 97 | @Override |
| 98 | public int compare(ControllerNode o1, ControllerNode o2) { |
| 99 | return o1.id().toString().compareTo(o2.id().toString()); |
| 100 | } |
| 101 | }; |
| 102 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 103 | |
| 104 | private final Timer timer = new Timer("topology-view"); |
| 105 | |
| 106 | private static final int MAX_EVENTS = 500; |
| 107 | private static final int MAX_BATCH_MS = 1000; |
| 108 | private static final int MAX_IDLE_MS = 500; |
| 109 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 110 | private final ApplicationId appId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 111 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 112 | private Connection connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 113 | private FrameConnection control; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 114 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 115 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 116 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 117 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 118 | private final LinkListener linkListener = new InternalLinkListener(); |
| 119 | private final HostListener hostListener = new InternalHostListener(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 120 | private final IntentListener intentListener = new InternalIntentListener(); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 121 | private final FlowRuleListener flowListener = new InternalFlowListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 122 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 123 | private final EventAccumulator eventAccummulator = new InternalEventAccummulator(); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 124 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 125 | private boolean summaryEnabled = true; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 126 | private TimerTask trafficTask; |
| 127 | private ObjectNode trafficEvent; |
| 128 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 129 | private long lastActive = System.currentTimeMillis(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 130 | private boolean listenersRemoved = false; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 131 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 132 | private TopologyViewIntentFilter intentFilter; |
| 133 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Creates a new web-socket for serving data to GUI topology view. |
| 136 | * |
| 137 | * @param directory service directory |
| 138 | */ |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 139 | public TopologyViewWebSocket(ServiceDirectory directory) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 140 | super(directory); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 141 | intentFilter = new TopologyViewIntentFilter(intentService, deviceService, |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 142 | hostService, linkService); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 143 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Issues a close on the connection. |
| 148 | */ |
| 149 | synchronized void close() { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 150 | removeListeners(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 151 | if (connection.isOpen()) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 152 | connection.close(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Indicates if this connection is idle. |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 158 | * |
| 159 | * @return true if idle or closed |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 160 | */ |
| 161 | synchronized boolean isIdle() { |
| 162 | boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS; |
| 163 | if (idle || !connection.isOpen()) { |
| 164 | return true; |
| 165 | } |
| 166 | try { |
| 167 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 168 | } catch (IOException e) { |
| 169 | log.warn("Unable to send ping message due to: ", e); |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 174 | @Override |
| 175 | public void onOpen(Connection connection) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 176 | log.info("GUI client connected"); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 177 | this.connection = connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 178 | this.control = (FrameConnection) connection; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 179 | addListeners(); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 180 | |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 181 | sendAllInstances(null); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 182 | sendAllDevices(); |
| 183 | sendAllLinks(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 184 | sendAllHosts(); |
| 185 | } |
| 186 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 187 | @Override |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 188 | public synchronized void onClose(int closeCode, String message) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 189 | removeListeners(); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 190 | timer.cancel(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 191 | log.info("GUI client disconnected"); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 196 | lastActive = System.currentTimeMillis(); |
| 197 | return true; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onMessage(String data) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 202 | lastActive = System.currentTimeMillis(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 203 | try { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 204 | processMessage((ObjectNode) mapper.reader().readTree(data)); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 205 | } catch (Exception e) { |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 206 | log.warn("Unable to parse GUI request {} due to {}", data, e); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 207 | log.warn("Boom!!!!", e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 208 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 211 | // Processes the specified event. |
| 212 | private void processMessage(ObjectNode event) { |
| 213 | String type = string(event, "event", "unknown"); |
| 214 | if (type.equals("requestDetails")) { |
| 215 | requestDetails(event); |
| 216 | } else if (type.equals("updateMeta")) { |
| 217 | updateMetaUi(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 218 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 219 | } else if (type.equals("addHostIntent")) { |
| 220 | createHostIntent(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 221 | } else if (type.equals("addMultiSourceIntent")) { |
| 222 | createMultiSourceIntent(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 223 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 224 | } else if (type.equals("requestTraffic")) { |
| 225 | requestTraffic(event); |
| 226 | } else if (type.equals("requestAllTraffic")) { |
| 227 | requestAllTraffic(event); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 228 | } else if (type.equals("requestDeviceLinkFlows")) { |
| 229 | requestDeviceLinkFlows(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 230 | } else if (type.equals("cancelTraffic")) { |
| 231 | cancelTraffic(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 232 | |
| 233 | } else if (type.equals("requestSummary")) { |
| 234 | requestSummary(event); |
| 235 | } else if (type.equals("cancelSummary")) { |
| 236 | cancelSummary(event); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame^] | 237 | |
| 238 | } else if (type.equals("equalizeMasters")) { |
| 239 | equalizeMasters(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 243 | // Sends the specified data to the client. |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 244 | private synchronized void sendMessage(ObjectNode data) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 245 | try { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 246 | if (connection.isOpen()) { |
| 247 | connection.sendMessage(data.toString()); |
| 248 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 249 | } catch (IOException e) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 250 | log.warn("Unable to send message {} to GUI due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 251 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 254 | // Sends all controller nodes to the client as node-added messages. |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 255 | private void sendAllInstances(String messageType) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 256 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 257 | Collections.sort(nodes, NODE_COMPARATOR); |
| 258 | for (ControllerNode node : nodes) { |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 259 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 260 | messageType)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 261 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 264 | // Sends all devices to the client as device-added messages. |
| 265 | private void sendAllDevices() { |
| 266 | for (Device device : deviceService.getDevices()) { |
| 267 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 268 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 271 | // Sends all links to the client as link-added messages. |
| 272 | private void sendAllLinks() { |
| 273 | for (Link link : linkService.getLinks()) { |
| 274 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 275 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // Sends all hosts to the client as host-added messages. |
| 279 | private void sendAllHosts() { |
| 280 | for (Host host : hostService.getHosts()) { |
| 281 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 282 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Sends back device or host details. |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 286 | private void requestDetails(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 287 | ObjectNode payload = payload(event); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 288 | String type = string(payload, "class", "unknown"); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 289 | long sid = number(event, "sid"); |
| 290 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 291 | if (type.equals("device")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 292 | sendMessage(deviceDetails(deviceId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 293 | } else if (type.equals("host")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 294 | sendMessage(hostDetails(hostId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 298 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 299 | // Creates host-to-host intent. |
| 300 | private void createHostIntent(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 301 | ObjectNode payload = payload(event); |
| 302 | long id = number(event, "sid"); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 303 | // TODO: add protection against device ids and non-existent hosts. |
| 304 | HostId one = hostId(string(payload, "one")); |
| 305 | HostId two = hostId(string(payload, "two")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 306 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 307 | HostToHostIntent intent = |
| 308 | new HostToHostIntent(appId, one, two, |
| 309 | DefaultTrafficSelector.builder().build(), |
| 310 | DefaultTrafficTreatment.builder().build()); |
| 311 | startMonitoring(event); |
| 312 | intentService.submit(intent); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 315 | // Creates multi-source-to-single-dest intent. |
| 316 | private void createMultiSourceIntent(ObjectNode event) { |
| 317 | ObjectNode payload = payload(event); |
| 318 | long id = number(event, "sid"); |
| 319 | // TODO: add protection against device ids and non-existent hosts. |
| 320 | Set<HostId> src = getHostIds((ArrayNode) payload.path("src")); |
| 321 | HostId dst = hostId(string(payload, "dst")); |
| 322 | Host dstHost = hostService.getHost(dst); |
| 323 | |
| 324 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 325 | |
| 326 | // FIXME: clearly, this is not enough |
| 327 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 328 | .matchEthType(Ethernet.TYPE_IPV4) |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 329 | .matchEthDst(dstHost.mac()).build(); |
| 330 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 331 | |
| 332 | MultiPointToSinglePointIntent intent = |
| 333 | new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 334 | ingressPoints, dstHost.location()); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 335 | startMonitoring(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 336 | intentService.submit(intent); |
| 337 | } |
| 338 | |
| 339 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 340 | Set<ConnectPoint> points = new HashSet<>(); |
| 341 | for (HostId hostId : hostIds) { |
| 342 | points.add(getHostLocation(hostId)); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 343 | } |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 344 | return points; |
| 345 | } |
| 346 | |
| 347 | private HostLocation getHostLocation(HostId hostId) { |
| 348 | return hostService.getHost(hostId).location(); |
| 349 | } |
| 350 | |
| 351 | // Produces a list of host ids from the specified JSON array. |
| 352 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 353 | Set<HostId> hostIds = new HashSet<>(); |
| 354 | for (JsonNode id : ids) { |
| 355 | hostIds.add(hostId(id.asText())); |
| 356 | } |
| 357 | return hostIds; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | private synchronized long startMonitoring(ObjectNode event) { |
| 362 | if (trafficTask != null) { |
| 363 | stopMonitoring(); |
| 364 | } |
| 365 | trafficEvent = event; |
| 366 | trafficTask = new TrafficMonitor(); |
| 367 | timer.schedule(trafficTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 368 | return number(event, "sid"); |
| 369 | } |
| 370 | |
| 371 | private synchronized void stopMonitoring() { |
| 372 | if (trafficTask != null) { |
| 373 | trafficTask.cancel(); |
| 374 | trafficTask = null; |
| 375 | trafficEvent = null; |
| 376 | } |
| 377 | } |
| 378 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 379 | // Subscribes for host traffic messages. |
| 380 | private synchronized void requestAllTraffic(ObjectNode event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 381 | long sid = startMonitoring(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 382 | sendMessage(trafficSummaryMessage(sid)); |
| 383 | } |
| 384 | |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 385 | private void requestDeviceLinkFlows(ObjectNode event) { |
| 386 | ObjectNode payload = payload(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 387 | long sid = startMonitoring(event); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 388 | |
| 389 | // Get the set of selected hosts and their intents. |
| 390 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 391 | Set<Host> hosts = new HashSet<>(); |
| 392 | Set<Device> devices = getDevices(ids); |
| 393 | |
| 394 | // If there is a hover node, include it in the hosts and find intents. |
| 395 | String hover = string(payload, "hover"); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 396 | if (!isNullOrEmpty(hover)) { |
| 397 | addHover(hosts, devices, hover); |
| 398 | } |
| 399 | sendMessage(flowSummaryMessage(sid, devices)); |
| 400 | } |
| 401 | |
| 402 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 403 | // Subscribes for host traffic messages. |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 404 | private synchronized void requestTraffic(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 405 | ObjectNode payload = payload(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 406 | if (!payload.has("ids")) { |
| 407 | return; |
| 408 | } |
| 409 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 410 | long sid = startMonitoring(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 411 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 412 | // Get the set of selected hosts and their intents. |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 413 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 414 | Set<Host> hosts = getHosts(ids); |
| 415 | Set<Device> devices = getDevices(ids); |
| 416 | Set<Intent> intents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 417 | |
| 418 | // If there is a hover node, include it in the hosts and find intents. |
| 419 | String hover = string(payload, "hover"); |
| 420 | Set<Intent> hoverIntents; |
| 421 | if (!isNullOrEmpty(hover)) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 422 | addHover(hosts, devices, hover); |
| 423 | hoverIntents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 424 | intents.removeAll(hoverIntents); |
| 425 | |
| 426 | // Send an initial message to highlight all links of all monitored intents. |
| 427 | sendMessage(trafficMessage(sid, |
| 428 | new TrafficClass("primary", hoverIntents), |
| 429 | new TrafficClass("secondary", intents))); |
| 430 | |
| 431 | } else { |
| 432 | // Send an initial message to highlight all links of all monitored intents. |
| 433 | sendMessage(trafficMessage(sid, new TrafficClass("primary", intents))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | // Cancels sending traffic messages. |
| 438 | private void cancelTraffic(ObjectNode event) { |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 439 | sendMessage(trafficMessage(number(event, "sid"))); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 440 | stopMonitoring(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 443 | |
| 444 | // Subscribes for summary messages. |
| 445 | private synchronized void requestSummary(ObjectNode event) { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 446 | summaryEnabled = true; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 447 | sendMessage(summmaryMessage(number(event, "sid"))); |
| 448 | } |
| 449 | |
| 450 | // Cancels sending summary messages. |
| 451 | private synchronized void cancelSummary(ObjectNode event) { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 452 | summaryEnabled = false; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame^] | 456 | // Forces mastership role rebalancing. |
| 457 | private void equalizeMasters(ObjectNode event) { |
| 458 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 459 | } |
| 460 | |
| 461 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 462 | // Adds all internal listeners. |
| 463 | private void addListeners() { |
| 464 | clusterService.addListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 465 | mastershipService.addListener(mastershipListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 466 | deviceService.addListener(deviceListener); |
| 467 | linkService.addListener(linkListener); |
| 468 | hostService.addListener(hostListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 469 | intentService.addListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 470 | flowService.addListener(flowListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // Removes all internal listeners. |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 474 | private synchronized void removeListeners() { |
| 475 | if (!listenersRemoved) { |
| 476 | listenersRemoved = true; |
| 477 | clusterService.removeListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 478 | mastershipService.removeListener(mastershipListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 479 | deviceService.removeListener(deviceListener); |
| 480 | linkService.removeListener(linkListener); |
| 481 | hostService.removeListener(hostListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 482 | intentService.removeListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 483 | flowService.removeListener(flowListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 484 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Cluster event listener. |
| 488 | private class InternalClusterListener implements ClusterEventListener { |
| 489 | @Override |
| 490 | public void event(ClusterEvent event) { |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 491 | sendMessage(instanceMessage(event, null)); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // Mastership change listener |
| 496 | private class InternalMastershipListener implements MastershipListener { |
| 497 | @Override |
| 498 | public void event(MastershipEvent event) { |
| 499 | sendAllInstances("updateInstance"); |
| 500 | Device device = deviceService.getDevice(event.subject()); |
| 501 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 505 | // Device event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 506 | private class InternalDeviceListener implements DeviceListener { |
| 507 | @Override |
| 508 | public void event(DeviceEvent event) { |
| 509 | sendMessage(deviceMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 510 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 514 | // Link event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 515 | private class InternalLinkListener implements LinkListener { |
| 516 | @Override |
| 517 | public void event(LinkEvent event) { |
| 518 | sendMessage(linkMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 519 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 523 | // Host event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 524 | private class InternalHostListener implements HostListener { |
| 525 | @Override |
| 526 | public void event(HostEvent event) { |
| 527 | sendMessage(hostMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 528 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 532 | // Intent event listener. |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 533 | private class InternalIntentListener implements IntentListener { |
| 534 | @Override |
| 535 | public void event(IntentEvent event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 536 | if (trafficEvent != null) { |
| 537 | requestTraffic(trafficEvent); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 538 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 539 | eventAccummulator.add(event); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // Intent event listener. |
| 544 | private class InternalFlowListener implements FlowRuleListener { |
| 545 | @Override |
| 546 | public void event(FlowRuleEvent event) { |
| 547 | eventAccummulator.add(event); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 550 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 551 | private class TrafficMonitor extends TimerTask { |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 552 | @Override |
| 553 | public void run() { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 554 | try { |
| 555 | if (trafficEvent != null) { |
| 556 | String type = string(trafficEvent, "event", "unknown"); |
| 557 | if (type.equals("requestAllTraffic")) { |
| 558 | requestAllTraffic(trafficEvent); |
| 559 | } else if (type.equals("requestDeviceLinkFlows")) { |
| 560 | requestDeviceLinkFlows(trafficEvent); |
| 561 | } else { |
| 562 | requestTraffic(trafficEvent); |
| 563 | } |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 564 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 565 | } catch (Exception e) { |
| 566 | log.warn("Unable to handle traffic request due to {}", e.getMessage()); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 570 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 571 | // Accumulates events to drive methodic update of the summary pane. |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 572 | private class InternalEventAccummulator extends AbstractEventAccumulator { |
| 573 | protected InternalEventAccummulator() { |
| 574 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 575 | } |
| 576 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 577 | @Override |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 578 | public void processEvents(List<Event> events) { |
| 579 | try { |
| 580 | if (summaryEnabled) { |
| 581 | sendMessage(summmaryMessage(0)); |
| 582 | } |
| 583 | } catch (Exception e) { |
| 584 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 585 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 586 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 589 | } |
| 590 | |