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.ContinuousResource; |
| 22 | import org.onosproject.net.resource.ContinuousResourceId; |
| 23 | import org.onosproject.net.resource.DiscreteResourceId; |
| 24 | import org.onosproject.net.resource.Resource; |
| 25 | import org.onosproject.net.resource.ResourceAllocation; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 26 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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 | |
| 33 | import java.util.LinkedHashSet; |
| 34 | import java.util.List; |
| 35 | import java.util.Set; |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 37 | import java.util.stream.Stream; |
| 38 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 39 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 40 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Consistent substore for continuous resources. |
| 44 | */ |
| 45 | class ConsistentContinuousResourceSubStore implements ConsistentResourceSubStore |
| 46 | <ContinuousResourceId, ContinuousResource, TransactionalContinuousResourceSubStore> { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 47 | private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; |
| 48 | private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap; |
| 49 | |
Yuta HIGUCHI | aaab65e | 2018-05-08 11:23:54 -0700 | [diff] [blame^] | 50 | @SuppressWarnings("ReturnValueIgnored") |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 51 | ConsistentContinuousResourceSubStore(StorageService service) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 52 | this.consumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder() |
| 53 | .withName(MapNames.CONTINUOUS_CONSUMER_MAP) |
| 54 | .withSerializer(SERIALIZER) |
| 55 | .build(); |
| 56 | this.childMap = service.<DiscreteResourceId, Set<ContinuousResource>>consistentMapBuilder() |
| 57 | .withName(MapNames.CONTINUOUS_CHILD_MAP) |
| 58 | .withSerializer(SERIALIZER) |
| 59 | .build(); |
| 60 | |
Sacheth Hegde | 5a91f39 | 2018-05-07 17:11:34 -0700 | [diff] [blame] | 61 | Tools.retryable( |
| 62 | () -> childMap.putIfAbsent(Resource.ROOT.id(), new LinkedHashSet<>()), |
| 63 | StorageException.ConcurrentModification.class, |
| 64 | Integer.MAX_VALUE, |
| 65 | 50 |
| 66 | ).get(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public TransactionalContinuousResourceSubStore transactional(TransactionContext tx) { |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 71 | return new TransactionalContinuousResourceSubStore(tx); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // computational complexity: O(n) where n is the number of the existing allocations for the resource |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 77 | Versioned<ContinuousResourceAllocation> allocations = consumers.get(resource); |
| 78 | if (allocations == null) { |
| 79 | return ImmutableList.of(); |
| 80 | } |
| 81 | |
| 82 | return allocations.value().allocations().stream() |
| 83 | .filter(x -> x.resource().id().equals(resource)) |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 84 | .collect(ImmutableList.toImmutableList()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public Set<ContinuousResource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 89 | Versioned<Set<ContinuousResource>> children = childMap.get(parent); |
| 90 | |
| 91 | if (children == null) { |
| 92 | return ImmutableSet.of(); |
| 93 | } |
| 94 | |
| 95 | return children.value(); |
| 96 | } |
| 97 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public Set<ContinuousResource> getChildResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 100 | // naive implementation |
| 101 | return getChildResources(parent).stream() |
| 102 | .filter(x -> x.isTypeOf(cls)) |
| 103 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 104 | } |
| 105 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 106 | @Override |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 107 | public boolean isAvailable(ContinuousResource resource) { |
| 108 | // check if it's registered or not. |
| 109 | Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id()); |
| 110 | if (children == null) { |
| 111 | return false; |
| 112 | } |
| 113 | |
Yuta HIGUCHI | 9f111e6 | 2017-03-07 17:26:33 -0800 | [diff] [blame] | 114 | boolean notEnoughRegistered = children.value().stream() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 115 | .filter(c -> c.id().equals(resource.id())) |
| 116 | .findFirst() |
Yuta HIGUCHI | 9f111e6 | 2017-03-07 17:26:33 -0800 | [diff] [blame] | 117 | .map(registered -> registered.value() < resource.value()) |
| 118 | .orElse(true); |
| 119 | if (notEnoughRegistered) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 120 | // Capacity < requested, can never satisfy |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // check if there's enough left |
| 125 | Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id()); |
| 126 | if (allocation == null) { |
| 127 | // no allocation (=no consumer) full registered resources available |
| 128 | return true; |
| 129 | } |
| 130 | |
Sho SHIMIZU | 38bcfcf | 2016-05-13 15:48:54 -0700 | [diff] [blame] | 131 | return allocation.value().hasEnoughResource(resource); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 136 | Set<ContinuousResource> children = getChildResources(parent); |
| 137 | if (children.isEmpty()) { |
| 138 | return Stream.of(); |
| 139 | } |
| 140 | |
| 141 | return children.stream() |
| 142 | .filter(x -> x.id().equals(parent.child(cls))) |
| 143 | // we don't use cascading simple predicates like follows to reduce accesses to consistent map |
| 144 | // .filter(x -> continuousConsumers.containsKey(x.id())) |
| 145 | // .filter(x -> continuousConsumers.get(x.id()) != null) |
| 146 | // .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); |
| 147 | .filter(resource -> { |
| 148 | Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id()); |
| 149 | if (allocation == null) { |
| 150 | return false; |
| 151 | } |
| 152 | return !allocation.value().allocations().isEmpty(); |
| 153 | }); |
| 154 | } |
| 155 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 158 | return consumers.values().stream() |
Sho SHIMIZU | e99af22 | 2016-05-12 11:47:48 -0700 | [diff] [blame] | 159 | .flatMap(x -> x.value().allocations().stream()) |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 160 | .filter(x -> x.consumerId().equals(consumerId)) |
Sho SHIMIZU | e99af22 | 2016-05-12 11:47:48 -0700 | [diff] [blame] | 161 | // this cast is safe because this class stores |
| 162 | // continuous resource allocations only |
| 163 | .map(x -> (ContinuousResource) x.resource()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 164 | } |
| 165 | } |