blob: e9c9ad741b0677f110f37881938689953cf11629 [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;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070021import org.onosproject.net.resource.ContinuousResource;
22import org.onosproject.net.resource.ContinuousResourceId;
23import org.onosproject.net.resource.DiscreteResourceId;
24import org.onosproject.net.resource.Resource;
25import org.onosproject.net.resource.ResourceAllocation;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070026import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070027import org.onosproject.store.service.ConsistentMap;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070028import org.onosproject.store.service.StorageService;
29import org.onosproject.store.service.TransactionContext;
30import org.onosproject.store.service.Versioned;
31
32import java.util.LinkedHashSet;
33import java.util.List;
34import java.util.Set;
35import java.util.stream.Stream;
36
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070037import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070038
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070039class ConsistentContinuousResourceSubStore {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070040 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
41 private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
42
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070043 ConsistentContinuousResourceSubStore(StorageService service) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070044 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 Jampanic6371882016-06-03 21:30:17 -070053 childMap.put(Resource.ROOT.id(), new LinkedHashSet<>());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070054 }
55
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070056 TransactionalContinuousResourceSubStore transactional(TransactionContext tx) {
57 return new TransactionalContinuousResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070058 }
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 SHIMIZU38bcfcf2016-05-13 15:48:54 -0700105 return allocation.value().hasEnoughResource(resource);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700106 }
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 Shiotabd1974c2016-04-29 18:44:17 -0700129 Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700130 return consumers.values().stream()
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700131 .flatMap(x -> x.value().allocations().stream())
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700132 .filter(x -> x.consumerId().equals(consumerId))
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700133 // this cast is safe because this class stores
134 // continuous resource allocations only
135 .map(x -> (ContinuousResource) x.resource());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700136 }
137}