blob: a6e0e9d1893bce5f33954420451e8d70aca91ab2 [file] [log] [blame]
Simon Hunte6f64612017-04-28 00:01:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Hunte6f64612017-04-28 00:01:48 -07003 *
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
18package org.onosproject.ui.impl.topo;
19
Sean Condonadeb7162019-04-13 20:56:14 +010020import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.node.ArrayNode;
Simon Hunte6f64612017-04-28 00:01:48 -070022import com.fasterxml.jackson.databind.node.ObjectNode;
23import com.google.common.collect.ImmutableSet;
24import org.onlab.osgi.ServiceDirectory;
Sean Condonadeb7162019-04-13 20:56:14 +010025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.FilteredConnectPoint;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.HostLocation;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.intent.HostToHostIntent;
36import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Simon Hunte6f64612017-04-28 00:01:48 -070037import org.onosproject.ui.RequestHandler;
38import org.onosproject.ui.UiConnection;
Simon Hunt9e2413e2017-05-03 14:25:40 -070039import org.onosproject.ui.impl.TrafficMonitorBase.Mode;
Simon Hunt0e161092017-05-08 17:41:38 -070040import org.onosproject.ui.impl.UiWebSocket;
Simon Hunt1911fe42017-05-02 18:25:58 -070041import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt0e161092017-05-08 17:41:38 -070042import org.onosproject.ui.model.topo.UiLinkId;
43import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunt8e258112017-05-05 13:19:04 -070044import org.onosproject.ui.topo.Highlights;
Sean Condonadeb7162019-04-13 20:56:14 +010045import org.onosproject.ui.topo.NodeSelection;
Simon Hunte6f64612017-04-28 00:01:48 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.Collection;
Sean Condonadeb7162019-04-13 20:56:14 +010050import java.util.HashSet;
Simon Hunt0e161092017-05-08 17:41:38 -070051import java.util.Map;
Sean Condonadeb7162019-04-13 20:56:14 +010052import java.util.Set;
Simon Hunte6f64612017-04-28 00:01:48 -070053
Sean Condonadeb7162019-04-13 20:56:14 +010054import static org.onosproject.net.HostId.hostId;
Simon Hunt8e258112017-05-05 13:19:04 -070055import static org.onosproject.ui.topo.TopoJson.topo2HighlightsMessage;
56
Simon Hunte6f64612017-04-28 00:01:48 -070057/**
58 * Server-side component to handle messages pertaining to topo-2 traffic.
59 */
Sean Condonadeb7162019-04-13 20:56:14 +010060public class Topo2TrafficMessageHandler extends TopoologyTrafficMessageHandlerAbstract {
Simon Hunte6f64612017-04-28 00:01:48 -070061
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 Condonadeb7162019-04-13 20:56:14 +010067 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 Hunte6f64612017-04-28 00:01:48 -070070
Simon Hunt9e2413e2017-05-03 14:25:40 -070071 // 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 Hunt1911fe42017-05-02 18:25:58 -070076
Sean Condonadeb7162019-04-13 20:56:14 +010077 // 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 Hunt1911fe42017-05-02 18:25:58 -070083 protected ServicesBundle services;
Sean Condonadeb7162019-04-13 20:56:14 +010084 private static final String MY_APP_ID = "org.onosproject.gui";
85 private ApplicationId appId;
Simon Hunt0e161092017-05-08 17:41:38 -070086 private UiTopoSession topoSession;
Sean Condonadeb7162019-04-13 20:56:14 +010087 private Topo2OverlayCache overlay2Cache;
88 private Traffic2Monitor traffic2;
Simon Hunt1911fe42017-05-02 18:25:58 -070089
90
Simon Hunte6f64612017-04-28 00:01:48 -070091 @Override
92 public void init(UiConnection connection, ServiceDirectory directory) {
93 super.init(connection, directory);
Sean Condonadeb7162019-04-13 20:56:14 +010094 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt1911fe42017-05-02 18:25:58 -070095 services = new ServicesBundle(directory);
Thomas Vachuskaa5e986d2021-04-06 11:14:09 -070096 traffic2 = new Traffic2Monitor(services, this);
Simon Hunt0e161092017-05-08 17:41:38 -070097 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunte6f64612017-04-28 00:01:48 -070098 }
99
100 @Override
101 protected Collection<RequestHandler> createRequestHandlers() {
102 return ImmutableSet.of(
103 new Topo2AllTraffic(),
Sean Condonadeb7162019-04-13 20:56:14 +0100104 new Topo2CancelTraffic(),
105 new Topo2AddHostIntent(),
106 new Topo2AddMultiSourceIntent()
Simon Hunte6f64612017-04-28 00:01:48 -0700107 );
108 }
109
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700110 /**
Sean Condonadeb7162019-04-13 20:56:14 +0100111 * Injects the topology overlay cache.
112 *
113 * @param overlay2Cache injected cache
114 */
115 public void setOverlayCache(Topo2OverlayCache overlay2Cache) {
116 this.overlay2Cache = overlay2Cache;
117 }
118
119 /**
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700120 * Shuts down the background traffic monitoring task.
121 */
122 void ceaseAndDesist() {
Sean Condonadeb7162019-04-13 20:56:14 +0100123 traffic2.stopMonitoring();
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700124 }
125
Simon Hunt8e258112017-05-05 13:19:04 -0700126 /**
127 * Sends a highlights message back to the client.
128 *
129 * @param highlights the highlights for transmission
130 */
Sean Condonadeb7162019-04-13 20:56:14 +0100131 @Override
132 public void sendHighlights(Highlights highlights) {
Simon Hunt8e258112017-05-05 13:19:04 -0700133 sendMessage(topo2HighlightsMessage(highlights));
134 }
135
Simon Hunt0e161092017-05-08 17:41:38 -0700136 /**
137 * Asks the topo session for the relevant synth links for current region.
138 * The returned map is keyed by "original" link.
139 *
140 * @return synth link map
141 */
142 Map<UiLinkId, UiSynthLink> retrieveRelevantSynthLinks() {
143 return topoSession.relevantSynthLinks();
144 }
145
Simon Hunte6f64612017-04-28 00:01:48 -0700146 // ==================================================================
147
148 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700149
Simon Hunte6f64612017-04-28 00:01:48 -0700150 private Topo2AllTraffic() {
151 super(REQUEST_ALL_TRAFFIC);
152 }
153
154 @Override
155 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700156 String mode = string(payload, TRAFFIC_TYPE);
157 log.debug("SHOW TRAFFIC: {}", mode);
158
Simon Hunte6f64612017-04-28 00:01:48 -0700159 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700160 case FLOW_STATS_BYTES:
Sean Condonadeb7162019-04-13 20:56:14 +0100161 traffic2.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700162 break;
163
Simon Hunt9e2413e2017-05-03 14:25:40 -0700164 case PORT_STATS_BIT_SEC:
Sean Condonadeb7162019-04-13 20:56:14 +0100165 traffic2.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700166 break;
167
Simon Hunt9e2413e2017-05-03 14:25:40 -0700168 case PORT_STATS_PKT_SEC:
Sean Condonadeb7162019-04-13 20:56:14 +0100169 traffic2.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700170 break;
171
172 default:
173 log.warn("Unknown traffic monitor type: " + mode);
174 break;
175 }
176 }
177 }
178
179 private final class Topo2CancelTraffic extends RequestHandler {
180 private Topo2CancelTraffic() {
181 super(CANCEL_TRAFFIC);
182 }
183
184 @Override
185 public void process(ObjectNode payload) {
186 log.debug("CANCEL TRAFFIC");
Sean Condonadeb7162019-04-13 20:56:14 +0100187 traffic2.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700188 }
189 }
Sean Condonadeb7162019-04-13 20:56:14 +0100190
191 private final class Topo2AddHostIntent extends RequestHandler {
192 private Topo2AddHostIntent() {
193 super(ADD_HOST_INTENT);
194 }
195
196 @Override
197 public void process(ObjectNode payload) {
198 // TODO: add protection against device ids and non-existent hosts.
199 HostId one = hostId(string(payload, ONE));
200 HostId two = hostId(string(payload, TWO));
201
202 HostToHostIntent intent = HostToHostIntent.builder()
203 .appId(appId)
204 .one(one)
205 .two(two)
206 .build();
207
208 services.intent().submit(intent);
209 if (overlay2Cache.isActive(Traffic2Overlay.OVERLAY_ID)) {
210 traffic2.monitor(intent);
211 }
212 }
213 }
214
215 private final class Topo2AddMultiSourceIntent extends RequestHandler {
216 private Topo2AddMultiSourceIntent() {
217 super(ADD_MULTI_SRC_INTENT);
218 }
219
220 @Override
221 public void process(ObjectNode payload) {
222 // TODO: add protection against device ids and non-existent hosts.
223 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
224 HostId dst = hostId(string(payload, DST));
225 Host dstHost = services.host().getHost(dst);
226
227 Set<FilteredConnectPoint> ingressPoints = getHostLocations(src);
228
229 // FIXME: clearly, this is not enough
230 TrafficSelector selector = DefaultTrafficSelector.builder()
231 .matchEthDst(dstHost.mac()).build();
232 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
233
234 MultiPointToSinglePointIntent intent =
235 MultiPointToSinglePointIntent.builder()
236 .appId(appId)
237 .selector(selector)
238 .treatment(treatment)
239 .filteredIngressPoints(ingressPoints)
240 .filteredEgressPoint(new FilteredConnectPoint(dstHost.location()))
241 .build();
242
243 services.intent().submit(intent);
244 if (overlay2Cache.isActive(Traffic2Overlay.OVERLAY_ID)) {
245 traffic2.monitor(intent);
246 }
247 }
248 }
249
250 private final class ReqRelatedIntents extends RequestHandler {
251 private ReqRelatedIntents() {
252 super(REQ_RELATED_INTENTS);
253 }
254
255 @Override
256 public void process(ObjectNode payload) {
257 traffic2.monitor(Mode.RELATED_INTENTS, makeNodeSelection(payload));
258 }
259 }
260
261 // Produces a list of host ids from the specified JSON array.
262 private Set<HostId> getHostIds(ArrayNode ids) {
263 Set<HostId> hostIds = new HashSet<>();
264 for (JsonNode id : ids) {
265 hostIds.add(hostId(id.asText()));
266 }
267 return hostIds;
268 }
269
270 private Set<FilteredConnectPoint> getHostLocations(Set<HostId> hostIds) {
271 Set<FilteredConnectPoint> points = new HashSet<>();
272 for (HostId hostId : hostIds) {
273 points.add(new FilteredConnectPoint(getHostLocation(hostId)));
274 }
275 return points;
276 }
277
278 private HostLocation getHostLocation(HostId hostId) {
279 return services.host().getHost(hostId).location();
280 }
281
282 private NodeSelection makeNodeSelection(ObjectNode payload) {
283 return new NodeSelection(payload, services.device(), services.host(),
284 services.link());
285 }
Simon Hunte6f64612017-04-28 00:01:48 -0700286}
287
288