blob: 785ae31789ca5bbdc7051ff8a345d8aed5fa04c3 [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.onosproject.net.resource.ContinuousResource;
21import org.onosproject.net.resource.ContinuousResourceId;
22import org.onosproject.net.resource.DiscreteResourceId;
23import org.onosproject.net.resource.Resource;
24import org.onosproject.net.resource.ResourceAllocation;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070025import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070026import org.onosproject.store.service.ConsistentMap;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070027import org.onosproject.store.service.StorageService;
28import org.onosproject.store.service.TransactionContext;
29import org.onosproject.store.service.Versioned;
30
31import java.util.LinkedHashSet;
32import java.util.List;
33import java.util.Set;
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070034import java.util.stream.Collectors;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070035import 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
Jordan Halterman6359c832017-03-29 16:53:21 -070039
40/**
41 * Consistent substore for continuous resources.
42 */
43class ConsistentContinuousResourceSubStore implements ConsistentResourceSubStore
44 <ContinuousResourceId, ContinuousResource, TransactionalContinuousResourceSubStore> {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070045 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
46 private ConsistentMap<DiscreteResourceId, Set<ContinuousResource>> childMap;
47
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070048 ConsistentContinuousResourceSubStore(StorageService service) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070049 this.consumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder()
50 .withName(MapNames.CONTINUOUS_CONSUMER_MAP)
51 .withSerializer(SERIALIZER)
52 .build();
53 this.childMap = service.<DiscreteResourceId, Set<ContinuousResource>>consistentMapBuilder()
54 .withName(MapNames.CONTINUOUS_CHILD_MAP)
55 .withSerializer(SERIALIZER)
56 .build();
57
Madan Jampanic6371882016-06-03 21:30:17 -070058 childMap.put(Resource.ROOT.id(), new LinkedHashSet<>());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070059 }
60
Jordan Halterman6359c832017-03-29 16:53:21 -070061 @Override
62 public TransactionalContinuousResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070063 return new TransactionalContinuousResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070064 }
65
66 // computational complexity: O(n) where n is the number of the existing allocations for the resource
Jordan Halterman6359c832017-03-29 16:53:21 -070067 @Override
68 public List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070069 Versioned<ContinuousResourceAllocation> allocations = consumers.get(resource);
70 if (allocations == null) {
71 return ImmutableList.of();
72 }
73
74 return allocations.value().allocations().stream()
75 .filter(x -> x.resource().id().equals(resource))
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070076 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070077 }
78
Jordan Halterman6359c832017-03-29 16:53:21 -070079 @Override
80 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070081 Versioned<Set<ContinuousResource>> children = childMap.get(parent);
82
83 if (children == null) {
84 return ImmutableSet.of();
85 }
86
87 return children.value();
88 }
89
Jordan Halterman6359c832017-03-29 16:53:21 -070090 @Override
91 public Set<ContinuousResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070092 // naive implementation
93 return getChildResources(parent).stream()
94 .filter(x -> x.isTypeOf(cls))
95 .collect(Collectors.toCollection(LinkedHashSet::new));
96 }
97
Jordan Halterman6359c832017-03-29 16:53:21 -070098 @Override
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070099 public boolean isAvailable(ContinuousResource resource) {
100 // check if it's registered or not.
101 Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id());
102 if (children == null) {
103 return false;
104 }
105
Yuta HIGUCHI9f111e62017-03-07 17:26:33 -0800106 boolean notEnoughRegistered = children.value().stream()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700107 .filter(c -> c.id().equals(resource.id()))
108 .findFirst()
Yuta HIGUCHI9f111e62017-03-07 17:26:33 -0800109 .map(registered -> registered.value() < resource.value())
110 .orElse(true);
111 if (notEnoughRegistered) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700112 // Capacity < requested, can never satisfy
113 return false;
114 }
115
116 // check if there's enough left
117 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
118 if (allocation == null) {
119 // no allocation (=no consumer) full registered resources available
120 return true;
121 }
122
Sho SHIMIZU38bcfcf2016-05-13 15:48:54 -0700123 return allocation.value().hasEnoughResource(resource);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700124 }
125
Jordan Halterman6359c832017-03-29 16:53:21 -0700126 @Override
127 public Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700128 Set<ContinuousResource> children = getChildResources(parent);
129 if (children.isEmpty()) {
130 return Stream.of();
131 }
132
133 return children.stream()
134 .filter(x -> x.id().equals(parent.child(cls)))
135 // we don't use cascading simple predicates like follows to reduce accesses to consistent map
136 // .filter(x -> continuousConsumers.containsKey(x.id()))
137 // .filter(x -> continuousConsumers.get(x.id()) != null)
138 // .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty());
139 .filter(resource -> {
140 Versioned<ContinuousResourceAllocation> allocation = consumers.get(resource.id());
141 if (allocation == null) {
142 return false;
143 }
144 return !allocation.value().allocations().isEmpty();
145 });
146 }
147
Jordan Halterman6359c832017-03-29 16:53:21 -0700148 @Override
149 public Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700150 return consumers.values().stream()
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700151 .flatMap(x -> x.value().allocations().stream())
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700152 .filter(x -> x.consumerId().equals(consumerId))
Sho SHIMIZUe99af222016-05-12 11:47:48 -0700153 // this cast is safe because this class stores
154 // continuous resource allocations only
155 .map(x -> (ContinuousResource) x.resource());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700156 }
157}