Allow interface service to provide all interface that contain given IP

Change-Id: Ic6bc02585ed66e598505b65e7d4f29828dac36a1
diff --git a/apps/vpls/src/test/java/org/onosproject/vpls/VplsNeighbourHandlerTest.java b/apps/vpls/src/test/java/org/onosproject/vpls/VplsNeighbourHandlerTest.java
index f461492..412e937 100644
--- a/apps/vpls/src/test/java/org/onosproject/vpls/VplsNeighbourHandlerTest.java
+++ b/apps/vpls/src/test/java/org/onosproject/vpls/VplsNeighbourHandlerTest.java
@@ -820,5 +820,12 @@
                     .findFirst()
                     .orElse(null);
         }
+
+        @Override
+        public Set<Interface> getMatchingInterfaces(IpAddress ip) {
+            return availableInterfaces.stream()
+                    .filter(intf -> intf.ipAddressesList().contains(ip))
+                    .collect(Collectors.toSet());
+        }
     }
 }
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
index f959a5c..4de235f 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
@@ -79,4 +79,13 @@
      * @return interface
      */
     Interface getMatchingInterface(IpAddress ip);
+
+    /**
+     * Returns all interfaces that have an address that is in the same
+     * subnet as the given IP address.
+     *
+     * @param ip IP address to find matching subnet interface for
+     * @return a set of interfaces
+     */
+    Set<Interface> getMatchingInterfaces(IpAddress ip);
 }
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/intf/InterfaceServiceAdapter.java b/incubator/api/src/test/java/org/onosproject/incubator/net/intf/InterfaceServiceAdapter.java
index bc91019..8768b1f 100644
--- a/incubator/api/src/test/java/org/onosproject/incubator/net/intf/InterfaceServiceAdapter.java
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/intf/InterfaceServiceAdapter.java
@@ -57,6 +57,11 @@
     }
 
     @Override
+    public Set<Interface> getMatchingInterfaces(IpAddress ip) {
+        return null;
+    }
+
+    @Override
     public void addListener(InterfaceListener listener) {
 
     }
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
index 2b4ecbf..05ef6a7 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
@@ -42,10 +42,12 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.stream.Stream;
 
 import static java.util.stream.Collectors.collectingAndThen;
 import static java.util.stream.Collectors.toSet;
@@ -125,8 +127,8 @@
     public Set<Interface> getInterfacesByIp(IpAddress ip) {
         return interfaces.values()
                 .stream()
-                .flatMap(set -> set.stream())
-                .filter(intf -> intf.ipAddresses()
+                .flatMap(Collection::stream)
+                .filter(intf -> intf.ipAddressesList()
                         .stream()
                         .anyMatch(ia -> ia.ipAddress().equals(ip)))
                 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
@@ -134,22 +136,28 @@
 
     @Override
     public Interface getMatchingInterface(IpAddress ip) {
-        Optional<Interface> match = interfaces.values()
-                .stream()
-                .flatMap(set -> set.stream())
-                .filter(intf -> intf.ipAddresses()
-                        .stream()
-                        .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
-                .findFirst();
+        return getMatchingInterfacesStream(ip).findFirst().orElse(null);
+    }
 
-        return match.orElse(null);
+    @Override
+    public Set<Interface> getMatchingInterfaces(IpAddress ip) {
+        return getMatchingInterfacesStream(ip).collect(toSet());
+    }
+
+    private Stream<Interface> getMatchingInterfacesStream(IpAddress ip) {
+        return interfaces.values()
+                .stream()
+                .flatMap(Collection::stream)
+                .filter(intf -> intf.ipAddressesList()
+                        .stream()
+                        .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)));
     }
 
     @Override
     public Set<Interface> getInterfacesByVlan(VlanId vlan) {
         return interfaces.values()
                 .stream()
-                .flatMap(set -> set.stream())
+                .flatMap(Collection::stream)
                 .filter(intf -> intf.vlan().equals(vlan))
                 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
     }