blob: 7b4a4b5fd9b9a04c5d921852ab0e5c2005f10de5 [file] [log] [blame]
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07001/*
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 */
16package org.onosproject.store.resource.impl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070020import org.onlab.util.GuavaCollectors;
21import org.onlab.util.Tools;
22import org.onosproject.net.resource.ContinuousResource;
23import org.onosproject.net.resource.ContinuousResourceId;
24import org.onosproject.net.resource.DiscreteResourceId;
25import org.onosproject.net.resource.Resource;
26import org.onosproject.net.resource.ResourceAllocation;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070027import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070028import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.ConsistentMapException;
30import org.onosproject.store.service.StorageService;
31import org.onosproject.store.service.TransactionContext;
32import org.onosproject.store.service.Versioned;
33
34import java.util.LinkedHashSet;
35import java.util.List;
36import java.util.Set;
37import java.util.stream.Stream;
38
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070039import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES;
40import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY;
41import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070042
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070043class ConsistentContinuousResourceSubStore {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070044 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
45 private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
46
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070047 ConsistentContinuousResourceSubStore(StorageService service) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070048 this.consumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder()
49 .withName(MapNames.CONTINUOUS_CONSUMER_MAP)
50 .withSerializer(SERIALIZER)
51 .build();
52 this.childMap = service.<DiscreteResourceId, Set<ContinuousResource>>consistentMapBuilder()
53 .withName(MapNames.CONTINUOUS_CHILD_MAP)
54 .withSerializer(SERIALIZER)
55 .build();
56
57 Tools.retryable(() -> childMap.put(Resource.ROOT.id(), new LinkedHashSet<>()),
58 ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY);
59 }
60
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070061 TransactionalContinuousResourceSubStore transactional(TransactionContext tx) {
62 return new TransactionalContinuousResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070063 }
64
65 // computational complexity: O(n) where n is the number of the existing allocations for the resource
66 List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) {
67 Versioned<ContinuousResourceAllocation> allocations = consumers.get(resource);
68 if (allocations == null) {
69 return ImmutableList.of();
70 }
71
72 return allocations.value().allocations().stream()
73 .filter(x -> x.resource().id().equals(resource))
74 .collect(GuavaCollectors.toImmutableList());
75 }
76
77 Set<ContinuousResource> getChildResources(DiscreteResourceId parent) {
78 Versioned<Set<ContinuousResource>> children = childMap.get(parent);
79
80 if (children == null) {
81 return ImmutableSet.of();
82 }
83
84 return children.value();
85 }
86
87 public boolean isAvailable(ContinuousResource resource) {
88 // check if it's registered or not.
89 Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id());
90 if (children == null) {
91 return false;
92 }
93
94 ContinuousResource registered = children.value().stream()
95 .filter(c -> c.id().equals(resource.id()))
96 .findFirst()
97 .get();
98 if (registered.value() < resource.value()) {
99 // Capacity < requested, can never satisfy
100 return false;
101 }
102
103 // check if there's enough left
104 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
105 if (allocation == null) {
106 // no allocation (=no consumer) full registered resources available
107 return true;
108 }
109
Sho SHIMIZU38bcfcf2016-05-13 15:48:54 -0700110 return allocation.value().hasEnoughResource(resource);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700111 }
112
113 <T> Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
114 Set<ContinuousResource> children = getChildResources(parent);
115 if (children.isEmpty()) {
116 return Stream.of();
117 }
118
119 return children.stream()
120 .filter(x -> x.id().equals(parent.child(cls)))
121 // we don't use cascading simple predicates like follows to reduce accesses to consistent map
122 // .filter(x -> continuousConsumers.containsKey(x.id()))
123 // .filter(x -> continuousConsumers.get(x.id()) != null)
124 // .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty());
125 .filter(resource -> {
126 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
127 if (allocation == null) {
128 return false;
129 }
130 return !allocation.value().allocations().isEmpty();
131 });
132 }
133
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700134 Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700135 return consumers.values().stream()
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700136 .flatMap(x -> x.value().allocations().stream())
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700137 .filter(x -> x.consumerId().equals(consumerId))
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700138 // this cast is safe because this class stores
139 // continuous resource allocations only
140 .map(x -> (ContinuousResource) x.resource());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700141 }
142}