blob: 5f5d84dd84e4ed2a2d173921a045b882eb8dfa55 [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 Hunt9e2413e2017-05-03 14:25:40 -070083 // configuration parameters
Simon Hunt1911fe42017-05-02 18:25:58 -070084 private static final long TRAFFIC_PERIOD = 5000;
85
Simon Hunt1911fe42017-05-02 18:25:58 -070086 protected ServicesBundle services;
Sean Condonadeb7162019-04-13 20:56:14 +010087 private static final String MY_APP_ID = "org.onosproject.gui";
88 private ApplicationId appId;
Simon Hunt0e161092017-05-08 17:41:38 -070089 private UiTopoSession topoSession;
Sean Condonadeb7162019-04-13 20:56:14 +010090 private Topo2OverlayCache overlay2Cache;
91 private Traffic2Monitor traffic2;
Simon Hunt1911fe42017-05-02 18:25:58 -070092
93
Simon Hunte6f64612017-04-28 00:01:48 -070094 @Override
95 public void init(UiConnection connection, ServiceDirectory directory) {
96 super.init(connection, directory);
Sean Condonadeb7162019-04-13 20:56:14 +010097 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt1911fe42017-05-02 18:25:58 -070098 services = new ServicesBundle(directory);
Sean Condonadeb7162019-04-13 20:56:14 +010099 traffic2 = new Traffic2Monitor(TRAFFIC_PERIOD, services, this);
Simon Hunt0e161092017-05-08 17:41:38 -0700100 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunte6f64612017-04-28 00:01:48 -0700101 }
102
103 @Override
104 protected Collection<RequestHandler> createRequestHandlers() {
105 return ImmutableSet.of(
106 new Topo2AllTraffic(),
Sean Condonadeb7162019-04-13 20:56:14 +0100107 new Topo2CancelTraffic(),
108 new Topo2AddHostIntent(),
109 new Topo2AddMultiSourceIntent()
Simon Hunte6f64612017-04-28 00:01:48 -0700110 );
111 }
112
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700113 /**
Sean Condonadeb7162019-04-13 20:56:14 +0100114 * 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 Hunt2d7cd6f2017-05-04 13:04:50 -0700123 * Shuts down the background traffic monitoring task.
124 */
125 void ceaseAndDesist() {
Sean Condonadeb7162019-04-13 20:56:14 +0100126 traffic2.stopMonitoring();
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700127 }
128
Simon Hunt8e258112017-05-05 13:19:04 -0700129 /**
130 * Sends a highlights message back to the client.
131 *
132 * @param highlights the highlights for transmission
133 */
Sean Condonadeb7162019-04-13 20:56:14 +0100134 @Override
135 public void sendHighlights(Highlights highlights) {
Simon Hunt8e258112017-05-05 13:19:04 -0700136 sendMessage(topo2HighlightsMessage(highlights));
137 }
138
Simon Hunt0e161092017-05-08 17:41:38 -0700139 /**
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 Hunte6f64612017-04-28 00:01:48 -0700149 // ==================================================================
150
151 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700152
Simon Hunte6f64612017-04-28 00:01:48 -0700153 private Topo2AllTraffic() {
154 super(REQUEST_ALL_TRAFFIC);
155 }
156
157 @Override
158 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700159 String mode = string(payload, TRAFFIC_TYPE);
160 log.debug("SHOW TRAFFIC: {}", mode);
161
Simon Hunte6f64612017-04-28 00:01:48 -0700162 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700163 case FLOW_STATS_BYTES:
Sean Condonadeb7162019-04-13 20:56:14 +0100164 traffic2.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700165 break;
166
Simon Hunt9e2413e2017-05-03 14:25:40 -0700167 case PORT_STATS_BIT_SEC:
Sean Condonadeb7162019-04-13 20:56:14 +0100168 traffic2.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700169 break;
170
Simon Hunt9e2413e2017-05-03 14:25:40 -0700171 case PORT_STATS_PKT_SEC:
Sean Condonadeb7162019-04-13 20:56:14 +0100172 traffic2.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700173 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 Condonadeb7162019-04-13 20:56:14 +0100190 traffic2.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700191 }
192 }
Sean Condonadeb7162019-04-13 20:56:14 +0100193
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 Hunte6f64612017-04-28 00:01:48 -0700289}
290
291