Add a method to check the type of resource

Change-Id: If4b44688c0a93a43dfa9a4c2cb77c52599159751
diff --git a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
index d6e637f..3b8ca89 100644
--- a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
@@ -24,6 +24,7 @@
 import java.util.Arrays;
 import java.util.Collections;
 
+import com.google.common.collect.Iterables;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
@@ -158,7 +159,7 @@
             if (r instanceof ContinuousResource) {
                 // non-aggregatable terminal node
                 nonAggregatable.add(r);
-            } else if (aggregatableTypes.contains(r.last().getClass())) {
+            } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
                 // aggregatable & terminal node
                 String className = r.last().getClass().getSimpleName();
                 aggregatables.put(className, r);
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
index fcfcdd5..a55124e 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
@@ -48,6 +48,14 @@
         return id;
     }
 
+    @Override
+    public boolean isTypeOf(Class<?> type) {
+        checkNotNull(type);
+
+        String typeName = (String) id.components().get(id.components().size() - 1);
+        return typeName.equals(type.getCanonicalName());
+    }
+
     /**
      * The user of this methods must receive the return value as Double or double.
      * Otherwise, this methods throws an exception.
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
index 867cfbf..6ce51d3 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
@@ -45,6 +45,17 @@
         return id;
     }
 
+    @Override
+    public boolean isTypeOf(Class<?> type) {
+        checkNotNull(type);
+
+        if (isRoot()) {
+            return false;
+        }
+
+        return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass());
+    }
+
     /**
      * The user of this methods must receive the return value as the correct type.
      * Otherwise, this methods throws an exception.
@@ -78,6 +89,10 @@
         return id.components().get(id.components().size() - 1);
     }
 
+    private boolean isRoot() {
+        return id.equals(ResourceId.ROOT);
+    }
+
     @Override
     public DiscreteResource child(Object child) {
         checkArgument(!(child instanceof Class<?>));
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
index 1e3e61a..f2a0c73 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
@@ -48,6 +48,14 @@
     ResourceId id();
 
     /**
+     * Checks if the type of this instance is the specified type.
+     *
+     * @param type type of resource to be checked
+     * @return true if this resource is the type of the specified type. Otherwise, false.
+     */
+    boolean isTypeOf(Class<?> type);
+
+    /**
      * Checks if the type of this instance is the sub-type of the specified type.
      *
      * @param ancestor type of resource to be checked.
diff --git a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
index a92bbd6..bc6fd29 100644
--- a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
+++ b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
@@ -90,6 +90,19 @@
     }
 
     @Test
+    public void testTypeOf() {
+        DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
+        assertThat(discrete.isTypeOf(DeviceId.class), is(false));
+        assertThat(discrete.isTypeOf(PortNumber.class), is(false));
+        assertThat(discrete.isTypeOf(VlanId.class), is(true));
+
+        ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
+        assertThat(continuous.isTypeOf(DeviceId.class), is(false));
+        assertThat(continuous.isTypeOf(PortNumber.class), is(false));
+        assertThat(continuous.isTypeOf(Bandwidth.class), is(true));
+    }
+
+    @Test
     public void testSubTypeOf() {
         DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
         assertThat(discrete.isSubTypeOf(DeviceId.class), is(true));
diff --git a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
index a8ddf9a..16bbad3 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
@@ -429,7 +429,7 @@
         }
 
         Stream<DiscreteResource> discrete = children.value().stream()
-                .filter(x -> x.last().getClass().equals(cls))
+                .filter(x -> x.isTypeOf(cls))
                 .filter(x -> x instanceof DiscreteResource)
                 .map(x -> ((DiscreteResource) x))
                 .filter(x -> discreteConsumers.containsKey(x.id()));