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