Sho SHIMIZU
Committed by Gerrit Code Review

Hide underlying components from public use

- Remove components() method from Resource
- Add isTypeOf() method to Resource

Change-Id: I390eb6057fc7e2c3e93eb2a18d161f661ad6c180
......@@ -18,7 +18,6 @@ package org.onosproject.net.newresource;
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 @@ public final class ContinuousResource implements Resource {
}
@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
......
......@@ -18,7 +18,6 @@ package org.onosproject.net.newresource;
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 @@ public final class DiscreteResource implements Resource {
}
@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
......
......@@ -17,7 +17,6 @@ package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import java.util.List;
import java.util.Optional;
/**
......@@ -42,11 +41,13 @@ public interface Resource {
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.
......
......@@ -24,7 +24,6 @@ import org.onosproject.net.PortNumber;
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 @@ public class ResourceTest {
}
@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 @@ public class ResourceTest {
}
@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();
......
......@@ -61,7 +61,6 @@ import java.util.Collection;
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 @@ public class ObjectiveTracker implements ObjectiveTrackerService {
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;
......