blob: be9d59261d6081ba748d8734d72c2c64af7c8583 [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
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070050 ConsistentContinuousResourceSubStore(StorageService service) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070051 this.consumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder()
52 .withName(MapNames.CONTINUOUS_CONSUMER_MAP)
53 .withSerializer(SERIALIZER)
54 .build();
55 this.childMap = service.<DiscreteResourceId, Set<ContinuousResource>>consistentMapBuilder()
56 .withName(MapNames.CONTINUOUS_CHILD_MAP)
57 .withSerializer(SERIALIZER)
58 .build();
59
Sacheth Hegde5a91f392018-05-07 17:11:34 -070060 Tools.retryable(
61 () -> childMap.putIfAbsent(Resource.ROOT.id(), new LinkedHashSet<>()),
62 StorageException.ConcurrentModification.class,
63 Integer.MAX_VALUE,
64 50
65 ).get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070066 }
67
Jordan Halterman6359c832017-03-29 16:53:21 -070068 @Override
69 public TransactionalContinuousResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070070 return new TransactionalContinuousResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070071 }
72
73 // computational complexity: O(n) where n is the number of the existing allocations for the resource
Jordan Halterman6359c832017-03-29 16:53:21 -070074 @Override
75 public List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070076 Versioned<ContinuousResourceAllocation> allocations = consumers.get(resource);
77 if (allocations == null) {
78 return ImmutableList.of();
79 }
80
81 return allocations.value().allocations().stream()
82 .filter(x -> x.resource().id().equals(resource))
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070083 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070084 }
85
Jordan Halterman6359c832017-03-29 16:53:21 -070086 @Override
87 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070088 Versioned<Set<ContinuousResource>> children = childMap.get(parent);
89
90 if (children == null) {
91 return ImmutableSet.of();
92 }
93
94 return children.value();
95 }
96
Jordan Halterman6359c832017-03-29 16:53:21 -070097 @Override
98 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070099 // naive implementation
100 return getChildResources(parent).stream()
101 .filter(x -> x.isTypeOf(cls))
102 .collect(Collectors.toCollection(LinkedHashSet::new));
103 }
104
Jordan Halterman6359c832017-03-29 16:53:21 -0700105 @Override
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700106 public boolean isAvailable(ContinuousResource resource) {
107 // check if it's registered or not.
108 Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id());
109 if (children == null) {
110 return false;
111 }
112
Yuta HIGUCHI9f111e62017-03-07 17:26:33 -0800113 boolean notEnoughRegistered = children.value().stream()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700114 .filter(c -> c.id().equals(resource.id()))
115 .findFirst()
Yuta HIGUCHI9f111e62017-03-07 17:26:33 -0800116 .map(registered -> registered.value() < resource.value())
117 .orElse(true);
118 if (notEnoughRegistered) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700119 // Capacity < requested, can never satisfy
120 return false;
121 }
122
123 // check if there's enough left
124 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
125 if (allocation == null) {
126 // no allocation (=no consumer) full registered resources available
127 return true;
128 }
129
Sho SHIMIZU38bcfcf2016-05-13 15:48:54 -0700130 return allocation.value().hasEnoughResource(resource);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700131 }
132
Jordan Halterman6359c832017-03-29 16:53:21 -0700133 @Override
134 public Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700135 Set<ContinuousResource> children = getChildResources(parent);
136 if (children.isEmpty()) {
137 return Stream.of();
138 }
139
140 return children.stream()
141 .filter(x -> x.id().equals(parent.child(cls)))
142 // we don't use cascading simple predicates like follows to reduce accesses to consistent map
143 // .filter(x -> continuousConsumers.containsKey(x.id()))
144 // .filter(x -> continuousConsumers.get(x.id()) != null)
145 // .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty());
146 .filter(resource -> {
147 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
148 if (allocation == null) {
149 return false;
150 }
151 return !allocation.value().allocations().isEmpty();
152 });
153 }
154
Jordan Halterman6359c832017-03-29 16:53:21 -0700155 @Override
156 public Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700157 return consumers.values().stream()
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700158 .flatMap(x -> x.value().allocations().stream())
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700159 .filter(x -> x.consumerId().equals(consumerId))
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700160 // this cast is safe because this class stores
161 // continuous resource allocations only
162 .map(x -> (ContinuousResource) x.resource());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700163 }
164}