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