Committed by
Gerrit Code Review
ONOS-3296: Support continuous type resources
Change-Id: I155e41e7a7c1750ff45986a55bedab353485d3fa
Showing
7 changed files
with
46 additions
and
33 deletions
| ... | @@ -126,13 +126,13 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource | ... | @@ -126,13 +126,13 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource |
| 126 | boolean release(ResourceConsumer consumer); | 126 | boolean release(ResourceConsumer consumer); |
| 127 | 127 | ||
| 128 | /** | 128 | /** |
| 129 | - * Returns resource allocation of the specified resource. | 129 | + * Returns resource allocations of the specified resource. |
| 130 | * | 130 | * |
| 131 | * @param resource resource to check the allocation | 131 | * @param resource resource to check the allocation |
| 132 | - * @return allocation information enclosed by Optional. | 132 | + * @return list of allocation information. |
| 133 | - * If the resource is not allocated, the return value is empty. | 133 | + * If the resource is not allocated, the return value is an empty list. |
| 134 | */ | 134 | */ |
| 135 | - Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource); | 135 | + List<ResourceAllocation> getResourceAllocation(ResourcePath resource); |
| 136 | 136 | ||
| 137 | /** | 137 | /** |
| 138 | * Returns allocated resources being as children of the specified parent and being the specified resource type. | 138 | * Returns allocated resources being as children of the specified parent and being the specified resource type. | ... | ... |
| 1 | /* | 1 | /* |
| 2 | - * Copyright 2015 Open Networking Laboratory | 2 | + * Copyright 2015-2016 Open Networking Laboratory |
| 3 | * | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. | 5 | * you may not use this file except in compliance with the License. |
| ... | @@ -20,7 +20,6 @@ import org.onosproject.store.Store; | ... | @@ -20,7 +20,6 @@ import org.onosproject.store.Store; |
| 20 | 20 | ||
| 21 | import java.util.Collection; | 21 | import java.util.Collection; |
| 22 | import java.util.List; | 22 | import java.util.List; |
| 23 | -import java.util.Optional; | ||
| 24 | 23 | ||
| 25 | /** | 24 | /** |
| 26 | * Service for storing resource and consumer information. | 25 | * Service for storing resource and consumer information. |
| ... | @@ -77,12 +76,23 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat | ... | @@ -77,12 +76,23 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat |
| 77 | boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers); | 76 | boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers); |
| 78 | 77 | ||
| 79 | /** | 78 | /** |
| 80 | - * Returns the resource consumer to whom the specified resource is allocated. | 79 | + * Returns the resource consumers to whom the specified resource is allocated. |
| 80 | + * The return value is a list having only one element when the given resource is discrete type. | ||
| 81 | + * The return value may have multiple elements when the given resource is continuous type. | ||
| 81 | * | 82 | * |
| 82 | * @param resource resource whose allocated consumer to be returned | 83 | * @param resource resource whose allocated consumer to be returned |
| 83 | - * @return resource consumer who are allocated the resource | 84 | + * @return resource consumers who are allocated the resource. |
| 85 | + * Returns empty list if there is no such consumer. | ||
| 84 | */ | 86 | */ |
| 85 | - Optional<ResourceConsumer> getConsumer(ResourcePath resource); | 87 | + List<ResourceConsumer> getConsumers(ResourcePath resource); |
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * Returns the availability of the specified resource. | ||
| 91 | + * | ||
| 92 | + * @param resource resource to check the availability | ||
| 93 | + * @return true if available, otherwise false | ||
| 94 | + */ | ||
| 95 | + boolean isAvailable(ResourcePath resource); | ||
| 86 | 96 | ||
| 87 | /** | 97 | /** |
| 88 | * Returns a collection of the resources allocated to the specified consumer. | 98 | * Returns a collection of the resources allocated to the specified consumer. | ... | ... |
| ... | @@ -313,9 +313,11 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu | ... | @@ -313,9 +313,11 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu |
| 313 | OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port()); | 313 | OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port()); |
| 314 | Optional<IntentId> intentId = | 314 | Optional<IntentId> intentId = |
| 315 | resourceService.getResourceAllocation(ResourcePath.discrete(ochCP.deviceId(), ochCP.port())) | 315 | resourceService.getResourceAllocation(ResourcePath.discrete(ochCP.deviceId(), ochCP.port())) |
| 316 | + .stream() | ||
| 316 | .map(ResourceAllocation::consumer) | 317 | .map(ResourceAllocation::consumer) |
| 317 | .filter(x -> x instanceof IntentId) | 318 | .filter(x -> x instanceof IntentId) |
| 318 | - .map(x -> (IntentId) x); | 319 | + .map(x -> (IntentId) x) |
| 320 | + .findAny(); | ||
| 319 | 321 | ||
| 320 | if (isAvailable(intentId.orElse(null))) { | 322 | if (isAvailable(intentId.orElse(null))) { |
| 321 | return ochPort; | 323 | return ochPort; |
| ... | @@ -332,9 +334,12 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu | ... | @@ -332,9 +334,12 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu |
| 332 | 334 | ||
| 333 | Optional<IntentId> intentId = | 335 | Optional<IntentId> intentId = |
| 334 | resourceService.getResourceAllocation(ResourcePath.discrete(oduPort.deviceId(), port.number())) | 336 | resourceService.getResourceAllocation(ResourcePath.discrete(oduPort.deviceId(), port.number())) |
| 337 | + .stream() | ||
| 335 | .map(ResourceAllocation::consumer) | 338 | .map(ResourceAllocation::consumer) |
| 336 | .filter(x -> x instanceof IntentId) | 339 | .filter(x -> x instanceof IntentId) |
| 337 | - .map(x -> (IntentId) x); | 340 | + .map(x -> (IntentId) x) |
| 341 | + .findAny(); | ||
| 342 | + | ||
| 338 | if (isAvailable(intentId.orElse(null))) { | 343 | if (isAvailable(intentId.orElse(null))) { |
| 339 | return (OchPort) port; | 344 | return (OchPort) port; |
| 340 | } | 345 | } | ... | ... |
| 1 | /* | 1 | /* |
| 2 | - * Copyright 2015 Open Networking Laboratory | 2 | + * Copyright 2015-2016 Open Networking Laboratory |
| 3 | * | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. | 5 | * you may not use this file except in compliance with the License. |
| ... | @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate; | ... | @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 24 | 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.onosproject.event.AbstractListenerManager; | 27 | import org.onosproject.event.AbstractListenerManager; |
| 27 | import org.onosproject.net.newresource.ResourceAdminService; | 28 | import org.onosproject.net.newresource.ResourceAdminService; |
| 28 | import org.onosproject.net.newresource.ResourceAllocation; | 29 | import org.onosproject.net.newresource.ResourceAllocation; |
| ... | @@ -34,10 +35,8 @@ import org.onosproject.net.newresource.ResourcePath; | ... | @@ -34,10 +35,8 @@ import org.onosproject.net.newresource.ResourcePath; |
| 34 | import org.onosproject.net.newresource.ResourceStore; | 35 | import org.onosproject.net.newresource.ResourceStore; |
| 35 | import org.onosproject.net.newresource.ResourceStoreDelegate; | 36 | import org.onosproject.net.newresource.ResourceStoreDelegate; |
| 36 | 37 | ||
| 37 | -import java.util.ArrayList; | ||
| 38 | import java.util.Collection; | 38 | import java.util.Collection; |
| 39 | import java.util.List; | 39 | import java.util.List; |
| 40 | -import java.util.Optional; | ||
| 41 | import java.util.stream.Collectors; | 40 | import java.util.stream.Collectors; |
| 42 | 41 | ||
| 43 | import static com.google.common.base.Preconditions.checkNotNull; | 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| ... | @@ -107,11 +106,13 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -107,11 +106,13 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
| 107 | } | 106 | } |
| 108 | 107 | ||
| 109 | @Override | 108 | @Override |
| 110 | - public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) { | 109 | + public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) { |
| 111 | checkNotNull(resource); | 110 | checkNotNull(resource); |
| 112 | 111 | ||
| 113 | - Optional<ResourceConsumer> consumer = store.getConsumer(resource); | 112 | + List<ResourceConsumer> consumers = store.getConsumers(resource); |
| 114 | - return consumer.map(x -> new ResourceAllocation(resource, x)); | 113 | + return consumers.stream() |
| 114 | + .map(x -> new ResourceAllocation(resource, x)) | ||
| 115 | + .collect(GuavaCollectors.toImmutableList()); | ||
| 115 | } | 116 | } |
| 116 | 117 | ||
| 117 | @Override | 118 | @Override |
| ... | @@ -119,17 +120,12 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -119,17 +120,12 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
| 119 | checkNotNull(parent); | 120 | checkNotNull(parent); |
| 120 | checkNotNull(cls); | 121 | checkNotNull(cls); |
| 121 | 122 | ||
| 123 | + // We access store twice in this method, then the store may be updated by others | ||
| 122 | Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls); | 124 | Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls); |
| 123 | - List<ResourceAllocation> allocations = new ArrayList<>(resources.size()); | 125 | + return resources.stream() |
| 124 | - for (ResourcePath resource: resources) { | 126 | + .flatMap(resource -> store.getConsumers(resource).stream() |
| 125 | - // We access store twice in this method, then the store may be updated by others | 127 | + .map(consumer -> new ResourceAllocation(resource, consumer))) |
| 126 | - Optional<ResourceConsumer> consumer = store.getConsumer(resource); | 128 | + .collect(GuavaCollectors.toImmutableList()); |
| 127 | - if (consumer.isPresent()) { | ||
| 128 | - allocations.add(new ResourceAllocation(resource, consumer.get())); | ||
| 129 | - } | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | - return allocations; | ||
| 133 | } | 129 | } |
| 134 | 130 | ||
| 135 | @Override | 131 | @Override |
| ... | @@ -149,7 +145,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -149,7 +145,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
| 149 | Collection<ResourcePath> children = store.getChildResources(parent); | 145 | Collection<ResourcePath> children = store.getChildResources(parent); |
| 150 | return children.stream() | 146 | return children.stream() |
| 151 | // We access store twice in this method, then the store may be updated by others | 147 | // We access store twice in this method, then the store may be updated by others |
| 152 | - .filter(x -> !store.getConsumer(x).isPresent()) | 148 | + .filter(store::isAvailable) |
| 153 | .collect(Collectors.toList()); | 149 | .collect(Collectors.toList()); |
| 154 | } | 150 | } |
| 155 | 151 | ||
| ... | @@ -157,8 +153,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -157,8 +153,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
| 157 | public boolean isAvailable(ResourcePath resource) { | 153 | public boolean isAvailable(ResourcePath resource) { |
| 158 | checkNotNull(resource); | 154 | checkNotNull(resource); |
| 159 | 155 | ||
| 160 | - Optional<ResourceConsumer> consumer = store.getConsumer(resource); | 156 | + return store.isAvailable(resource); |
| 161 | - return !consumer.isPresent(); | ||
| 162 | } | 157 | } |
| 163 | 158 | ||
| 164 | @Override | 159 | @Override | ... | ... |
| ... | @@ -66,9 +66,10 @@ class MockResourceService implements ResourceService { | ... | @@ -66,9 +66,10 @@ class MockResourceService implements ResourceService { |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | @Override | 68 | @Override |
| 69 | - public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) { | 69 | + public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) { |
| 70 | return Optional.ofNullable(assignment.get(resource)) | 70 | return Optional.ofNullable(assignment.get(resource)) |
| 71 | - .map(x -> new ResourceAllocation(resource, x)); | 71 | + .map(x -> ImmutableList.of(new ResourceAllocation(resource, x))) |
| 72 | + .orElse(ImmutableList.of()); | ||
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | @Override | 75 | @Override | ... | ... |
This diff is collapsed. Click to expand it.
| ... | @@ -204,6 +204,7 @@ import java.util.Arrays; | ... | @@ -204,6 +204,7 @@ import java.util.Arrays; |
| 204 | import java.util.Collections; | 204 | import java.util.Collections; |
| 205 | import java.util.HashMap; | 205 | import java.util.HashMap; |
| 206 | import java.util.HashSet; | 206 | import java.util.HashSet; |
| 207 | +import java.util.LinkedHashSet; | ||
| 207 | import java.util.LinkedList; | 208 | import java.util.LinkedList; |
| 208 | import java.util.Optional; | 209 | import java.util.Optional; |
| 209 | import java.util.concurrent.ConcurrentHashMap; | 210 | import java.util.concurrent.ConcurrentHashMap; |
| ... | @@ -239,7 +240,8 @@ public final class KryoNamespaces { | ... | @@ -239,7 +240,8 @@ public final class KryoNamespaces { |
| 239 | .register(CopyOnWriteArraySet.class) | 240 | .register(CopyOnWriteArraySet.class) |
| 240 | .register(ArrayList.class, | 241 | .register(ArrayList.class, |
| 241 | LinkedList.class, | 242 | LinkedList.class, |
| 242 | - HashSet.class | 243 | + HashSet.class, |
| 244 | + LinkedHashSet.class | ||
| 243 | ) | 245 | ) |
| 244 | .register(Maps.immutableEntry("a", "b").getClass()) | 246 | .register(Maps.immutableEntry("a", "b").getClass()) |
| 245 | .register(new ArraysAsListSerializer(), Arrays.asList().getClass()) | 247 | .register(new ArraysAsListSerializer(), Arrays.asList().getClass()) | ... | ... |
-
Please register or login to post a comment