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() { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 266 | // Send optical first, others later for layered rendering |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 267 | for (Device device : deviceService.getDevices()) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 268 | if (device.type() == Device.Type.ROADM) { |
| 269 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 270 | } |
| 271 | } |
| 272 | for (Device device : deviceService.getDevices()) { |
| 273 | if (device.type() != Device.Type.ROADM) { |
| 274 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 275 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 276 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 279 | // Sends all links to the client as link-added messages. |
| 280 | private void sendAllLinks() { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 281 | // Send optical first, others later for layered rendering |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 282 | for (Link link : linkService.getLinks()) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 283 | if (link.type() == Link.Type.OPTICAL) { |
| 284 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 285 | } |
| 286 | } |
| 287 | for (Link link : linkService.getLinks()) { |
| 288 | if (link.type() != Link.Type.OPTICAL) { |
| 289 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 290 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 291 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Sends all hosts to the client as host-added messages. |
| 295 | private void sendAllHosts() { |
| 296 | for (Host host : hostService.getHosts()) { |
| 297 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 298 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Sends back device or host details. |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 302 | private void requestDetails(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 303 | ObjectNode payload = payload(event); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 304 | String type = string(payload, "class", "unknown"); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 305 | long sid = number(event, "sid"); |
| 306 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 307 | if (type.equals("device")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 308 | sendMessage(deviceDetails(deviceId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 309 | } else if (type.equals("host")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 310 | sendMessage(hostDetails(hostId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 314 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 315 | // Creates host-to-host intent. |
| 316 | private void createHostIntent(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 317 | ObjectNode payload = payload(event); |
| 318 | long id = number(event, "sid"); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 319 | // TODO: add protection against device ids and non-existent hosts. |
| 320 | HostId one = hostId(string(payload, "one")); |
| 321 | HostId two = hostId(string(payload, "two")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 322 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 323 | HostToHostIntent intent = |
| 324 | new HostToHostIntent(appId, one, two, |
| 325 | DefaultTrafficSelector.builder().build(), |
| 326 | DefaultTrafficTreatment.builder().build()); |
| 327 | startMonitoring(event); |
| 328 | intentService.submit(intent); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 331 | // Creates multi-source-to-single-dest intent. |
| 332 | private void createMultiSourceIntent(ObjectNode event) { |
| 333 | ObjectNode payload = payload(event); |
| 334 | long id = number(event, "sid"); |
| 335 | // TODO: add protection against device ids and non-existent hosts. |
| 336 | Set<HostId> src = getHostIds((ArrayNode) payload.path("src")); |
| 337 | HostId dst = hostId(string(payload, "dst")); |
| 338 | Host dstHost = hostService.getHost(dst); |
| 339 | |
| 340 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 341 | |
| 342 | // FIXME: clearly, this is not enough |
| 343 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 344 | .matchEthType(Ethernet.TYPE_IPV4) |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 345 | .matchEthDst(dstHost.mac()).build(); |
| 346 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 347 | |
| 348 | MultiPointToSinglePointIntent intent = |
| 349 | new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 350 | ingressPoints, dstHost.location()); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 351 | startMonitoring(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 352 | intentService.submit(intent); |
| 353 | } |
| 354 | |
| 355 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 356 | Set<ConnectPoint> points = new HashSet<>(); |
| 357 | for (HostId hostId : hostIds) { |
| 358 | points.add(getHostLocation(hostId)); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 359 | } |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 360 | return points; |
| 361 | } |
| 362 | |
| 363 | private HostLocation getHostLocation(HostId hostId) { |
| 364 | return hostService.getHost(hostId).location(); |
| 365 | } |
| 366 | |
| 367 | // Produces a list of host ids from the specified JSON array. |
| 368 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 369 | Set<HostId> hostIds = new HashSet<>(); |
| 370 | for (JsonNode id : ids) { |
| 371 | hostIds.add(hostId(id.asText())); |
| 372 | } |
| 373 | return hostIds; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | private synchronized long startMonitoring(ObjectNode event) { |
| 378 | if (trafficTask != null) { |
| 379 | stopMonitoring(); |
| 380 | } |
| 381 | trafficEvent = event; |
| 382 | trafficTask = new TrafficMonitor(); |
| 383 | timer.schedule(trafficTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 384 | return number(event, "sid"); |
| 385 | } |
| 386 | |
| 387 | private synchronized void stopMonitoring() { |
| 388 | if (trafficTask != null) { |
| 389 | trafficTask.cancel(); |
| 390 | trafficTask = null; |
| 391 | trafficEvent = null; |
| 392 | } |
| 393 | } |
| 394 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 395 | // Subscribes for host traffic messages. |
| 396 | private synchronized void requestAllTraffic(ObjectNode event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 397 | long sid = startMonitoring(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 398 | sendMessage(trafficSummaryMessage(sid)); |
| 399 | } |
| 400 | |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 401 | private void requestDeviceLinkFlows(ObjectNode event) { |
| 402 | ObjectNode payload = payload(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 403 | long sid = startMonitoring(event); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 404 | |
| 405 | // Get the set of selected hosts and their intents. |
| 406 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 407 | Set<Host> hosts = new HashSet<>(); |
| 408 | Set<Device> devices = getDevices(ids); |
| 409 | |
| 410 | // If there is a hover node, include it in the hosts and find intents. |
| 411 | String hover = string(payload, "hover"); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 412 | if (!isNullOrEmpty(hover)) { |
| 413 | addHover(hosts, devices, hover); |
| 414 | } |
| 415 | sendMessage(flowSummaryMessage(sid, devices)); |
| 416 | } |
| 417 | |
| 418 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 419 | // Subscribes for host traffic messages. |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 420 | private synchronized void requestTraffic(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 421 | ObjectNode payload = payload(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 422 | if (!payload.has("ids")) { |
| 423 | return; |
| 424 | } |
| 425 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 426 | long sid = startMonitoring(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 427 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 428 | // Get the set of selected hosts and their intents. |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 429 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 430 | Set<Host> hosts = getHosts(ids); |
| 431 | Set<Device> devices = getDevices(ids); |
| 432 | Set<Intent> intents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 433 | |
| 434 | // If there is a hover node, include it in the hosts and find intents. |
| 435 | String hover = string(payload, "hover"); |
| 436 | Set<Intent> hoverIntents; |
| 437 | if (!isNullOrEmpty(hover)) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 438 | addHover(hosts, devices, hover); |
| 439 | hoverIntents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 440 | intents.removeAll(hoverIntents); |
| 441 | |
| 442 | // Send an initial message to highlight all links of all monitored intents. |
| 443 | sendMessage(trafficMessage(sid, |
| 444 | new TrafficClass("primary", hoverIntents), |
| 445 | new TrafficClass("secondary", intents))); |
| 446 | |
| 447 | } else { |
| 448 | // Send an initial message to highlight all links of all monitored intents. |
| 449 | sendMessage(trafficMessage(sid, new TrafficClass("primary", intents))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | // Cancels sending traffic messages. |
| 454 | private void cancelTraffic(ObjectNode event) { |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 455 | sendMessage(trafficMessage(number(event, "sid"))); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 456 | stopMonitoring(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 459 | |
| 460 | // Subscribes for summary messages. |
| 461 | private synchronized void requestSummary(ObjectNode event) { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 462 | summaryEnabled = true; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 463 | sendMessage(summmaryMessage(number(event, "sid"))); |
| 464 | } |
| 465 | |
| 466 | // Cancels sending summary messages. |
| 467 | private synchronized void cancelSummary(ObjectNode event) { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 468 | summaryEnabled = false; |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 472 | // Forces mastership role rebalancing. |
| 473 | private void equalizeMasters(ObjectNode event) { |
| 474 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 475 | } |
| 476 | |
| 477 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 478 | // Adds all internal listeners. |
| 479 | private void addListeners() { |
| 480 | clusterService.addListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 481 | mastershipService.addListener(mastershipListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 482 | deviceService.addListener(deviceListener); |
| 483 | linkService.addListener(linkListener); |
| 484 | hostService.addListener(hostListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 485 | intentService.addListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 486 | flowService.addListener(flowListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Removes all internal listeners. |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 490 | private synchronized void removeListeners() { |
| 491 | if (!listenersRemoved) { |
| 492 | listenersRemoved = true; |
| 493 | clusterService.removeListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 494 | mastershipService.removeListener(mastershipListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 495 | deviceService.removeListener(deviceListener); |
| 496 | linkService.removeListener(linkListener); |
| 497 | hostService.removeListener(hostListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 498 | intentService.removeListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 499 | flowService.removeListener(flowListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 500 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Cluster event listener. |
| 504 | private class InternalClusterListener implements ClusterEventListener { |
| 505 | @Override |
| 506 | public void event(ClusterEvent event) { |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 507 | sendMessage(instanceMessage(event, null)); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Mastership change listener |
| 512 | private class InternalMastershipListener implements MastershipListener { |
| 513 | @Override |
| 514 | public void event(MastershipEvent event) { |
| 515 | sendAllInstances("updateInstance"); |
| 516 | Device device = deviceService.getDevice(event.subject()); |
| 517 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 521 | // Device event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 522 | private class InternalDeviceListener implements DeviceListener { |
| 523 | @Override |
| 524 | public void event(DeviceEvent event) { |
| 525 | sendMessage(deviceMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 526 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 530 | // Link event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 531 | private class InternalLinkListener implements LinkListener { |
| 532 | @Override |
| 533 | public void event(LinkEvent event) { |
| 534 | sendMessage(linkMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 535 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 539 | // Host event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 540 | private class InternalHostListener implements HostListener { |
| 541 | @Override |
| 542 | public void event(HostEvent event) { |
| 543 | sendMessage(hostMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 544 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 548 | // Intent event listener. |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 549 | private class InternalIntentListener implements IntentListener { |
| 550 | @Override |
| 551 | public void event(IntentEvent event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 552 | if (trafficEvent != null) { |
| 553 | requestTraffic(trafficEvent); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 554 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 555 | eventAccummulator.add(event); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // Intent event listener. |
| 560 | private class InternalFlowListener implements FlowRuleListener { |
| 561 | @Override |
| 562 | public void event(FlowRuleEvent event) { |
| 563 | eventAccummulator.add(event); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 566 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 567 | private class TrafficMonitor extends TimerTask { |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 568 | @Override |
| 569 | public void run() { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 570 | try { |
| 571 | if (trafficEvent != null) { |
| 572 | String type = string(trafficEvent, "event", "unknown"); |
| 573 | if (type.equals("requestAllTraffic")) { |
| 574 | requestAllTraffic(trafficEvent); |
| 575 | } else if (type.equals("requestDeviceLinkFlows")) { |
| 576 | requestDeviceLinkFlows(trafficEvent); |
| 577 | } else { |
| 578 | requestTraffic(trafficEvent); |
| 579 | } |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 580 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 581 | } catch (Exception e) { |
| 582 | log.warn("Unable to handle traffic request due to {}", e.getMessage()); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 583 | log.warn("Boom!", e); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 587 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 588 | // Accumulates events to drive methodic update of the summary pane. |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 589 | private class InternalEventAccummulator extends AbstractEventAccumulator { |
| 590 | protected InternalEventAccummulator() { |
| 591 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 592 | } |
| 593 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 594 | @Override |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 595 | public void processEvents(List<Event> events) { |
| 596 | try { |
| 597 | if (summaryEnabled) { |
| 598 | sendMessage(summmaryMessage(0)); |
| 599 | } |
| 600 | } catch (Exception e) { |
| 601 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 602 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 603 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 606 | } |
| 607 | |