Committed by
Gerrit Code Review
Tighten return type to specific resource type
Change-Id: If279efae78e59de5bb8fde35e11def761158abf9
Showing
2 changed files
with
8 additions
and
10 deletions
| ... | @@ -21,7 +21,6 @@ import org.onlab.util.GuavaCollectors; | ... | @@ -21,7 +21,6 @@ import org.onlab.util.GuavaCollectors; |
| 21 | import org.onosproject.net.resource.ContinuousResource; | 21 | import org.onosproject.net.resource.ContinuousResource; |
| 22 | import org.onosproject.net.resource.ContinuousResourceId; | 22 | import org.onosproject.net.resource.ContinuousResourceId; |
| 23 | import org.onosproject.net.resource.DiscreteResourceId; | 23 | import org.onosproject.net.resource.DiscreteResourceId; |
| 24 | -import org.onosproject.net.resource.Resource; | ||
| 25 | import org.onosproject.net.resource.ResourceAllocation; | 24 | import org.onosproject.net.resource.ResourceAllocation; |
| 26 | import org.onosproject.net.resource.ResourceConsumer; | 25 | import org.onosproject.net.resource.ResourceConsumer; |
| 27 | import org.onosproject.store.service.TransactionContext; | 26 | import org.onosproject.store.service.TransactionContext; |
| ... | @@ -35,6 +34,7 @@ import java.util.Optional; | ... | @@ -35,6 +34,7 @@ import java.util.Optional; |
| 35 | import java.util.Set; | 34 | import java.util.Set; |
| 36 | import java.util.stream.Collectors; | 35 | import java.util.stream.Collectors; |
| 37 | 36 | ||
| 37 | +import static com.google.common.base.Preconditions.checkArgument; | ||
| 38 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; | 38 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 39 | import static org.onosproject.store.resource.impl.ResourceStoreUtil.hasEnoughResource; | 39 | import static org.onosproject.store.resource.impl.ResourceStoreUtil.hasEnoughResource; |
| 40 | 40 | ||
| ... | @@ -49,10 +49,9 @@ class TransactionalContinuousResourceStore { | ... | @@ -49,10 +49,9 @@ class TransactionalContinuousResourceStore { |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | // iterate over the values in the set: O(n) operation | 51 | // iterate over the values in the set: O(n) operation |
| 52 | - Optional<Resource> lookup(ContinuousResourceId id) { | 52 | + Optional<ContinuousResource> lookup(ContinuousResourceId id) { |
| 53 | - if (!id.parent().isPresent()) { | 53 | + // continuous resource always has its parent |
| 54 | - return Optional.of(Resource.ROOT); | 54 | + checkArgument(id.parent().isPresent()); |
| 55 | - } | ||
| 56 | 55 | ||
| 57 | Set<ContinuousResource> values = childMap.get(id.parent().get()); | 56 | Set<ContinuousResource> values = childMap.get(id.parent().get()); |
| 58 | if (values == null) { | 57 | if (values == null) { |
| ... | @@ -61,7 +60,6 @@ class TransactionalContinuousResourceStore { | ... | @@ -61,7 +60,6 @@ class TransactionalContinuousResourceStore { |
| 61 | 60 | ||
| 62 | return values.stream() | 61 | return values.stream() |
| 63 | .filter(x -> x.id().equals(id)) | 62 | .filter(x -> x.id().equals(id)) |
| 64 | - .map(x -> (Resource) x) | ||
| 65 | .findFirst(); | 63 | .findFirst(); |
| 66 | } | 64 | } |
| 67 | 65 | ||
| ... | @@ -128,12 +126,12 @@ class TransactionalContinuousResourceStore { | ... | @@ -128,12 +126,12 @@ class TransactionalContinuousResourceStore { |
| 128 | 126 | ||
| 129 | boolean allocate(ResourceConsumer consumer, ContinuousResource request) { | 127 | boolean allocate(ResourceConsumer consumer, ContinuousResource request) { |
| 130 | // if the resource is not registered, then abort | 128 | // if the resource is not registered, then abort |
| 131 | - Optional<Resource> lookedUp = lookup(request.id()); | 129 | + Optional<ContinuousResource> lookedUp = lookup(request.id()); |
| 132 | if (!lookedUp.isPresent()) { | 130 | if (!lookedUp.isPresent()) { |
| 133 | return false; | 131 | return false; |
| 134 | } | 132 | } |
| 135 | // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId | 133 | // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId |
| 136 | - ContinuousResource original = (ContinuousResource) lookedUp.get(); | 134 | + ContinuousResource original = lookedUp.get(); |
| 137 | ContinuousResourceAllocation allocations = consumers.get(request.id()); | 135 | ContinuousResourceAllocation allocations = consumers.get(request.id()); |
| 138 | if (!hasEnoughResource(original, request, allocations)) { | 136 | if (!hasEnoughResource(original, request, allocations)) { |
| 139 | return false; | 137 | return false; | ... | ... |
| ... | @@ -44,7 +44,7 @@ class TransactionalDiscreteResourceStore { | ... | @@ -44,7 +44,7 @@ class TransactionalDiscreteResourceStore { |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | // check the existence in the set: O(1) operation | 46 | // check the existence in the set: O(1) operation |
| 47 | - Optional<Resource> lookup(DiscreteResourceId id) { | 47 | + Optional<DiscreteResource> lookup(DiscreteResourceId id) { |
| 48 | if (!id.parent().isPresent()) { | 48 | if (!id.parent().isPresent()) { |
| 49 | return Optional.of(Resource.ROOT); | 49 | return Optional.of(Resource.ROOT); |
| 50 | } | 50 | } |
| ... | @@ -115,7 +115,7 @@ class TransactionalDiscreteResourceStore { | ... | @@ -115,7 +115,7 @@ class TransactionalDiscreteResourceStore { |
| 115 | 115 | ||
| 116 | boolean allocate(ResourceConsumer consumer, DiscreteResource resource) { | 116 | boolean allocate(ResourceConsumer consumer, DiscreteResource resource) { |
| 117 | // if the resource is not registered, then abort | 117 | // if the resource is not registered, then abort |
| 118 | - Optional<Resource> lookedUp = lookup(resource.id()); | 118 | + Optional<DiscreteResource> lookedUp = lookup(resource.id()); |
| 119 | if (!lookedUp.isPresent()) { | 119 | if (!lookedUp.isPresent()) { |
| 120 | return false; | 120 | return false; |
| 121 | } | 121 | } | ... | ... |
-
Please register or login to post a comment