Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.newresource.impl; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 24 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.newresource.ResourcePath; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 26 | import org.onosproject.net.newresource.ResourceStore; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.ConsistentMap; |
| 29 | import org.onosproject.store.service.Serializer; |
| 30 | import org.onosproject.store.service.StorageService; |
| 31 | import org.onosproject.store.service.TransactionContext; |
| 32 | import org.onosproject.store.service.TransactionException; |
| 33 | import org.onosproject.store.service.TransactionalMap; |
| 34 | import org.onosproject.store.service.Versioned; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Iterator; |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | import java.util.Optional; |
| 43 | import java.util.stream.Collectors; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkArgument; |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
| 47 | |
| 48 | /** |
| 49 | * Implementation of ResourceStore using TransactionalMap. |
| 50 | */ |
| 51 | @Component(immediate = true, enabled = false) |
| 52 | @Service |
| 53 | @Beta |
| 54 | public class ConsistentResourceStore implements ResourceStore { |
| 55 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 56 | |
| 57 | private static final String MAP_NAME = "onos-resource-consumers"; |
| 58 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected StorageService service; |
| 62 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 63 | private ConsistentMap<ResourcePath, ResourceConsumer> consumers; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 64 | |
| 65 | @Activate |
| 66 | public void activate() { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 67 | consumers = service.<ResourcePath, ResourceConsumer>consistentMapBuilder() |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 68 | .withName(MAP_NAME) |
| 69 | .withSerializer(SERIALIZER) |
| 70 | .build(); |
| 71 | } |
| 72 | |
| 73 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 74 | public Optional<ResourceConsumer> getConsumer(ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 75 | checkNotNull(resource); |
| 76 | |
| 77 | Versioned<ResourceConsumer> consumer = consumers.get(resource); |
| 78 | if (consumer == null) { |
| 79 | return Optional.empty(); |
| 80 | } |
| 81 | |
| 82 | return Optional.of(consumer.value()); |
| 83 | } |
| 84 | |
| 85 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 86 | public boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 87 | checkNotNull(resources); |
| 88 | checkNotNull(consumer); |
| 89 | |
| 90 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 91 | tx.begin(); |
| 92 | |
| 93 | try { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 94 | TransactionalMap<ResourcePath, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER); |
| 95 | for (ResourcePath resource: resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 96 | ResourceConsumer existing = txMap.putIfAbsent(resource, consumer); |
| 97 | // if the resource is already allocated to another consumer, the whole allocation fails |
| 98 | if (existing != null) { |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 99 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 102 | |
| 103 | return commitTransaction(tx); |
Sho SHIMIZU | 264e4b7 | 2015-08-12 12:22:14 -0700 | [diff] [blame] | 104 | } catch (TransactionException e) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 105 | log.error("Exception thrown, abort the transaction", e); |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 106 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 111 | public boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 112 | checkNotNull(resources); |
| 113 | checkNotNull(consumers); |
| 114 | checkArgument(resources.size() == consumers.size()); |
| 115 | |
| 116 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 117 | tx.begin(); |
| 118 | |
| 119 | try { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 120 | TransactionalMap<ResourcePath, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER); |
| 121 | Iterator<ResourcePath> resourceIte = resources.iterator(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 122 | Iterator<ResourceConsumer> consumerIte = consumers.iterator(); |
| 123 | |
| 124 | while (resourceIte.hasNext() && consumerIte.hasNext()) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 125 | ResourcePath resource = resourceIte.next(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 126 | ResourceConsumer consumer = consumerIte.next(); |
| 127 | |
| 128 | // if this single release fails (because the resource is allocated to another consumer, |
| 129 | // the whole release fails |
| 130 | if (!txMap.remove(resource, consumer)) { |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 131 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 135 | return commitTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 136 | } catch (TransactionException e) { |
| 137 | log.error("Exception thrown, abort the transaction", e); |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 138 | return abortTransaction(tx); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 143 | public Collection<ResourcePath> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 144 | checkNotNull(consumer); |
| 145 | |
| 146 | // NOTE: getting all entries may become performance bottleneck |
| 147 | // TODO: revisit for better backend data structure |
| 148 | return consumers.entrySet().stream() |
| 149 | .filter(x -> x.getValue().value().equals(consumer)) |
| 150 | .map(Map.Entry::getKey) |
| 151 | .collect(Collectors.toList()); |
| 152 | } |
| 153 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 154 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 155 | public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) { |
| 156 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 157 | checkNotNull(cls); |
| 158 | |
| 159 | // NOTE: getting all entries may become performance bottleneck |
| 160 | // TODO: revisit for better backend data structure |
| 161 | return consumers.entrySet().stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 162 | .filter(x -> x.getKey().parent().isPresent() && x.getKey().parent().get().equals(parent)) |
| 163 | .filter(x -> x.getKey().lastComponent().getClass() == cls) |
| 164 | .map(Map.Entry::getKey) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 165 | .collect(Collectors.toList()); |
| 166 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Abort the transaction. |
| 170 | * |
| 171 | * @param tx transaction context |
| 172 | * @return always false |
| 173 | */ |
| 174 | private boolean abortTransaction(TransactionContext tx) { |
| 175 | tx.abort(); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Commit the transaction. |
| 181 | * |
| 182 | * @param tx transaction context |
| 183 | * @return always true |
| 184 | */ |
| 185 | private boolean commitTransaction(TransactionContext tx) { |
| 186 | tx.commit(); |
| 187 | return true; |
| 188 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 189 | } |