blob: 06b458e8f196e377a4bf5edd6795b1c733e85969 [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;
20import org.onlab.util.Tools;
21import org.onosproject.net.resource.DiscreteResource;
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.net.resource.Resources;
27import org.onosproject.store.service.ConsistentMap;
28import org.onosproject.store.service.ConsistentMapException;
29import 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.Map;
36import java.util.Set;
37import java.util.stream.Stream;
38
39import 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;
42
43class ConsistentDiscreteResourceStore {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070044 private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070045 private ConsistentMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
46
47 ConsistentDiscreteResourceStore(StorageService service) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070048 this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070049 .withName(MapNames.DISCRETE_CONSUMER_MAP)
50 .withSerializer(SERIALIZER)
51 .build();
52 this.childMap = service.<DiscreteResourceId, Set<DiscreteResource>>consistentMapBuilder()
53 .withName(MapNames.DISCRETE_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
61 TransactionalDiscreteResourceStore transactional(TransactionContext tx) {
62 return new TransactionalDiscreteResourceStore(tx);
63 }
64
65 // computational complexity: O(1)
66 List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070067 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
68 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070069 return ImmutableList.of();
70 }
71
Naoki Shiotabd1974c2016-04-29 18:44:17 -070072 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070073 }
74
75 Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
76 Versioned<Set<DiscreteResource>> children = childMap.get(parent);
77
78 if (children == null) {
79 return ImmutableSet.of();
80 }
81
82 return children.value();
83 }
84
85 boolean isAvailable(DiscreteResource resource) {
86 return getResourceAllocations(resource.id()).isEmpty();
87 }
88
89 <T> Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
90 Set<DiscreteResource> children = getChildResources(parent);
91 if (children.isEmpty()) {
92 return Stream.of();
93 }
94
95 return children.stream()
96 .filter(x -> x.isTypeOf(cls))
97 .filter(x -> consumers.containsKey(x.id()));
98 }
99
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700100 Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700101 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700102 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700103 .map(Map.Entry::getKey)
104 .map(x -> Resources.discrete(x).resource());
105 }
106}