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.onlab.util.GuavaCollectors; |
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; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.StorageService; |
| 29 | import org.onosproject.store.service.TransactionContext; |
| 30 | import org.onosproject.store.service.Versioned; |
| 31 | |
| 32 | import java.util.LinkedHashSet; |
| 33 | import java.util.List; |
| 34 | import java.util.Set; |
| 35 | import java.util.stream.Stream; |
| 36 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 37 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 38 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 39 | class ConsistentContinuousResourceSubStore { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 40 | private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; |
| 41 | private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap; |
| 42 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 43 | ConsistentContinuousResourceSubStore(StorageService service) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 44 | this.consumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder() |
| 45 | .withName(MapNames.CONTINUOUS_CONSUMER_MAP) |
| 46 | .withSerializer(SERIALIZER) |
| 47 | .build(); |
| 48 | this.childMap = service.<DiscreteResourceId, Set<ContinuousResource>>consistentMapBuilder() |
| 49 | .withName(MapNames.CONTINUOUS_CHILD_MAP) |
| 50 | .withSerializer(SERIALIZER) |
| 51 | .build(); |
| 52 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame^] | 53 | childMap.put(Resource.ROOT.id(), new LinkedHashSet<>()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 56 | TransactionalContinuousResourceSubStore transactional(TransactionContext tx) { |
| 57 | return new TransactionalContinuousResourceSubStore(tx); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // computational complexity: O(n) where n is the number of the existing allocations for the resource |
| 61 | List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) { |
| 62 | Versioned<ContinuousResourceAllocation> allocations = consumers.get(resource); |
| 63 | if (allocations == null) { |
| 64 | return ImmutableList.of(); |
| 65 | } |
| 66 | |
| 67 | return allocations.value().allocations().stream() |
| 68 | .filter(x -> x.resource().id().equals(resource)) |
| 69 | .collect(GuavaCollectors.toImmutableList()); |
| 70 | } |
| 71 | |
| 72 | Set<ContinuousResource> getChildResources(DiscreteResourceId parent) { |
| 73 | Versioned<Set<ContinuousResource>> children = childMap.get(parent); |
| 74 | |
| 75 | if (children == null) { |
| 76 | return ImmutableSet.of(); |
| 77 | } |
| 78 | |
| 79 | return children.value(); |
| 80 | } |
| 81 | |
| 82 | public boolean isAvailable(ContinuousResource resource) { |
| 83 | // check if it's registered or not. |
| 84 | Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id()); |
| 85 | if (children == null) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | ContinuousResource registered = children.value().stream() |
| 90 | .filter(c -> c.id().equals(resource.id())) |
| 91 | .findFirst() |
| 92 | .get(); |
| 93 | if (registered.value() < resource.value()) { |
| 94 | // Capacity < requested, can never satisfy |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // check if there's enough left |
| 99 | Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id()); |
| 100 | if (allocation == null) { |
| 101 | // no allocation (=no consumer) full registered resources available |
| 102 | return true; |
| 103 | } |
| 104 | |
Sho SHIMIZU | 38bcfcf | 2016-05-13 15:48:54 -0700 | [diff] [blame] | 105 | return allocation.value().hasEnoughResource(resource); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | <T> Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
| 109 | Set<ContinuousResource> children = getChildResources(parent); |
| 110 | if (children.isEmpty()) { |
| 111 | return Stream.of(); |
| 112 | } |
| 113 | |
| 114 | return children.stream() |
| 115 | .filter(x -> x.id().equals(parent.child(cls))) |
| 116 | // we don't use cascading simple predicates like follows to reduce accesses to consistent map |
| 117 | // .filter(x -> continuousConsumers.containsKey(x.id())) |
| 118 | // .filter(x -> continuousConsumers.get(x.id()) != null) |
| 119 | // .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); |
| 120 | .filter(resource -> { |
| 121 | Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id()); |
| 122 | if (allocation == null) { |
| 123 | return false; |
| 124 | } |
| 125 | return !allocation.value().allocations().isEmpty(); |
| 126 | }); |
| 127 | } |
| 128 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 129 | Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 130 | return consumers.values().stream() |
Sho SHIMIZU | e99af22 | 2016-05-12 11:47:48 -0700 | [diff] [blame] | 131 | .flatMap(x -> x.value().allocations().stream()) |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 132 | .filter(x -> x.consumerId().equals(consumerId)) |
Sho SHIMIZU | e99af22 | 2016-05-12 11:47:48 -0700 | [diff] [blame] | 133 | // this cast is safe because this class stores |
| 134 | // continuous resource allocations only |
| 135 | .map(x -> (ContinuousResource) x.resource()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 136 | } |
| 137 | } |