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