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; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 20 | import org.onosproject.net.resource.ContinuousResource; |
| 21 | import org.onosproject.net.resource.ContinuousResourceId; |
| 22 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 23 | import org.onosproject.net.resource.ResourceAllocation; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 24 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.TransactionContext; |
| 26 | import org.onosproject.store.service.TransactionalMap; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.LinkedHashSet; |
| 31 | import java.util.List; |
| 32 | import java.util.Optional; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 36 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 37 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 38 | import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 39 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame^] | 40 | class TransactionalContinuousResourceSubStore { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 41 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 42 | private final TransactionalMap<DiscreteResourceId, Set<ContinuousResource>> childMap; |
| 43 | private final TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; |
| 44 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame^] | 45 | TransactionalContinuousResourceSubStore(TransactionContext tx) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 46 | this.childMap = tx.getTransactionalMap(MapNames.CONTINUOUS_CHILD_MAP, SERIALIZER); |
| 47 | this.consumers = tx.getTransactionalMap(MapNames.CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
| 48 | } |
| 49 | |
| 50 | // iterate over the values in the set: O(n) operation |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 51 | Optional<ContinuousResource> lookup(ContinuousResourceId id) { |
| 52 | // continuous resource always has its parent |
| 53 | checkArgument(id.parent().isPresent()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 54 | |
| 55 | Set<ContinuousResource> values = childMap.get(id.parent().get()); |
| 56 | if (values == null) { |
| 57 | return Optional.empty(); |
| 58 | } |
| 59 | |
| 60 | return values.stream() |
| 61 | .filter(x -> x.id().equals(id)) |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 62 | .findFirst(); |
| 63 | } |
| 64 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 65 | boolean register(DiscreteResourceId key, List<ContinuousResource> values) { |
Sho SHIMIZU | 1bf46b1 | 2016-05-06 15:42:29 -0700 | [diff] [blame] | 66 | // short-circuit: receiving empty resource is regarded as success |
| 67 | if (values.isEmpty()) { |
| 68 | return true; |
| 69 | } |
| 70 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 71 | Set<ContinuousResource> requested = new LinkedHashSet<>(values); |
| 72 | Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested); |
| 73 | if (oldValues == null) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | Set<ContinuousResource> addedValues = Sets.difference(requested, oldValues); |
| 78 | // no new value, then no-op |
| 79 | if (addedValues.isEmpty()) { |
| 80 | // don't write to map because all values are already stored |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | Set<ContinuousResourceId> addedIds = addedValues.stream() |
| 85 | .map(ContinuousResource::id) |
| 86 | .collect(Collectors.toSet()); |
| 87 | // if the value is not found but the same ID is found |
| 88 | // (this happens only when being continuous resource) |
| 89 | if (oldValues.stream().anyMatch(x -> addedIds.contains(x.id()))) { |
| 90 | // no-op, but indicating failure (reject the request) |
| 91 | return false; |
| 92 | } |
| 93 | Set<ContinuousResource> newValues = new LinkedHashSet<>(oldValues); |
| 94 | newValues.addAll(addedValues); |
| 95 | return childMap.replace(key, oldValues, newValues); |
| 96 | } |
| 97 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 98 | boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) { |
Sho SHIMIZU | 1bf46b1 | 2016-05-06 15:42:29 -0700 | [diff] [blame] | 99 | // short-circuit: receiving empty resource is regarded as success |
| 100 | if (values.isEmpty()) { |
| 101 | return true; |
| 102 | } |
| 103 | |
Sho SHIMIZU | c7ffdfe | 2016-05-06 18:38:41 -0700 | [diff] [blame] | 104 | // even if one of the resources is allocated to a consumer, |
| 105 | // all unregistrations are regarded as failure |
| 106 | boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id())); |
| 107 | if (allocated) { |
| 108 | log.warn("Failed to unregister {}: allocation exists", key); |
| 109 | return false; |
| 110 | } |
| 111 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 112 | Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); |
| 113 | if (oldValues == null) { |
| 114 | log.trace("No-Op removing values. key {} did not exist", key); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
| 119 | // don't write map because none of the values are stored |
| 120 | log.trace("No-Op removing values. key {} did not contain {}", key, values); |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | LinkedHashSet<ContinuousResource> newValues = new LinkedHashSet<>(oldValues); |
| 125 | newValues.removeAll(values); |
| 126 | return childMap.replace(key, oldValues, newValues); |
| 127 | } |
| 128 | |
Sho SHIMIZU | d82dc5b | 2016-05-13 13:50:29 -0700 | [diff] [blame] | 129 | private boolean isAllocated(ContinuousResourceId id) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 130 | ContinuousResourceAllocation allocations = consumers.get(id); |
| 131 | return allocations != null && !allocations.allocations().isEmpty(); |
| 132 | } |
| 133 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 134 | boolean allocate(ResourceConsumerId consumerId, ContinuousResource request) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 135 | // if the resource is not registered, then abort |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 136 | Optional<ContinuousResource> lookedUp = lookup(request.id()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 137 | if (!lookedUp.isPresent()) { |
| 138 | return false; |
| 139 | } |
| 140 | // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId |
Sho SHIMIZU | a81141b | 2016-05-11 08:05:45 -0700 | [diff] [blame] | 141 | ContinuousResource original = lookedUp.get(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 142 | ContinuousResourceAllocation allocations = consumers.get(request.id()); |
| 143 | if (!hasEnoughResource(original, request, allocations)) { |
| 144 | return false; |
| 145 | } |
| 146 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 147 | return appendValue(original, new ResourceAllocation(request, consumerId)); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Appends the specified ResourceAllocation to the existing values stored in the map |
| 151 | // computational complexity: O(n) where n is the number of the elements in the associated allocation |
| 152 | private boolean appendValue(ContinuousResource original, ResourceAllocation value) { |
| 153 | ContinuousResourceAllocation oldValue = consumers.putIfAbsent(original.id(), |
| 154 | new ContinuousResourceAllocation(original, ImmutableList.of(value))); |
| 155 | if (oldValue == null) { |
| 156 | return true; |
| 157 | } |
| 158 | |
Sho SHIMIZU | efb7511 | 2016-05-13 15:24:34 -0700 | [diff] [blame] | 159 | ContinuousResourceAllocation newValue = oldValue.allocate(original, value); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 160 | return consumers.replace(original.id(), oldValue, newValue); |
| 161 | } |
| 162 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 163 | boolean release(ContinuousResource resource, ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 164 | ContinuousResourceAllocation oldAllocation = consumers.get(resource.id()); |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 165 | ContinuousResourceAllocation newAllocation = oldAllocation.release(resource, consumerId); |
Satish K | b05d70a | 2016-05-13 17:04:49 +0530 | [diff] [blame] | 166 | |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 167 | return consumers.replace(resource.id(), oldAllocation, newAllocation); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 168 | } |
| 169 | } |