Hide underlying components from public use
- Remove components() method from Resource
- Add isTypeOf() method to Resource
Change-Id: I390eb6057fc7e2c3e93eb2a18d161f661ad6c180
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 431fcd8..b2e5395 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
@@ -18,7 +18,6 @@
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
-import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -74,8 +73,15 @@
}
@Override
- public List<Object> components() {
- return id.components();
+ public boolean isTypeOf(Class<?> ancestorType) {
+ String typeName = (String) id.components().get(id.components().size() - 1);
+ boolean foundInLeaf = typeName.equals(ancestorType.getCanonicalName());
+ boolean foundInAncestor = id.components().subList(0, id.components().size()).stream()
+ .map(Object::getClass)
+ .filter(x -> x.equals(ancestorType))
+ .findAny()
+ .isPresent();
+ return foundInAncestor || foundInLeaf;
}
@Override
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 4ed281c..c120e59 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
@@ -18,7 +18,6 @@
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
-import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -64,8 +63,12 @@
}
@Override
- public List<Object> components() {
- return id.components();
+ public boolean isTypeOf(Class<?> ancestorType) {
+ return id.components().stream()
+ .map(Object::getClass)
+ .filter(x -> x.equals(ancestorType))
+ .findAny()
+ .isPresent();
}
@Override
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 199e9c5..fc696d5 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
@@ -17,7 +17,6 @@
import com.google.common.annotations.Beta;
-import java.util.List;
import java.util.Optional;
/**
@@ -42,11 +41,13 @@
DiscreteResource ROOT = new DiscreteResource();
/**
- * Returns the components of this resource.
+ * Checks if the type of this instance is the specified type.
*
- * @return the components of this resource
+ * @param ancestorType type of resource to be checked.
+ * @return true if this resource is under the resource whose type is the given type.
*/
- List<Object> components();
+ // TODO: find more proper name
+ boolean isTypeOf(Class<?> ancestorType);
/**
* Returns the volume of this resource.
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 59f327f..838c898 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
@@ -24,7 +24,6 @@
import java.util.Optional;
-import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@@ -53,13 +52,6 @@
}
@Test
- public void testComponents() {
- Resource port = Resources.discrete(D1, P1).resource();
-
- assertThat(port.components(), contains(D1, P1));
- }
-
- @Test
public void testIdEquality() {
ResourceId id1 = Resources.discrete(D1, P1, VLAN1).id();
ResourceId sameAsId1 = Resources.discrete(D1, P1, VLAN1).id();
@@ -98,6 +90,21 @@
}
@Test
+ public void testTypeOf() {
+ DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
+ assertThat(discrete.isTypeOf(DeviceId.class), is(true));
+ assertThat(discrete.isTypeOf(PortNumber.class), is(true));
+ assertThat(discrete.isTypeOf(VlanId.class), is(true));
+ assertThat(discrete.isTypeOf(Bandwidth.class), is(false));
+
+ ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
+ assertThat(continuous.isTypeOf(DeviceId.class), is(true));
+ assertThat(continuous.isTypeOf(PortNumber.class), is(true));
+ assertThat(continuous.isTypeOf(Bandwidth.class), is(true));
+ assertThat(continuous.isTypeOf(VlanId.class), is(false));
+ }
+
+ @Test
public void testBase() {
Resource resource = Resources.discrete(D1).resource();
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java b/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java
index ef1c0fd..76e3ef6 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java
@@ -61,7 +61,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
-import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -303,11 +302,7 @@
private class InternalResourceListener implements ResourceListener {
@Override
public void event(ResourceEvent event) {
- Optional<Class<?>> deviceEvent = event.subject().components().stream()
- .map(Object::getClass)
- .filter(x -> x == PortNumber.class)
- .findFirst();
- if (deviceEvent.isPresent()) {
+ if (event.subject().isTypeOf(PortNumber.class)) {
executorService.execute(() -> {
if (delegate == null) {
return;