blob: 882c67d8d220c71d04363ee01a75e1d587b81354 [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
69 boolean appendValues(DiscreteResourceId key, List<ContinuousResource> values) {
70 Set<ContinuousResource> requested = new LinkedHashSet<>(values);
71 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
72 if (oldValues == null) {
73 return true;
74 }
75
76 Set<ContinuousResource> addedValues = Sets.difference(requested, oldValues);
77 // no new value, then no-op
78 if (addedValues.isEmpty()) {
79 // don't write to map because all values are already stored
80 return true;
81 }
82
83 Set<ContinuousResourceId> addedIds = addedValues.stream()
84 .map(ContinuousResource::id)
85 .collect(Collectors.toSet());
86 // if the value is not found but the same ID is found
87 // (this happens only when being continuous resource)
88 if (oldValues.stream().anyMatch(x -> addedIds.contains(x.id()))) {
89 // no-op, but indicating failure (reject the request)
90 return false;
91 }
92 Set<ContinuousResource> newValues = new LinkedHashSet<>(oldValues);
93 newValues.addAll(addedValues);
94 return childMap.replace(key, oldValues, newValues);
95 }
96
97 boolean removeValues(DiscreteResourceId key, List<ContinuousResource> values) {
98 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
99 if (oldValues == null) {
100 log.trace("No-Op removing values. key {} did not exist", key);
101 return true;
102 }
103
104 if (values.stream().allMatch(x -> !oldValues.contains(x))) {
105 // don't write map because none of the values are stored
106 log.trace("No-Op removing values. key {} did not contain {}", key, values);
107 return true;
108 }
109
110 LinkedHashSet<ContinuousResource> newValues = new LinkedHashSet<>(oldValues);
111 newValues.removeAll(values);
112 return childMap.replace(key, oldValues, newValues);
113 }
114
115 boolean isAllocated(ContinuousResourceId id) {
116 ContinuousResourceAllocation allocations = consumers.get(id);
117 return allocations != null && !allocations.allocations().isEmpty();
118 }
119
120 boolean allocate(ResourceConsumer consumer, ContinuousResource request) {
121 // if the resource is not registered, then abort
122 Optional<Resource> lookedUp = lookup(request.id());
123 if (!lookedUp.isPresent()) {
124 return false;
125 }
126 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
127 ContinuousResource original = (ContinuousResource) lookedUp.get();
128 ContinuousResourceAllocation allocations = consumers.get(request.id());
129 if (!hasEnoughResource(original, request, allocations)) {
130 return false;
131 }
132
133 boolean success = appendValue(original, new ResourceAllocation(request, consumer));
134 if (!success) {
135 return false;
136 }
137
138 return true;
139 }
140
141 // Appends the specified ResourceAllocation to the existing values stored in the map
142 // computational complexity: O(n) where n is the number of the elements in the associated allocation
143 private boolean appendValue(ContinuousResource original, ResourceAllocation value) {
144 ContinuousResourceAllocation oldValue = consumers.putIfAbsent(original.id(),
145 new ContinuousResourceAllocation(original, ImmutableList.of(value)));
146 if (oldValue == null) {
147 return true;
148 }
149
150 if (oldValue.allocations().contains(value)) {
151 // don't write to map because all values are already stored
152 return true;
153 }
154
155 ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original,
156 ImmutableList.<ResourceAllocation>builder()
157 .addAll(oldValue.allocations())
158 .add(value)
159 .build());
160 return consumers.replace(original.id(), oldValue, newValue);
161 }
162
163 boolean release(ContinuousResource resource, ResourceConsumer consumer) {
164 ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
165 ImmutableList<ResourceAllocation> newAllocations = oldAllocation.allocations().stream()
166 .filter(x -> !(x.consumer().equals(consumer) &&
167 ((ContinuousResource) x.resource()).value() == resource.value()))
168 .collect(GuavaCollectors.toImmutableList());
169
170 if (!consumers.replace(resource.id(), oldAllocation,
171 new ContinuousResourceAllocation(oldAllocation.original(), newAllocations))) {
172 return false;
173 }
174
175 return true;
176 }
177}