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