Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.util.AbstractAccumulator; |
| 24 | import org.onlab.util.Accumulator; |
| 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; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 30 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.mastership.MastershipAdminService; |
| 33 | import org.onosproject.mastership.MastershipEvent; |
| 34 | import org.onosproject.mastership.MastershipListener; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.Device; |
Simon Hunt | de99e0b | 2015-10-23 18:54:06 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
| 39 | import org.onosproject.net.HostId; |
| 40 | import org.onosproject.net.HostLocation; |
| 41 | import org.onosproject.net.Link; |
| 42 | import org.onosproject.net.device.DeviceEvent; |
| 43 | import org.onosproject.net.device.DeviceListener; |
| 44 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 45 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 46 | import org.onosproject.net.flow.FlowRuleEvent; |
| 47 | import org.onosproject.net.flow.FlowRuleListener; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.host.HostEvent; |
| 51 | import org.onosproject.net.host.HostListener; |
| 52 | import org.onosproject.net.intent.HostToHostIntent; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 53 | import org.onosproject.net.intent.Intent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 54 | import org.onosproject.net.intent.IntentEvent; |
| 55 | import org.onosproject.net.intent.IntentListener; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 56 | import org.onosproject.net.intent.Key; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 57 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 58 | import org.onosproject.net.link.LinkEvent; |
| 59 | import org.onosproject.net.link.LinkListener; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 60 | import org.onosproject.ui.JsonUtils; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 61 | import org.onosproject.ui.RequestHandler; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 62 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 63 | import org.onosproject.ui.impl.TrafficMonitor.Mode; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 64 | import org.onosproject.ui.topo.Highlights; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 65 | import org.onosproject.ui.topo.NodeSelection; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 66 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 67 | |
| 68 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | import java.util.Collection; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 70 | import java.util.Collections; |
| 71 | import java.util.Comparator; |
| 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| 74 | import java.util.Set; |
| 75 | import java.util.Timer; |
| 76 | import java.util.TimerTask; |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 77 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 78 | |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 79 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 80 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 81 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 82 | import static org.onosproject.net.DeviceId.deviceId; |
| 83 | import static org.onosproject.net.HostId.hostId; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 84 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 85 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED; |
| 86 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 87 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 88 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 89 | import static org.onosproject.ui.JsonUtils.envelope; |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 90 | import static org.onosproject.ui.topo.TopoJson.highlightsMessage; |
| 91 | import static org.onosproject.ui.topo.TopoJson.json; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Web socket capable of interacting with the GUI topology view. |
| 95 | */ |
| 96 | public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase { |
| 97 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 98 | // incoming event types |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 99 | private static final String REQ_DETAILS = "requestDetails"; |
| 100 | private static final String UPDATE_META = "updateMeta"; |
| 101 | private static final String ADD_HOST_INTENT = "addHostIntent"; |
| 102 | private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent"; |
| 103 | private static final String REQ_RELATED_INTENTS = "requestRelatedIntents"; |
| 104 | private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent"; |
| 105 | private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent"; |
| 106 | private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic"; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 107 | private static final String SEL_INTENT = "selectIntent"; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 108 | private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic"; |
| 109 | private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 110 | private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows"; |
| 111 | private static final String CANCEL_TRAFFIC = "cancelTraffic"; |
| 112 | private static final String REQ_SUMMARY = "requestSummary"; |
| 113 | private static final String CANCEL_SUMMARY = "cancelSummary"; |
| 114 | private static final String EQ_MASTERS = "equalizeMasters"; |
| 115 | private static final String SPRITE_LIST_REQ = "spriteListRequest"; |
| 116 | private static final String SPRITE_DATA_REQ = "spriteDataRequest"; |
| 117 | private static final String TOPO_START = "topoStart"; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 118 | private static final String TOPO_HEARTBEAT = "topoHeartbeat"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 119 | private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 120 | private static final String TOPO_STOP = "topoStop"; |
| 121 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 122 | // outgoing event types |
| 123 | private static final String SHOW_SUMMARY = "showSummary"; |
| 124 | private static final String SHOW_DETAILS = "showDetails"; |
| 125 | private static final String SPRITE_LIST_RESPONSE = "spriteListResponse"; |
| 126 | private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse"; |
| 127 | private static final String UPDATE_INSTANCE = "updateInstance"; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 128 | private static final String TOPO_START_DONE = "topoStartDone"; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 129 | |
| 130 | // fields |
| 131 | private static final String ID = "id"; |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 132 | private static final String KEY = "key"; |
| 133 | private static final String APP_ID = "appId"; |
| 134 | private static final String APP_NAME = "appName"; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 135 | private static final String DEVICE = "device"; |
| 136 | private static final String HOST = "host"; |
| 137 | private static final String CLASS = "class"; |
| 138 | private static final String UNKNOWN = "unknown"; |
| 139 | private static final String ONE = "one"; |
| 140 | private static final String TWO = "two"; |
| 141 | private static final String SRC = "src"; |
| 142 | private static final String DST = "dst"; |
| 143 | private static final String DATA = "data"; |
| 144 | private static final String NAME = "name"; |
| 145 | private static final String NAMES = "names"; |
| 146 | private static final String ACTIVATE = "activate"; |
| 147 | private static final String DEACTIVATE = "deactivate"; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 148 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 149 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 150 | private static final String MY_APP_ID = "org.onosproject.gui"; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 151 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 152 | private static final long TRAFFIC_PERIOD = 5000; |
| 153 | private static final long SUMMARY_PERIOD = 30000; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 154 | |
| 155 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 156 | (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 157 | |
| 158 | |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 159 | private final Timer timer = new Timer("onos-topology-view"); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 160 | |
| 161 | private static final int MAX_EVENTS = 1000; |
| 162 | private static final int MAX_BATCH_MS = 5000; |
| 163 | private static final int MAX_IDLE_MS = 1000; |
| 164 | |
| 165 | private ApplicationId appId; |
| 166 | |
| 167 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
| 168 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
| 169 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 170 | private final LinkListener linkListener = new InternalLinkListener(); |
| 171 | private final HostListener hostListener = new InternalHostListener(); |
| 172 | private final IntentListener intentListener = new InternalIntentListener(); |
| 173 | private final FlowRuleListener flowListener = new InternalFlowListener(); |
| 174 | |
| 175 | private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator(); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 176 | private final ExecutorService msgSender = |
| 177 | newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 178 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 179 | private TopoOverlayCache overlayCache; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 180 | private TrafficMonitor traffic; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 181 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 182 | private TimerTask summaryTask = null; |
| 183 | private boolean summaryRunning = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 184 | |
| 185 | private boolean listenersRemoved = false; |
| 186 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 187 | |
| 188 | @Override |
| 189 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 190 | super.init(connection, directory); |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 191 | appId = directory.get(CoreService.class).registerApplication(MY_APP_ID); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 192 | traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void destroy() { |
| 197 | cancelAllRequests(); |
Thomas Vachuska | 2bb4863 | 2015-04-28 14:40:42 -0700 | [diff] [blame] | 198 | removeListeners(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 199 | super.destroy(); |
| 200 | } |
| 201 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 202 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 203 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 204 | return ImmutableSet.of( |
| 205 | new TopoStart(), |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 206 | new TopoHeartbeat(), |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 207 | new TopoSelectOverlay(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 208 | new TopoStop(), |
| 209 | new ReqSummary(), |
| 210 | new CancelSummary(), |
| 211 | new SpriteListReq(), |
| 212 | new SpriteDataReq(), |
| 213 | new RequestDetails(), |
| 214 | new UpdateMeta(), |
| 215 | new EqMasters(), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 216 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 217 | // TODO: migrate traffic related to separate app |
| 218 | new AddHostIntent(), |
| 219 | new AddMultiSourceIntent(), |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 220 | |
| 221 | new ReqAllFlowTraffic(), |
| 222 | new ReqAllPortTraffic(), |
| 223 | new ReqDevLinkFlows(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 224 | new ReqRelatedIntents(), |
| 225 | new ReqNextIntent(), |
| 226 | new ReqPrevIntent(), |
| 227 | new ReqSelectedIntentTraffic(), |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 228 | new SelIntent(), |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 229 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 230 | new CancelTraffic() |
| 231 | ); |
| 232 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 233 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Injects the topology overlay cache. |
| 236 | * |
| 237 | * @param overlayCache injected cache |
| 238 | */ |
| 239 | void setOverlayCache(TopoOverlayCache overlayCache) { |
| 240 | this.overlayCache = overlayCache; |
| 241 | } |
| 242 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 243 | // ================================================================== |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 244 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 245 | private final class TopoStart extends RequestHandler { |
| 246 | private TopoStart() { |
| 247 | super(TOPO_START); |
| 248 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 249 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void process(long sid, ObjectNode payload) { |
| 252 | addListeners(); |
| 253 | sendAllInstances(null); |
| 254 | sendAllDevices(); |
| 255 | sendAllLinks(); |
| 256 | sendAllHosts(); |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 257 | sendTopoStartDone(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 261 | private final class TopoHeartbeat extends RequestHandler { |
| 262 | private TopoHeartbeat() { |
| 263 | super(TOPO_HEARTBEAT); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void process(long sid, ObjectNode payload) { |
| 268 | // place holder for now |
| 269 | } |
| 270 | } |
| 271 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 272 | private final class TopoSelectOverlay extends RequestHandler { |
| 273 | private TopoSelectOverlay() { |
| 274 | super(TOPO_SELECT_OVERLAY); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 279 | String deact = string(payload, DEACTIVATE); |
| 280 | String act = string(payload, ACTIVATE); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 281 | overlayCache.switchOverlay(deact, act); |
| 282 | } |
| 283 | } |
| 284 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 285 | private final class TopoStop extends RequestHandler { |
| 286 | private TopoStop() { |
| 287 | super(TOPO_STOP); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 1ad5927 | 2015-11-10 15:23:21 -0800 | [diff] [blame] | 292 | removeListeners(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 293 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 294 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | private final class ReqSummary extends RequestHandler { |
| 299 | private ReqSummary() { |
| 300 | super(REQ_SUMMARY); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void process(long sid, ObjectNode payload) { |
| 305 | requestSummary(sid); |
| 306 | startSummaryMonitoring(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private final class CancelSummary extends RequestHandler { |
| 311 | private CancelSummary() { |
| 312 | super(CANCEL_SUMMARY); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void process(long sid, ObjectNode payload) { |
| 317 | stopSummaryMonitoring(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private final class SpriteListReq extends RequestHandler { |
| 322 | private SpriteListReq() { |
| 323 | super(SPRITE_LIST_REQ); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 328 | ObjectNode root = objectNode(); |
| 329 | ArrayNode names = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 330 | get(SpriteService.class).getNames().forEach(names::add); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 331 | root.set(NAMES, names); |
| 332 | sendMessage(SPRITE_LIST_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | private final class SpriteDataReq extends RequestHandler { |
| 337 | private SpriteDataReq() { |
| 338 | super(SPRITE_DATA_REQ); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 343 | String name = string(payload, NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 344 | ObjectNode root = objectNode(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 345 | root.set(DATA, get(SpriteService.class).get(name)); |
| 346 | sendMessage(SPRITE_DATA_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | private final class RequestDetails extends RequestHandler { |
| 351 | private RequestDetails() { |
| 352 | super(REQ_DETAILS); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 357 | String type = string(payload, CLASS, UNKNOWN); |
| 358 | String id = string(payload, ID); |
| 359 | PropertyPanel pp = null; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 360 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 361 | if (type.equals(DEVICE)) { |
Simon Hunt | de99e0b | 2015-10-23 18:54:06 -0700 | [diff] [blame] | 362 | DeviceId did = deviceId(id); |
| 363 | pp = deviceDetails(did, sid); |
| 364 | overlayCache.currentOverlay().modifyDeviceDetails(pp, did); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 365 | } else if (type.equals(HOST)) { |
Simon Hunt | de99e0b | 2015-10-23 18:54:06 -0700 | [diff] [blame] | 366 | HostId hid = hostId(id); |
| 367 | pp = hostDetails(hid, sid); |
| 368 | overlayCache.currentOverlay().modifyHostDetails(pp, hid); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 369 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 370 | |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 371 | sendMessage(envelope(SHOW_DETAILS, sid, json(pp))); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | private final class UpdateMeta extends RequestHandler { |
| 376 | private UpdateMeta() { |
| 377 | super(UPDATE_META); |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void process(long sid, ObjectNode payload) { |
| 382 | updateMetaUi(payload); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | private final class EqMasters extends RequestHandler { |
| 387 | private EqMasters() { |
| 388 | super(EQ_MASTERS); |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void process(long sid, ObjectNode payload) { |
| 393 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 394 | } |
| 395 | } |
| 396 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 397 | |
| 398 | // ========= ----------------------------------------------------------------- |
| 399 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 400 | // === TODO: move traffic related classes to traffic app |
| 401 | |
| 402 | private final class AddHostIntent extends RequestHandler { |
| 403 | private AddHostIntent() { |
| 404 | super(ADD_HOST_INTENT); |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void process(long sid, ObjectNode payload) { |
| 409 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 410 | HostId one = hostId(string(payload, ONE)); |
| 411 | HostId two = hostId(string(payload, TWO)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 412 | |
| 413 | HostToHostIntent intent = HostToHostIntent.builder() |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 414 | .appId(appId) |
| 415 | .one(one) |
| 416 | .two(two) |
| 417 | .build(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 418 | |
| 419 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 420 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 421 | traffic.monitor(intent); |
| 422 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | private final class AddMultiSourceIntent extends RequestHandler { |
| 427 | private AddMultiSourceIntent() { |
| 428 | super(ADD_MULTI_SRC_INTENT); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void process(long sid, ObjectNode payload) { |
| 433 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 434 | Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC)); |
| 435 | HostId dst = hostId(string(payload, DST)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 436 | Host dstHost = hostService.getHost(dst); |
| 437 | |
| 438 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 439 | |
| 440 | // FIXME: clearly, this is not enough |
| 441 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 442 | .matchEthDst(dstHost.mac()).build(); |
| 443 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 444 | |
| 445 | MultiPointToSinglePointIntent intent = |
| 446 | MultiPointToSinglePointIntent.builder() |
| 447 | .appId(appId) |
| 448 | .selector(selector) |
| 449 | .treatment(treatment) |
| 450 | .ingressPoints(ingressPoints) |
| 451 | .egressPoint(dstHost.location()) |
| 452 | .build(); |
| 453 | |
| 454 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 455 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 456 | traffic.monitor(intent); |
| 457 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 461 | // ========= ----------------------------------------------------------------- |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 462 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 463 | private final class ReqAllFlowTraffic extends RequestHandler { |
| 464 | private ReqAllFlowTraffic() { |
| 465 | super(REQ_ALL_FLOW_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 470 | traffic.monitor(Mode.ALL_FLOW_TRAFFIC); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | private final class ReqAllPortTraffic extends RequestHandler { |
| 475 | private ReqAllPortTraffic() { |
| 476 | super(REQ_ALL_PORT_TRAFFIC); |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 481 | traffic.monitor(Mode.ALL_PORT_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | private final class ReqDevLinkFlows extends RequestHandler { |
| 486 | private ReqDevLinkFlows() { |
| 487 | super(REQ_DEV_LINK_FLOWS); |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 492 | NodeSelection nodeSelection = |
| 493 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 494 | traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | private final class ReqRelatedIntents extends RequestHandler { |
| 499 | private ReqRelatedIntents() { |
| 500 | super(REQ_RELATED_INTENTS); |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void process(long sid, ObjectNode payload) { |
| 505 | NodeSelection nodeSelection = |
| 506 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 507 | traffic.monitor(Mode.RELATED_INTENTS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | private final class ReqNextIntent extends RequestHandler { |
| 512 | private ReqNextIntent() { |
| 513 | super(REQ_NEXT_INTENT); |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 518 | traffic.selectNextIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | private final class ReqPrevIntent extends RequestHandler { |
| 523 | private ReqPrevIntent() { |
| 524 | super(REQ_PREV_INTENT); |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 529 | traffic.selectPreviousIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | private final class ReqSelectedIntentTraffic extends RequestHandler { |
| 534 | private ReqSelectedIntentTraffic() { |
| 535 | super(REQ_SEL_INTENT_TRAFFIC); |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 540 | traffic.monitor(Mode.SELECTED_INTENT); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 544 | private final class SelIntent extends RequestHandler { |
| 545 | private SelIntent() { |
| 546 | super(SEL_INTENT); |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public void process(long sid, ObjectNode payload) { |
| 551 | int appId = Integer.parseInt(string(payload, APP_ID)); |
| 552 | String appName = string(payload, APP_NAME); |
| 553 | ApplicationId applicId = new DefaultApplicationId(appId, appName); |
| 554 | long intentKey = Long.decode(string(payload, KEY)); |
| 555 | |
| 556 | Key key = Key.of(intentKey, applicId); |
| 557 | log.debug("Attempting to select intent key={}", key); |
| 558 | |
| 559 | Intent intent = intentService.getIntent(key); |
| 560 | if (intent == null) { |
| 561 | log.debug("no such intent found!"); |
| 562 | } else { |
| 563 | log.debug("starting to monitor intent {}", key); |
| 564 | traffic.monitor(intent); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 569 | private final class CancelTraffic extends RequestHandler { |
| 570 | private CancelTraffic() { |
| 571 | super(CANCEL_TRAFFIC); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 576 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | //======================================================================= |
| 581 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 582 | // Converts highlights to JSON format and sends the message to the client |
| 583 | protected void sendHighlights(Highlights highlights) { |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 584 | sendMessage(highlightsMessage(highlights)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 587 | // Subscribes for summary messages. |
| 588 | private synchronized void requestSummary(long sid) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 589 | PropertyPanel pp = summmaryMessage(sid); |
| 590 | overlayCache.currentOverlay().modifySummary(pp); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 591 | sendMessage(envelope(SHOW_SUMMARY, sid, json(pp))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 594 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 595 | private void cancelAllRequests() { |
| 596 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 597 | traffic.stopMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Sends all controller nodes to the client as node-added messages. |
| 601 | private void sendAllInstances(String messageType) { |
| 602 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 603 | Collections.sort(nodes, NODE_COMPARATOR); |
| 604 | for (ControllerNode node : nodes) { |
| 605 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 606 | messageType)); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | // Sends all devices to the client as device-added messages. |
| 611 | private void sendAllDevices() { |
| 612 | // Send optical first, others later for layered rendering |
| 613 | for (Device device : deviceService.getDevices()) { |
| 614 | if (device.type() == Device.Type.ROADM) { |
| 615 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 616 | } |
| 617 | } |
| 618 | for (Device device : deviceService.getDevices()) { |
| 619 | if (device.type() != Device.Type.ROADM) { |
| 620 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // Sends all links to the client as link-added messages. |
| 626 | private void sendAllLinks() { |
| 627 | // Send optical first, others later for layered rendering |
| 628 | for (Link link : linkService.getLinks()) { |
| 629 | if (link.type() == Link.Type.OPTICAL) { |
| 630 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 631 | } |
| 632 | } |
| 633 | for (Link link : linkService.getLinks()) { |
| 634 | if (link.type() != Link.Type.OPTICAL) { |
| 635 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | // Sends all hosts to the client as host-added messages. |
| 641 | private void sendAllHosts() { |
| 642 | for (Host host : hostService.getHosts()) { |
| 643 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 644 | } |
| 645 | } |
| 646 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 647 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 648 | Set<ConnectPoint> points = new HashSet<>(); |
| 649 | for (HostId hostId : hostIds) { |
| 650 | points.add(getHostLocation(hostId)); |
| 651 | } |
| 652 | return points; |
| 653 | } |
| 654 | |
| 655 | private HostLocation getHostLocation(HostId hostId) { |
| 656 | return hostService.getHost(hostId).location(); |
| 657 | } |
| 658 | |
| 659 | // Produces a list of host ids from the specified JSON array. |
| 660 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 661 | Set<HostId> hostIds = new HashSet<>(); |
| 662 | for (JsonNode id : ids) { |
| 663 | hostIds.add(hostId(id.asText())); |
| 664 | } |
| 665 | return hostIds; |
| 666 | } |
| 667 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 668 | private void sendTopoStartDone() { |
| 669 | sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode())); |
| 670 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 671 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 672 | private synchronized void startSummaryMonitoring() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 673 | stopSummaryMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 674 | summaryTask = new SummaryMonitor(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 675 | timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 676 | summaryRunning = true; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | private synchronized void stopSummaryMonitoring() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 680 | if (summaryTask != null) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 681 | summaryTask.cancel(); |
| 682 | summaryTask = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 683 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 684 | summaryRunning = false; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 687 | |
| 688 | // Adds all internal listeners. |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 689 | private synchronized void addListeners() { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 690 | listenersRemoved = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 691 | clusterService.addListener(clusterListener); |
| 692 | mastershipService.addListener(mastershipListener); |
| 693 | deviceService.addListener(deviceListener); |
| 694 | linkService.addListener(linkListener); |
| 695 | hostService.addListener(hostListener); |
| 696 | intentService.addListener(intentListener); |
| 697 | flowService.addListener(flowListener); |
| 698 | } |
| 699 | |
| 700 | // Removes all internal listeners. |
| 701 | private synchronized void removeListeners() { |
| 702 | if (!listenersRemoved) { |
| 703 | listenersRemoved = true; |
| 704 | clusterService.removeListener(clusterListener); |
| 705 | mastershipService.removeListener(mastershipListener); |
| 706 | deviceService.removeListener(deviceListener); |
| 707 | linkService.removeListener(linkListener); |
| 708 | hostService.removeListener(hostListener); |
| 709 | intentService.removeListener(intentListener); |
| 710 | flowService.removeListener(flowListener); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // Cluster event listener. |
| 715 | private class InternalClusterListener implements ClusterEventListener { |
| 716 | @Override |
| 717 | public void event(ClusterEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 718 | msgSender.execute(() -> sendMessage(instanceMessage(event, null))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | // Mastership change listener |
| 723 | private class InternalMastershipListener implements MastershipListener { |
| 724 | @Override |
| 725 | public void event(MastershipEvent event) { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 726 | msgSender.execute(() -> { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 727 | sendAllInstances(UPDATE_INSTANCE); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 728 | Device device = deviceService.getDevice(event.subject()); |
| 729 | if (device != null) { |
| 730 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
| 731 | } |
| 732 | }); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | // Device event listener. |
| 737 | private class InternalDeviceListener implements DeviceListener { |
| 738 | @Override |
| 739 | public void event(DeviceEvent event) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 740 | if (event.type() != PORT_STATS_UPDATED) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 741 | msgSender.execute(() -> sendMessage(deviceMessage(event))); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 742 | eventAccummulator.add(event); |
| 743 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | // Link event listener. |
| 748 | private class InternalLinkListener implements LinkListener { |
| 749 | @Override |
| 750 | public void event(LinkEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 751 | msgSender.execute(() -> sendMessage(linkMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 752 | eventAccummulator.add(event); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Host event listener. |
| 757 | private class InternalHostListener implements HostListener { |
| 758 | @Override |
| 759 | public void event(HostEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 760 | msgSender.execute(() -> sendMessage(hostMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 761 | eventAccummulator.add(event); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // Intent event listener. |
| 766 | private class InternalIntentListener implements IntentListener { |
| 767 | @Override |
| 768 | public void event(IntentEvent event) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 769 | msgSender.execute(traffic::pokeIntent); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 770 | eventAccummulator.add(event); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Intent event listener. |
| 775 | private class InternalFlowListener implements FlowRuleListener { |
| 776 | @Override |
| 777 | public void event(FlowRuleEvent event) { |
| 778 | eventAccummulator.add(event); |
| 779 | } |
| 780 | } |
| 781 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 782 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 783 | // === SUMMARY MONITORING |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 784 | |
| 785 | // Periodic update of the summary information |
| 786 | private class SummaryMonitor extends TimerTask { |
| 787 | @Override |
| 788 | public void run() { |
| 789 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 790 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 791 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 792 | } |
| 793 | } catch (Exception e) { |
| 794 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 795 | log.warn("Boom!", e); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | // Accumulates events to drive methodic update of the summary pane. |
| 801 | private class InternalEventAccummulator extends AbstractAccumulator<Event> { |
| 802 | protected InternalEventAccummulator() { |
| 803 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public void processItems(List<Event> items) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 808 | // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 809 | long now = System.currentTimeMillis(); |
| 810 | String me = this.toString(); |
| 811 | String miniMe = me.replaceAll("^.*@", "me@"); |
| 812 | log.debug("Time: {}; this: {}, processing items ({} events)", |
| 813 | now, miniMe, items.size()); |
| 814 | // End-of-Debugging |
| 815 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 816 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 817 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 818 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 819 | } |
| 820 | } catch (Exception e) { |
| 821 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 822 | log.debug("Boom!", e); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |