Committed by
Gerrit Code Review
Hide underlying components from public use
- Remove components() method from Resource - Add isTypeOf() method to Resource Change-Id: I390eb6057fc7e2c3e93eb2a18d161f661ad6c180
Showing
5 changed files
with
36 additions
and
24 deletions
... | @@ -18,7 +18,6 @@ package org.onosproject.net.newresource; | ... | @@ -18,7 +18,6 @@ package org.onosproject.net.newresource; |
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.base.MoreObjects; | 19 | import com.google.common.base.MoreObjects; |
20 | 20 | ||
21 | -import java.util.List; | ||
22 | import java.util.Objects; | 21 | import java.util.Objects; |
23 | import java.util.Optional; | 22 | import java.util.Optional; |
24 | 23 | ||
... | @@ -74,8 +73,15 @@ public final class ContinuousResource implements Resource { | ... | @@ -74,8 +73,15 @@ public final class ContinuousResource implements Resource { |
74 | } | 73 | } |
75 | 74 | ||
76 | @Override | 75 | @Override |
77 | - public List<Object> components() { | 76 | + public boolean isTypeOf(Class<?> ancestorType) { |
78 | - return id.components(); | 77 | + String typeName = (String) id.components().get(id.components().size() - 1); |
78 | + boolean foundInLeaf = typeName.equals(ancestorType.getCanonicalName()); | ||
79 | + boolean foundInAncestor = id.components().subList(0, id.components().size()).stream() | ||
80 | + .map(Object::getClass) | ||
81 | + .filter(x -> x.equals(ancestorType)) | ||
82 | + .findAny() | ||
83 | + .isPresent(); | ||
84 | + return foundInAncestor || foundInLeaf; | ||
79 | } | 85 | } |
80 | 86 | ||
81 | @Override | 87 | @Override | ... | ... |
... | @@ -18,7 +18,6 @@ package org.onosproject.net.newresource; | ... | @@ -18,7 +18,6 @@ package org.onosproject.net.newresource; |
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.base.MoreObjects; | 19 | import com.google.common.base.MoreObjects; |
20 | 20 | ||
21 | -import java.util.List; | ||
22 | import java.util.Objects; | 21 | import java.util.Objects; |
23 | import java.util.Optional; | 22 | import java.util.Optional; |
24 | 23 | ||
... | @@ -64,8 +63,12 @@ public final class DiscreteResource implements Resource { | ... | @@ -64,8 +63,12 @@ public final class DiscreteResource implements Resource { |
64 | } | 63 | } |
65 | 64 | ||
66 | @Override | 65 | @Override |
67 | - public List<Object> components() { | 66 | + public boolean isTypeOf(Class<?> ancestorType) { |
68 | - return id.components(); | 67 | + return id.components().stream() |
68 | + .map(Object::getClass) | ||
69 | + .filter(x -> x.equals(ancestorType)) | ||
70 | + .findAny() | ||
71 | + .isPresent(); | ||
69 | } | 72 | } |
70 | 73 | ||
71 | @Override | 74 | @Override | ... | ... |
... | @@ -17,7 +17,6 @@ package org.onosproject.net.newresource; | ... | @@ -17,7 +17,6 @@ package org.onosproject.net.newresource; |
17 | 17 | ||
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | 19 | ||
20 | -import java.util.List; | ||
21 | import java.util.Optional; | 20 | import java.util.Optional; |
22 | 21 | ||
23 | /** | 22 | /** |
... | @@ -42,11 +41,13 @@ public interface Resource { | ... | @@ -42,11 +41,13 @@ public interface Resource { |
42 | DiscreteResource ROOT = new DiscreteResource(); | 41 | DiscreteResource ROOT = new DiscreteResource(); |
43 | 42 | ||
44 | /** | 43 | /** |
45 | - * Returns the components of this resource. | 44 | + * Checks if the type of this instance is the specified type. |
46 | * | 45 | * |
47 | - * @return the components of this resource | 46 | + * @param ancestorType type of resource to be checked. |
47 | + * @return true if this resource is under the resource whose type is the given type. | ||
48 | */ | 48 | */ |
49 | - List<Object> components(); | 49 | + // TODO: find more proper name |
50 | + boolean isTypeOf(Class<?> ancestorType); | ||
50 | 51 | ||
51 | /** | 52 | /** |
52 | * Returns the volume of this resource. | 53 | * Returns the volume of this resource. | ... | ... |
... | @@ -24,7 +24,6 @@ import org.onosproject.net.PortNumber; | ... | @@ -24,7 +24,6 @@ import org.onosproject.net.PortNumber; |
24 | 24 | ||
25 | import java.util.Optional; | 25 | import java.util.Optional; |
26 | 26 | ||
27 | -import static org.hamcrest.Matchers.contains; | ||
28 | import static org.hamcrest.Matchers.is; | 27 | import static org.hamcrest.Matchers.is; |
29 | import static org.junit.Assert.assertThat; | 28 | import static org.junit.Assert.assertThat; |
30 | 29 | ||
... | @@ -53,13 +52,6 @@ public class ResourceTest { | ... | @@ -53,13 +52,6 @@ public class ResourceTest { |
53 | } | 52 | } |
54 | 53 | ||
55 | @Test | 54 | @Test |
56 | - public void testComponents() { | ||
57 | - Resource port = Resources.discrete(D1, P1).resource(); | ||
58 | - | ||
59 | - assertThat(port.components(), contains(D1, P1)); | ||
60 | - } | ||
61 | - | ||
62 | - @Test | ||
63 | public void testIdEquality() { | 55 | public void testIdEquality() { |
64 | ResourceId id1 = Resources.discrete(D1, P1, VLAN1).id(); | 56 | ResourceId id1 = Resources.discrete(D1, P1, VLAN1).id(); |
65 | ResourceId sameAsId1 = Resources.discrete(D1, P1, VLAN1).id(); | 57 | ResourceId sameAsId1 = Resources.discrete(D1, P1, VLAN1).id(); |
... | @@ -98,6 +90,21 @@ public class ResourceTest { | ... | @@ -98,6 +90,21 @@ public class ResourceTest { |
98 | } | 90 | } |
99 | 91 | ||
100 | @Test | 92 | @Test |
93 | + public void testTypeOf() { | ||
94 | + DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource(); | ||
95 | + assertThat(discrete.isTypeOf(DeviceId.class), is(true)); | ||
96 | + assertThat(discrete.isTypeOf(PortNumber.class), is(true)); | ||
97 | + assertThat(discrete.isTypeOf(VlanId.class), is(true)); | ||
98 | + assertThat(discrete.isTypeOf(Bandwidth.class), is(false)); | ||
99 | + | ||
100 | + ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()); | ||
101 | + assertThat(continuous.isTypeOf(DeviceId.class), is(true)); | ||
102 | + assertThat(continuous.isTypeOf(PortNumber.class), is(true)); | ||
103 | + assertThat(continuous.isTypeOf(Bandwidth.class), is(true)); | ||
104 | + assertThat(continuous.isTypeOf(VlanId.class), is(false)); | ||
105 | + } | ||
106 | + | ||
107 | + @Test | ||
101 | public void testBase() { | 108 | public void testBase() { |
102 | Resource resource = Resources.discrete(D1).resource(); | 109 | Resource resource = Resources.discrete(D1).resource(); |
103 | 110 | ... | ... |
... | @@ -61,7 +61,6 @@ import java.util.Collection; | ... | @@ -61,7 +61,6 @@ import java.util.Collection; |
61 | import java.util.Collections; | 61 | import java.util.Collections; |
62 | import java.util.HashSet; | 62 | import java.util.HashSet; |
63 | import java.util.List; | 63 | import java.util.List; |
64 | -import java.util.Optional; | ||
65 | import java.util.Set; | 64 | import java.util.Set; |
66 | import java.util.concurrent.ExecutorService; | 65 | import java.util.concurrent.ExecutorService; |
67 | import java.util.concurrent.Executors; | 66 | import java.util.concurrent.Executors; |
... | @@ -303,11 +302,7 @@ public class ObjectiveTracker implements ObjectiveTrackerService { | ... | @@ -303,11 +302,7 @@ public class ObjectiveTracker implements ObjectiveTrackerService { |
303 | private class InternalResourceListener implements ResourceListener { | 302 | private class InternalResourceListener implements ResourceListener { |
304 | @Override | 303 | @Override |
305 | public void event(ResourceEvent event) { | 304 | public void event(ResourceEvent event) { |
306 | - Optional<Class<?>> deviceEvent = event.subject().components().stream() | 305 | + if (event.subject().isTypeOf(PortNumber.class)) { |
307 | - .map(Object::getClass) | ||
308 | - .filter(x -> x == PortNumber.class) | ||
309 | - .findFirst(); | ||
310 | - if (deviceEvent.isPresent()) { | ||
311 | executorService.execute(() -> { | 306 | executorService.execute(() -> { |
312 | if (delegate == null) { | 307 | if (delegate == null) { |
313 | return; | 308 | return; | ... | ... |
-
Please register or login to post a comment