daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworkingui; |
| 17 | |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.base.Strings; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.collect.Lists; |
| 25 | import com.google.common.collect.Sets; |
| 26 | import com.google.common.collect.Streams; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 27 | import org.apache.commons.io.IOUtils; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 28 | import org.onlab.osgi.ServiceDirectory; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 29 | import org.onlab.util.DefaultHashMap; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 31 | import org.onosproject.cluster.NodeId; |
| 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.net.AnnotationKeys; |
| 34 | import org.onosproject.net.Annotations; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Element; |
| 38 | import org.onosproject.net.Host; |
| 39 | import org.onosproject.net.HostId; |
| 40 | import org.onosproject.net.Path; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigService; |
| 42 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 43 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 44 | import org.onosproject.net.device.DeviceEvent; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceService; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 46 | import org.onosproject.net.driver.Driver; |
| 47 | import org.onosproject.net.driver.DriverService; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 48 | import org.onosproject.net.host.HostService; |
| 49 | import org.onosproject.net.topology.PathService; |
| 50 | import org.onosproject.ui.JsonUtils; |
| 51 | import org.onosproject.ui.RequestHandler; |
| 52 | import org.onosproject.ui.UiConnection; |
| 53 | import org.onosproject.ui.UiMessageHandler; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 54 | import org.onosproject.ui.topo.Highlights; |
| 55 | import org.onosproject.ui.topo.HostHighlight; |
| 56 | import org.onosproject.ui.topo.NodeBadge; |
| 57 | import org.onosproject.ui.topo.NodeBadge.Status; |
| 58 | import org.onosproject.ui.topo.TopoJson; |
| 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
| 62 | import javax.ws.rs.client.Client; |
| 63 | import javax.ws.rs.client.ClientBuilder; |
| 64 | import javax.ws.rs.client.Entity; |
| 65 | import javax.ws.rs.client.Invocation; |
| 66 | import javax.ws.rs.client.WebTarget; |
| 67 | import javax.ws.rs.core.MediaType; |
| 68 | import javax.ws.rs.core.Response; |
| 69 | import java.io.ByteArrayInputStream; |
| 70 | import java.io.IOException; |
| 71 | import java.io.InputStream; |
| 72 | import java.nio.charset.StandardCharsets; |
| 73 | import java.util.Base64; |
| 74 | import java.util.Collection; |
| 75 | import java.util.List; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 76 | import java.util.Map; |
| 77 | import java.util.Objects; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 78 | import java.util.Set; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 79 | import java.util.concurrent.ConcurrentHashMap; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 80 | |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 81 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 82 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 83 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 84 | import static org.onosproject.net.Device.Type.SWITCH; |
| 85 | import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GEO; |
| 86 | import static org.onosproject.net.config.basics.BasicElementConfig.LOC_TYPE_GRID; |
| 87 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * OpenStack Networking UI message handler. |
| 91 | */ |
| 92 | public 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 Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 129 | private DriverService driverService; |
| 130 | private MastershipService mastershipService; |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 131 | 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 Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 136 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 137 | private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT = |
| 138 | new DefaultHashMap<>("updateDevice"); |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 139 | |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 140 | static { |
| 141 | DEVICE_EVENT.put(DEVICE_REMOVED, "removeDevice"); |
| 142 | } |
daniel park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 143 | |
| 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 Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 151 | 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 park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Park | 819f4e8 | 2018-07-02 14:22:31 +0900 | [diff] [blame] | 158 | 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 park | 128c52c | 2017-09-04 13:15:51 +0900 | [diff] [blame] | 251 | @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 | } |