Fix for ONOS-291. Highlighting intents in ONOS GUI for selected links.

Change-Id: I757aa40b96d92014fa2d720539da20dd309ec9b1
diff --git a/core/api/src/main/java/org/onosproject/ui/topo/NodeSelection.java b/core/api/src/main/java/org/onosproject/ui/topo/NodeSelection.java
index b87d0b7..ce5680e 100644
--- a/core/api/src/main/java/org/onosproject/ui/topo/NodeSelection.java
+++ b/core/api/src/main/java/org/onosproject/ui/topo/NodeSelection.java
@@ -19,11 +19,14 @@
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Device;
 import org.onosproject.net.Element;
 import org.onosproject.net.Host;
+import org.onosproject.net.Link;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.host.HostService;
+import org.onosproject.net.link.LinkService;
 import org.onosproject.ui.JsonUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,11 +36,12 @@
 import java.util.Set;
 
 import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
 import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.HostId.hostId;
 
 /**
- * Encapsulates a selection of devices and/or hosts from the topology view.
+ * Encapsulates a selection of devices, hosts and links from the topology view.
  */
 public class NodeSelection {
 
@@ -46,31 +50,38 @@
 
     private static final String IDS = "ids";
     private static final String HOVER = "hover";
+    private static final String LINK_ID_DELIM = "-";
 
     private final DeviceService deviceService;
     private final HostService hostService;
+    private final LinkService linkService;
 
     private final Set<String> ids;
     private final String hover;
 
     private final Set<Device> devices = new HashSet<>();
     private final Set<Host> hosts = new HashSet<>();
+    private final Set<Link> links = new HashSet<>();
     private Element hovered;
 
     /**
      * Creates a node selection entity, from the given payload, using the
-     * supplied device and host services. Note that if a device or host was
-     * hovered over by the mouse, it is available via {@link #hovered()}.
+     * supplied link, device and host services. Note that if a link, device
+     * or host was hovered over by the mouse, it is available
+     * via {@link #hovered()}.
      *
      * @param payload message payload
      * @param deviceService device service
      * @param hostService host service
+     * @param linkService link service
      */
     public NodeSelection(ObjectNode payload,
                          DeviceService deviceService,
-                         HostService hostService) {
+                         HostService hostService,
+                         LinkService linkService) {
         this.deviceService = deviceService;
         this.hostService = hostService;
+        this.linkService = linkService;
 
         ids = extractIds(payload);
         hover = extractHover(payload);
@@ -82,8 +93,9 @@
             setHoveredElement();
         }
 
-        // now go find the devices and hosts that are in the selection list
-        Set<String> unmatched = findDevices(ids);
+        // now go find the links, devices and hosts that are in the selection list
+        Set<String> unmatched = findLinks(ids);
+        unmatched = findDevices(unmatched);
         unmatched = findHosts(unmatched);
         if (unmatched.size() > 0) {
             log.debug("Skipping unmatched IDs {}", unmatched);
@@ -101,6 +113,15 @@
     }
 
     /**
+     * Returns a view of the selected links (hover not included).
+     *
+     * @return selected links
+     */
+    public Set<Link> links() {
+        return Collections.unmodifiableSet(links);
+    }
+
+    /**
      * Returns a view of the selected devices, including the hovered device
      * if there was one.
      *
@@ -144,7 +165,24 @@
     }
 
     /**
-     * Returns the element (host or device) over which the mouse was hovering,
+     * Returns a view of the selected links, including the hovered link
+     * if thee was one.
+     *
+     * @return selected (plus hovered) links
+     */
+    public Set<Link> linksWithHover() {
+        Set<Link> withHover;
+        if (hovered != null && hovered instanceof Link) {
+            withHover = new HashSet<>(links);
+            withHover.add((Link) hovered);
+        } else {
+            withHover = links;
+        }
+        return Collections.unmodifiableSet(withHover);
+    }
+
+    /**
+     * Returns the element (link, host or device) over which the mouse was hovering,
      * or null.
      *
      * @return element hovered over
@@ -159,7 +197,7 @@
      * @return true if nothing selected
      */
     public boolean none() {
-        return devices().size() == 0 && hosts().size() == 0;
+        return devices().isEmpty() && hosts().isEmpty() && links().isEmpty();
     }
 
     @Override
@@ -169,6 +207,7 @@
                 ", hover='" + hover + '\'' +
                 ", #devices=" + devices.size() +
                 ", #hosts=" + hosts.size() +
+                ", #links=" + links.size() +
                 '}';
     }
 
@@ -248,4 +287,34 @@
         }
         return unmatched;
     }
+
+    private Set<String> findLinks(Set<String> ids) {
+        Set<String> unmatched = new HashSet<>();
+        ConnectPoint cpSrc, cpDst;
+        Link link;
+
+        for (String id : ids) {
+            try {
+                String[] connectPoints = id.split(LINK_ID_DELIM);
+                if (connectPoints.length != 2) {
+                    unmatched.add(id);
+                    continue;
+                }
+
+                cpSrc = deviceConnectPoint(connectPoints[0]);
+                cpDst = deviceConnectPoint(connectPoints[1]);
+                link = linkService.getLink(cpSrc, cpDst);
+
+                if (link != null) {
+                    links.add(link);
+                } else {
+                    unmatched.add(id);
+                }
+
+            } catch (Exception e) {
+                unmatched.add(id);
+            }
+        }
+        return unmatched;
+    }
 }
diff --git a/core/api/src/test/java/org/onosproject/ui/topo/NodeSelectionTest.java b/core/api/src/test/java/org/onosproject/ui/topo/NodeSelectionTest.java
index 6dbaf15..97f9843 100644
--- a/core/api/src/test/java/org/onosproject/ui/topo/NodeSelectionTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/topo/NodeSelectionTest.java
@@ -21,18 +21,24 @@
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableSet;
 import org.junit.Test;
+import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultDevice;
 import org.onosproject.net.DefaultHost;
+import org.onosproject.net.DefaultLink;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
+import org.onosproject.net.Link;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.device.DeviceServiceAdapter;
 import org.onosproject.net.host.HostService;
 import org.onosproject.net.host.HostServiceAdapter;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.net.link.LinkServiceAdapter;
 
 import static org.junit.Assert.*;
+import static org.onosproject.net.Link.Type.DIRECT;
 
 /**
  * Unit tests for {@link NodeSelection}.
@@ -51,20 +57,31 @@
         }
     }
 
+    private static class FakeLink extends DefaultLink {
+        FakeLink(ConnectPoint src, ConnectPoint dst) {
+            super(null, src, dst, DIRECT, Link.State.ACTIVE);
+        }
+    }
+
     private final ObjectMapper mapper = new ObjectMapper();
 
     private static final String IDS = "ids";
     private static final String HOVER = "hover";
 
-    private static final DeviceId DEVICE_1_ID = DeviceId.deviceId("Device-1");
-    private static final DeviceId DEVICE_2_ID = DeviceId.deviceId("Device-2");
+    private static final DeviceId DEVICE_1_ID = DeviceId.deviceId("Device1");
+    private static final DeviceId DEVICE_2_ID = DeviceId.deviceId("Device2");
     private static final HostId HOST_A_ID = HostId.hostId("aa:aa:aa:aa:aa:aa/1");
     private static final HostId HOST_B_ID = HostId.hostId("bb:bb:bb:bb:bb:bb/2");
+    private static final String LINK_1_ID = "Device1/1-Device2/2";
+    private static final ConnectPoint CP_SRC = ConnectPoint.deviceConnectPoint("Device1/1");
+    private static final ConnectPoint CP_DST = ConnectPoint.deviceConnectPoint("Device2/2");
 
     private static final Device DEVICE_1 = new FakeDevice(DEVICE_1_ID);
     private static final Device DEVICE_2 = new FakeDevice(DEVICE_2_ID);
     private static final Host HOST_A = new FakeHost(HOST_A_ID);
     private static final Host HOST_B = new FakeHost(HOST_B_ID);
+    private static final Link LINK_A = new FakeLink(CP_SRC, CP_DST);
+    private static final Link LINK_B = new FakeLink(CP_DST, CP_SRC);
 
     // ==================
     // == FAKE SERVICES
@@ -94,8 +111,21 @@
         }
     }
 
+    private static class FakeLinks extends LinkServiceAdapter {
+        @Override
+        public Link getLink(ConnectPoint src, ConnectPoint dst) {
+            if (CP_SRC.equals(src) && CP_DST.equals(dst)) {
+                return LINK_A;
+            } else if (CP_SRC.equals(dst) && CP_DST.equals(src)) {
+                return LINK_B;
+            }
+            return null;
+        }
+    }
+
     private DeviceService deviceService = new FakeDevices();
     private HostService hostService = new FakeHosts();
+    private LinkService linkService = new FakeLinks();
 
     private NodeSelection ns;
 
@@ -108,7 +138,7 @@
     }
 
     private NodeSelection createNodeSelection(ObjectNode payload) {
-        return new NodeSelection(payload, deviceService, hostService);
+        return new NodeSelection(payload, deviceService, hostService, linkService);
     }
 
     // selection JSON payload creation methods
@@ -134,6 +164,13 @@
         ids.add(HOST_A_ID.toString());
         return payload;
     }
+    private ObjectNode oneLinkSelected() {
+        ObjectNode payload = objectNode();
+        ArrayNode ids = arrayNode();
+        payload.set(IDS, ids);
+        ids.add(LINK_1_ID.toString());
+        return payload;
+    }
 
     private ObjectNode twoHostsOneDeviceSelected() {
         ObjectNode payload = objectNode();
@@ -204,6 +241,21 @@
     }
 
     @Test
+    public void oneLink() {
+        ns = createNodeSelection(oneLinkSelected());
+        assertEquals("unexpected devices", 0, ns.devices().size());
+        assertEquals("unexpected devices w/hover", 0, ns.devicesWithHover().size());
+        assertEquals("unexpected hosts", 0, ns.hosts().size());
+        assertEquals("unexpected hosts w/hover", 0, ns.hostsWithHover().size());
+        assertEquals("missing link", 1, ns.links().size());
+        assertTrue("missing link A", ns.links().contains(LINK_A));
+        assertEquals("missing link w/hover", 1, ns.linksWithHover().size());
+        assertTrue("missing link A w/hover", ns.linksWithHover().contains(LINK_A));
+        assertFalse("unexpected selection", ns.none());
+        assertNull("hover?", ns.hovered());
+    }
+
+    @Test
     public void twoHostsOneDevice() {
         ns = createNodeSelection(twoHostsOneDeviceSelected());
         assertEquals("missing device", 1, ns.devices().size());