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