Committed by
Gerrit Code Review
Use ResourceId when unregistering resources instead of Resource
Change-Id: Ib3d1c611ad9eb4491693ea10ee50ea0123f20ee2
Showing
6 changed files
with
44 additions
and
43 deletions
... | @@ -48,22 +48,21 @@ public interface ResourceAdminService { | ... | @@ -48,22 +48,21 @@ public interface ResourceAdminService { |
48 | /** | 48 | /** |
49 | * Unregisters the specified resources. | 49 | * Unregisters the specified resources. |
50 | * | 50 | * |
51 | - * @param resources resources to be unregistered | 51 | + * @param ids IDs of resources to be unregistered |
52 | * @return true if unregistration is successfully done, false otherwise. Unregistration | 52 | * @return true if unregistration is successfully done, false otherwise. Unregistration |
53 | * succeeds when each resource is not registered or unallocated. | 53 | * succeeds when each resource is not registered or unallocated. |
54 | */ | 54 | */ |
55 | - // TODO: might need to change the first argument type to ResourceId | 55 | + default boolean unregisterResources(ResourceId... ids) { |
56 | - default boolean unregisterResources(Resource... resources) { | 56 | + return unregisterResources(ImmutableList.copyOf(ids)); |
57 | - return unregisterResources(ImmutableList.copyOf(resources)); | ||
58 | } | 57 | } |
59 | 58 | ||
60 | /** | 59 | /** |
61 | * Unregisters the specified resources. | 60 | * Unregisters the specified resources. |
62 | * | 61 | * |
63 | - * @param resources resources to be unregistered | 62 | + * @param ids IDs of resources to be unregistered |
64 | * @return true if unregistration is successfully done, false otherwise. Unregistration | 63 | * @return true if unregistration is successfully done, false otherwise. Unregistration |
65 | * succeeds when each resource is not registered or unallocated. | 64 | * succeeds when each resource is not registered or unallocated. |
66 | */ | 65 | */ |
67 | // TODO: might need to change the first argument type to ResourceId | 66 | // TODO: might need to change the first argument type to ResourceId |
68 | - boolean unregisterResources(List<Resource> resources); | 67 | + boolean unregisterResources(List<ResourceId> ids); |
69 | } | 68 | } | ... | ... |
... | @@ -45,10 +45,10 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat | ... | @@ -45,10 +45,10 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat |
45 | * or none of the given resources is unregistered. The whole unregistration fails when any one of the | 45 | * or none of the given resources is unregistered. The whole unregistration fails when any one of the |
46 | * resource can't be unregistered. | 46 | * resource can't be unregistered. |
47 | * | 47 | * |
48 | - * @param resources resources to be unregistered | 48 | + * @param ids resources to be unregistered |
49 | * @return true if the registration succeeds, false otherwise | 49 | * @return true if the registration succeeds, false otherwise |
50 | */ | 50 | */ |
51 | - boolean unregister(List<Resource> resources); | 51 | + boolean unregister(List<ResourceId> ids); |
52 | 52 | ||
53 | /** | 53 | /** |
54 | * Allocates the specified resources to the specified consumer in transactional way. | 54 | * Allocates the specified resources to the specified consumer in transactional way. | ... | ... |
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onosproject.net.newresource.impl; | 16 | package org.onosproject.net.newresource.impl; |
17 | 17 | ||
18 | import com.google.common.collect.ImmutableSet; | 18 | import com.google.common.collect.ImmutableSet; |
19 | +import com.google.common.collect.Lists; | ||
19 | import org.onlab.packet.MplsLabel; | 20 | import org.onlab.packet.MplsLabel; |
20 | import org.onlab.packet.VlanId; | 21 | import org.onlab.packet.VlanId; |
21 | import org.onlab.util.Bandwidth; | 22 | import org.onlab.util.Bandwidth; |
... | @@ -133,7 +134,7 @@ final class ResourceDeviceListener implements DeviceListener { | ... | @@ -133,7 +134,7 @@ final class ResourceDeviceListener implements DeviceListener { |
133 | executor.submit(() -> { | 134 | executor.submit(() -> { |
134 | DiscreteResource devResource = Resources.discrete(device.id()).resource(); | 135 | DiscreteResource devResource = Resources.discrete(device.id()).resource(); |
135 | List<Resource> allResources = getDescendantResources(devResource); | 136 | List<Resource> allResources = getDescendantResources(devResource); |
136 | - adminService.unregisterResources(allResources); | 137 | + adminService.unregisterResources(Lists.transform(allResources, Resource::id)); |
137 | }); | 138 | }); |
138 | } | 139 | } |
139 | 140 | ||
... | @@ -189,7 +190,7 @@ final class ResourceDeviceListener implements DeviceListener { | ... | @@ -189,7 +190,7 @@ final class ResourceDeviceListener implements DeviceListener { |
189 | executor.submit(() -> { | 190 | executor.submit(() -> { |
190 | DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource(); | 191 | DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource(); |
191 | List<Resource> allResources = getDescendantResources(portResource); | 192 | List<Resource> allResources = getDescendantResources(portResource); |
192 | - adminService.unregisterResources(allResources); | 193 | + adminService.unregisterResources(Lists.transform(allResources, Resource::id)); |
193 | }); | 194 | }); |
194 | } | 195 | } |
195 | 196 | ... | ... |
... | @@ -178,10 +178,10 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -178,10 +178,10 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
178 | } | 178 | } |
179 | 179 | ||
180 | @Override | 180 | @Override |
181 | - public boolean unregisterResources(List<Resource> resources) { | 181 | + public boolean unregisterResources(List<ResourceId> ids) { |
182 | - checkNotNull(resources); | 182 | + checkNotNull(ids); |
183 | 183 | ||
184 | - return store.unregister(resources); | 184 | + return store.unregister(ids); |
185 | } | 185 | } |
186 | 186 | ||
187 | private class InternalStoreDelegate implements ResourceStoreDelegate { | 187 | private class InternalStoreDelegate implements ResourceStoreDelegate { | ... | ... |
... | @@ -117,7 +117,7 @@ final class ResourceNetworkConfigListener implements NetworkConfigListener { | ... | @@ -117,7 +117,7 @@ final class ResourceNetworkConfigListener implements NetworkConfigListener { |
117 | // FIXME Following should be an update to the value based on port speed | 117 | // FIXME Following should be an update to the value based on port speed |
118 | if (!adminService.unregisterResources(Resources.continuous(cp.deviceId(), | 118 | if (!adminService.unregisterResources(Resources.continuous(cp.deviceId(), |
119 | cp.port(), | 119 | cp.port(), |
120 | - Bandwidth.class).resource(0))) { | 120 | + Bandwidth.class).id())) { |
121 | log.warn("Failed to unregister Bandwidth for {}", cp); | 121 | log.warn("Failed to unregister Bandwidth for {}", cp); |
122 | } | 122 | } |
123 | break; | 123 | break; |
... | @@ -148,7 +148,7 @@ final class ResourceNetworkConfigListener implements NetworkConfigListener { | ... | @@ -148,7 +148,7 @@ final class ResourceNetworkConfigListener implements NetworkConfigListener { |
148 | // returns true (success) | 148 | // returns true (success) |
149 | 149 | ||
150 | if (!adminService.unregisterResources( | 150 | if (!adminService.unregisterResources( |
151 | - Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).resource(0))) { | 151 | + Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).id())) { |
152 | log.warn("unregisterResources for {} failed", cp); | 152 | log.warn("unregisterResources for {} failed", cp); |
153 | } | 153 | } |
154 | return adminService.registerResources(Resources.continuous(cp.deviceId(), | 154 | return adminService.registerResources(Resources.continuous(cp.deviceId(), | ... | ... |
... | @@ -167,8 +167,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -167,8 +167,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
167 | .collect(Collectors.groupingBy(x -> x.parent().get())); | 167 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
168 | 168 | ||
169 | for (Map.Entry<DiscreteResource, List<Resource>> entry: resourceMap.entrySet()) { | 169 | for (Map.Entry<DiscreteResource, List<Resource>> entry: resourceMap.entrySet()) { |
170 | - Optional<DiscreteResource> child = lookup(childTxMap, entry.getKey()); | 170 | + if (!lookup(childTxMap, entry.getKey().id()).isPresent()) { |
171 | - if (!child.isPresent()) { | ||
172 | return abortTransaction(tx); | 171 | return abortTransaction(tx); |
173 | } | 172 | } |
174 | 173 | ||
... | @@ -189,8 +188,8 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -189,8 +188,8 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
189 | } | 188 | } |
190 | 189 | ||
191 | @Override | 190 | @Override |
192 | - public boolean unregister(List<Resource> resources) { | 191 | + public boolean unregister(List<ResourceId> ids) { |
193 | - checkNotNull(resources); | 192 | + checkNotNull(ids); |
194 | 193 | ||
195 | TransactionContext tx = service.transactionContextBuilder().build(); | 194 | TransactionContext tx = service.transactionContextBuilder().build(); |
196 | tx.begin(); | 195 | tx.begin(); |
... | @@ -203,8 +202,13 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -203,8 +202,13 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
203 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); | 202 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
204 | 203 | ||
205 | // Extract Discrete instances from resources | 204 | // Extract Discrete instances from resources |
206 | - Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() | 205 | + List<Resource> resources = ids.stream() |
207 | .filter(x -> x.parent().isPresent()) | 206 | .filter(x -> x.parent().isPresent()) |
207 | + .map(x -> lookup(childTxMap, x)) | ||
208 | + .filter(Optional::isPresent) | ||
209 | + .map(Optional::get) | ||
210 | + .collect(Collectors.toList()); | ||
211 | + Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() | ||
208 | .collect(Collectors.groupingBy(x -> x.parent().get().id())); | 212 | .collect(Collectors.groupingBy(x -> x.parent().get().id())); |
209 | 213 | ||
210 | // even if one of the resources is allocated to a consumer, | 214 | // even if one of the resources is allocated to a consumer, |
... | @@ -241,7 +245,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -241,7 +245,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
241 | .collect(Collectors.toList()); | 245 | .collect(Collectors.toList()); |
242 | notifyDelegate(events); | 246 | notifyDelegate(events); |
243 | } else { | 247 | } else { |
244 | - log.warn("Failed to unregister {}: Commit failed.", resources); | 248 | + log.warn("Failed to unregister {}: Commit failed.", ids); |
245 | } | 249 | } |
246 | return success; | 250 | return success; |
247 | } | 251 | } |
... | @@ -263,7 +267,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -263,7 +267,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
263 | 267 | ||
264 | for (Resource resource: resources) { | 268 | for (Resource resource: resources) { |
265 | if (resource instanceof DiscreteResource) { | 269 | if (resource instanceof DiscreteResource) { |
266 | - if (!lookup(childTxMap, resource).isPresent()) { | 270 | + if (!lookup(childTxMap, resource.id()).isPresent()) { |
267 | return abortTransaction(tx); | 271 | return abortTransaction(tx); |
268 | } | 272 | } |
269 | 273 | ||
... | @@ -272,18 +276,20 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -272,18 +276,20 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
272 | return abortTransaction(tx); | 276 | return abortTransaction(tx); |
273 | } | 277 | } |
274 | } else if (resource instanceof ContinuousResource) { | 278 | } else if (resource instanceof ContinuousResource) { |
275 | - Optional<ContinuousResource> continuous = lookup(childTxMap, (ContinuousResource) resource); | 279 | + Optional<Resource> lookedUp = lookup(childTxMap, resource.id()); |
276 | - if (!continuous.isPresent()) { | 280 | + if (!lookedUp.isPresent()) { |
277 | return abortTransaction(tx); | 281 | return abortTransaction(tx); |
278 | } | 282 | } |
279 | 283 | ||
280 | - ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.get().id()); | 284 | + // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId |
281 | - if (!hasEnoughResource(continuous.get(), (ContinuousResource) resource, allocations)) { | 285 | + ContinuousResource continuous = (ContinuousResource) lookedUp.get(); |
286 | + ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.id()); | ||
287 | + if (!hasEnoughResource(continuous, (ContinuousResource) resource, allocations)) { | ||
282 | return abortTransaction(tx); | 288 | return abortTransaction(tx); |
283 | } | 289 | } |
284 | 290 | ||
285 | boolean success = appendValue(continuousConsumerTxMap, | 291 | boolean success = appendValue(continuousConsumerTxMap, |
286 | - continuous.get(), new ResourceAllocation(continuous.get(), consumer)); | 292 | + continuous, new ResourceAllocation(continuous, consumer)); |
287 | if (!success) { | 293 | if (!success) { |
288 | return abortTransaction(tx); | 294 | return abortTransaction(tx); |
289 | } | 295 | } |
... | @@ -534,34 +540,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -534,34 +540,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
534 | } | 540 | } |
535 | 541 | ||
536 | /** | 542 | /** |
537 | - * Returns the resource which has the same key as the key of the specified resource | 543 | + * Returns the resource which has the same key as the specified resource ID |
538 | - * in the list as a value of the map. | 544 | + * in the set as a value of the map. |
539 | * | 545 | * |
540 | - * @param map map storing parent - child relationship of resources | 546 | + * @param childTxMap map storing parent - child relationship of resources |
541 | - * @param resource resource to be checked for its key | 547 | + * @param id ID of resource to be checked |
542 | * @return the resource which is regarded as the same as the specified resource | 548 | * @return the resource which is regarded as the same as the specified resource |
543 | */ | 549 | */ |
544 | - // Naive implementation, which traverses all elements in the list | 550 | + // Naive implementation, which traverses all elements in the set |
545 | // computational complexity: O(n) where n is the number of elements | 551 | // computational complexity: O(n) where n is the number of elements |
546 | // in the associated set | 552 | // in the associated set |
547 | - private <T extends Resource> Optional<T> lookup( | 553 | + private Optional<Resource> lookup(TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap, ResourceId id) { |
548 | - TransactionalMap<DiscreteResourceId, Set<Resource>> map, T resource) { | 554 | + if (!id.parent().isPresent()) { |
549 | - // if it is root, always returns itself | 555 | + return Optional.of(Resource.ROOT); |
550 | - if (!resource.parent().isPresent()) { | ||
551 | - return Optional.of(resource); | ||
552 | } | 556 | } |
553 | 557 | ||
554 | - Set<Resource> values = map.get(resource.parent().get().id()); | 558 | + Set<Resource> values = childTxMap.get(id.parent().get()); |
555 | if (values == null) { | 559 | if (values == null) { |
556 | return Optional.empty(); | 560 | return Optional.empty(); |
557 | } | 561 | } |
558 | 562 | ||
559 | - @SuppressWarnings("unchecked") | 563 | + return values.stream() |
560 | - Optional<T> result = values.stream() | 564 | + .filter(x -> x.id().equals(id)) |
561 | - .filter(x -> x.id().equals(resource.id())) | ||
562 | - .map(x -> (T) x) | ||
563 | .findFirst(); | 565 | .findFirst(); |
564 | - return result; | ||
565 | } | 566 | } |
566 | 567 | ||
567 | /** | 568 | /** | ... | ... |
-
Please register or login to post a comment