blob: 83589dc3d46691f1177c3314512db384a0d2925d [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.Sets;
19import org.onosproject.net.resource.DiscreteResource;
20import org.onosproject.net.resource.DiscreteResourceId;
21import org.onosproject.net.resource.Resource;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070022import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070023import org.onosproject.net.resource.Resources;
24import org.onosproject.store.service.TransactionContext;
25import org.onosproject.store.service.TransactionalMap;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.util.LinkedHashSet;
30import java.util.List;
31import java.util.Optional;
32import java.util.Set;
33
34import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
35
36class TransactionalDiscreteResourceStore {
37 private final Logger log = LoggerFactory.getLogger(getClass());
38 private final TransactionalMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070039 private final TransactionalMap<DiscreteResourceId, ResourceConsumerId> consumers;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070040
41 TransactionalDiscreteResourceStore(TransactionContext tx) {
42 this.childMap = tx.getTransactionalMap(MapNames.DISCRETE_CHILD_MAP, SERIALIZER);
43 this.consumers = tx.getTransactionalMap(MapNames.DISCRETE_CONSUMER_MAP, SERIALIZER);
44 }
45
46 // check the existence in the set: O(1) operation
Sho SHIMIZUa81141b2016-05-11 08:05:45 -070047 Optional<DiscreteResource> lookup(DiscreteResourceId id) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070048 if (!id.parent().isPresent()) {
49 return Optional.of(Resource.ROOT);
50 }
51
52 Set<DiscreteResource> values = childMap.get(id.parent().get());
53 if (values == null) {
54 return Optional.empty();
55 }
56
57 DiscreteResource resource = Resources.discrete(id).resource();
58 if (values.contains(resource)) {
59 return Optional.of(resource);
60 } else {
61 return Optional.empty();
62 }
63 }
64
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070065 boolean register(DiscreteResourceId key, List<DiscreteResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070066 // short-circuit: receiving empty resource is regarded as success
67 if (values.isEmpty()) {
68 return true;
69 }
70
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070071 Set<DiscreteResource> requested = new LinkedHashSet<>(values);
72 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested);
73 if (oldValues == null) {
74 return true;
75 }
76
77 Set<DiscreteResource> addedValues = Sets.difference(requested, oldValues);
78 // no new value, then no-op
79 if (addedValues.isEmpty()) {
80 // don't write to map because all values are already stored
81 return true;
82 }
83
84 Set<DiscreteResource> newValues = new LinkedHashSet<>(oldValues);
85 newValues.addAll(addedValues);
86 return childMap.replace(key, oldValues, newValues);
87 }
88
Sho SHIMIZUa2d99eb2016-05-06 14:52:55 -070089 boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) {
Sho SHIMIZU1bf46b12016-05-06 15:42:29 -070090 // short-circuit: receiving empty resource is regarded as success
91 if (values.isEmpty()) {
92 return true;
93 }
94
Sho SHIMIZUc7ffdfe2016-05-06 18:38:41 -070095 // even if one of the resources is allocated to a consumer,
96 // all unregistrations are regarded as failure
97 boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id()));
98 if (allocated) {
99 log.warn("Failed to unregister {}: allocation exists", key);
100 return false;
101 }
102
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700103 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
104 if (oldValues == null) {
105 log.trace("No-Op removing values. key {} did not exist", key);
106 return true;
107 }
108
109 if (values.stream().allMatch(x -> !oldValues.contains(x))) {
110 // don't write map because none of the values are stored
111 log.trace("No-Op removing values. key {} did not contain {}", key, values);
112 return true;
113 }
114
115 LinkedHashSet<DiscreteResource> newValues = new LinkedHashSet<>(oldValues);
116 newValues.removeAll(values);
117 return childMap.replace(key, oldValues, newValues);
118 }
119
Sho SHIMIZUd82dc5b2016-05-13 13:50:29 -0700120 private boolean isAllocated(DiscreteResourceId id) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700121 return consumers.get(id) != null;
122 }
123
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700124 boolean allocate(ResourceConsumerId consumerId, DiscreteResource resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700125 // if the resource is not registered, then abort
Sho SHIMIZUa81141b2016-05-11 08:05:45 -0700126 Optional<DiscreteResource> lookedUp = lookup(resource.id());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700127 if (!lookedUp.isPresent()) {
128 return false;
129 }
130
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700131 ResourceConsumerId oldValue = consumers.put(resource.id(), consumerId);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700132 return oldValue == null;
133 }
134
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700135 boolean release(DiscreteResource resource, ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700136 // if this single release fails (because the resource is allocated to another consumer)
137 // the whole release fails
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700138 if (!consumers.remove(resource.id(), consumerId)) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700139 return false;
140 }
141
142 return true;
143 }
144}