blob: ec01cececa973cfffd557046f55f008c06b28e86 [file] [log] [blame]
daniel park128c52c2017-09-04 13:15:51 +09001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.onosproject.openstacknetworkingui;
17
Daniel Park819f4e82018-07-02 14:22:31 +090018import com.fasterxml.jackson.databind.JsonNode;
daniel park128c52c2017-09-04 13:15:51 +090019import com.fasterxml.jackson.databind.ObjectMapper;
Daniel Park819f4e82018-07-02 14:22:31 +090020import com.fasterxml.jackson.databind.node.ArrayNode;
daniel park128c52c2017-09-04 13:15:51 +090021import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.base.Strings;
23import com.google.common.collect.ImmutableSet;
24import com.google.common.collect.Lists;
25import com.google.common.collect.Sets;
26import com.google.common.collect.Streams;
Daniel Park819f4e82018-07-02 14:22:31 +090027import org.apache.commons.io.IOUtils;
daniel park128c52c2017-09-04 13:15:51 +090028import org.onlab.osgi.ServiceDirectory;
Daniel Park819f4e82018-07-02 14:22:31 +090029import org.onlab.util.DefaultHashMap;
daniel park128c52c2017-09-04 13:15:51 +090030import org.onosproject.cluster.ClusterService;
Daniel Park819f4e82018-07-02 14:22:31 +090031import org.onosproject.cluster.NodeId;
32import org.onosproject.mastership.MastershipService;
33import org.onosproject.net.AnnotationKeys;
34import org.onosproject.net.Annotations;
daniel park128c52c2017-09-04 13:15:51 +090035import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Element;
38import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
40import org.onosproject.net.Path;
Daniel Park819f4e82018-07-02 14:22:31 +090041import org.onosproject.net.config.NetworkConfigService;
42import org.onosproject.net.config.basics.BasicDeviceConfig;
43import org.onosproject.net.config.basics.BasicElementConfig;
44import org.onosproject.net.device.DeviceEvent;
daniel park128c52c2017-09-04 13:15:51 +090045import org.onosproject.net.device.DeviceService;
Daniel Park819f4e82018-07-02 14:22:31 +090046import org.onosproject.net.driver.Driver;
47import org.onosproject.net.driver.DriverService;
daniel park128c52c2017-09-04 13:15:51 +090048import org.onosproject.net.host.HostService;
49import org.onosproject.net.topology.PathService;
50import org.onosproject.ui.JsonUtils;
51import org.onosproject.ui.RequestHandler;
52import org.onosproject.ui.UiConnection;
53import org.onosproject.ui.UiMessageHandler;
daniel park128c52c2017-09-04 13:15:51 +090054import org.onosproject.ui.topo.Highlights;
55import org.onosproject.ui.topo.HostHighlight;
56import org.onosproject.ui.topo.NodeBadge;
57import org.onosproject.ui.topo.NodeBadge.Status;
58import org.onosproject.ui.topo.TopoJson;
59import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
62import javax.ws.rs.client.Client;
63import javax.ws.rs.client.ClientBuilder;
64import javax.ws.rs.client.Entity;
65import javax.ws.rs.client.Invocation;
66import javax.ws.rs.client.WebTarget;
67import javax.ws.rs.core.MediaType;
68import javax.ws.rs.core.Response;
69import java.io.ByteArrayInputStream;
70import java.io.IOException;
71import java.io.InputStream;
72import java.nio.charset.StandardCharsets;
73import java.util.Base64;
74import java.util.Collection;
75import java.util.List;
Daniel Park819f4e82018-07-02 14:22:31 +090076import java.util.Map;
77import java.util.Objects;
daniel park128c52c2017-09-04 13:15:51 +090078import java.util.Set;
Daniel Park819f4e82018-07-02 14:22:31 +090079import java.util.concurrent.ConcurrentHashMap;
daniel park128c52c2017-09-04 13:15:51 +090080
Daniel Park819f4e82018-07-02 14:22:31 +090081import static com.google.common.base.Strings.isNullOrEmpty;
82import static org.onosproject.net.AnnotationKeys.DRIVER;
daniel park128c52c2017-09-04 13:15:51 +090083import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Daniel Park819f4e82018-07-02 14:22:31 +090084import static org.onosproject.net.Device.Type.SWITCH;
85import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GEO;
86import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GRID;
87import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
daniel park128c52c2017-09-04 13:15:51 +090088
89/**
90 * OpenStack Networking UI message handler.
91 */
92public class OpenstackNetworkingUiMessageHandler extends UiMessageHandler {
93
94 private static final String OPENSTACK_NETWORKING_UI_START = "openstackNetworkingUiStart";
95 private static final String OPENSTACK_NETWORKING_UI_UPDATE = "openstackNetworkingUiUpdate";
96 private static final String OPENSTACK_NETWORKING_UI_STOP = "openstackNetworkingUiStop";
97 private static final String ANNOTATION_NETWORK_ID = "networkId";
98 private static final String FLOW_TRACE_REQUEST = "flowTraceRequest";
99 private static final String SRC_IP = "srcIp";
100 private static final String DST_IP = "dstIp";
101 private static final String ANNOTATION_SEGMENT_ID = "segId";
102 private static final String AUTHORIZATION = "Authorization";
103 private static final String COMMAND = "command";
104 private static final String FLOW_TRACE = "flowtrace";
105 private static final String REVERSE = "reverse";
106 private static final String TRANSACTION_ID = "transaction_id";
107 private static final String TRANSACTION_VALUE = "sona";
108 private static final String APP_REST_URL = "app_rest_url";
109 private static final String MATCHING_FIELDS = "matchingfields";
110 private static final String SOURCE_IP = "source_ip";
111 private static final String DESTINATION_IP = "destination_ip";
112 private static final String TO_GATEWAY = "to_gateway";
113 private static final String IP_PROTOCOL = "ip_protocol";
114 private static final String HTTP = "http://";
115 private static final String OPENSTACK_NETWORKING_UI_RESULT = ":8181/onos/openstacknetworkingui/result";
116
117 private static final String ID = "id";
118 private static final String MODE = "mode";
119 private static final String MOUSE = "mouse";
120
121 private enum Mode { IDLE, MOUSE }
122
123 private final Logger log = LoggerFactory.getLogger(getClass());
124
125 private DeviceService deviceService;
126 private HostService hostService;
127 private PathService pathService;
128 private ClusterService clusterService;
Daniel Park819f4e82018-07-02 14:22:31 +0900129 private DriverService driverService;
130 private MastershipService mastershipService;
daniel park128c52c2017-09-04 13:15:51 +0900131 private String restUrl;
132 private String restAuthInfo;
133 private Mode currentMode = Mode.IDLE;
134 private Element elementOfNote;
135 private final Client client = ClientBuilder.newClient();
Daniel Park819f4e82018-07-02 14:22:31 +0900136 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
137 private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
138 new DefaultHashMap<>("updateDevice");
daniel park128c52c2017-09-04 13:15:51 +0900139
Daniel Park819f4e82018-07-02 14:22:31 +0900140 static {
141 DEVICE_EVENT.put(DEVICE_REMOVED, "removeDevice");
142 }
daniel park128c52c2017-09-04 13:15:51 +0900143
144 @Override
145 public void init(UiConnection connection, ServiceDirectory directory) {
146 super.init(connection, directory);
147 deviceService = directory.get(DeviceService.class);
148 hostService = directory.get(HostService.class);
149 pathService = directory.get(PathService.class);
150 clusterService = directory.get(ClusterService.class);
Daniel Park819f4e82018-07-02 14:22:31 +0900151 driverService = directory.get((DriverService.class));
152 mastershipService = directory.get(MastershipService.class);
153
154 // Removes non switch devices such as an ovsdb device
155 removeNonSwitchDevices();
daniel park128c52c2017-09-04 13:15:51 +0900156 }
157
Daniel Park819f4e82018-07-02 14:22:31 +0900158 private void removeNonSwitchDevices() {
159 Streams.stream(deviceService.getAvailableDevices())
160 .filter(device -> device.type() != SWITCH)
161 .forEach(device -> sendMessage(deviceMessage(new DeviceEvent(DEVICE_REMOVED, device))));
162 }
163
164 // Produces a device event message to the client.
165 protected ObjectNode deviceMessage(DeviceEvent event) {
166 Device device = event.subject();
167 String uiType = device.annotations().value(AnnotationKeys.UI_TYPE);
168 String driverName = device.annotations().value(DRIVER);
169 Driver driver = driverName == null ? null : driverService.getDriver(driverName);
170 String devType = uiType != null ? uiType :
171 (driver != null ? driver.getProperty(AnnotationKeys.UI_TYPE) : null);
172 if (devType == null) {
173 devType = device.type().toString().toLowerCase();
174 }
175 String name = device.annotations().value(AnnotationKeys.NAME);
176 name = isNullOrEmpty(name) ? device.id().toString() : name;
177
178 ObjectNode payload = objectNode()
179 .put("id", device.id().toString())
180 .put("type", devType)
181 .put("online", deviceService.isAvailable(device.id()))
182 .put("master", master(device.id()));
183
184 payload.set("labels", labels("", name, device.id().toString()));
185 payload.set("props", props(device.annotations()));
186
187 BasicDeviceConfig cfg = get(NetworkConfigService.class)
188 .getConfig(device.id(), BasicDeviceConfig.class);
189 if (!addLocation(cfg, payload)) {
190 addMetaUi(device.id().toString(), payload);
191 }
192
193 String type = DEVICE_EVENT.get(event.type());
194 return JsonUtils.envelope(type, payload);
195 }
196
197 // Returns the name of the master node for the specified device id.
198 private String master(DeviceId deviceId) {
199 NodeId master = mastershipService.getMasterFor(deviceId);
200 return master != null ? master.toString() : "";
201 }
202
203 // Encodes the specified list of labels a JSON array.
204 private ArrayNode labels(String... labels) {
205 ArrayNode json = arrayNode();
206 for (String label : labels) {
207 json.add(label);
208 }
209 return json;
210 }
211
212 private boolean addLocation(BasicElementConfig cfg, ObjectNode payload) {
213 if (cfg != null) {
214 String locType = cfg.locType();
215 boolean isGeo = Objects.equals(locType, LOC_TYPE_GEO);
216 boolean isGrid = Objects.equals(locType, LOC_TYPE_GRID);
217 if (isGeo || isGrid) {
218 try {
219 ObjectNode loc = objectNode()
220 .put("locType", locType)
221 .put("latOrY", isGeo ? cfg.latitude() : cfg.gridY())
222 .put("longOrX", isGeo ? cfg.longitude() : cfg.gridX());
223 payload.set("location", loc);
224 return true;
225 } catch (NumberFormatException e) {
226 log.warn("Invalid location data: {}", cfg);
227 }
228 }
229 }
230 return false;
231 }
232
233 // Adds meta UI information for the specified object.
234 private void addMetaUi(String id, ObjectNode payload) {
235 ObjectNode meta = metaUi.get(id);
236 if (meta != null) {
237 payload.set("metaUi", meta);
238 }
239 }
240
241 // Produces JSON structure from annotations.
242 private JsonNode props(Annotations annotations) {
243 ObjectNode props = objectNode();
244 if (annotations != null) {
245 for (String key : annotations.keys()) {
246 props.put(key, annotations.value(key));
247 }
248 }
249 return props;
250 }
daniel park128c52c2017-09-04 13:15:51 +0900251 @Override
252 protected Collection<RequestHandler> createRequestHandlers() {
253 return ImmutableSet.of(
254 new DisplayStartHandler(),
255 new DisplayUpdateHandler(),
256 new DisplayStopHandler(),
257 new FlowTraceRequestHandler()
258 );
259 }
260
261 public void setRestUrl(String ipAddress) {
262 restUrl = "http://" + ipAddress + ":8000/trace_request";
263 }
264
265 public String restUrl() {
266 return restUrl;
267 }
268
269 public void setRestAuthInfo(String id, String password) {
270 restAuthInfo = Base64.getEncoder().encodeToString(id.concat(":").concat(password).getBytes());
271 }
272
273 public String restAuthInfo() {
274 return restAuthInfo;
275 }
276
277 private final class DisplayStartHandler extends RequestHandler {
278
279 public DisplayStartHandler() {
280 super(OPENSTACK_NETWORKING_UI_START);
281 }
282
283 @Override
284 public void process(ObjectNode payload) {
285 String mode = string(payload, MODE);
286
287 log.debug("Start Display: mode [{}]", mode);
288 clearState();
289 clearForMode();
290
291 switch (mode) {
292 case MOUSE:
293 currentMode = Mode.MOUSE;
294 sendMouseData();
295 break;
296
297 default:
298 currentMode = Mode.IDLE;
299 break;
300 }
301 }
302 }
303
304 private final class FlowTraceRequestHandler extends RequestHandler {
305 public FlowTraceRequestHandler() {
306 super(FLOW_TRACE_REQUEST);
307 }
308
309 @Override
310 public void process(ObjectNode payload) {
311 String srcIp = string(payload, SRC_IP);
312 String dstIp = string(payload, DST_IP);
313 log.debug("SendEvent called with src IP: {}, dst IP: {}", srcIp, dstIp);
314
315 ObjectNode objectNode = getFlowTraceRequestAsJson(srcIp, dstIp);
316 InputStream byteArrayInputStream
317 = new ByteArrayInputStream(objectNode.toString().getBytes());
318
319 Invocation.Builder builder = getClientBuilder(restUrl);
320
321 if (builder == null) {
322 log.error("Fail to get the client builder for the trace from {} to {}", srcIp, dstIp);
323 return;
324 }
325
326 try {
327 Response response = builder.header(AUTHORIZATION, restAuthInfo.toString())
328 .post(Entity.entity(IOUtils.toString(byteArrayInputStream, StandardCharsets.UTF_8),
329 MediaType.APPLICATION_JSON_TYPE));
330
331 log.debug("Response from server: {}", response);
332
333 if (response.getStatus() != 200) {
334 log.error("FlowTraceRequest failed because of {}", response);
335 }
336
337 } catch (IOException e) {
338 log.error("Exception occured because of {}", e.toString());
339 }
340
341 }
342 }
343
344 private ObjectNode getFlowTraceRequestAsJson(String srcIp, String dstIp) {
345 ObjectMapper mapper = new ObjectMapper();
346 String controllerUrl = HTTP + clusterService.getLocalNode().ip()
347 + OPENSTACK_NETWORKING_UI_RESULT;
348
349 ObjectNode objectNode = mapper.createObjectNode();
350
351 objectNode.put(COMMAND, FLOW_TRACE)
352 .put(REVERSE, false)
353 .put(TRANSACTION_ID, TRANSACTION_VALUE)
354 .put(APP_REST_URL, controllerUrl);
355
356 if (srcIp.equals(dstIp)) {
357 objectNode.putObject(MATCHING_FIELDS)
358 .put(SOURCE_IP, srcIp)
359 .put(DESTINATION_IP, dstIp)
360 .put(TO_GATEWAY, true)
361 .put(IP_PROTOCOL, 1);
362
363 } else {
364 objectNode.putObject(MATCHING_FIELDS)
365 .put(SOURCE_IP, srcIp)
366 .put(DESTINATION_IP, dstIp);
367 }
368 return objectNode;
369 }
370
371 private Invocation.Builder getClientBuilder(String url) {
372 if (Strings.isNullOrEmpty(url)) {
373 log.warn("URL in not set");
374 return null;
375 }
376
377 WebTarget wt = client.target(url);
378
379 return wt.request(MediaType.APPLICATION_JSON_TYPE);
380 }
381
382 private final class DisplayUpdateHandler extends RequestHandler {
383 public DisplayUpdateHandler() {
384 super(OPENSTACK_NETWORKING_UI_UPDATE);
385 }
386
387 @Override
388 public void process(ObjectNode payload) {
389 String id = string(payload, ID);
390 log.debug("Update Display: id [{}]", id);
391 if (!Strings.isNullOrEmpty(id)) {
392 updateForMode(id);
393 } else {
394 clearForMode();
395 }
396 }
397 }
398
399 private final class DisplayStopHandler extends RequestHandler {
400 public DisplayStopHandler() {
401 super(OPENSTACK_NETWORKING_UI_STOP);
402 }
403
404 @Override
405 public void process(ObjectNode payload) {
406 log.debug("Stop Display");
407 clearState();
408 clearForMode();
409 }
410 }
411
412 // === ------------
413
414 private void clearState() {
415 currentMode = Mode.IDLE;
416 elementOfNote = null;
417 }
418
419 private void updateForMode(String id) {
420
421 try {
422 HostId hid = HostId.hostId(id);
423 elementOfNote = hostService.getHost(hid);
424
425 } catch (Exception e) {
426 try {
427 DeviceId did = DeviceId.deviceId(id);
428 elementOfNote = deviceService.getDevice(did);
429
430 } catch (Exception e2) {
431 log.debug("Unable to process ID [{}]", id);
432 elementOfNote = null;
433 }
434 }
435
436 switch (currentMode) {
437 case MOUSE:
438 sendMouseData();
439 break;
440
441 default:
442 break;
443 }
444
445 }
446
447 private void clearForMode() {
448 sendHighlights(new Highlights());
449 }
450
451 private void sendHighlights(Highlights highlights) {
452 sendMessage(TopoJson.highlightsMessage(highlights));
453 }
454
455 public void sendMessagetoUi(String type, ObjectNode payload) {
456 sendMessage(JsonUtils.envelope(type, payload));
457 }
458
459 private int getVni(Host host) {
460 String vni = host.annotations().value(ANNOTATION_SEGMENT_ID);
461
462 return vni == null ? 0 : Integer.valueOf(vni).intValue();
463 }
464
465 private void sendMouseData() {
466 Highlights highlights = new Highlights();
467
468 if (elementOfNote != null && elementOfNote instanceof Device) {
469 DeviceId deviceId = (DeviceId) elementOfNote.id();
470
471 List<OpenstackLink> edgeLinks = edgeLinks(deviceId);
472
473 edgeLinks.forEach(edgeLink -> {
474 highlights.add(edgeLink.highlight(OpenstackLink.RequestType.DEVICE_SELECTED));
475 });
476
477 hostService.getConnectedHosts(deviceId).forEach(host -> {
478 HostHighlight hostHighlight = new HostHighlight(host.id().toString());
479 hostHighlight.setBadge(createBadge(getVni(host)));
480 highlights.add(hostHighlight);
481 });
482
483 sendHighlights(highlights);
484
485 } else if (elementOfNote != null && elementOfNote instanceof Host) {
486
487 HostId hostId = HostId.hostId(elementOfNote.id().toString());
488 if (!hostMadeFromOpenstack(hostId)) {
489 return;
490 }
491
492 List<OpenstackLink> openstackLinks = linksInSameNetwork(hostId);
493
494 openstackLinks.forEach(openstackLink -> {
495 highlights.add(openstackLink.highlight(OpenstackLink.RequestType.HOST_SELECTED));
496 });
497
498 hostHighlightsInSameNetwork(hostId).forEach(highlights::add);
499
500 sendHighlights(highlights);
501
502 }
503 }
504
505 private boolean hostMadeFromOpenstack(HostId hostId) {
506 return hostService.getHost(hostId).annotations()
507 .value(ANNOTATION_NETWORK_ID) == null ? false : true;
508 }
509
510 private String networkId(HostId hostId) {
511 return hostService.getHost(hostId).annotations().value(ANNOTATION_NETWORK_ID);
512 }
513
514 private Set<HostHighlight> hostHighlightsInSameNetwork(HostId hostId) {
515
516 Set<HostHighlight> hostHighlights = Sets.newHashSet();
517 Streams.stream(hostService.getHosts())
518 .filter(host -> isHostInSameNetwork(host, networkId(hostId)))
519 .forEach(host -> {
520 HostHighlight hostHighlight = new HostHighlight(host.id().toString());
521 hostHighlight.setBadge(createBadge(getVni(host)));
522 hostHighlights.add(hostHighlight);
523 });
524
525 return hostHighlights;
526 }
527
528 private List<OpenstackLink> edgeLinks(DeviceId deviceId) {
529 OpenstackLinkMap openstackLinkMap = new OpenstackLinkMap();
530
531 hostService.getConnectedHosts(deviceId).forEach(host -> {
532 openstackLinkMap.add(createEdgeLink(host, true));
533 openstackLinkMap.add(createEdgeLink(host, false));
534 });
535
536 List<OpenstackLink> edgeLinks = Lists.newArrayList();
537
538 openstackLinkMap.biLinks().forEach(edgeLinks::add);
539
540 return edgeLinks;
541 }
542
543 private List<OpenstackLink> linksInSameNetwork(HostId hostId) {
544 OpenstackLinkMap linkMap = new OpenstackLinkMap();
545
546 Streams.stream(hostService.getHosts())
547 .filter(host -> isHostInSameNetwork(host, networkId(hostId)))
548 .forEach(host -> {
549 linkMap.add(createEdgeLink(host, true));
550 linkMap.add(createEdgeLink(host, false));
551
552 Set<Path> paths = pathService.getPaths(hostId,
553 host.id());
554
555 if (!paths.isEmpty()) {
556 paths.forEach(path -> path.links().forEach(linkMap::add));
557 }
558 });
559
560 List<OpenstackLink> openstackLinks = Lists.newArrayList();
561
562 linkMap.biLinks().forEach(openstackLinks::add);
563
564 return openstackLinks;
565 }
566
567 private boolean isHostInSameNetwork(Host host, String networkId) {
568 return hostService.getHost(host.id()).annotations()
569 .value(ANNOTATION_NETWORK_ID).equals(networkId);
570 }
571
572 private NodeBadge createBadge(int n) {
573 return NodeBadge.number(Status.INFO, n, "Openstack Node");
574 }
575}