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) { |
| 292 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 293 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | private final class ReqSummary extends RequestHandler { |
| 298 | private ReqSummary() { |
| 299 | super(REQ_SUMMARY); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void process(long sid, ObjectNode payload) { |
| 304 | requestSummary(sid); |
| 305 | startSummaryMonitoring(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | private final class CancelSummary extends RequestHandler { |
| 310 | private CancelSummary() { |
| 311 | super(CANCEL_SUMMARY); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void process(long sid, ObjectNode payload) { |
| 316 | stopSummaryMonitoring(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private final class SpriteListReq extends RequestHandler { |
| 321 | private SpriteListReq() { |
| 322 | super(SPRITE_LIST_REQ); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 327 | ObjectNode root = objectNode(); |
| 328 | ArrayNode names = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 329 | get(SpriteService.class).getNames().forEach(names::add); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 330 | root.set(NAMES, names); |
| 331 | sendMessage(SPRITE_LIST_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | private final class SpriteDataReq extends RequestHandler { |
| 336 | private SpriteDataReq() { |
| 337 | super(SPRITE_DATA_REQ); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 342 | String name = string(payload, NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 343 | ObjectNode root = objectNode(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 344 | root.set(DATA, get(SpriteService.class).get(name)); |
| 345 | sendMessage(SPRITE_DATA_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | private final class RequestDetails extends RequestHandler { |
| 350 | private RequestDetails() { |
| 351 | super(REQ_DETAILS); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 356 | String type = string(payload, CLASS, UNKNOWN); |
| 357 | String id = string(payload, ID); |
| 358 | PropertyPanel pp = null; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 359 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 360 | if (type.equals(DEVICE)) { |
Simon Hunt | de99e0b | 2015-10-23 18:54:06 -0700 | [diff] [blame] | 361 | DeviceId did = deviceId(id); |
| 362 | pp = deviceDetails(did, sid); |
| 363 | overlayCache.currentOverlay().modifyDeviceDetails(pp, did); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 364 | } else if (type.equals(HOST)) { |
Simon Hunt | de99e0b | 2015-10-23 18:54:06 -0700 | [diff] [blame] | 365 | HostId hid = hostId(id); |
| 366 | pp = hostDetails(hid, sid); |
| 367 | overlayCache.currentOverlay().modifyHostDetails(pp, hid); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 368 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 369 | |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 370 | sendMessage(envelope(SHOW_DETAILS, sid, json(pp))); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | private final class UpdateMeta extends RequestHandler { |
| 375 | private UpdateMeta() { |
| 376 | super(UPDATE_META); |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public void process(long sid, ObjectNode payload) { |
| 381 | updateMetaUi(payload); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | private final class EqMasters extends RequestHandler { |
| 386 | private EqMasters() { |
| 387 | super(EQ_MASTERS); |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void process(long sid, ObjectNode payload) { |
| 392 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 393 | } |
| 394 | } |
| 395 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 396 | |
| 397 | // ========= ----------------------------------------------------------------- |
| 398 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 399 | // === TODO: move traffic related classes to traffic app |
| 400 | |
| 401 | private final class AddHostIntent extends RequestHandler { |
| 402 | private AddHostIntent() { |
| 403 | super(ADD_HOST_INTENT); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public void process(long sid, ObjectNode payload) { |
| 408 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 409 | HostId one = hostId(string(payload, ONE)); |
| 410 | HostId two = hostId(string(payload, TWO)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 411 | |
| 412 | HostToHostIntent intent = HostToHostIntent.builder() |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 413 | .appId(appId) |
| 414 | .one(one) |
| 415 | .two(two) |
| 416 | .build(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 417 | |
| 418 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 419 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 420 | traffic.monitor(intent); |
| 421 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | private final class AddMultiSourceIntent extends RequestHandler { |
| 426 | private AddMultiSourceIntent() { |
| 427 | super(ADD_MULTI_SRC_INTENT); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void process(long sid, ObjectNode payload) { |
| 432 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 433 | Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC)); |
| 434 | HostId dst = hostId(string(payload, DST)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 435 | Host dstHost = hostService.getHost(dst); |
| 436 | |
| 437 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 438 | |
| 439 | // FIXME: clearly, this is not enough |
| 440 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 441 | .matchEthDst(dstHost.mac()).build(); |
| 442 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 443 | |
| 444 | MultiPointToSinglePointIntent intent = |
| 445 | MultiPointToSinglePointIntent.builder() |
| 446 | .appId(appId) |
| 447 | .selector(selector) |
| 448 | .treatment(treatment) |
| 449 | .ingressPoints(ingressPoints) |
| 450 | .egressPoint(dstHost.location()) |
| 451 | .build(); |
| 452 | |
| 453 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 454 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 455 | traffic.monitor(intent); |
| 456 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 460 | // ========= ----------------------------------------------------------------- |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 461 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 462 | private final class ReqAllFlowTraffic extends RequestHandler { |
| 463 | private ReqAllFlowTraffic() { |
| 464 | super(REQ_ALL_FLOW_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 469 | traffic.monitor(Mode.ALL_FLOW_TRAFFIC); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | private final class ReqAllPortTraffic extends RequestHandler { |
| 474 | private ReqAllPortTraffic() { |
| 475 | super(REQ_ALL_PORT_TRAFFIC); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 480 | traffic.monitor(Mode.ALL_PORT_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | private final class ReqDevLinkFlows extends RequestHandler { |
| 485 | private ReqDevLinkFlows() { |
| 486 | super(REQ_DEV_LINK_FLOWS); |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 491 | NodeSelection nodeSelection = |
| 492 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 493 | traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | private final class ReqRelatedIntents extends RequestHandler { |
| 498 | private ReqRelatedIntents() { |
| 499 | super(REQ_RELATED_INTENTS); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void process(long sid, ObjectNode payload) { |
| 504 | NodeSelection nodeSelection = |
| 505 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 506 | traffic.monitor(Mode.RELATED_INTENTS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | private final class ReqNextIntent extends RequestHandler { |
| 511 | private ReqNextIntent() { |
| 512 | super(REQ_NEXT_INTENT); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 517 | traffic.selectNextIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | private final class ReqPrevIntent extends RequestHandler { |
| 522 | private ReqPrevIntent() { |
| 523 | super(REQ_PREV_INTENT); |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 528 | traffic.selectPreviousIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | private final class ReqSelectedIntentTraffic extends RequestHandler { |
| 533 | private ReqSelectedIntentTraffic() { |
| 534 | super(REQ_SEL_INTENT_TRAFFIC); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 539 | traffic.monitor(Mode.SELECTED_INTENT); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 543 | private final class SelIntent extends RequestHandler { |
| 544 | private SelIntent() { |
| 545 | super(SEL_INTENT); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void process(long sid, ObjectNode payload) { |
| 550 | int appId = Integer.parseInt(string(payload, APP_ID)); |
| 551 | String appName = string(payload, APP_NAME); |
| 552 | ApplicationId applicId = new DefaultApplicationId(appId, appName); |
| 553 | long intentKey = Long.decode(string(payload, KEY)); |
| 554 | |
| 555 | Key key = Key.of(intentKey, applicId); |
| 556 | log.debug("Attempting to select intent key={}", key); |
| 557 | |
| 558 | Intent intent = intentService.getIntent(key); |
| 559 | if (intent == null) { |
| 560 | log.debug("no such intent found!"); |
| 561 | } else { |
| 562 | log.debug("starting to monitor intent {}", key); |
| 563 | traffic.monitor(intent); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 568 | private final class CancelTraffic extends RequestHandler { |
| 569 | private CancelTraffic() { |
| 570 | super(CANCEL_TRAFFIC); |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 575 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | //======================================================================= |
| 580 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 581 | // Converts highlights to JSON format and sends the message to the client |
| 582 | protected void sendHighlights(Highlights highlights) { |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 583 | sendMessage(highlightsMessage(highlights)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 586 | // Subscribes for summary messages. |
| 587 | private synchronized void requestSummary(long sid) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 588 | PropertyPanel pp = summmaryMessage(sid); |
| 589 | overlayCache.currentOverlay().modifySummary(pp); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 590 | sendMessage(envelope(SHOW_SUMMARY, sid, json(pp))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 593 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 594 | private void cancelAllRequests() { |
| 595 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 596 | traffic.stopMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // Sends all controller nodes to the client as node-added messages. |
| 600 | private void sendAllInstances(String messageType) { |
| 601 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 602 | Collections.sort(nodes, NODE_COMPARATOR); |
| 603 | for (ControllerNode node : nodes) { |
| 604 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 605 | messageType)); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Sends all devices to the client as device-added messages. |
| 610 | private void sendAllDevices() { |
| 611 | // Send optical first, others later for layered rendering |
| 612 | for (Device device : deviceService.getDevices()) { |
| 613 | if (device.type() == Device.Type.ROADM) { |
| 614 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 615 | } |
| 616 | } |
| 617 | for (Device device : deviceService.getDevices()) { |
| 618 | if (device.type() != Device.Type.ROADM) { |
| 619 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // Sends all links to the client as link-added messages. |
| 625 | private void sendAllLinks() { |
| 626 | // Send optical first, others later for layered rendering |
| 627 | for (Link link : linkService.getLinks()) { |
| 628 | if (link.type() == Link.Type.OPTICAL) { |
| 629 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 630 | } |
| 631 | } |
| 632 | for (Link link : linkService.getLinks()) { |
| 633 | if (link.type() != Link.Type.OPTICAL) { |
| 634 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // Sends all hosts to the client as host-added messages. |
| 640 | private void sendAllHosts() { |
| 641 | for (Host host : hostService.getHosts()) { |
| 642 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 643 | } |
| 644 | } |
| 645 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 646 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 647 | Set<ConnectPoint> points = new HashSet<>(); |
| 648 | for (HostId hostId : hostIds) { |
| 649 | points.add(getHostLocation(hostId)); |
| 650 | } |
| 651 | return points; |
| 652 | } |
| 653 | |
| 654 | private HostLocation getHostLocation(HostId hostId) { |
| 655 | return hostService.getHost(hostId).location(); |
| 656 | } |
| 657 | |
| 658 | // Produces a list of host ids from the specified JSON array. |
| 659 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 660 | Set<HostId> hostIds = new HashSet<>(); |
| 661 | for (JsonNode id : ids) { |
| 662 | hostIds.add(hostId(id.asText())); |
| 663 | } |
| 664 | return hostIds; |
| 665 | } |
| 666 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 667 | private void sendTopoStartDone() { |
| 668 | sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode())); |
| 669 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 670 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 671 | private synchronized void startSummaryMonitoring() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 672 | stopSummaryMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 673 | summaryTask = new SummaryMonitor(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 674 | timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 675 | summaryRunning = true; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | private synchronized void stopSummaryMonitoring() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 679 | if (summaryTask != null) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 680 | summaryTask.cancel(); |
| 681 | summaryTask = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 682 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 683 | summaryRunning = false; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 686 | |
| 687 | // Adds all internal listeners. |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 688 | private synchronized void addListeners() { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 689 | listenersRemoved = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 690 | clusterService.addListener(clusterListener); |
| 691 | mastershipService.addListener(mastershipListener); |
| 692 | deviceService.addListener(deviceListener); |
| 693 | linkService.addListener(linkListener); |
| 694 | hostService.addListener(hostListener); |
| 695 | intentService.addListener(intentListener); |
| 696 | flowService.addListener(flowListener); |
| 697 | } |
| 698 | |
| 699 | // Removes all internal listeners. |
| 700 | private synchronized void removeListeners() { |
| 701 | if (!listenersRemoved) { |
| 702 | listenersRemoved = true; |
| 703 | clusterService.removeListener(clusterListener); |
| 704 | mastershipService.removeListener(mastershipListener); |
| 705 | deviceService.removeListener(deviceListener); |
| 706 | linkService.removeListener(linkListener); |
| 707 | hostService.removeListener(hostListener); |
| 708 | intentService.removeListener(intentListener); |
| 709 | flowService.removeListener(flowListener); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // Cluster event listener. |
| 714 | private class InternalClusterListener implements ClusterEventListener { |
| 715 | @Override |
| 716 | public void event(ClusterEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 717 | msgSender.execute(() -> sendMessage(instanceMessage(event, null))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | // Mastership change listener |
| 722 | private class InternalMastershipListener implements MastershipListener { |
| 723 | @Override |
| 724 | public void event(MastershipEvent event) { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 725 | msgSender.execute(() -> { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 726 | sendAllInstances(UPDATE_INSTANCE); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 727 | Device device = deviceService.getDevice(event.subject()); |
| 728 | if (device != null) { |
| 729 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
| 730 | } |
| 731 | }); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | // Device event listener. |
| 736 | private class InternalDeviceListener implements DeviceListener { |
| 737 | @Override |
| 738 | public void event(DeviceEvent event) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 739 | if (event.type() != PORT_STATS_UPDATED) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 740 | msgSender.execute(() -> sendMessage(deviceMessage(event))); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 741 | eventAccummulator.add(event); |
| 742 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | // Link event listener. |
| 747 | private class InternalLinkListener implements LinkListener { |
| 748 | @Override |
| 749 | public void event(LinkEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 750 | msgSender.execute(() -> sendMessage(linkMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 751 | eventAccummulator.add(event); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Host event listener. |
| 756 | private class InternalHostListener implements HostListener { |
| 757 | @Override |
| 758 | public void event(HostEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 759 | msgSender.execute(() -> sendMessage(hostMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 760 | eventAccummulator.add(event); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Intent event listener. |
| 765 | private class InternalIntentListener implements IntentListener { |
| 766 | @Override |
| 767 | public void event(IntentEvent event) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 768 | msgSender.execute(traffic::pokeIntent); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 769 | eventAccummulator.add(event); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // Intent event listener. |
| 774 | private class InternalFlowListener implements FlowRuleListener { |
| 775 | @Override |
| 776 | public void event(FlowRuleEvent event) { |
| 777 | eventAccummulator.add(event); |
| 778 | } |
| 779 | } |
| 780 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 781 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 782 | // === SUMMARY MONITORING |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 783 | |
| 784 | // Periodic update of the summary information |
| 785 | private class SummaryMonitor extends TimerTask { |
| 786 | @Override |
| 787 | public void run() { |
| 788 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 789 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 790 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 791 | } |
| 792 | } catch (Exception e) { |
| 793 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 794 | log.warn("Boom!", e); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // Accumulates events to drive methodic update of the summary pane. |
| 800 | private class InternalEventAccummulator extends AbstractAccumulator<Event> { |
| 801 | protected InternalEventAccummulator() { |
| 802 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 803 | } |
| 804 | |
| 805 | @Override |
| 806 | public void processItems(List<Event> items) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 807 | // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 808 | long now = System.currentTimeMillis(); |
| 809 | String me = this.toString(); |
| 810 | String miniMe = me.replaceAll("^.*@", "me@"); |
| 811 | log.debug("Time: {}; this: {}, processing items ({} events)", |
| 812 | now, miniMe, items.size()); |
| 813 | // End-of-Debugging |
| 814 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 815 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 816 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 817 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 818 | } |
| 819 | } catch (Exception e) { |
| 820 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 821 | log.debug("Boom!", e); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |