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