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 | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 93 | private static final long TRAFFIC_FREQUENCY = 2000; |
| 94 | private static final long SUMMARY_FREQUENCY = 30000; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 95 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 96 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
| 97 | new Comparator<ControllerNode>() { |
| 98 | @Override |
| 99 | public int compare(ControllerNode o1, ControllerNode o2) { |
| 100 | return o1.id().toString().compareTo(o2.id().toString()); |
| 101 | } |
| 102 | }; |
| 103 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 104 | |
| 105 | private final Timer timer = new Timer("topology-view"); |
| 106 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 107 | private static final int MAX_EVENTS = 1000; |
| 108 | private static final int MAX_BATCH_MS = 5000; |
| 109 | private static final int MAX_IDLE_MS = 1000; |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 110 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 111 | private final ApplicationId appId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 112 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 113 | private Connection connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 114 | private FrameConnection control; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 115 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 116 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 117 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 118 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 119 | private final LinkListener linkListener = new InternalLinkListener(); |
| 120 | private final HostListener hostListener = new InternalHostListener(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 121 | private final IntentListener intentListener = new InternalIntentListener(); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 122 | private final FlowRuleListener flowListener = new InternalFlowListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 123 | |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 124 | private final EventAccumulator eventAccummulator = new InternalEventAccummulator(); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 125 | |
| 126 | private TimerTask trafficTask; |
| 127 | private ObjectNode trafficEvent; |
| 128 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 129 | private TimerTask summaryTask; |
| 130 | private ObjectNode summaryEvent; |
| 131 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 132 | private long lastActive = System.currentTimeMillis(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 133 | private boolean listenersRemoved = false; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 134 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 135 | private TopologyViewIntentFilter intentFilter; |
| 136 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 137 | // Current selection context |
| 138 | private Set<Host> selectedHosts; |
| 139 | private Set<Device> selectedDevices; |
| 140 | private List<Intent> selectedIntents; |
| 141 | private int currentIntentIndex = -1; |
| 142 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Creates a new web-socket for serving data to GUI topology view. |
| 145 | * |
| 146 | * @param directory service directory |
| 147 | */ |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 148 | public TopologyViewWebSocket(ServiceDirectory directory) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 149 | super(directory); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 150 | intentFilter = new TopologyViewIntentFilter(intentService, deviceService, |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 151 | hostService, linkService); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 152 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Issues a close on the connection. |
| 157 | */ |
| 158 | synchronized void close() { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 159 | removeListeners(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 160 | if (connection.isOpen()) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 161 | connection.close(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Indicates if this connection is idle. |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 167 | * |
| 168 | * @return true if idle or closed |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 169 | */ |
| 170 | synchronized boolean isIdle() { |
| 171 | boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS; |
| 172 | if (idle || !connection.isOpen()) { |
| 173 | return true; |
| 174 | } |
| 175 | try { |
| 176 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 177 | } catch (IOException e) { |
| 178 | log.warn("Unable to send ping message due to: ", e); |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 183 | @Override |
| 184 | public void onOpen(Connection connection) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 185 | log.info("GUI client connected"); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 186 | this.connection = connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 187 | this.control = (FrameConnection) connection; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 188 | addListeners(); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 189 | |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 190 | sendAllInstances(null); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 191 | sendAllDevices(); |
| 192 | sendAllLinks(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 193 | sendAllHosts(); |
| 194 | } |
| 195 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 196 | @Override |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 197 | public synchronized void onClose(int closeCode, String message) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 198 | removeListeners(); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 199 | timer.cancel(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 200 | log.info("GUI client disconnected"); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 205 | lastActive = System.currentTimeMillis(); |
| 206 | return true; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onMessage(String data) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 211 | lastActive = System.currentTimeMillis(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 212 | try { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 213 | processMessage((ObjectNode) mapper.reader().readTree(data)); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 214 | } catch (Exception e) { |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 215 | log.warn("Unable to parse GUI request {} due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 216 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 219 | // Processes the specified event. |
| 220 | private void processMessage(ObjectNode event) { |
| 221 | String type = string(event, "event", "unknown"); |
| 222 | if (type.equals("requestDetails")) { |
| 223 | requestDetails(event); |
| 224 | } else if (type.equals("updateMeta")) { |
| 225 | updateMetaUi(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 226 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 227 | } else if (type.equals("addHostIntent")) { |
| 228 | createHostIntent(event); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 229 | } else if (type.equals("addMultiSourceIntent")) { |
| 230 | createMultiSourceIntent(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 231 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 232 | } else if (type.equals("requestRelatedIntents")) { |
| 233 | requestRelatedIntents(event); |
| 234 | } else if (type.equals("requestNextRelatedIntent")) { |
| 235 | requestNextRelatedIntent(event); |
| 236 | } else if (type.equals("requestSelectedIntentTraffic")) { |
| 237 | requestSelectedIntentTraffic(event); |
| 238 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 239 | } else if (type.equals("requestAllTraffic")) { |
| 240 | requestAllTraffic(event); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 241 | startTrafficMonitoring(event); |
| 242 | |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 243 | } else if (type.equals("requestDeviceLinkFlows")) { |
| 244 | requestDeviceLinkFlows(event); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 245 | startTrafficMonitoring(event); |
| 246 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 247 | } else if (type.equals("cancelTraffic")) { |
| 248 | cancelTraffic(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 249 | |
| 250 | } else if (type.equals("requestSummary")) { |
| 251 | requestSummary(event); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 252 | startSummaryMonitoring(event); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 253 | } else if (type.equals("cancelSummary")) { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 254 | stopSummaryMonitoring(); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 255 | |
| 256 | } else if (type.equals("equalizeMasters")) { |
| 257 | equalizeMasters(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 261 | // Sends the specified data to the client. |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 262 | private synchronized void sendMessage(ObjectNode data) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 263 | try { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 264 | if (connection.isOpen()) { |
| 265 | connection.sendMessage(data.toString()); |
| 266 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 267 | } catch (IOException e) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 268 | log.warn("Unable to send message {} to GUI due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 269 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 272 | // Sends all controller nodes to the client as node-added messages. |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 273 | private void sendAllInstances(String messageType) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 274 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 275 | Collections.sort(nodes, NODE_COMPARATOR); |
| 276 | for (ControllerNode node : nodes) { |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 277 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 278 | messageType)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 279 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 282 | // Sends all devices to the client as device-added messages. |
| 283 | private void sendAllDevices() { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 284 | // Send optical first, others later for layered rendering |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 285 | for (Device device : deviceService.getDevices()) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 286 | if (device.type() == Device.Type.ROADM) { |
| 287 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 288 | } |
| 289 | } |
| 290 | for (Device device : deviceService.getDevices()) { |
| 291 | if (device.type() != Device.Type.ROADM) { |
| 292 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 293 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 294 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 297 | // Sends all links to the client as link-added messages. |
| 298 | private void sendAllLinks() { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 299 | // Send optical first, others later for layered rendering |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 300 | for (Link link : linkService.getLinks()) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 301 | if (link.type() == Link.Type.OPTICAL) { |
| 302 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 303 | } |
| 304 | } |
| 305 | for (Link link : linkService.getLinks()) { |
| 306 | if (link.type() != Link.Type.OPTICAL) { |
| 307 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 308 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 309 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Sends all hosts to the client as host-added messages. |
| 313 | private void sendAllHosts() { |
| 314 | for (Host host : hostService.getHosts()) { |
| 315 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 316 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Sends back device or host details. |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 320 | private void requestDetails(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 321 | ObjectNode payload = payload(event); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 322 | String type = string(payload, "class", "unknown"); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 323 | long sid = number(event, "sid"); |
| 324 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 325 | if (type.equals("device")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 326 | sendMessage(deviceDetails(deviceId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 327 | } else if (type.equals("host")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 328 | sendMessage(hostDetails(hostId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 332 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 333 | // Creates host-to-host intent. |
| 334 | private void createHostIntent(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 335 | ObjectNode payload = payload(event); |
| 336 | long id = number(event, "sid"); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 337 | // TODO: add protection against device ids and non-existent hosts. |
| 338 | HostId one = hostId(string(payload, "one")); |
| 339 | HostId two = hostId(string(payload, "two")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 340 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 341 | HostToHostIntent intent = |
| 342 | new HostToHostIntent(appId, one, two, |
| 343 | DefaultTrafficSelector.builder().build(), |
| 344 | DefaultTrafficTreatment.builder().build()); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 345 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 346 | intentService.submit(intent); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 347 | startMonitoringIntent(event, intent); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 350 | // Creates multi-source-to-single-dest intent. |
| 351 | private void createMultiSourceIntent(ObjectNode event) { |
| 352 | ObjectNode payload = payload(event); |
| 353 | long id = number(event, "sid"); |
| 354 | // TODO: add protection against device ids and non-existent hosts. |
| 355 | Set<HostId> src = getHostIds((ArrayNode) payload.path("src")); |
| 356 | HostId dst = hostId(string(payload, "dst")); |
| 357 | Host dstHost = hostService.getHost(dst); |
| 358 | |
| 359 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 360 | |
| 361 | // FIXME: clearly, this is not enough |
| 362 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 363 | .matchEthType(Ethernet.TYPE_IPV4) |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 364 | .matchEthDst(dstHost.mac()).build(); |
| 365 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 366 | |
| 367 | MultiPointToSinglePointIntent intent = |
| 368 | new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 369 | ingressPoints, dstHost.location()); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 370 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 371 | intentService.submit(intent); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 372 | startMonitoringIntent(event, intent); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 375 | |
| 376 | private synchronized void startMonitoringIntent(ObjectNode event, Intent intent) { |
| 377 | selectedHosts = new HashSet<>(); |
| 378 | selectedDevices = new HashSet<>(); |
| 379 | selectedIntents = new ArrayList<>(); |
| 380 | selectedIntents.add(intent); |
| 381 | currentIntentIndex = -1; |
| 382 | requestNextRelatedIntent(event); |
| 383 | requestSelectedIntentTraffic(event); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 388 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 389 | Set<ConnectPoint> points = new HashSet<>(); |
| 390 | for (HostId hostId : hostIds) { |
| 391 | points.add(getHostLocation(hostId)); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 392 | } |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 393 | return points; |
| 394 | } |
| 395 | |
| 396 | private HostLocation getHostLocation(HostId hostId) { |
| 397 | return hostService.getHost(hostId).location(); |
| 398 | } |
| 399 | |
| 400 | // Produces a list of host ids from the specified JSON array. |
| 401 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 402 | Set<HostId> hostIds = new HashSet<>(); |
| 403 | for (JsonNode id : ids) { |
| 404 | hostIds.add(hostId(id.asText())); |
| 405 | } |
| 406 | return hostIds; |
| 407 | } |
| 408 | |
| 409 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 410 | private synchronized long startTrafficMonitoring(ObjectNode event) { |
| 411 | stopTrafficMonitoring(); |
Thomas Vachuska | 9edca30 | 2014-11-22 17:06:42 -0800 | [diff] [blame] | 412 | trafficEvent = event; |
| 413 | trafficTask = new TrafficMonitor(); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 414 | timer.schedule(trafficTask, TRAFFIC_FREQUENCY, TRAFFIC_FREQUENCY); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 415 | return number(event, "sid"); |
| 416 | } |
| 417 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 418 | private synchronized void stopTrafficMonitoring() { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 419 | if (trafficTask != null) { |
| 420 | trafficTask.cancel(); |
| 421 | trafficTask = null; |
| 422 | trafficEvent = null; |
| 423 | } |
| 424 | } |
| 425 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 426 | // Subscribes for host traffic messages. |
| 427 | private synchronized void requestAllTraffic(ObjectNode event) { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 428 | long sid = startTrafficMonitoring(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 429 | sendMessage(trafficSummaryMessage(sid)); |
| 430 | } |
| 431 | |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 432 | private void requestDeviceLinkFlows(ObjectNode event) { |
| 433 | ObjectNode payload = payload(event); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 434 | long sid = startTrafficMonitoring(event); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 435 | |
| 436 | // Get the set of selected hosts and their intents. |
| 437 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 438 | Set<Host> hosts = new HashSet<>(); |
| 439 | Set<Device> devices = getDevices(ids); |
| 440 | |
| 441 | // If there is a hover node, include it in the hosts and find intents. |
| 442 | String hover = string(payload, "hover"); |
Thomas Vachuska | 29617e5 | 2014-11-20 03:17:46 -0800 | [diff] [blame] | 443 | if (!isNullOrEmpty(hover)) { |
| 444 | addHover(hosts, devices, hover); |
| 445 | } |
| 446 | sendMessage(flowSummaryMessage(sid, devices)); |
| 447 | } |
| 448 | |
| 449 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 450 | // Requests related intents message. |
| 451 | private synchronized void requestRelatedIntents(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 452 | ObjectNode payload = payload(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 453 | if (!payload.has("ids")) { |
| 454 | return; |
| 455 | } |
| 456 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 457 | long sid = number(event, "sid"); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 458 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 459 | // Cancel any other traffic monitoring mode. |
| 460 | stopTrafficMonitoring(); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 461 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 462 | String hover = string(payload, "hover"); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 463 | if (haveSelectedIntents()) { |
| 464 | // Get the set of selected hosts and their intents. |
| 465 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 466 | selectedHosts = getHosts(ids); |
| 467 | selectedDevices = getDevices(ids); |
| 468 | selectedIntents = intentFilter.findPathIntents(selectedHosts, selectedDevices, |
| 469 | intentService.getIntents()); |
| 470 | currentIntentIndex = -1; |
| 471 | |
| 472 | // Send a message to highlight all links of all monitored intents. |
| 473 | sendMessage(trafficMessage(sid, new TrafficClass("primary", selectedIntents))); |
| 474 | } |
| 475 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 476 | if (!isNullOrEmpty(hover)) { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 477 | // If there is a hover node, include it in the selection and find intents. |
| 478 | processExtendedSelection(sid, hover); |
| 479 | } |
| 480 | } |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 481 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 482 | private boolean haveSelectedIntents() { |
| 483 | return selectedIntents != null && !selectedIntents.isEmpty(); |
| 484 | } |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 485 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 486 | private void processExtendedSelection(long sid, String hover) { |
| 487 | Set<Host> hoverSelHosts = new HashSet<>(selectedHosts); |
| 488 | Set<Device> hoverSelDevices = new HashSet<>(selectedDevices); |
| 489 | addHover(hoverSelHosts, hoverSelDevices, hover); |
| 490 | |
| 491 | List<Intent> primary = |
| 492 | intentFilter.findPathIntents(hoverSelHosts, hoverSelDevices, |
| 493 | selectedIntents); |
| 494 | Set<Intent> secondary = new HashSet<>(selectedIntents); |
| 495 | secondary.removeAll(primary); |
| 496 | |
| 497 | // Send a message to highlight all links of all monitored intents. |
| 498 | sendMessage(trafficMessage(sid, new TrafficClass("primary", primary), |
| 499 | new TrafficClass("secondary", secondary))); |
| 500 | } |
| 501 | |
| 502 | // Requests next of the related intents. |
| 503 | private void requestNextRelatedIntent(ObjectNode event) { |
| 504 | if (haveSelectedIntents()) { |
| 505 | currentIntentIndex = (currentIntentIndex + 1) % selectedIntents.size(); |
| 506 | Intent selectedIntent = selectedIntents.get(currentIntentIndex); |
| 507 | log.info("Requested next intent {}", selectedIntent.id()); |
| 508 | |
| 509 | Set<Intent> primary = new HashSet<>(); |
| 510 | primary.add(selectedIntent); |
| 511 | |
| 512 | Set<Intent> secondary = new HashSet<>(selectedIntents); |
| 513 | secondary.remove(selectedIntent); |
| 514 | |
| 515 | // Send a message to highlight all links of the selected intent. |
| 516 | sendMessage(trafficMessage(number(event, "sid"), |
| 517 | new TrafficClass("primary", primary), |
| 518 | new TrafficClass("secondary", secondary))); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Requests monitoring of traffic for the selected intent. |
| 523 | private void requestSelectedIntentTraffic(ObjectNode event) { |
| 524 | if (haveSelectedIntents()) { |
| 525 | Intent selectedIntent = selectedIntents.get(currentIntentIndex); |
| 526 | log.info("Requested traffic for selected {}", selectedIntent.id()); |
| 527 | |
| 528 | Set<Intent> primary = new HashSet<>(); |
| 529 | primary.add(selectedIntent); |
| 530 | |
| 531 | // Send a message to highlight all links of the selected intent. |
| 532 | sendMessage(trafficMessage(number(event, "sid"), |
| 533 | new TrafficClass("primary", primary, true))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | // Cancels sending traffic messages. |
| 538 | private void cancelTraffic(ObjectNode event) { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 539 | selectedIntents = null; |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 540 | sendMessage(trafficMessage(number(event, "sid"))); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 541 | stopTrafficMonitoring(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 544 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 545 | private synchronized long startSummaryMonitoring(ObjectNode event) { |
| 546 | stopSummaryMonitoring(); |
| 547 | summaryEvent = event; |
| 548 | summaryTask = new SummaryMonitor(); |
| 549 | timer.schedule(summaryTask, SUMMARY_FREQUENCY, SUMMARY_FREQUENCY); |
| 550 | return number(event, "sid"); |
| 551 | } |
| 552 | |
| 553 | private synchronized void stopSummaryMonitoring() { |
| 554 | if (summaryEvent != null) { |
| 555 | summaryTask.cancel(); |
| 556 | summaryTask = null; |
| 557 | summaryEvent = null; |
| 558 | } |
| 559 | } |
| 560 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 561 | // Subscribes for summary messages. |
| 562 | private synchronized void requestSummary(ObjectNode event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 563 | sendMessage(summmaryMessage(number(event, "sid"))); |
| 564 | } |
| 565 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 566 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 567 | // Forces mastership role rebalancing. |
| 568 | private void equalizeMasters(ObjectNode event) { |
| 569 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 570 | } |
| 571 | |
| 572 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 573 | // Adds all internal listeners. |
| 574 | private void addListeners() { |
| 575 | clusterService.addListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 576 | mastershipService.addListener(mastershipListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 577 | deviceService.addListener(deviceListener); |
| 578 | linkService.addListener(linkListener); |
| 579 | hostService.addListener(hostListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 580 | intentService.addListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 581 | flowService.addListener(flowListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Removes all internal listeners. |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 585 | private synchronized void removeListeners() { |
| 586 | if (!listenersRemoved) { |
| 587 | listenersRemoved = true; |
| 588 | clusterService.removeListener(clusterListener); |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 589 | mastershipService.removeListener(mastershipListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 590 | deviceService.removeListener(deviceListener); |
| 591 | linkService.removeListener(linkListener); |
| 592 | hostService.removeListener(hostListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 593 | intentService.removeListener(intentListener); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 594 | flowService.removeListener(flowListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 595 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // Cluster event listener. |
| 599 | private class InternalClusterListener implements ClusterEventListener { |
| 600 | @Override |
| 601 | public void event(ClusterEvent event) { |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 602 | sendMessage(instanceMessage(event, null)); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | // Mastership change listener |
| 607 | private class InternalMastershipListener implements MastershipListener { |
| 608 | @Override |
| 609 | public void event(MastershipEvent event) { |
| 610 | sendAllInstances("updateInstance"); |
| 611 | Device device = deviceService.getDevice(event.subject()); |
| 612 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 616 | // Device event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 617 | private class InternalDeviceListener implements DeviceListener { |
| 618 | @Override |
| 619 | public void event(DeviceEvent event) { |
| 620 | sendMessage(deviceMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 621 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 625 | // Link event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 626 | private class InternalLinkListener implements LinkListener { |
| 627 | @Override |
| 628 | public void event(LinkEvent event) { |
| 629 | sendMessage(linkMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 630 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 634 | // Host event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 635 | private class InternalHostListener implements HostListener { |
| 636 | @Override |
| 637 | public void event(HostEvent event) { |
| 638 | sendMessage(hostMessage(event)); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 639 | eventAccummulator.add(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 643 | // Intent event listener. |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 644 | private class InternalIntentListener implements IntentListener { |
| 645 | @Override |
| 646 | public void event(IntentEvent event) { |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 647 | if (trafficEvent != null) { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 648 | requestSelectedIntentTraffic(trafficEvent); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 649 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 650 | eventAccummulator.add(event); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // Intent event listener. |
| 655 | private class InternalFlowListener implements FlowRuleListener { |
| 656 | @Override |
| 657 | public void event(FlowRuleEvent event) { |
| 658 | eventAccummulator.add(event); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 659 | } |
| 660 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 661 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 662 | // Periodic update of the traffic information |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 663 | private class TrafficMonitor extends TimerTask { |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 664 | @Override |
| 665 | public void run() { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 666 | try { |
| 667 | if (trafficEvent != null) { |
| 668 | String type = string(trafficEvent, "event", "unknown"); |
| 669 | if (type.equals("requestAllTraffic")) { |
| 670 | requestAllTraffic(trafficEvent); |
| 671 | } else if (type.equals("requestDeviceLinkFlows")) { |
| 672 | requestDeviceLinkFlows(trafficEvent); |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 673 | } else if (type.equals("requestSelectedIntentTraffic")) { |
| 674 | requestSelectedIntentTraffic(trafficEvent); |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 675 | } |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 676 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 677 | } catch (Exception e) { |
| 678 | log.warn("Unable to handle traffic request due to {}", e.getMessage()); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 679 | log.warn("Boom!", e); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 683 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 684 | // Periodic update of the summary information |
| 685 | private class SummaryMonitor extends TimerTask { |
| 686 | @Override |
| 687 | public void run() { |
| 688 | try { |
| 689 | if (summaryEvent != null) { |
| 690 | requestSummary(summaryEvent); |
| 691 | } |
| 692 | } catch (Exception e) { |
| 693 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 698 | // Accumulates events to drive methodic update of the summary pane. |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 699 | private class InternalEventAccummulator extends AbstractEventAccumulator { |
| 700 | protected InternalEventAccummulator() { |
| 701 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 702 | } |
| 703 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 704 | @Override |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 705 | public void processEvents(List<Event> events) { |
| 706 | try { |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame^] | 707 | if (summaryEvent != null) { |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 708 | sendMessage(summmaryMessage(0)); |
| 709 | } |
| 710 | } catch (Exception e) { |
| 711 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 712 | } |
Thomas Vachuska | 5bde31f | 2014-11-25 15:29:18 -0800 | [diff] [blame] | 713 | |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 716 | } |
| 717 | |