blob: 687576c3fb24ad4d7182ebbd53bc08abe0b1f825 [file] [log] [blame]
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07001/*
2 * Copyright 2015 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.newresource.impl;
17
18import com.google.common.annotations.Beta;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070024import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070025import org.onosproject.net.newresource.ResourcePath;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070026import org.onosproject.net.newresource.ResourceStore;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.StorageService;
31import org.onosproject.store.service.TransactionContext;
32import org.onosproject.store.service.TransactionException;
33import org.onosproject.store.service.TransactionalMap;
34import org.onosproject.store.service.Versioned;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070038import java.util.ArrayList;
39import java.util.Arrays;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070040import java.util.Collection;
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070041import java.util.Collections;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070042import java.util.Iterator;
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070043import java.util.LinkedHashSet;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070044import java.util.List;
45import java.util.Map;
46import java.util.Optional;
47import java.util.stream.Collectors;
48
49import static com.google.common.base.Preconditions.checkArgument;
50import static com.google.common.base.Preconditions.checkNotNull;
51
52/**
53 * Implementation of ResourceStore using TransactionalMap.
54 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070055@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070056@Service
57@Beta
58public class ConsistentResourceStore implements ResourceStore {
59 private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class);
60
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070061 private static final String CONSUMER_MAP = "onos-resource-consumers";
62 private static final String CHILD_MAP = "onos-resource-children";
63 private static final Serializer SERIALIZER = Serializer.using(
64 Arrays.asList(KryoNamespaces.BASIC, KryoNamespaces.API));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070065
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected StorageService service;
68
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070069 private ConsistentMap<ResourcePath, ResourceConsumer> consumerMap;
70 private ConsistentMap<ResourcePath, List<ResourcePath>> childMap;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070071
72 @Activate
73 public void activate() {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070074 consumerMap = service.<ResourcePath, ResourceConsumer>consistentMapBuilder()
75 .withName(CONSUMER_MAP)
76 .withSerializer(SERIALIZER)
77 .build();
78 childMap = service.<ResourcePath, List<ResourcePath>>consistentMapBuilder()
79 .withName(CHILD_MAP)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070080 .withSerializer(SERIALIZER)
81 .build();
82 }
83
84 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070085 public Optional<ResourceConsumer> getConsumer(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070086 checkNotNull(resource);
87
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070088 Versioned<ResourceConsumer> consumer = consumerMap.get(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 if (consumer == null) {
90 return Optional.empty();
91 }
92
93 return Optional.of(consumer.value());
94 }
95
96 @Override
Sho SHIMIZU83e17a02015-08-20 14:07:05 -070097 public boolean register(List<ResourcePath> resources) {
98 checkNotNull(resources);
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070099
100 TransactionContext tx = service.transactionContextBuilder().build();
101 tx.begin();
102
103 try {
104 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
105 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
106
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700107 Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
108 .filter(x -> x.parent().isPresent())
109 .collect(Collectors.groupingBy(x -> x.parent().get()));
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700110
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700111 for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
112 if (!isRegistered(childTxMap, entry.getKey())) {
113 return abortTransaction(tx);
114 }
115
116 if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) {
117 return abortTransaction(tx);
118 }
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700119 }
120
121 return commitTransaction(tx);
122 } catch (TransactionException e) {
123 log.error("Exception thrown, abort the transaction", e);
124 return abortTransaction(tx);
125 }
126 }
127
128 @Override
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700129 public boolean unregister(List<ResourcePath> resources) {
130 checkNotNull(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700131
132 TransactionContext tx = service.transactionContextBuilder().build();
133 tx.begin();
134
135 try {
136 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
137 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
138 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
139 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
140
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700141 Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
142 .filter(x -> x.parent().isPresent())
143 .collect(Collectors.groupingBy(x -> x.parent().get()));
144
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700145 // even if one of the resources is allocated to a consumer,
146 // all unregistrations are regarded as failure
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700147 for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
148 if (entry.getValue().stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
149 return abortTransaction(tx);
150 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700151
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700152 if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) {
153 return abortTransaction(tx);
154 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700155 }
156
157 return commitTransaction(tx);
158 } catch (TransactionException e) {
159 log.error("Exception thrown, abort the transaction", e);
160 return abortTransaction(tx);
161 }
162 }
163
164 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700165 public boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700166 checkNotNull(resources);
167 checkNotNull(consumer);
168
169 TransactionContext tx = service.transactionContextBuilder().build();
170 tx.begin();
171
172 try {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700173 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
174 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
175 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
176 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
177
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700178 for (ResourcePath resource: resources) {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700179 if (!isRegistered(childTxMap, resource)) {
180 return abortTransaction(tx);
181 }
182
183 ResourceConsumer oldValue = consumerTxMap.put(resource, consumer);
184 if (oldValue != null) {
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700185 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700186 }
187 }
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700188
189 return commitTransaction(tx);
Sho SHIMIZU264e4b72015-08-12 12:22:14 -0700190 } catch (TransactionException e) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700191 log.error("Exception thrown, abort the transaction", e);
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700192 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700193 }
194 }
195
196 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700197 public boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700198 checkNotNull(resources);
199 checkNotNull(consumers);
200 checkArgument(resources.size() == consumers.size());
201
202 TransactionContext tx = service.transactionContextBuilder().build();
203 tx.begin();
204
205 try {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700206 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
207 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700208 Iterator<ResourcePath> resourceIte = resources.iterator();
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700209 Iterator<ResourceConsumer> consumerIte = consumers.iterator();
210
211 while (resourceIte.hasNext() && consumerIte.hasNext()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700212 ResourcePath resource = resourceIte.next();
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700213 ResourceConsumer consumer = consumerIte.next();
214
215 // if this single release fails (because the resource is allocated to another consumer,
216 // the whole release fails
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700217 if (!consumerTxMap.remove(resource, consumer)) {
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700218 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700219 }
220 }
221
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700222 return commitTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700223 } catch (TransactionException e) {
224 log.error("Exception thrown, abort the transaction", e);
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700225 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700226 }
227 }
228
229 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700230 public Collection<ResourcePath> getResources(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700231 checkNotNull(consumer);
232
233 // NOTE: getting all entries may become performance bottleneck
234 // TODO: revisit for better backend data structure
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700235 return consumerMap.entrySet().stream()
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700236 .filter(x -> x.getValue().value().equals(consumer))
237 .map(Map.Entry::getKey)
238 .collect(Collectors.toList());
239 }
240
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700241 @Override
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700242 public Collection<ResourcePath> getChildResources(ResourcePath parent) {
243 checkNotNull(parent);
244
245 Versioned<List<ResourcePath>> children = childMap.get(parent);
246 if (children == null) {
247 return Collections.emptyList();
248 }
249
250 return children.value();
251 }
252
253 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700254 public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) {
255 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700256 checkNotNull(cls);
257
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700258 Versioned<List<ResourcePath>> children = childMap.get(parent);
259 if (children == null) {
260 return Collections.emptyList();
261 }
262
263 return children.value().stream()
264 .filter(x -> x.lastComponent().getClass().equals(cls))
265 .filter(consumerMap::containsKey)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700266 .collect(Collectors.toList());
267 }
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700268
269 /**
270 * Abort the transaction.
271 *
272 * @param tx transaction context
273 * @return always false
274 */
275 private boolean abortTransaction(TransactionContext tx) {
276 tx.abort();
277 return false;
278 }
279
280 /**
281 * Commit the transaction.
282 *
283 * @param tx transaction context
284 * @return always true
285 */
286 private boolean commitTransaction(TransactionContext tx) {
287 tx.commit();
288 return true;
289 }
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700290
291 /**
292 * Appends the values to the existing values associated with the specified key.
Sho SHIMIZU4568c412015-08-21 16:39:07 -0700293 * If the map already has all the given values, appending will not happen.
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700294 *
295 * @param map map holding multiple values for a key
296 * @param key key specifying values
297 * @param values values to be appended
298 * @param <K> type of the key
299 * @param <V> type of the element of the list
300 * @return true if the operation succeeds, false otherwise.
301 */
Sho SHIMIZU2a1b2332015-08-18 22:40:12 -0700302 private <K, V> boolean appendValues(TransactionalMap<K, List<V>> map, K key, List<V> values) {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700303 List<V> oldValues = map.get(key);
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700304 if (oldValues == null) {
Sho SHIMIZU4568c412015-08-21 16:39:07 -0700305 return map.replace(key, oldValues, new ArrayList<>(values));
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700306 }
307
Sho SHIMIZU4568c412015-08-21 16:39:07 -0700308 LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
309 if (oldSet.containsAll(values)) {
310 // don't write to map because all values are already stored
311 return true;
312 }
313
314 oldSet.addAll(values);
315 return map.replace(key, oldValues, new ArrayList<>(oldSet));
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700316 }
317
318 /**
Sho SHIMIZUba1f83b2015-10-14 08:11:20 -0700319 * Removes the values from the existing values associated with the specified key.
Sho SHIMIZU5618ee52015-08-21 17:19:44 -0700320 * If the map doesn't contain the given values, removal will not happen.
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700321 *
322 * @param map map holding multiple values for a key
323 * @param key key specifying values
324 * @param values values to be removed
325 * @param <K> type of the key
326 * @param <V> type of the element of the list
327 * @return true if the operation succeeds, false otherwise
328 */
329 private <K, V> boolean removeValues(TransactionalMap<K, List<V>> map, K key, List<V> values) {
330 List<V> oldValues = map.get(key);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700331 if (oldValues == null) {
Sho SHIMIZU5618ee52015-08-21 17:19:44 -0700332 return map.replace(key, oldValues, new ArrayList<>());
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700333 }
334
Sho SHIMIZU5618ee52015-08-21 17:19:44 -0700335 LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
336 if (values.stream().allMatch(x -> !oldSet.contains(x))) {
337 // don't write map because none of the values are stored
338 return true;
339 }
340
341 oldSet.removeAll(values);
342 return map.replace(key, oldValues, new ArrayList<>(oldSet));
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700343 }
344
345 /**
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700346 * Checks if the specified resource is registered as a child of a resource in the map.
347 *
348 * @param map map storing parent - child relationship of resources
349 * @param resource resource to be checked
350 * @return true if the resource is registered, false otherwise.
351 */
352 private boolean isRegistered(TransactionalMap<ResourcePath, List<ResourcePath>> map, ResourcePath resource) {
353 // root is always regarded to be registered
Sho SHIMIZU01120782015-08-21 15:48:43 -0700354 if (resource.isRoot()) {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700355 return true;
356 }
357
358 List<ResourcePath> value = map.get(resource.parent().get());
359 return value != null && value.contains(resource);
360 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700361}