Sho SHIMIZU
Committed by Gerrit Code Review

Add a method to get resource type name: simpleTypeName()

last() is removed as the new method takes over its role

Change-Id: Id3737529787da5d5bb513355cdbc443f2b7b17e2
......@@ -115,7 +115,7 @@ public class ResourcesCommand extends AbstractShellCommand {
print("ROOT");
} else {
if (resource instanceof ContinuousResource) {
String s = ((String) resource.last());
String s = resource.simpleTypeName();
String simpleName = s.substring(s.lastIndexOf('.') + 1);
print("%s%s: %f", Strings.repeat(" ", level),
simpleName,
......@@ -126,7 +126,7 @@ public class ResourcesCommand extends AbstractShellCommand {
// Continuous resource is terminal node, stop here
return;
} else {
String resourceName = resource.last().getClass().getSimpleName();
String resourceName = resource.simpleTypeName();
String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
if (toString.startsWith(resourceName)) {
......@@ -161,7 +161,7 @@ public class ResourcesCommand extends AbstractShellCommand {
nonAggregatable.add(r);
} else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
// aggregatable & terminal node
String className = r.last().getClass().getSimpleName();
String className = r.simpleTypeName();
aggregatables.put(className, r);
} else {
nonAggregatable.add(r);
......@@ -216,8 +216,7 @@ public class ResourcesCommand extends AbstractShellCommand {
String resourceName;
if (resource instanceof ContinuousResource) {
String s = (String) resource.last();
resourceName = s.substring(s.lastIndexOf('.') + 1);
resourceName = resource.simpleTypeName();
} else if (resource instanceof DiscreteResource) {
// TODO This distributed store access incurs overhead.
// This should be merged with the one in printResource()
......@@ -225,7 +224,7 @@ public class ResourcesCommand extends AbstractShellCommand {
// resource which has children should be printed
return true;
}
resourceName = resource.last().getClass().getSimpleName();
resourceName = resource.simpleTypeName();
} else {
log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
return false;
......
......@@ -49,6 +49,11 @@ public final class ContinuousResource implements Resource {
}
@Override
public String simpleTypeName() {
return id.simpleTypeName();
}
@Override
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
......@@ -98,14 +103,6 @@ public final class ContinuousResource implements Resource {
}
@Override
public Object last() {
if (id.components().isEmpty()) {
return null;
}
return id.components().get(id.components().size() - 1);
}
@Override
public DiscreteResource child(Object child) {
throw new UnsupportedOperationException();
}
......
......@@ -47,6 +47,11 @@ public final class ContinuousResourceId extends ResourceId {
return components;
}
@Override
String simpleTypeName() {
return name;
}
/**
* {@inheritDoc}
*
......
......@@ -46,6 +46,11 @@ public final class DiscreteResource implements Resource {
}
@Override
public String simpleTypeName() {
return id.simpleTypeName();
}
@Override
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
......@@ -80,14 +85,6 @@ public final class DiscreteResource implements Resource {
return Optional.of(value);
}
@Override
public Object last() {
if (id.components().isEmpty()) {
return null;
}
return id.components().get(id.components().size() - 1);
}
private boolean isRoot() {
return id.equals(ResourceId.ROOT);
}
......
......@@ -45,6 +45,15 @@ public final class DiscreteResourceId extends ResourceId {
}
@Override
String simpleTypeName() {
if (components.isEmpty()) {
return "Root";
}
return components.get(components.size() - 1).getClass().getSimpleName();
}
@Override
public DiscreteResourceId child(Object child) {
checkArgument(!(child instanceof Class<?>));
......
......@@ -48,6 +48,17 @@ public interface Resource {
ResourceId id();
/**
* Returns the simple type name of this resource.
*
* Example:<br>
* Resource: DeviceId:1/PortNumber:1/VlanId:200<br>
* Simple type name: VlanId<br>
*
* @return the simple type name of this resource
*/
String simpleTypeName();
/**
* Checks if the type of this instance is the specified type.
*
* @param type type of resource to be checked
......@@ -75,14 +86,6 @@ public interface Resource {
<T> Optional<T> valueAs(Class<T> type);
/**
* Returns the last component of this instance.
*
* @return the last component of this instance.
* The return value is equal to the last object of {@code components()}.
*/
Object last();
/**
* Returns the parent resource of this instance.
* E.g. if this resource is Link:1/VLAN ID:100, the return value is the resource for Link:1.
*
......
......@@ -29,6 +29,8 @@ public abstract class ResourceId {
abstract ImmutableList<Object> components();
abstract String simpleTypeName();
/**
* Returns the parent resource ID of this instance.
*
......
......@@ -21,6 +21,9 @@ import org.onlab.util.Bandwidth;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Unit test for ContinuousResourceId.
*/
......@@ -42,4 +45,11 @@ public class ContinuousResourceIdTest {
new EqualsTester()
.addEqualityGroup(id1, sameAsId1);
}
@Test
public void testSimpleTypeName() {
ContinuousResourceId id1 = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()).id();
assertThat(id1.simpleTypeName(), is("Bandwidth"));
}
}
......
......@@ -21,6 +21,9 @@ import org.onlab.packet.VlanId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Unit test for DiscreteResourceId.
*/
......@@ -41,4 +44,15 @@ public class DiscreteResourceIdTest {
.addEqualityGroup(id1, sameAsId1)
.addEqualityGroup(id2);
}
@Test
public void testSimpleTypeName() {
DiscreteResourceId id = Resources.discrete(D1, P1, VLAN1).id();
assertThat(id.simpleTypeName(), is("VlanId"));
}
@Test
public void testSimpleTypeNameOfRoot() {
assertThat(ResourceId.ROOT.simpleTypeName(), is("Root"));
}
}
......
......@@ -91,14 +91,6 @@ public class DiscreteResourceTest {
}
@Test
public void testBase() {
DiscreteResource resource = Resources.discrete(D1).resource();
DeviceId child = (DeviceId) resource.last();
assertThat(child, is(D1));
}
@Test
public void testValueAs() {
DiscreteResource resource = Resources.discrete(D1).resource();
......