blob: 7a630fad9f2c1bf7d19aa2680174cb4920b0c285 [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;
20import org.onlab.util.GuavaCollectors;
21import org.onosproject.net.resource.ContinuousResource;
22import org.onosproject.net.resource.ContinuousResourceId;
23import org.onosproject.net.resource.DiscreteResourceId;
24import org.onosproject.net.resource.Resource;
25import org.onosproject.net.resource.ResourceAllocation;
26import org.onosproject.net.resource.ResourceConsumer;
27import org.onosproject.store.resource.impl.ConsistentResourceStore.ContinuousResourceAllocation;
28import org.onosproject.store.service.TransactionContext;
29import org.onosproject.store.service.TransactionalMap;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.LinkedHashSet;
34import java.util.List;
35import java.util.Optional;
36import java.util.Set;
37import java.util.stream.Collectors;
38
39import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
40import static org.onosproject.store.resource.impl.ResourceStoreUtil.hasEnoughResource;
41
42class TransactionalContinuousResourceStore {
43 private final Logger log = LoggerFactory.getLogger(getClass());
44 private final TransactionalMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
45 private final TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
46
47 TransactionalContinuousResourceStore(TransactionContext tx) {
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
53 Optional<Resource> lookup(ContinuousResourceId id) {
54 if (!id.parent().isPresent()) {
55 return Optional.of(Resource.ROOT);
56 }
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))
65 .map(x -> (Resource) x)
66 .findFirst();
67 }
68
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070069 boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070070 // short-circuit: receiving empty resource is regarded as success
71 if (values.isEmpty()) {
72 return true;
73 }
74
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070075 Set<ContinuousResource> requested = new LinkedHashSet<>(values);
76 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
77 if (oldValues == null) {
78 return true;
79 }
80
81 Set<ContinuousResource> addedValues = Sets.difference(requested, oldValues);
82 // no new value, then no-op
83 if (addedValues.isEmpty()) {
84 // don't write to map because all values are already stored
85 return true;
86 }
87
88 Set<ContinuousResourceId> addedIds = addedValues.stream()
89 .map(ContinuousResource::id)
90 .collect(Collectors.toSet());
91 // if the value is not found but the same ID is found
92 // (this happens only when being continuous resource)
93 if (oldValues.stream().anyMatch(x -> addedIds.contains(x.id()))) {
94 // no-op, but indicating failure (reject the request)
95 return false;
96 }
97 Set<ContinuousResource> newValues = new LinkedHashSet<>(oldValues);
98 newValues.addAll(addedValues);
99 return childMap.replace(key, oldValues, newValues);
100 }
101
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -0700102 boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -0700103 // short-circuit: receiving empty resource is regarded as success
104 if (values.isEmpty()) {
105 return true;
106 }
107
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700108 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
109 if (oldValues == null) {
110 log.trace("No-Op removing values. key {} did not exist", key);
111 return true;
112 }
113
114 if (values.stream().allMatch(x -> !oldValues.contains(x))) {
115 // don't write map because none of the values are stored
116 log.trace("No-Op removing values. key {} did not contain {}", key, values);
117 return true;
118 }
119
120 LinkedHashSet<ContinuousResource> newValues = new LinkedHashSet<>(oldValues);
121 newValues.removeAll(values);
122 return childMap.replace(key, oldValues, newValues);
123 }
124
125 boolean isAllocated(ContinuousResourceId id) {
126 ContinuousResourceAllocation allocations = consumers.get(id);
127 return allocations != null && !allocations.allocations().isEmpty();
128 }
129
130 boolean allocate(ResourceConsumer consumer, ContinuousResource request) {
131 // if the resource is not registered, then abort
132 Optional<Resource> lookedUp = lookup(request.id());
133 if (!lookedUp.isPresent()) {
134 return false;
135 }
136 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
137 ContinuousResource original = (ContinuousResource) lookedUp.get();
138 ContinuousResourceAllocation allocations = consumers.get(request.id());
139 if (!hasEnoughResource(original, request, allocations)) {
140 return false;
141 }
142
143 boolean success = appendValue(original, new ResourceAllocation(request, consumer));
144 if (!success) {
145 return false;
146 }
147
148 return true;
149 }
150
151 // Appends the specified ResourceAllocation to the existing values stored in the map
152 // computational complexity: O(n) where n is the number of the elements in the associated allocation
153 private boolean appendValue(ContinuousResource original, ResourceAllocation value) {
154 ContinuousResourceAllocation oldValue = consumers.putIfAbsent(original.id(),
155 new ContinuousResourceAllocation(original, ImmutableList.of(value)));
156 if (oldValue == null) {
157 return true;
158 }
159
160 if (oldValue.allocations().contains(value)) {
161 // don't write to map because all values are already stored
162 return true;
163 }
164
165 ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original,
166 ImmutableList.<ResourceAllocation>builder()
167 .addAll(oldValue.allocations())
168 .add(value)
169 .build());
170 return consumers.replace(original.id(), oldValue, newValue);
171 }
172
173 boolean release(ContinuousResource resource, ResourceConsumer consumer) {
174 ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
175 ImmutableList<ResourceAllocation> newAllocations = oldAllocation.allocations().stream()
176 .filter(x -> !(x.consumer().equals(consumer) &&
177 ((ContinuousResource) x.resource()).value() == resource.value()))
178 .collect(GuavaCollectors.toImmutableList());
179
180 if (!consumers.replace(resource.id(), oldAllocation,
181 new ContinuousResourceAllocation(oldAllocation.original(), newAllocations))) {
182 return false;
183 }
184
185 return true;
186 }
187}