Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.onlab.util.GuavaCollectors; |
| 21 | import org.onosproject.net.resource.ContinuousResource; |
| 22 | import org.onosproject.net.resource.ContinuousResourceId; |
| 23 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 24 | import org.onosproject.net.resource.ResourceAllocation; |
| 25 | import org.onosproject.net.resource.ResourceConsumer; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.TransactionContext; |
| 27 | import org.onosproject.store.service.TransactionalMap; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.util.LinkedHashSet; |
| 32 | import java.util.List; |
| 33 | import java.util.Optional; |
| 34 | import java.util.Set; |
| 35 | import java.util.stream.Collectors; |
| 36 | |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 38 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 39 | import static org.onosproject.store.resource.impl.ResourceStoreUtil.hasEnoughResource; |
| 40 | |
| 41 | class TransactionalContinuousResourceStore { |
| 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | private final TransactionalMap<DiscreteResourceId, Set<ContinuousResource>> childMap; |
| 44 | private final TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; |
| 45 | |
| 46 | TransactionalContinuousResourceStore(TransactionContext tx) { |
| 47 | this.childMap = tx.getTransactionalMap(MapNames.CONTINUOUS_CHILD_MAP, SERIALIZER); |
| 48 | this.consumers = tx.getTransactionalMap(MapNames.CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
| 49 | } |
| 50 | |
| 51 | // iterate over the values in the set: O(n) operation |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 52 | Optional<ContinuousResource> lookup(ContinuousResourceId id) { |
| 53 | // continuous resource always has its parent |
| 54 | checkArgument(id.parent().isPresent()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 55 | |
| 56 | Set<ContinuousResource> values = childMap.get(id.parent().get()); |
| 57 | if (values == null) { |
| 58 | return Optional.empty(); |
| 59 | } |
| 60 | |
| 61 | return values.stream() |
| 62 | .filter(x -> x.id().equals(id)) |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 63 | .findFirst(); |
| 64 | } |
| 65 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 66 | boolean register(DiscreteResourceId key, List<ContinuousResource> values) { |
Sho SHIMIZU | 1bf46b1 | 2016-05-06 15:42:29 -0700 | [diff] [blame] | 67 | // short-circuit: receiving empty resource is regarded as success |
| 68 | if (values.isEmpty()) { |
| 69 | return true; |
| 70 | } |
| 71 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 72 | Set<ContinuousResource> requested = new LinkedHashSet<>(values); |
| 73 | Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested); |
| 74 | if (oldValues == null) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | Set<ContinuousResource> addedValues = Sets.difference(requested, oldValues); |
| 79 | // no new value, then no-op |
| 80 | if (addedValues.isEmpty()) { |
| 81 | // don't write to map because all values are already stored |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | Set<ContinuousResourceId> addedIds = addedValues.stream() |
| 86 | .map(ContinuousResource::id) |
| 87 | .collect(Collectors.toSet()); |
| 88 | // if the value is not found but the same ID is found |
| 89 | // (this happens only when being continuous resource) |
| 90 | if (oldValues.stream().anyMatch(x -> addedIds.contains(x.id()))) { |
| 91 | // no-op, but indicating failure (reject the request) |
| 92 | return false; |
| 93 | } |
| 94 | Set<ContinuousResource> newValues = new LinkedHashSet<>(oldValues); |
| 95 | newValues.addAll(addedValues); |
| 96 | return childMap.replace(key, oldValues, newValues); |
| 97 | } |
| 98 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 99 | boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) { |
Sho SHIMIZU | 1bf46b1 | 2016-05-06 15:42:29 -0700 | [diff] [blame] | 100 | // short-circuit: receiving empty resource is regarded as success |
| 101 | if (values.isEmpty()) { |
| 102 | return true; |
| 103 | } |
| 104 | |
Sho SHIMIZU | c7ffdfe | 2016-05-06 18:38:41 -0700 | [diff] [blame^] | 105 | // even if one of the resources is allocated to a consumer, |
| 106 | // all unregistrations are regarded as failure |
| 107 | boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id())); |
| 108 | if (allocated) { |
| 109 | log.warn("Failed to unregister {}: allocation exists", key); |
| 110 | return false; |
| 111 | } |
| 112 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 113 | Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); |
| 114 | if (oldValues == null) { |
| 115 | log.trace("No-Op removing values. key {} did not exist", key); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
| 120 | // don't write map because none of the values are stored |
| 121 | log.trace("No-Op removing values. key {} did not contain {}", key, values); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | LinkedHashSet<ContinuousResource> newValues = new LinkedHashSet<>(oldValues); |
| 126 | newValues.removeAll(values); |
| 127 | return childMap.replace(key, oldValues, newValues); |
| 128 | } |
| 129 | |
| 130 | boolean isAllocated(ContinuousResourceId id) { |
| 131 | ContinuousResourceAllocation allocations = consumers.get(id); |
| 132 | return allocations != null && !allocations.allocations().isEmpty(); |
| 133 | } |
| 134 | |
| 135 | boolean allocate(ResourceConsumer consumer, ContinuousResource request) { |
| 136 | // if the resource is not registered, then abort |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 137 | Optional<ContinuousResource> lookedUp = lookup(request.id()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 138 | if (!lookedUp.isPresent()) { |
| 139 | return false; |
| 140 | } |
| 141 | // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 142 | ContinuousResource original = lookedUp.get(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 143 | ContinuousResourceAllocation allocations = consumers.get(request.id()); |
| 144 | if (!hasEnoughResource(original, request, allocations)) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | boolean success = appendValue(original, new ResourceAllocation(request, consumer)); |
| 149 | if (!success) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | // Appends the specified ResourceAllocation to the existing values stored in the map |
| 157 | // computational complexity: O(n) where n is the number of the elements in the associated allocation |
| 158 | private boolean appendValue(ContinuousResource original, ResourceAllocation value) { |
| 159 | ContinuousResourceAllocation oldValue = consumers.putIfAbsent(original.id(), |
| 160 | new ContinuousResourceAllocation(original, ImmutableList.of(value))); |
| 161 | if (oldValue == null) { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | if (oldValue.allocations().contains(value)) { |
| 166 | // don't write to map because all values are already stored |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original, |
| 171 | ImmutableList.<ResourceAllocation>builder() |
| 172 | .addAll(oldValue.allocations()) |
| 173 | .add(value) |
| 174 | .build()); |
| 175 | return consumers.replace(original.id(), oldValue, newValue); |
| 176 | } |
| 177 | |
| 178 | boolean release(ContinuousResource resource, ResourceConsumer consumer) { |
| 179 | ContinuousResourceAllocation oldAllocation = consumers.get(resource.id()); |
| 180 | ImmutableList<ResourceAllocation> newAllocations = oldAllocation.allocations().stream() |
| 181 | .filter(x -> !(x.consumer().equals(consumer) && |
| 182 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 183 | .collect(GuavaCollectors.toImmutableList()); |
| 184 | |
| 185 | if (!consumers.replace(resource.id(), oldAllocation, |
| 186 | new ContinuousResourceAllocation(oldAllocation.original(), newAllocations))) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | } |