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.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.util.Tools; |
| 21 | import org.onosproject.net.resource.DiscreteResource; |
| 22 | import org.onosproject.net.resource.DiscreteResourceId; |
| 23 | import org.onosproject.net.resource.Resource; |
| 24 | import org.onosproject.net.resource.ResourceAllocation; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 25 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 26 | import org.onosproject.net.resource.Resources; |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.ConsistentMapException; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.onosproject.store.service.TransactionContext; |
| 31 | import org.onosproject.store.service.Versioned; |
| 32 | |
| 33 | import java.util.LinkedHashSet; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | import java.util.stream.Stream; |
| 38 | |
| 39 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES; |
| 40 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY; |
| 41 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 42 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame^] | 43 | class ConsistentDiscreteResourceSubStore { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 44 | private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 45 | private ConsistentMap<DiscreteResourceId, Set<DiscreteResource>> childMap; |
| 46 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame^] | 47 | ConsistentDiscreteResourceSubStore(StorageService service) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 48 | this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 49 | .withName(MapNames.DISCRETE_CONSUMER_MAP) |
| 50 | .withSerializer(SERIALIZER) |
| 51 | .build(); |
| 52 | this.childMap = service.<DiscreteResourceId, Set<DiscreteResource>>consistentMapBuilder() |
| 53 | .withName(MapNames.DISCRETE_CHILD_MAP) |
| 54 | .withSerializer(SERIALIZER) |
| 55 | .build(); |
| 56 | |
| 57 | Tools.retryable(() -> childMap.put(Resource.ROOT.id(), new LinkedHashSet<>()), |
| 58 | ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY); |
| 59 | } |
| 60 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame^] | 61 | TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) { |
| 62 | return new TransactionalDiscreteResourceSubStore(tx); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // computational complexity: O(1) |
| 66 | List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 67 | Versioned<ResourceConsumerId> consumerId = consumers.get(resource); |
| 68 | if (consumerId == null) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 69 | return ImmutableList.of(); |
| 70 | } |
| 71 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 72 | return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value())); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | Set<DiscreteResource> getChildResources(DiscreteResourceId parent) { |
| 76 | Versioned<Set<DiscreteResource>> children = childMap.get(parent); |
| 77 | |
| 78 | if (children == null) { |
| 79 | return ImmutableSet.of(); |
| 80 | } |
| 81 | |
| 82 | return children.value(); |
| 83 | } |
| 84 | |
| 85 | boolean isAvailable(DiscreteResource resource) { |
| 86 | return getResourceAllocations(resource.id()).isEmpty(); |
| 87 | } |
| 88 | |
| 89 | <T> Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
| 90 | Set<DiscreteResource> children = getChildResources(parent); |
| 91 | if (children.isEmpty()) { |
| 92 | return Stream.of(); |
| 93 | } |
| 94 | |
| 95 | return children.stream() |
| 96 | .filter(x -> x.isTypeOf(cls)) |
| 97 | .filter(x -> consumers.containsKey(x.id())); |
| 98 | } |
| 99 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 100 | Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 101 | return consumers.entrySet().stream() |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 102 | .filter(x -> x.getValue().value().equals(consumerId)) |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 103 | .map(Map.Entry::getKey) |
| 104 | .map(x -> Resources.discrete(x).resource()); |
| 105 | } |
| 106 | } |