blob: 3c99da9b154cae558849bfb6fafd8d47f25320ce [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;
22import org.onosproject.net.resource.ResourceConsumer;
23import 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;
39 private final TransactionalMap<DiscreteResourceId, ResourceConsumer> consumers;
40
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
47 Optional<Resource> lookup(DiscreteResourceId id) {
48 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 SHIMIZU22fb2832016-05-06 11:44:03 -070095 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
96 if (oldValues == null) {
97 log.trace("No-Op removing values. key {} did not exist", key);
98 return true;
99 }
100
101 if (values.stream().allMatch(x -> !oldValues.contains(x))) {
102 // don't write map because none of the values are stored
103 log.trace("No-Op removing values. key {} did not contain {}", key, values);
104 return true;
105 }
106
107 LinkedHashSet<DiscreteResource> newValues = new LinkedHashSet<>(oldValues);
108 newValues.removeAll(values);
109 return childMap.replace(key, oldValues, newValues);
110 }
111
112 boolean isAllocated(DiscreteResourceId id) {
113 return consumers.get(id) != null;
114 }
115
116 boolean allocate(ResourceConsumer consumer, DiscreteResource resource) {
117 // if the resource is not registered, then abort
118 Optional<Resource> lookedUp = lookup(resource.id());
119 if (!lookedUp.isPresent()) {
120 return false;
121 }
122
123 ResourceConsumer oldValue = consumers.put(resource.id(), consumer);
124 return oldValue == null;
125 }
126
127 boolean release(DiscreteResource resource, ResourceConsumer consumer) {
128 // if this single release fails (because the resource is allocated to another consumer)
129 // the whole release fails
130 if (!consumers.remove(resource.id(), consumer)) {
131 return false;
132 }
133
134 return true;
135 }
136}