Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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; |
Sacheth Hegde | 5a91f39 | 2018-05-07 17:11:34 -0700 | [diff] [blame] | 20 | import org.onlab.util.Tools; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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; |
Sacheth Hegde | 5a91f39 | 2018-05-07 17:11:34 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.StorageException; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 38 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 39 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Consistent substore for discrete resources. |
| 42 | */ |
| 43 | class ConsistentDiscreteResourceSubStore implements ConsistentResourceSubStore |
| 44 | <DiscreteResourceId, DiscreteResource, TransactionalDiscreteResourceSubStore> { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 45 | private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers; |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 46 | private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 47 | |
Yuta HIGUCHI | aaab65e | 2018-05-08 11:23:54 -0700 | [diff] [blame] | 48 | @SuppressWarnings("ReturnValueIgnored") |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 49 | ConsistentDiscreteResourceSubStore(StorageService service) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 50 | this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 51 | .withName(MapNames.DISCRETE_CONSUMER_MAP) |
| 52 | .withSerializer(SERIALIZER) |
| 53 | .build(); |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 54 | this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 55 | .withName(MapNames.DISCRETE_CHILD_MAP) |
| 56 | .withSerializer(SERIALIZER) |
| 57 | .build(); |
| 58 | |
Sacheth Hegde | 5a91f39 | 2018-05-07 17:11:34 -0700 | [diff] [blame] | 59 | Tools.retryable( |
| 60 | () -> childMap.putIfAbsent(Resource.ROOT.id(), DiscreteResources.empty()), |
| 61 | StorageException.ConcurrentModification.class, |
| 62 | Integer.MAX_VALUE, |
| 63 | 50 |
| 64 | ).get(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 67 | @Override |
| 68 | public TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) { |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 69 | return new TransactionalDiscreteResourceSubStore(tx); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // computational complexity: O(1) |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 75 | Versioned<ResourceConsumerId> consumerId = consumers.get(resource); |
| 76 | if (consumerId == null) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 77 | return ImmutableList.of(); |
| 78 | } |
| 79 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 80 | return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value())); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public Set<DiscreteResource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 85 | Versioned<DiscreteResources> children = childMap.get(parent); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 86 | |
| 87 | if (children == null) { |
| 88 | return ImmutableSet.of(); |
| 89 | } |
| 90 | |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 91 | return children.value().values(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 96 | Versioned<DiscreteResources> children = childMap.get(parent); |
| 97 | |
| 98 | if (children == null) { |
| 99 | return ImmutableSet.of(); |
| 100 | } |
| 101 | |
| 102 | return children.value().valuesOf(cls); |
| 103 | } |
| 104 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 105 | @Override |
| 106 | public boolean isAvailable(DiscreteResource resource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 107 | return getResourceAllocations(resource.id()).isEmpty(); |
| 108 | } |
| 109 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 112 | Set<DiscreteResource> children = getChildResources(parent); |
| 113 | if (children.isEmpty()) { |
| 114 | return Stream.of(); |
| 115 | } |
| 116 | |
| 117 | return children.stream() |
| 118 | .filter(x -> x.isTypeOf(cls)) |
| 119 | .filter(x -> consumers.containsKey(x.id())); |
| 120 | } |
| 121 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 124 | return consumers.entrySet().stream() |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 125 | .filter(x -> x.getValue().value().equals(consumerId)) |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 126 | .map(Map.Entry::getKey) |
| 127 | .map(x -> Resources.discrete(x).resource()); |
| 128 | } |
| 129 | } |