Add method to get available resources filtered by its type

Change-Id: I034bdcf4e9a399af6c68c7ed7f53185bba9b2e27
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
index 64ec60a..99733cd 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
@@ -163,6 +163,27 @@
     Set<Resource> getAvailableResources(DiscreteResourceId parent);
 
     /**
+     * Returns available resources which are child resources of the specified parent and
+     * whose type is the specified type.
+     *
+     * @param parent parent resource ID
+     * @param cls class to specify a type of resource
+     * @param <T> type of the resource
+     * @return available resources of the specified type under the specified parent resource
+     */
+    <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls);
+
+    /**
+     * Returns available resource values which are the values of the child resource of
+     * the specified parent and whose type is the specified type.
+     *
+     * @param parent parent resource ID
+     * @param cls class to specify a type of resource
+     * @param <T> type of the resource
+     * @return available resource value of the specified type under the specified parent resource
+     */
+    <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls);
+    /**
      * Returns resources registered under the specified resource.
      *
      * @param parent parent resource ID
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
index 71ae925..28b8a04 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
@@ -17,7 +17,6 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Sets;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -27,7 +26,6 @@
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.MplsLabel;
 import org.onlab.packet.VlanId;
-import org.onlab.util.Tools;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
@@ -158,9 +156,9 @@
     }
 
     private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
-        return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
-                .flatMap(x -> Tools.stream(x.valueAs(MplsLabel.class)))
-                .collect(Collectors.toSet());
+        return resourceService.getAvailableResourceValues(
+                Resources.discrete(cp.deviceId(), cp.port()).id(),
+                MplsLabel.class);
     }
 
     private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
index b113b07..0430f99 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
@@ -24,7 +24,6 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onlab.util.Frequency;
-import org.onlab.util.Tools;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.ChannelSpacing;
 import org.onosproject.net.ConnectPoint;
@@ -217,9 +216,7 @@
                         Resources.discrete(x.src().deviceId(), x.src().port()).id(),
                         Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
                 ))
-                .map(resourceService::getAvailableResources)
-                .map(x -> x.stream()
-                        .flatMap(r -> Tools.stream(r.valueAs(OchSignal.class))).collect(Collectors.toList()))
+                .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
                 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
                 .reduce(Sets::intersection)
                 .orElse(Collections.emptySet());
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
index a86138a..7d6edcb 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java
@@ -23,7 +23,6 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onlab.packet.VlanId;
-import org.onlab.util.Tools;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.net.ConnectPoint;
@@ -299,9 +298,9 @@
     }
 
     private Set<VlanId> findVlanId(ConnectPoint cp) {
-        return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
-                .flatMap(x -> Tools.stream(x.valueAs(VlanId.class)))
-                .collect(Collectors.toSet());
+        return resourceService.getAvailableResourceValues(
+                Resources.discrete(cp.deviceId(), cp.port()).id(),
+                VlanId.class);
     }
 
     private boolean isLast(List<Link> links, int i) {
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
index 1138fd6..88c3ab6 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
@@ -24,6 +24,7 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.GuavaCollectors;
+import org.onlab.util.Tools;
 import org.onosproject.event.AbstractListenerManager;
 import org.onosproject.net.newresource.DiscreteResourceId;
 import org.onosproject.net.newresource.ResourceAdminService;
@@ -150,6 +151,28 @@
     }
 
     @Override
+    public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
+        checkNotNull(parent);
+        checkNotNull(cls);
+
+        // naive implementation
+        return getAvailableResources(parent).stream()
+                .filter(resource -> resource.isTypeOf(cls))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
+        checkNotNull(parent);
+        checkNotNull(cls);
+
+        // naive implementation
+        return getAvailableResources(parent).stream()
+                .flatMap(resource -> Tools.stream(resource.valueAs(cls)))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
     public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
         checkNotNull(parent);
 
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java
index 4083f5b..424a1e1 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MockResourceService.java
@@ -19,6 +19,7 @@
 import com.google.common.collect.ImmutableSet;
 import org.onlab.packet.MplsLabel;
 import org.onlab.packet.VlanId;
+import org.onlab.util.Tools;
 import org.onosproject.net.newresource.ContinuousResourceId;
 import org.onosproject.net.newresource.DiscreteResource;
 import org.onosproject.net.newresource.DiscreteResourceId;
@@ -111,6 +112,21 @@
     }
 
     @Override
+    public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
+        return getAvailableResources(parent).stream()
+                .filter(x -> x.isTypeOf(cls))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
+        return getAvailableResources(parent).stream()
+                .filter(x -> x.isTypeOf(cls))
+                .flatMap(x -> Tools.stream(x.valueAs(cls)))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
     public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
         return getAvailableResources(parent);
     }