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