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
Showing
10 changed files
with
66 additions
and
38 deletions
... | @@ -115,7 +115,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -115,7 +115,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
115 | print("ROOT"); | 115 | print("ROOT"); |
116 | } else { | 116 | } else { |
117 | if (resource instanceof ContinuousResource) { | 117 | if (resource instanceof ContinuousResource) { |
118 | - String s = ((String) resource.last()); | 118 | + String s = resource.simpleTypeName(); |
119 | String simpleName = s.substring(s.lastIndexOf('.') + 1); | 119 | String simpleName = s.substring(s.lastIndexOf('.') + 1); |
120 | print("%s%s: %f", Strings.repeat(" ", level), | 120 | print("%s%s: %f", Strings.repeat(" ", level), |
121 | simpleName, | 121 | simpleName, |
... | @@ -126,7 +126,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -126,7 +126,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
126 | // Continuous resource is terminal node, stop here | 126 | // Continuous resource is terminal node, stop here |
127 | return; | 127 | return; |
128 | } else { | 128 | } else { |
129 | - String resourceName = resource.last().getClass().getSimpleName(); | 129 | + String resourceName = resource.simpleTypeName(); |
130 | 130 | ||
131 | String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); | 131 | String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); |
132 | if (toString.startsWith(resourceName)) { | 132 | if (toString.startsWith(resourceName)) { |
... | @@ -161,7 +161,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -161,7 +161,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
161 | nonAggregatable.add(r); | 161 | nonAggregatable.add(r); |
162 | } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) { | 162 | } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) { |
163 | // aggregatable & terminal node | 163 | // aggregatable & terminal node |
164 | - String className = r.last().getClass().getSimpleName(); | 164 | + String className = r.simpleTypeName(); |
165 | aggregatables.put(className, r); | 165 | aggregatables.put(className, r); |
166 | } else { | 166 | } else { |
167 | nonAggregatable.add(r); | 167 | nonAggregatable.add(r); |
... | @@ -216,8 +216,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -216,8 +216,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
216 | 216 | ||
217 | String resourceName; | 217 | String resourceName; |
218 | if (resource instanceof ContinuousResource) { | 218 | if (resource instanceof ContinuousResource) { |
219 | - String s = (String) resource.last(); | 219 | + resourceName = resource.simpleTypeName(); |
220 | - resourceName = s.substring(s.lastIndexOf('.') + 1); | ||
221 | } else if (resource instanceof DiscreteResource) { | 220 | } else if (resource instanceof DiscreteResource) { |
222 | // TODO This distributed store access incurs overhead. | 221 | // TODO This distributed store access incurs overhead. |
223 | // This should be merged with the one in printResource() | 222 | // This should be merged with the one in printResource() |
... | @@ -225,7 +224,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -225,7 +224,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
225 | // resource which has children should be printed | 224 | // resource which has children should be printed |
226 | return true; | 225 | return true; |
227 | } | 226 | } |
228 | - resourceName = resource.last().getClass().getSimpleName(); | 227 | + resourceName = resource.simpleTypeName(); |
229 | } else { | 228 | } else { |
230 | log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName()); | 229 | log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName()); |
231 | return false; | 230 | return false; | ... | ... |
... | @@ -49,6 +49,11 @@ public final class ContinuousResource implements Resource { | ... | @@ -49,6 +49,11 @@ public final class ContinuousResource implements Resource { |
49 | } | 49 | } |
50 | 50 | ||
51 | @Override | 51 | @Override |
52 | + public String simpleTypeName() { | ||
53 | + return id.simpleTypeName(); | ||
54 | + } | ||
55 | + | ||
56 | + @Override | ||
52 | public boolean isTypeOf(Class<?> type) { | 57 | public boolean isTypeOf(Class<?> type) { |
53 | checkNotNull(type); | 58 | checkNotNull(type); |
54 | 59 | ||
... | @@ -98,14 +103,6 @@ public final class ContinuousResource implements Resource { | ... | @@ -98,14 +103,6 @@ public final class ContinuousResource implements Resource { |
98 | } | 103 | } |
99 | 104 | ||
100 | @Override | 105 | @Override |
101 | - public Object last() { | ||
102 | - if (id.components().isEmpty()) { | ||
103 | - return null; | ||
104 | - } | ||
105 | - return id.components().get(id.components().size() - 1); | ||
106 | - } | ||
107 | - | ||
108 | - @Override | ||
109 | public DiscreteResource child(Object child) { | 106 | public DiscreteResource child(Object child) { |
110 | throw new UnsupportedOperationException(); | 107 | throw new UnsupportedOperationException(); |
111 | } | 108 | } | ... | ... |
... | @@ -47,6 +47,11 @@ public final class ContinuousResourceId extends ResourceId { | ... | @@ -47,6 +47,11 @@ public final class ContinuousResourceId extends ResourceId { |
47 | return components; | 47 | return components; |
48 | } | 48 | } |
49 | 49 | ||
50 | + @Override | ||
51 | + String simpleTypeName() { | ||
52 | + return name; | ||
53 | + } | ||
54 | + | ||
50 | /** | 55 | /** |
51 | * {@inheritDoc} | 56 | * {@inheritDoc} |
52 | * | 57 | * | ... | ... |
... | @@ -46,6 +46,11 @@ public final class DiscreteResource implements Resource { | ... | @@ -46,6 +46,11 @@ public final class DiscreteResource implements Resource { |
46 | } | 46 | } |
47 | 47 | ||
48 | @Override | 48 | @Override |
49 | + public String simpleTypeName() { | ||
50 | + return id.simpleTypeName(); | ||
51 | + } | ||
52 | + | ||
53 | + @Override | ||
49 | public boolean isTypeOf(Class<?> type) { | 54 | public boolean isTypeOf(Class<?> type) { |
50 | checkNotNull(type); | 55 | checkNotNull(type); |
51 | 56 | ||
... | @@ -80,14 +85,6 @@ public final class DiscreteResource implements Resource { | ... | @@ -80,14 +85,6 @@ public final class DiscreteResource implements Resource { |
80 | return Optional.of(value); | 85 | return Optional.of(value); |
81 | } | 86 | } |
82 | 87 | ||
83 | - @Override | ||
84 | - public Object last() { | ||
85 | - if (id.components().isEmpty()) { | ||
86 | - return null; | ||
87 | - } | ||
88 | - return id.components().get(id.components().size() - 1); | ||
89 | - } | ||
90 | - | ||
91 | private boolean isRoot() { | 88 | private boolean isRoot() { |
92 | return id.equals(ResourceId.ROOT); | 89 | return id.equals(ResourceId.ROOT); |
93 | } | 90 | } | ... | ... |
... | @@ -45,6 +45,15 @@ public final class DiscreteResourceId extends ResourceId { | ... | @@ -45,6 +45,15 @@ public final class DiscreteResourceId extends ResourceId { |
45 | } | 45 | } |
46 | 46 | ||
47 | @Override | 47 | @Override |
48 | + String simpleTypeName() { | ||
49 | + if (components.isEmpty()) { | ||
50 | + return "Root"; | ||
51 | + } | ||
52 | + | ||
53 | + return components.get(components.size() - 1).getClass().getSimpleName(); | ||
54 | + } | ||
55 | + | ||
56 | + @Override | ||
48 | public DiscreteResourceId child(Object child) { | 57 | public DiscreteResourceId child(Object child) { |
49 | checkArgument(!(child instanceof Class<?>)); | 58 | checkArgument(!(child instanceof Class<?>)); |
50 | 59 | ... | ... |
... | @@ -48,6 +48,17 @@ public interface Resource { | ... | @@ -48,6 +48,17 @@ public interface Resource { |
48 | ResourceId id(); | 48 | ResourceId id(); |
49 | 49 | ||
50 | /** | 50 | /** |
51 | + * Returns the simple type name of this resource. | ||
52 | + * | ||
53 | + * Example:<br> | ||
54 | + * Resource: DeviceId:1/PortNumber:1/VlanId:200<br> | ||
55 | + * Simple type name: VlanId<br> | ||
56 | + * | ||
57 | + * @return the simple type name of this resource | ||
58 | + */ | ||
59 | + String simpleTypeName(); | ||
60 | + | ||
61 | + /** | ||
51 | * Checks if the type of this instance is the specified type. | 62 | * Checks if the type of this instance is the specified type. |
52 | * | 63 | * |
53 | * @param type type of resource to be checked | 64 | * @param type type of resource to be checked |
... | @@ -75,14 +86,6 @@ public interface Resource { | ... | @@ -75,14 +86,6 @@ public interface Resource { |
75 | <T> Optional<T> valueAs(Class<T> type); | 86 | <T> Optional<T> valueAs(Class<T> type); |
76 | 87 | ||
77 | /** | 88 | /** |
78 | - * Returns the last component of this instance. | ||
79 | - * | ||
80 | - * @return the last component of this instance. | ||
81 | - * The return value is equal to the last object of {@code components()}. | ||
82 | - */ | ||
83 | - Object last(); | ||
84 | - | ||
85 | - /** | ||
86 | * Returns the parent resource of this instance. | 89 | * Returns the parent resource of this instance. |
87 | * E.g. if this resource is Link:1/VLAN ID:100, the return value is the resource for Link:1. | 90 | * E.g. if this resource is Link:1/VLAN ID:100, the return value is the resource for Link:1. |
88 | * | 91 | * | ... | ... |
... | @@ -29,6 +29,8 @@ public abstract class ResourceId { | ... | @@ -29,6 +29,8 @@ public abstract class ResourceId { |
29 | 29 | ||
30 | abstract ImmutableList<Object> components(); | 30 | abstract ImmutableList<Object> components(); |
31 | 31 | ||
32 | + abstract String simpleTypeName(); | ||
33 | + | ||
32 | /** | 34 | /** |
33 | * Returns the parent resource ID of this instance. | 35 | * Returns the parent resource ID of this instance. |
34 | * | 36 | * | ... | ... |
... | @@ -21,6 +21,9 @@ import org.onlab.util.Bandwidth; | ... | @@ -21,6 +21,9 @@ import org.onlab.util.Bandwidth; |
21 | import org.onosproject.net.DeviceId; | 21 | import org.onosproject.net.DeviceId; |
22 | import org.onosproject.net.PortNumber; | 22 | import org.onosproject.net.PortNumber; |
23 | 23 | ||
24 | +import static org.hamcrest.Matchers.is; | ||
25 | +import static org.junit.Assert.assertThat; | ||
26 | + | ||
24 | /** | 27 | /** |
25 | * Unit test for ContinuousResourceId. | 28 | * Unit test for ContinuousResourceId. |
26 | */ | 29 | */ |
... | @@ -42,4 +45,11 @@ public class ContinuousResourceIdTest { | ... | @@ -42,4 +45,11 @@ public class ContinuousResourceIdTest { |
42 | new EqualsTester() | 45 | new EqualsTester() |
43 | .addEqualityGroup(id1, sameAsId1); | 46 | .addEqualityGroup(id1, sameAsId1); |
44 | } | 47 | } |
48 | + | ||
49 | + @Test | ||
50 | + public void testSimpleTypeName() { | ||
51 | + ContinuousResourceId id1 = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()).id(); | ||
52 | + | ||
53 | + assertThat(id1.simpleTypeName(), is("Bandwidth")); | ||
54 | + } | ||
45 | } | 55 | } | ... | ... |
... | @@ -21,6 +21,9 @@ import org.onlab.packet.VlanId; | ... | @@ -21,6 +21,9 @@ import org.onlab.packet.VlanId; |
21 | import org.onosproject.net.DeviceId; | 21 | import org.onosproject.net.DeviceId; |
22 | import org.onosproject.net.PortNumber; | 22 | import org.onosproject.net.PortNumber; |
23 | 23 | ||
24 | +import static org.hamcrest.Matchers.is; | ||
25 | +import static org.junit.Assert.assertThat; | ||
26 | + | ||
24 | /** | 27 | /** |
25 | * Unit test for DiscreteResourceId. | 28 | * Unit test for DiscreteResourceId. |
26 | */ | 29 | */ |
... | @@ -41,4 +44,15 @@ public class DiscreteResourceIdTest { | ... | @@ -41,4 +44,15 @@ public class DiscreteResourceIdTest { |
41 | .addEqualityGroup(id1, sameAsId1) | 44 | .addEqualityGroup(id1, sameAsId1) |
42 | .addEqualityGroup(id2); | 45 | .addEqualityGroup(id2); |
43 | } | 46 | } |
47 | + | ||
48 | + @Test | ||
49 | + public void testSimpleTypeName() { | ||
50 | + DiscreteResourceId id = Resources.discrete(D1, P1, VLAN1).id(); | ||
51 | + assertThat(id.simpleTypeName(), is("VlanId")); | ||
52 | + } | ||
53 | + | ||
54 | + @Test | ||
55 | + public void testSimpleTypeNameOfRoot() { | ||
56 | + assertThat(ResourceId.ROOT.simpleTypeName(), is("Root")); | ||
57 | + } | ||
44 | } | 58 | } | ... | ... |
... | @@ -91,14 +91,6 @@ public class DiscreteResourceTest { | ... | @@ -91,14 +91,6 @@ public class DiscreteResourceTest { |
91 | } | 91 | } |
92 | 92 | ||
93 | @Test | 93 | @Test |
94 | - public void testBase() { | ||
95 | - DiscreteResource resource = Resources.discrete(D1).resource(); | ||
96 | - | ||
97 | - DeviceId child = (DeviceId) resource.last(); | ||
98 | - assertThat(child, is(D1)); | ||
99 | - } | ||
100 | - | ||
101 | - @Test | ||
102 | public void testValueAs() { | 94 | public void testValueAs() { |
103 | DiscreteResource resource = Resources.discrete(D1).resource(); | 95 | DiscreteResource resource = Resources.discrete(D1).resource(); |
104 | 96 | ... | ... |
-
Please register or login to post a comment