Sho SHIMIZU
Committed by Gerrit Code Review

Use ResourceId or DiscreteResourceId when specifying a resource

Change-Id: I4e29558ec649510c8d08bb5e5f8ed10c189252e5
...@@ -30,8 +30,8 @@ import org.onosproject.net.OchSignal; ...@@ -30,8 +30,8 @@ import org.onosproject.net.OchSignal;
30 import org.onosproject.net.Port; 30 import org.onosproject.net.Port;
31 import org.onosproject.net.PortNumber; 31 import org.onosproject.net.PortNumber;
32 import org.onosproject.net.device.DeviceService; 32 import org.onosproject.net.device.DeviceService;
33 +import org.onosproject.net.newresource.DiscreteResourceId;
33 import org.onosproject.net.newresource.ResourceAllocation; 34 import org.onosproject.net.newresource.ResourceAllocation;
34 -import org.onosproject.net.newresource.Resource;
35 import org.onosproject.net.newresource.ResourceService; 35 import org.onosproject.net.newresource.ResourceService;
36 36
37 import com.google.common.base.Strings; 37 import com.google.common.base.Strings;
...@@ -107,7 +107,7 @@ public class AllocationsCommand extends AbstractShellCommand { ...@@ -107,7 +107,7 @@ public class AllocationsCommand extends AbstractShellCommand {
107 // TODO: Current design cannot deal with sub-resources 107 // TODO: Current design cannot deal with sub-resources
108 // (e.g., TX/RX under Port) 108 // (e.g., TX/RX under Port)
109 109
110 - Resource resource = Resources.discrete(did, num).resource(); 110 + DiscreteResourceId resource = Resources.discrete(did, num).id();
111 if (lambda) { 111 if (lambda) {
112 //print("Lambda resources:"); 112 //print("Lambda resources:");
113 Collection<ResourceAllocation> allocations 113 Collection<ResourceAllocation> allocations
......
...@@ -34,6 +34,7 @@ import org.onosproject.net.DeviceId; ...@@ -34,6 +34,7 @@ import org.onosproject.net.DeviceId;
34 import org.onosproject.net.PortNumber; 34 import org.onosproject.net.PortNumber;
35 import org.onosproject.net.TributarySlot; 35 import org.onosproject.net.TributarySlot;
36 import org.onosproject.net.newresource.ContinuousResource; 36 import org.onosproject.net.newresource.ContinuousResource;
37 +import org.onosproject.net.newresource.DiscreteResource;
37 import org.onosproject.net.newresource.Resource; 38 import org.onosproject.net.newresource.Resource;
38 import org.onosproject.net.newresource.ResourceService; 39 import org.onosproject.net.newresource.ResourceService;
39 40
...@@ -101,7 +102,13 @@ public class ResourcesCommand extends AbstractShellCommand { ...@@ -101,7 +102,13 @@ public class ResourcesCommand extends AbstractShellCommand {
101 102
102 private void printResource(Resource resource, int level) { 103 private void printResource(Resource resource, int level) {
103 // TODO add an option to show only available resource 104 // TODO add an option to show only available resource
104 - Set<Resource> children = resourceService.getRegisteredResources(resource); 105 + // workaround to preserve the original behavior of ResourceService#getRegisteredResources
106 + Set<Resource> children;
107 + if (resource instanceof DiscreteResource) {
108 + children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
109 + } else {
110 + children = Collections.emptySet();
111 + }
105 112
106 if (resource.equals(Resource.ROOT)) { 113 if (resource.equals(Resource.ROOT)) {
107 print("ROOT"); 114 print("ROOT");
......
...@@ -129,24 +129,22 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource ...@@ -129,24 +129,22 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
129 /** 129 /**
130 * Returns resource allocations of the specified resource. 130 * Returns resource allocations of the specified resource.
131 * 131 *
132 - * @param resource resource to check the allocation 132 + * @param id ID of the resource to check the allocation
133 * @return list of allocation information. 133 * @return list of allocation information.
134 * If the resource is not allocated, the return value is an empty list. 134 * If the resource is not allocated, the return value is an empty list.
135 */ 135 */
136 - // TODO: need to change the argument type to ResourceId 136 + List<ResourceAllocation> getResourceAllocations(ResourceId id);
137 - List<ResourceAllocation> getResourceAllocations(Resource resource);
138 137
139 /** 138 /**
140 * Returns allocated resources being as children of the specified parent and being the specified resource type. 139 * Returns allocated resources being as children of the specified parent and being the specified resource type.
141 * 140 *
142 - * @param parent parent resource 141 + * @param parent parent resource ID
143 * @param cls class to specify a type of resource 142 * @param cls class to specify a type of resource
144 * @param <T> type of the resource 143 * @param <T> type of the resource
145 * @return non-empty collection of resource allocations if resources are allocated with the subject and type, 144 * @return non-empty collection of resource allocations if resources are allocated with the subject and type,
146 * empty collection if no resource is allocated with the subject and type 145 * empty collection if no resource is allocated with the subject and type
147 */ 146 */
148 - // TODO: might need to change the first argument type to ResourceId or ResourceId.Discrete 147 + <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls);
149 - <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls);
150 148
151 /** 149 /**
152 * Returns resources allocated to the specified consumer. 150 * Returns resources allocated to the specified consumer.
...@@ -159,20 +157,18 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource ...@@ -159,20 +157,18 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
159 /** 157 /**
160 * Returns resources that point available child resources under the specified resource. 158 * Returns resources that point available child resources under the specified resource.
161 * 159 *
162 - * @param parent parent resource 160 + * @param parent parent resource ID
163 * @return available resources under the specified resource 161 * @return available resources under the specified resource
164 */ 162 */
165 - // TODO: need to change the argument type to ResourceId or ResourceId.Discrete 163 + Set<Resource> getAvailableResources(DiscreteResourceId parent);
166 - Set<Resource> getAvailableResources(Resource parent);
167 164
168 /** 165 /**
169 * Returns resources registered under the specified resource. 166 * Returns resources registered under the specified resource.
170 * 167 *
171 - * @param parent parent resource 168 + * @param parent parent resource ID
172 * @return registered resources under the specified resource 169 * @return registered resources under the specified resource
173 */ 170 */
174 - // TODO: need to change the argument type to ResourceId or ResourceId.Discrete 171 + Set<Resource> getRegisteredResources(DiscreteResourceId parent);
175 - Set<Resource> getRegisteredResources(Resource parent);
176 172
177 173
178 /** 174 /**
......
...@@ -81,12 +81,11 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat ...@@ -81,12 +81,11 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
81 * The return value is a list having only one element when the given resource is discrete type. 81 * The return value is a list having only one element when the given resource is discrete type.
82 * The return value may have multiple elements when the given resource is continuous type. 82 * The return value may have multiple elements when the given resource is continuous type.
83 * 83 *
84 - * @param resource resource whose allocated consumer to be returned 84 + * @param id ID of the resource whose allocated consumer to be returned
85 * @return resource consumers who are allocated the resource. 85 * @return resource consumers who are allocated the resource.
86 * Returns empty list if there is no such consumer. 86 * Returns empty list if there is no such consumer.
87 */ 87 */
88 - // TODO: need to change the argument type to ResourceId 88 + List<ResourceAllocation> getResourceAllocations(ResourceId id);
89 - List<ResourceConsumer> getConsumers(Resource resource);
90 89
91 /** 90 /**
92 * Returns the availability of the specified resource. 91 * Returns the availability of the specified resource.
...@@ -107,22 +106,20 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat ...@@ -107,22 +106,20 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
107 /** 106 /**
108 * Returns a set of the child resources of the specified parent. 107 * Returns a set of the child resources of the specified parent.
109 * 108 *
110 - * @param parent parent of the resource to be returned 109 + * @param parent ID of the parent of the resource to be returned
111 * @return a set of the child resources of the specified resource 110 * @return a set of the child resources of the specified resource
112 */ 111 */
113 - // TODO: need to change the argument type to ResourceId or ResourceId.Discrete 112 + Set<Resource> getChildResources(DiscreteResourceId parent);
114 - Set<Resource> getChildResources(Resource parent);
115 113
116 /** 114 /**
117 * Returns a collection of the resources which are children of the specified parent and 115 * Returns a collection of the resources which are children of the specified parent and
118 * whose type is the specified class. 116 * whose type is the specified class.
119 * 117 *
120 - * @param parent parent of the resources to be returned 118 + * @param parent ID of the parent of the resources to be returned
121 * @param cls class instance of the children 119 * @param cls class instance of the children
122 * @param <T> type of the resource 120 * @param <T> type of the resource
123 * @return a collection of the resources which belongs to the specified subject and 121 * @return a collection of the resources which belongs to the specified subject and
124 * whose type is the specified class. 122 * whose type is the specified class.
125 */ 123 */
126 - // TODO: need to change the argument type to ResourceId or ResourceId.Discrete 124 + <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls);
127 - <T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls);
128 } 125 }
......
...@@ -157,7 +157,7 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { ...@@ -157,7 +157,7 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
157 } 157 }
158 158
159 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) { 159 private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
160 - return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).resource()).stream() 160 + return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
161 .filter(x -> x.last() instanceof MplsLabel) 161 .filter(x -> x.last() instanceof MplsLabel)
162 .map(x -> (MplsLabel) x.last()) 162 .map(x -> (MplsLabel) x.last())
163 .collect(Collectors.toSet()); 163 .collect(Collectors.toSet());
......
...@@ -313,8 +313,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu ...@@ -313,8 +313,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
313 if (ochCP != null) { 313 if (ochCP != null) {
314 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port()); 314 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
315 Optional<IntentId> intentId = 315 Optional<IntentId> intentId =
316 - resourceService.getResourceAllocations( 316 + resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
317 - Resources.discrete(ochCP.deviceId(), ochCP.port()).resource())
318 .stream() 317 .stream()
319 .map(ResourceAllocation::consumer) 318 .map(ResourceAllocation::consumer)
320 .filter(x -> x instanceof IntentId) 319 .filter(x -> x instanceof IntentId)
...@@ -335,8 +334,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu ...@@ -335,8 +334,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
335 } 334 }
336 335
337 Optional<IntentId> intentId = 336 Optional<IntentId> intentId =
338 - resourceService.getResourceAllocations( 337 + resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
339 - Resources.discrete(oduPort.deviceId(), port.number()).resource())
340 .stream() 338 .stream()
341 .map(ResourceAllocation::consumer) 339 .map(ResourceAllocation::consumer)
342 .filter(x -> x instanceof IntentId) 340 .filter(x -> x instanceof IntentId)
......
...@@ -214,8 +214,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical ...@@ -214,8 +214,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical
214 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) { 214 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
215 return links.stream() 215 return links.stream()
216 .flatMap(x -> Stream.of( 216 .flatMap(x -> Stream.of(
217 - Resources.discrete(x.src().deviceId(), x.src().port()).resource(), 217 + Resources.discrete(x.src().deviceId(), x.src().port()).id(),
218 - Resources.discrete(x.dst().deviceId(), x.dst().port()).resource() 218 + Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
219 )) 219 ))
220 .map(resourceService::getAvailableResources) 220 .map(resourceService::getAvailableResources)
221 .map(x -> Iterables.filter(x, r -> r.last() instanceof OchSignal)) 221 .map(x -> Iterables.filter(x, r -> r.last() instanceof OchSignal))
......
...@@ -285,7 +285,7 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> { ...@@ -285,7 +285,7 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> {
285 } 285 }
286 286
287 private Set<VlanId> findVlanId(ConnectPoint cp) { 287 private Set<VlanId> findVlanId(ConnectPoint cp) {
288 - return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).resource()).stream() 288 + return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
289 .filter(x -> x.last() instanceof VlanId) 289 .filter(x -> x.last() instanceof VlanId)
290 .map(x -> (VlanId) x.last()) 290 .map(x -> (VlanId) x.last())
291 .collect(Collectors.toSet()); 291 .collect(Collectors.toSet());
......
...@@ -25,10 +25,12 @@ import org.apache.felix.scr.annotations.ReferenceCardinality; ...@@ -25,10 +25,12 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
25 import org.apache.felix.scr.annotations.Service; 25 import org.apache.felix.scr.annotations.Service;
26 import org.onlab.util.GuavaCollectors; 26 import org.onlab.util.GuavaCollectors;
27 import org.onosproject.event.AbstractListenerManager; 27 import org.onosproject.event.AbstractListenerManager;
28 +import org.onosproject.net.newresource.DiscreteResourceId;
28 import org.onosproject.net.newresource.ResourceAdminService; 29 import org.onosproject.net.newresource.ResourceAdminService;
29 import org.onosproject.net.newresource.ResourceAllocation; 30 import org.onosproject.net.newresource.ResourceAllocation;
30 import org.onosproject.net.newresource.ResourceConsumer; 31 import org.onosproject.net.newresource.ResourceConsumer;
31 import org.onosproject.net.newresource.ResourceEvent; 32 import org.onosproject.net.newresource.ResourceEvent;
33 +import org.onosproject.net.newresource.ResourceId;
32 import org.onosproject.net.newresource.ResourceListener; 34 import org.onosproject.net.newresource.ResourceListener;
33 import org.onosproject.net.newresource.ResourceService; 35 import org.onosproject.net.newresource.ResourceService;
34 import org.onosproject.net.newresource.Resource; 36 import org.onosproject.net.newresource.Resource;
...@@ -115,25 +117,21 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -115,25 +117,21 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
115 } 117 }
116 118
117 @Override 119 @Override
118 - public List<ResourceAllocation> getResourceAllocations(Resource resource) { 120 + public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
119 - checkNotNull(resource); 121 + checkNotNull(id);
120 122
121 - List<ResourceConsumer> consumers = store.getConsumers(resource); 123 + return store.getResourceAllocations(id);
122 - return consumers.stream()
123 - .map(x -> new ResourceAllocation(resource, x))
124 - .collect(GuavaCollectors.toImmutableList());
125 } 124 }
126 125
127 @Override 126 @Override
128 - public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) { 127 + public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
129 checkNotNull(parent); 128 checkNotNull(parent);
130 checkNotNull(cls); 129 checkNotNull(cls);
131 130
132 // We access store twice in this method, then the store may be updated by others 131 // We access store twice in this method, then the store may be updated by others
133 Collection<Resource> resources = store.getAllocatedResources(parent, cls); 132 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
134 return resources.stream() 133 return resources.stream()
135 - .flatMap(resource -> store.getConsumers(resource).stream() 134 + .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
136 - .map(consumer -> new ResourceAllocation(resource, consumer)))
137 .collect(GuavaCollectors.toImmutableList()); 135 .collect(GuavaCollectors.toImmutableList());
138 } 136 }
139 137
...@@ -148,7 +146,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -148,7 +146,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
148 } 146 }
149 147
150 @Override 148 @Override
151 - public Set<Resource> getAvailableResources(Resource parent) { 149 + public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
152 checkNotNull(parent); 150 checkNotNull(parent);
153 151
154 Set<Resource> children = store.getChildResources(parent); 152 Set<Resource> children = store.getChildResources(parent);
...@@ -159,7 +157,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -159,7 +157,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
159 } 157 }
160 158
161 @Override 159 @Override
162 - public Set<Resource> getRegisteredResources(Resource parent) { 160 + public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
163 checkNotNull(parent); 161 checkNotNull(parent);
164 162
165 return store.getChildResources(parent); 163 return store.getChildResources(parent);
......
...@@ -19,11 +19,16 @@ import com.google.common.collect.ImmutableList; ...@@ -19,11 +19,16 @@ import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet; 19 import com.google.common.collect.ImmutableSet;
20 import org.onlab.packet.MplsLabel; 20 import org.onlab.packet.MplsLabel;
21 import org.onlab.packet.VlanId; 21 import org.onlab.packet.VlanId;
22 +import org.onosproject.net.newresource.ContinuousResourceId;
23 +import org.onosproject.net.newresource.DiscreteResource;
24 +import org.onosproject.net.newresource.DiscreteResourceId;
22 import org.onosproject.net.newresource.ResourceAllocation; 25 import org.onosproject.net.newresource.ResourceAllocation;
23 import org.onosproject.net.newresource.ResourceConsumer; 26 import org.onosproject.net.newresource.ResourceConsumer;
27 +import org.onosproject.net.newresource.ResourceId;
24 import org.onosproject.net.newresource.ResourceListener; 28 import org.onosproject.net.newresource.ResourceListener;
25 import org.onosproject.net.newresource.Resource; 29 import org.onosproject.net.newresource.Resource;
26 import org.onosproject.net.newresource.ResourceService; 30 import org.onosproject.net.newresource.ResourceService;
31 +import org.onosproject.net.newresource.Resources;
27 32
28 import java.util.Collection; 33 import java.util.Collection;
29 import java.util.HashMap; 34 import java.util.HashMap;
...@@ -70,17 +75,21 @@ class MockResourceService implements ResourceService { ...@@ -70,17 +75,21 @@ class MockResourceService implements ResourceService {
70 } 75 }
71 76
72 @Override 77 @Override
73 - public List<ResourceAllocation> getResourceAllocations(Resource resource) { 78 + public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
74 - return Optional.ofNullable(assignment.get(resource)) 79 + if (id instanceof ContinuousResourceId) {
75 - .map(x -> ImmutableList.of(new ResourceAllocation(resource, x))) 80 + return ImmutableList.of();
81 + }
82 + DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource();
83 + return Optional.ofNullable(assignment.get(discrete))
84 + .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x)))
76 .orElse(ImmutableList.of()); 85 .orElse(ImmutableList.of());
77 } 86 }
78 87
79 @Override 88 @Override
80 - public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) { 89 + public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
81 return assignment.entrySet().stream() 90 return assignment.entrySet().stream()
82 .filter(x -> x.getKey().parent().isPresent()) 91 .filter(x -> x.getKey().parent().isPresent())
83 - .filter(x -> x.getKey().parent().get().equals(parent)) 92 + .filter(x -> x.getKey().parent().get().id().equals(parent))
84 .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) 93 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
85 .collect(Collectors.toList()); 94 .collect(Collectors.toList());
86 } 95 }
...@@ -94,16 +103,15 @@ class MockResourceService implements ResourceService { ...@@ -94,16 +103,15 @@ class MockResourceService implements ResourceService {
94 } 103 }
95 104
96 @Override 105 @Override
97 - public Set<Resource> getAvailableResources(Resource parent) { 106 + public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
98 - 107 + Collection<Resource> resources = new HashSet<>();
99 - Collection<Resource> resources = new HashSet<Resource>(); 108 + resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) 10)));
100 - resources.add(parent.child(VlanId.vlanId((short) 10))); 109 + resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
101 - resources.add(parent.child(MplsLabel.mplsLabel(10)));
102 return ImmutableSet.copyOf(resources); 110 return ImmutableSet.copyOf(resources);
103 } 111 }
104 112
105 @Override 113 @Override
106 - public Set<Resource> getRegisteredResources(Resource parent) { 114 + public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
107 return getAvailableResources(parent); 115 return getAvailableResources(parent);
108 } 116 }
109 117
......