blob: 2c8e4d88b8493e0e078829f5df0a373f291fc563 [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;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070038
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070039class TransactionalContinuousResourceSubStore {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070040 private final Logger log = LoggerFactory.getLogger(getClass());
41 private final TransactionalMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
42 private final TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
43
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070044 TransactionalContinuousResourceSubStore(TransactionContext tx) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070045 this.childMap = tx.getTransactionalMap(MapNames.CONTINUOUS_CHILD_MAP, SERIALIZER);
46 this.consumers = tx.getTransactionalMap(MapNames.CONTINUOUS_CONSUMER_MAP, SERIALIZER);
47 }
48
49 // iterate over the values in the set: O(n) operation
Sho SHIMIZUa81141b2016-05-11 08:05:45 -070050 Optional<ContinuousResource> lookup(ContinuousResourceId id) {
51 // continuous resource always has its parent
52 checkArgument(id.parent().isPresent());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070053
54 Set<ContinuousResource> values = childMap.get(id.parent().get());
55 if (values == null) {
56 return Optional.empty();
57 }
58
59 return values.stream()
60 .filter(x -> x.id().equals(id))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070061 .findFirst();
62 }
63
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070064 boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070065 // short-circuit: receiving empty resource is regarded as success
66 if (values.isEmpty()) {
67 return true;
68 }
69
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070070 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
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070097 boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070098 // short-circuit: receiving empty resource is regarded as success
99 if (values.isEmpty()) {
100 return true;
101 }
102
Sho SHIMIZUc7ffdfe2016-05-06 18:38:41 -0700103 // even if one of the resources is allocated to a consumer,
104 // all unregistrations are regarded as failure
105 boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id()));
106 if (allocated) {
107 log.warn("Failed to unregister {}: allocation exists", key);
108 return false;
109 }
110
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700111 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
112 if (oldValues == null) {
113 log.trace("No-Op removing values. key {} did not exist", key);
114 return true;
115 }
116
117 if (values.stream().allMatch(x -> !oldValues.contains(x))) {
118 // don't write map because none of the values are stored
119 log.trace("No-Op removing values. key {} did not contain {}", key, values);
120 return true;
121 }
122
123 LinkedHashSet<ContinuousResource> newValues = new LinkedHashSet<>(oldValues);
124 newValues.removeAll(values);
125 return childMap.replace(key, oldValues, newValues);
126 }
127
Sho SHIMIZUd82dc5b2016-05-13 13:50:29 -0700128 private boolean isAllocated(ContinuousResourceId id) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700129 ContinuousResourceAllocation allocations = consumers.get(id);
130 return allocations != null && !allocations.allocations().isEmpty();
131 }
132
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700133 boolean allocate(ResourceConsumerId consumerId, ContinuousResource request) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700134 // if the resource is not registered, then abort
Sho SHIMIZUa81141b2016-05-11 08:05:45 -0700135 Optional<ContinuousResource> lookedUp = lookup(request.id());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700136 if (!lookedUp.isPresent()) {
137 return false;
138 }
139 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
Sho SHIMIZUa81141b2016-05-11 08:05:45 -0700140 ContinuousResource original = lookedUp.get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700141 ContinuousResourceAllocation allocations = consumers.get(request.id());
Sho SHIMIZUdffe3b82016-05-13 15:44:57 -0700142 if (!Optional.ofNullable(allocations)
143 .orElse(ContinuousResourceAllocation.empty(original))
Sho SHIMIZU38bcfcf2016-05-13 15:48:54 -0700144 .hasEnoughResource(request)) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700145 return false;
146 }
147
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700148 return appendValue(original, new ResourceAllocation(request, consumerId));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700149 }
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
Sho SHIMIZUefb75112016-05-13 15:24:34 -0700160 ContinuousResourceAllocation newValue = oldValue.allocate(original, value);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700161 return consumers.replace(original.id(), oldValue, newValue);
162 }
163
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700164 boolean release(ContinuousResource resource, ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700165 ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
Sho SHIMIZUb2183962016-05-13 15:03:50 -0700166 ContinuousResourceAllocation newAllocation = oldAllocation.release(resource, consumerId);
Satish Kb05d70a2016-05-13 17:04:49 +0530167
Sho SHIMIZUb2183962016-05-13 15:03:50 -0700168 return consumers.replace(resource.id(), oldAllocation, newAllocation);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700169 }
170}