Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.onosproject.net.resource.DiscreteResource; |
| 20 | import org.onosproject.net.resource.DiscreteResourceId; |
| 21 | import org.onosproject.net.resource.Resource; |
| 22 | import org.onosproject.net.resource.ResourceConsumer; |
| 23 | import org.onosproject.net.resource.Resources; |
| 24 | import org.onosproject.store.service.TransactionContext; |
| 25 | import org.onosproject.store.service.TransactionalMap; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.util.LinkedHashSet; |
| 30 | import java.util.List; |
| 31 | import java.util.Optional; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 35 | |
| 36 | class 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 | |
| 65 | boolean appendValues(DiscreteResourceId key, List<DiscreteResource> values) { |
| 66 | Set<DiscreteResource> requested = new LinkedHashSet<>(values); |
| 67 | Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested); |
| 68 | if (oldValues == null) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | Set<DiscreteResource> addedValues = Sets.difference(requested, oldValues); |
| 73 | // no new value, then no-op |
| 74 | if (addedValues.isEmpty()) { |
| 75 | // don't write to map because all values are already stored |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | Set<DiscreteResource> newValues = new LinkedHashSet<>(oldValues); |
| 80 | newValues.addAll(addedValues); |
| 81 | return childMap.replace(key, oldValues, newValues); |
| 82 | } |
| 83 | |
| 84 | boolean removeValues(DiscreteResourceId key, List<DiscreteResource> values) { |
| 85 | Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); |
| 86 | if (oldValues == null) { |
| 87 | log.trace("No-Op removing values. key {} did not exist", key); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
| 92 | // don't write map because none of the values are stored |
| 93 | log.trace("No-Op removing values. key {} did not contain {}", key, values); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | LinkedHashSet<DiscreteResource> newValues = new LinkedHashSet<>(oldValues); |
| 98 | newValues.removeAll(values); |
| 99 | return childMap.replace(key, oldValues, newValues); |
| 100 | } |
| 101 | |
| 102 | boolean isAllocated(DiscreteResourceId id) { |
| 103 | return consumers.get(id) != null; |
| 104 | } |
| 105 | |
| 106 | boolean allocate(ResourceConsumer consumer, DiscreteResource resource) { |
| 107 | // if the resource is not registered, then abort |
| 108 | Optional<Resource> lookedUp = lookup(resource.id()); |
| 109 | if (!lookedUp.isPresent()) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | ResourceConsumer oldValue = consumers.put(resource.id(), consumer); |
| 114 | return oldValue == null; |
| 115 | } |
| 116 | |
| 117 | boolean release(DiscreteResource resource, ResourceConsumer consumer) { |
| 118 | // if this single release fails (because the resource is allocated to another consumer) |
| 119 | // the whole release fails |
| 120 | if (!consumers.remove(resource.id(), consumer)) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | } |