blob: a20c2b72dc234e886d20aed798a23859cc0636ef [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 */
55@Component(immediate = true, enabled = false)
56@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 SHIMIZUba41fc12015-08-12 15:43:22 -070097 public boolean register(ResourcePath resource, List<ResourcePath> children) {
98 checkNotNull(resource);
99 checkNotNull(children);
100
101 TransactionContext tx = service.transactionContextBuilder().build();
102 tx.begin();
103
104 try {
105 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
106 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
107
108 if (!isRegistered(childTxMap, resource)) {
109 return abortTransaction(tx);
110 }
111
112 if (!appendValue(childTxMap, resource, children)) {
113 return abortTransaction(tx);
114 }
115
116 return commitTransaction(tx);
117 } catch (TransactionException e) {
118 log.error("Exception thrown, abort the transaction", e);
119 return abortTransaction(tx);
120 }
121 }
122
123 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700124 public boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700125 checkNotNull(resources);
126 checkNotNull(consumer);
127
128 TransactionContext tx = service.transactionContextBuilder().build();
129 tx.begin();
130
131 try {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700132 TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
133 tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
134 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
135 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
136
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700137 for (ResourcePath resource: resources) {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700138 if (!isRegistered(childTxMap, resource)) {
139 return abortTransaction(tx);
140 }
141
142 ResourceConsumer oldValue = consumerTxMap.put(resource, consumer);
143 if (oldValue != null) {
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700144 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700145 }
146 }
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700147
148 return commitTransaction(tx);
Sho SHIMIZU264e4b72015-08-12 12:22:14 -0700149 } catch (TransactionException e) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700150 log.error("Exception thrown, abort the transaction", e);
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700151 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700152 }
153 }
154
155 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700156 public boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700157 checkNotNull(resources);
158 checkNotNull(consumers);
159 checkArgument(resources.size() == consumers.size());
160
161 TransactionContext tx = service.transactionContextBuilder().build();
162 tx.begin();
163
164 try {
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700165 TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
166 tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700167 Iterator<ResourcePath> resourceIte = resources.iterator();
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700168 Iterator<ResourceConsumer> consumerIte = consumers.iterator();
169
170 while (resourceIte.hasNext() && consumerIte.hasNext()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700171 ResourcePath resource = resourceIte.next();
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700172 ResourceConsumer consumer = consumerIte.next();
173
174 // if this single release fails (because the resource is allocated to another consumer,
175 // the whole release fails
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700176 if (!consumerTxMap.remove(resource, consumer)) {
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700177 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700178 }
179 }
180
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700181 return commitTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700182 } catch (TransactionException e) {
183 log.error("Exception thrown, abort the transaction", e);
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700184 return abortTransaction(tx);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700185 }
186 }
187
188 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700189 public Collection<ResourcePath> getResources(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700190 checkNotNull(consumer);
191
192 // NOTE: getting all entries may become performance bottleneck
193 // TODO: revisit for better backend data structure
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700194 return consumerMap.entrySet().stream()
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700195 .filter(x -> x.getValue().value().equals(consumer))
196 .map(Map.Entry::getKey)
197 .collect(Collectors.toList());
198 }
199
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700200 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700201 public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) {
202 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700203 checkNotNull(cls);
204
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700205 Versioned<List<ResourcePath>> children = childMap.get(parent);
206 if (children == null) {
207 return Collections.emptyList();
208 }
209
210 return children.value().stream()
211 .filter(x -> x.lastComponent().getClass().equals(cls))
212 .filter(consumerMap::containsKey)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700213 .collect(Collectors.toList());
214 }
Sho SHIMIZUd29847f2015-08-13 09:10:59 -0700215
216 /**
217 * Abort the transaction.
218 *
219 * @param tx transaction context
220 * @return always false
221 */
222 private boolean abortTransaction(TransactionContext tx) {
223 tx.abort();
224 return false;
225 }
226
227 /**
228 * Commit the transaction.
229 *
230 * @param tx transaction context
231 * @return always true
232 */
233 private boolean commitTransaction(TransactionContext tx) {
234 tx.commit();
235 return true;
236 }
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700237
238 /**
239 * Appends the values to the existing values associated with the specified key.
240 *
241 * @param map map holding multiple values for a key
242 * @param key key specifying values
243 * @param values values to be appended
244 * @param <K> type of the key
245 * @param <V> type of the element of the list
246 * @return true if the operation succeeds, false otherwise.
247 */
248 private <K, V> boolean appendValue(TransactionalMap<K, List<V>> map, K key, List<V> values) {
249 List<V> oldValues = map.get(key);
250 List<V> newValues;
251 if (oldValues == null) {
252 newValues = new ArrayList<>(values);
253 } else {
254 LinkedHashSet<V> newSet = new LinkedHashSet<>(oldValues);
255 newSet.addAll(values);
256 newValues = new ArrayList<>(newSet);
257 }
258
259 return map.replace(key, oldValues, newValues);
260 }
261
262 /**
263 * Checks if the specified resource is registered as a child of a resource in the map.
264 *
265 * @param map map storing parent - child relationship of resources
266 * @param resource resource to be checked
267 * @return true if the resource is registered, false otherwise.
268 */
269 private boolean isRegistered(TransactionalMap<ResourcePath, List<ResourcePath>> map, ResourcePath resource) {
270 // root is always regarded to be registered
271 if (!resource.parent().isPresent()) {
272 return true;
273 }
274
275 List<ResourcePath> value = map.get(resource.parent().get());
276 return value != null && value.contains(resource);
277 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700278}