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