blob: c8b6c2083eab26f79156b5902445322f423af2d8 [file] [log] [blame]
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07001/*
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 */
16package org.onosproject.store.resource.impl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Sets;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070020import org.onosproject.net.resource.ContinuousResource;
21import org.onosproject.net.resource.ContinuousResourceId;
22import org.onosproject.net.resource.DiscreteResourceId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070023import org.onosproject.net.resource.ResourceAllocation;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070024import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070025import org.onosproject.store.service.TransactionContext;
26import org.onosproject.store.service.TransactionalMap;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.LinkedHashSet;
31import java.util.List;
32import java.util.Optional;
33import java.util.Set;
34import java.util.stream.Collectors;
35
Sho SHIMIZUa81141b2016-05-11 08:05:45 -070036import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070037import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
38import static org.onosproject.store.resource.impl.ResourceStoreUtil.hasEnoughResource;
39
40class TransactionalContinuousResourceStore {
41 private final Logger log = LoggerFactory.getLogger(getClass());
42 private final TransactionalMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
43 private final TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
44
45 TransactionalContinuousResourceStore(TransactionContext tx) {
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 SHIMIZUa81141b2016-05-11 08:05:45 -070051 Optional<ContinuousResource> lookup(ContinuousResourceId id) {
52 // continuous resource always has its parent
53 checkArgument(id.parent().isPresent());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070054
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 SHIMIZU22fb2832016-05-06 11:44:03 -070062 .findFirst();
63 }
64
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070065 boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070066 // short-circuit: receiving empty resource is regarded as success
67 if (values.isEmpty()) {
68 return true;
69 }
70
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070071 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 SHIMIZUa2d99eb2016-05-06 14:52:55 -070098 boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070099 // short-circuit: receiving empty resource is regarded as success
100 if (values.isEmpty()) {
101 return true;
102 }
103
Sho SHIMIZUc7ffdfe2016-05-06 18:38:41 -0700104 // 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 SHIMIZU22fb2832016-05-06 11:44:03 -0700112 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 SHIMIZUd82dc5b2016-05-13 13:50:29 -0700129 private boolean isAllocated(ContinuousResourceId id) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700130 ContinuousResourceAllocation allocations = consumers.get(id);
131 return allocations != null && !allocations.allocations().isEmpty();
132 }
133
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700134 boolean allocate(ResourceConsumerId consumerId, ContinuousResource request) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700135 // if the resource is not registered, then abort
Sho SHIMIZUa81141b2016-05-11 08:05:45 -0700136 Optional<ContinuousResource> lookedUp = lookup(request.id());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700137 if (!lookedUp.isPresent()) {
138 return false;
139 }
140 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
Sho SHIMIZUa81141b2016-05-11 08:05:45 -0700141 ContinuousResource original = lookedUp.get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700142 ContinuousResourceAllocation allocations = consumers.get(request.id());
143 if (!hasEnoughResource(original, request, allocations)) {
144 return false;
145 }
146
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700147 return appendValue(original, new ResourceAllocation(request, consumerId));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700148 }
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 SHIMIZU22fb2832016-05-06 11:44:03 -0700159 ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original,
160 ImmutableList.<ResourceAllocation>builder()
161 .addAll(oldValue.allocations())
162 .add(value)
163 .build());
164 return consumers.replace(original.id(), oldValue, newValue);
165 }
166
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700167 boolean release(ContinuousResource resource, ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700168 ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
Sho SHIMIZUb2183962016-05-13 15:03:50 -0700169 ContinuousResourceAllocation newAllocation = oldAllocation.release(resource, consumerId);
Satish Kb05d70a2016-05-13 17:04:49 +0530170
Sho SHIMIZUb2183962016-05-13 15:03:50 -0700171 return consumers.replace(resource.id(), oldAllocation, newAllocation);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700172 }
173}