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