Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -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 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import org.onlab.osgi.ServiceDirectory; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.FilteredConnectPoint; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.intent.HostToHostIntent; |
| 36 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.RequestHandler; |
| 38 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.impl.TrafficMonitorBase.Mode; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.impl.UiWebSocket; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.model.topo.UiLinkId; |
| 43 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.topo.Highlights; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 45 | import org.onosproject.ui.topo.NodeSelection; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.util.Collection; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 50 | import java.util.HashSet; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 51 | import java.util.Map; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 52 | import java.util.Set; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 53 | |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 54 | import static org.onosproject.net.HostId.hostId; |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 55 | import static org.onosproject.ui.topo.TopoJson.topo2HighlightsMessage; |
| 56 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Server-side component to handle messages pertaining to topo-2 traffic. |
| 59 | */ |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 60 | public class Topo2TrafficMessageHandler extends TopoologyTrafficMessageHandlerAbstract { |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 61 | |
| 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 63 | |
| 64 | // === Inbound event identifiers |
| 65 | private static final String REQUEST_ALL_TRAFFIC = "topo2RequestAllTraffic"; |
| 66 | private static final String CANCEL_TRAFFIC = "topo2CancelTraffic"; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 67 | private static final String ADD_HOST_INTENT = "topo2AddHostIntent"; |
| 68 | private static final String ADD_MULTI_SRC_INTENT = "topo2AddMultiSourceIntent"; |
| 69 | private static final String REQ_RELATED_INTENTS = "topo2RequestRelatedIntents"; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 70 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 71 | // field values |
| 72 | private static final String TRAFFIC_TYPE = "trafficType"; |
| 73 | private static final String FLOW_STATS_BYTES = "flowStatsBytes"; |
| 74 | private static final String PORT_STATS_BIT_SEC = "portStatsBitSec"; |
| 75 | private static final String PORT_STATS_PKT_SEC = "portStatsPktSec"; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 76 | |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 77 | // fields |
| 78 | private static final String ONE = "one"; |
| 79 | private static final String TWO = "two"; |
| 80 | private static final String SRC = "src"; |
| 81 | private static final String DST = "dst"; |
| 82 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 83 | // configuration parameters |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 84 | private static final long TRAFFIC_PERIOD = 5000; |
| 85 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 86 | protected ServicesBundle services; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 87 | private static final String MY_APP_ID = "org.onosproject.gui"; |
| 88 | private ApplicationId appId; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 89 | private UiTopoSession topoSession; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 90 | private Topo2OverlayCache overlay2Cache; |
| 91 | private Traffic2Monitor traffic2; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 92 | |
| 93 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 96 | super.init(connection, directory); |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 97 | appId = directory.get(CoreService.class).registerApplication(MY_APP_ID); |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 98 | services = new ServicesBundle(directory); |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 99 | traffic2 = new Traffic2Monitor(TRAFFIC_PERIOD, services, this); |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 100 | topoSession = ((UiWebSocket) connection).topoSession(); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected Collection<RequestHandler> createRequestHandlers() { |
| 105 | return ImmutableSet.of( |
| 106 | new Topo2AllTraffic(), |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 107 | new Topo2CancelTraffic(), |
| 108 | new Topo2AddHostIntent(), |
| 109 | new Topo2AddMultiSourceIntent() |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 110 | ); |
| 111 | } |
| 112 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 113 | /** |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 114 | * Injects the topology overlay cache. |
| 115 | * |
| 116 | * @param overlay2Cache injected cache |
| 117 | */ |
| 118 | public void setOverlayCache(Topo2OverlayCache overlay2Cache) { |
| 119 | this.overlay2Cache = overlay2Cache; |
| 120 | } |
| 121 | |
| 122 | /** |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 123 | * Shuts down the background traffic monitoring task. |
| 124 | */ |
| 125 | void ceaseAndDesist() { |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 126 | traffic2.stopMonitoring(); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Sends a highlights message back to the client. |
| 131 | * |
| 132 | * @param highlights the highlights for transmission |
| 133 | */ |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 134 | @Override |
| 135 | public void sendHighlights(Highlights highlights) { |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 136 | sendMessage(topo2HighlightsMessage(highlights)); |
| 137 | } |
| 138 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Asks the topo session for the relevant synth links for current region. |
| 141 | * The returned map is keyed by "original" link. |
| 142 | * |
| 143 | * @return synth link map |
| 144 | */ |
| 145 | Map<UiLinkId, UiSynthLink> retrieveRelevantSynthLinks() { |
| 146 | return topoSession.relevantSynthLinks(); |
| 147 | } |
| 148 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 149 | // ================================================================== |
| 150 | |
| 151 | private final class Topo2AllTraffic extends RequestHandler { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 152 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 153 | private Topo2AllTraffic() { |
| 154 | super(REQUEST_ALL_TRAFFIC); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void process(ObjectNode payload) { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 159 | String mode = string(payload, TRAFFIC_TYPE); |
| 160 | log.debug("SHOW TRAFFIC: {}", mode); |
| 161 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 162 | switch (mode) { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 163 | case FLOW_STATS_BYTES: |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 164 | traffic2.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 165 | break; |
| 166 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 167 | case PORT_STATS_BIT_SEC: |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 168 | traffic2.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 169 | break; |
| 170 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 171 | case PORT_STATS_PKT_SEC: |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 172 | traffic2.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | default: |
| 176 | log.warn("Unknown traffic monitor type: " + mode); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private final class Topo2CancelTraffic extends RequestHandler { |
| 183 | private Topo2CancelTraffic() { |
| 184 | super(CANCEL_TRAFFIC); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void process(ObjectNode payload) { |
| 189 | log.debug("CANCEL TRAFFIC"); |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 190 | traffic2.stopMonitoring(); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 193 | |
| 194 | private final class Topo2AddHostIntent extends RequestHandler { |
| 195 | private Topo2AddHostIntent() { |
| 196 | super(ADD_HOST_INTENT); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void process(ObjectNode payload) { |
| 201 | // TODO: add protection against device ids and non-existent hosts. |
| 202 | HostId one = hostId(string(payload, ONE)); |
| 203 | HostId two = hostId(string(payload, TWO)); |
| 204 | |
| 205 | HostToHostIntent intent = HostToHostIntent.builder() |
| 206 | .appId(appId) |
| 207 | .one(one) |
| 208 | .two(two) |
| 209 | .build(); |
| 210 | |
| 211 | services.intent().submit(intent); |
| 212 | if (overlay2Cache.isActive(Traffic2Overlay.OVERLAY_ID)) { |
| 213 | traffic2.monitor(intent); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private final class Topo2AddMultiSourceIntent extends RequestHandler { |
| 219 | private Topo2AddMultiSourceIntent() { |
| 220 | super(ADD_MULTI_SRC_INTENT); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void process(ObjectNode payload) { |
| 225 | // TODO: add protection against device ids and non-existent hosts. |
| 226 | Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC)); |
| 227 | HostId dst = hostId(string(payload, DST)); |
| 228 | Host dstHost = services.host().getHost(dst); |
| 229 | |
| 230 | Set<FilteredConnectPoint> ingressPoints = getHostLocations(src); |
| 231 | |
| 232 | // FIXME: clearly, this is not enough |
| 233 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 234 | .matchEthDst(dstHost.mac()).build(); |
| 235 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 236 | |
| 237 | MultiPointToSinglePointIntent intent = |
| 238 | MultiPointToSinglePointIntent.builder() |
| 239 | .appId(appId) |
| 240 | .selector(selector) |
| 241 | .treatment(treatment) |
| 242 | .filteredIngressPoints(ingressPoints) |
| 243 | .filteredEgressPoint(new FilteredConnectPoint(dstHost.location())) |
| 244 | .build(); |
| 245 | |
| 246 | services.intent().submit(intent); |
| 247 | if (overlay2Cache.isActive(Traffic2Overlay.OVERLAY_ID)) { |
| 248 | traffic2.monitor(intent); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private final class ReqRelatedIntents extends RequestHandler { |
| 254 | private ReqRelatedIntents() { |
| 255 | super(REQ_RELATED_INTENTS); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void process(ObjectNode payload) { |
| 260 | traffic2.monitor(Mode.RELATED_INTENTS, makeNodeSelection(payload)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Produces a list of host ids from the specified JSON array. |
| 265 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 266 | Set<HostId> hostIds = new HashSet<>(); |
| 267 | for (JsonNode id : ids) { |
| 268 | hostIds.add(hostId(id.asText())); |
| 269 | } |
| 270 | return hostIds; |
| 271 | } |
| 272 | |
| 273 | private Set<FilteredConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 274 | Set<FilteredConnectPoint> points = new HashSet<>(); |
| 275 | for (HostId hostId : hostIds) { |
| 276 | points.add(new FilteredConnectPoint(getHostLocation(hostId))); |
| 277 | } |
| 278 | return points; |
| 279 | } |
| 280 | |
| 281 | private HostLocation getHostLocation(HostId hostId) { |
| 282 | return services.host().getHost(hostId).location(); |
| 283 | } |
| 284 | |
| 285 | private NodeSelection makeNodeSelection(ObjectNode payload) { |
| 286 | return new NodeSelection(payload, services.device(), services.host(), |
| 287 | services.link()); |
| 288 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |