Simon Hunt
Committed by Gerrit Code Review

ONOS-2186 - GUI Topo Overlay - (WIP)

- added devicesWithHover(), hostsWithHover(), hovered() to NodeSelection.
- wrote unit tests for NodeSelection.

Change-Id: I6dca0f4f0a4ce2412438c8411102034969ef4343
......@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.net.Device;
import org.onosproject.net.Element;
import org.onosproject.net.Host;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.host.HostService;
......@@ -55,10 +56,12 @@ public class NodeSelection {
private final Set<Device> devices = new HashSet<>();
private final Set<Host> hosts = new HashSet<>();
private Element hovered;
/**
* Creates a node selection entity, from the given payload, using the
* supplied device and host services.
* supplied device and host services. Note that if a device or host was
* hovered over by the mouse, it is available via {@link #hovered()}.
*
* @param payload message payload
* @param deviceService device service
......@@ -73,25 +76,24 @@ public class NodeSelection {
ids = extractIds(payload);
hover = extractHover(payload);
// start by extracting the hovered element if any
if (isNullOrEmpty(hover)) {
hovered = null;
} else {
setHoveredElement();
}
// now go find the devices and hosts that are in the selection list
Set<String> unmatched = findDevices(ids);
unmatched = findHosts(unmatched);
if (unmatched.size() > 0) {
log.debug("Skipping unmatched IDs {}", unmatched);
}
if (!isNullOrEmpty(hover)) {
unmatched = new HashSet<>();
unmatched.add(hover);
unmatched = findDevices(unmatched);
unmatched = findHosts(unmatched);
if (unmatched.size() > 0) {
log.debug("Skipping unmatched HOVER {}", unmatched);
}
}
}
/**
* Returns a view of the selected devices.
* Returns a view of the selected devices (hover not included).
*
* @return selected devices
*/
......@@ -100,7 +102,24 @@ public class NodeSelection {
}
/**
* Returns a view of the selected hosts.
* Returns a view of the selected devices, including the hovered device
* if there was one.
*
* @return selected (plus hovered) devices
*/
public Set<Device> devicesWithHover() {
Set<Device> withHover;
if (hovered != null && hovered instanceof Device) {
withHover = new HashSet<>(devices);
withHover.add((Device) hovered);
} else {
withHover = devices;
}
return Collections.unmodifiableSet(withHover);
}
/**
* Returns a view of the selected hosts (hover not included).
*
* @return selected hosts
*/
......@@ -109,6 +128,33 @@ public class NodeSelection {
}
/**
* Returns a view of the selected hosts, including the hovered host
* if thee was one.
*
* @return selected (plus hovered) hosts
*/
public Set<Host> hostsWithHover() {
Set<Host> withHover;
if (hovered != null && hovered instanceof Host) {
withHover = new HashSet<>(hosts);
withHover.add((Host) hovered);
} else {
withHover = hosts;
}
return Collections.unmodifiableSet(withHover);
}
/**
* Returns the element (host or device) over which the mouse was hovering,
* or null.
*
* @return element hovered over
*/
public Element hovered() {
return hovered;
}
/**
* Returns true if nothing is selected.
*
* @return true if nothing selected
......@@ -146,6 +192,26 @@ public class NodeSelection {
return JsonUtils.string(payload, HOVER);
}
private void setHoveredElement() {
Set<String> unmatched;
unmatched = new HashSet<>();
unmatched.add(hover);
unmatched = findDevices(unmatched);
if (devices.size() == 1) {
hovered = devices.iterator().next();
devices.clear();
} else {
unmatched = findHosts(unmatched);
if (hosts.size() == 1) {
hovered = hosts.iterator().next();
hosts.clear();
} else {
hovered = null;
log.debug("Skipping unmatched HOVER {}", unmatched);
}
}
}
private Set<String> findDevices(Set<String> ids) {
Set<String> unmatched = new HashSet<>();
Device device;
......@@ -156,9 +222,9 @@ public class NodeSelection {
if (device != null) {
devices.add(device);
} else {
log.debug("Device with ID {} not found", id);
unmatched.add(id);
}
} catch (IllegalArgumentException e) {
} catch (Exception e) {
unmatched.add(id);
}
}
......@@ -175,9 +241,9 @@ public class NodeSelection {
if (host != null) {
hosts.add(host);
} else {
log.debug("Host with ID {} not found", id);
unmatched.add(id);
}
} catch (IllegalArgumentException e) {
} catch (Exception e) {
unmatched.add(id);
}
}
......
......@@ -186,7 +186,7 @@ public class TrafficMonitor {
switch (mode) {
case DEV_LINK_FLOWS:
// only care about devices (not hosts)
if (selectedNodes.devices().isEmpty()) {
if (selectedNodes.devicesWithHover().isEmpty()) {
sendClearAll();
} else {
scheduleTask();
......@@ -371,11 +371,11 @@ public class TrafficMonitor {
private Highlights deviceLinkFlows() {
Highlights highlights = new Highlights();
if (selectedNodes != null && !selectedNodes.devices().isEmpty()) {
if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
// capture flow counts on bilinks
TrafficLinkMap linkMap = new TrafficLinkMap();
for (Device device : selectedNodes.devices()) {
for (Device device : selectedNodes.devicesWithHover()) {
Map<Link, Integer> counts = getLinkFlowCounts(device.id());
for (Link link : counts.keySet()) {
TrafficLink tlink = linkMap.add(link);
......
......@@ -50,7 +50,7 @@ public class IntentSelection {
*/
public IntentSelection(NodeSelection nodes, TopoIntentFilter filter) {
this.nodes = nodes;
intents = filter.findPathIntents(nodes.hosts(), nodes.devices());
intents = filter.findPathIntents(nodes.hostsWithHover(), nodes.devicesWithHover());
if (intents.size() == 1) {
index = 0; // pre-select a single intent
}
......