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