blob: 51cd4037dbf67914ab739fb71030da582407ea82 [file] [log] [blame]
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07003 *
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;
Sacheth Hegde5a91f392018-05-07 17:11:34 -070020import org.onlab.util.Tools;
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;
Sacheth Hegde5a91f392018-05-07 17:11:34 -070028import org.onosproject.store.service.StorageException;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070029import org.onosproject.store.service.StorageService;
30import org.onosproject.store.service.TransactionContext;
31import org.onosproject.store.service.Versioned;
32
33import java.util.LinkedHashSet;
34import java.util.List;
35import java.util.Set;
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070036import java.util.stream.Collectors;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070037import java.util.stream.Stream;
38
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070039import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070040
Jordan Halterman6359c832017-03-29 16:53:21 -070041
42/**
43 * Consistent substore for continuous resources.
44 */
45class ConsistentContinuousResourceSubStore implements ConsistentResourceSubStore
46 <ContinuousResourceId, ContinuousResource, TransactionalContinuousResourceSubStore> {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070047 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
48 private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
49
Yuta HIGUCHIaaab65e2018-05-08 11:23:54 -070050 @SuppressWarnings("ReturnValueIgnored")
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070051 ConsistentContinuousResourceSubStore(StorageService service) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070052 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 Hegde5a91f392018-05-07 17:11:34 -070061 Tools.retryable(
62 () -> childMap.putIfAbsent(Resource.ROOT.id(), new LinkedHashSet<>()),
63 StorageException.ConcurrentModification.class,
64 Integer.MAX_VALUE,
65 50
66 ).get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070067 }
68
Jordan Halterman6359c832017-03-29 16:53:21 -070069 @Override
70 public TransactionalContinuousResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070071 return new TransactionalContinuousResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070072 }
73
74 // computational complexity: O(n) where n is the number of the existing allocations for the resource
Jordan Halterman6359c832017-03-29 16:53:21 -070075 @Override
76 public List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070077 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 HIGUCHI498fa1d2017-05-17 16:08:40 -070084 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070085 }
86
Jordan Halterman6359c832017-03-29 16:53:21 -070087 @Override
88 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070089 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 Halterman6359c832017-03-29 16:53:21 -070098 @Override
99 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700100 // naive implementation
101 return getChildResources(parent).stream()
102 .filter(x -> x.isTypeOf(cls))
103 .collect(Collectors.toCollection(LinkedHashSet::new));
104 }
105
Jordan Halterman6359c832017-03-29 16:53:21 -0700106 @Override
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700107 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 HIGUCHI9f111e62017-03-07 17:26:33 -0800114 boolean notEnoughRegistered = children.value().stream()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700115 .filter(c -> c.id().equals(resource.id()))
116 .findFirst()
Yuta HIGUCHI9f111e62017-03-07 17:26:33 -0800117 .map(registered -> registered.value() < resource.value())
118 .orElse(true);
119 if (notEnoughRegistered) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700120 // 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 SHIMIZU38bcfcf2016-05-13 15:48:54 -0700131 return allocation.value().hasEnoughResource(resource);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700132 }
133
Jordan Halterman6359c832017-03-29 16:53:21 -0700134 @Override
135 public Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700136 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 Halterman6359c832017-03-29 16:53:21 -0700156 @Override
157 public Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700158 return consumers.values().stream()
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700159 .flatMap(x -> x.value().allocations().stream())
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700160 .filter(x -> x.consumerId().equals(consumerId))
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700161 // this cast is safe because this class stores
162 // continuous resource allocations only
163 .map(x -> (ContinuousResource) x.resource());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700164 }
165}