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