blob: c19e6c3dcb6c54383dedd37765bd78b30c80ac41 [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
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070033import java.util.List;
34import java.util.Map;
35import java.util.Set;
36import java.util.stream.Stream;
37
38import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES;
39import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY;
40import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
41
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070042class ConsistentDiscreteResourceSubStore {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070043 private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070044 private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070045
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070046 ConsistentDiscreteResourceSubStore(StorageService service) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070047 this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070048 .withName(MapNames.DISCRETE_CONSUMER_MAP)
49 .withSerializer(SERIALIZER)
50 .build();
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070051 this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070052 .withName(MapNames.DISCRETE_CHILD_MAP)
53 .withSerializer(SERIALIZER)
54 .build();
55
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070056 Tools.retryable(() -> childMap.put(Resource.ROOT.id(), DiscreteResources.empty()),
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070057 ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY);
58 }
59
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070060 TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) {
61 return new TransactionalDiscreteResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070062 }
63
64 // computational complexity: O(1)
65 List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070066 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
67 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070068 return ImmutableList.of();
69 }
70
Naoki Shiotabd1974c2016-04-29 18:44:17 -070071 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070072 }
73
74 Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070075 Versioned<DiscreteResources> children = childMap.get(parent);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070076
77 if (children == null) {
78 return ImmutableSet.of();
79 }
80
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070081 return children.value().values();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070082 }
83
84 boolean isAvailable(DiscreteResource resource) {
85 return getResourceAllocations(resource.id()).isEmpty();
86 }
87
88 <T> Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
89 Set<DiscreteResource> children = getChildResources(parent);
90 if (children.isEmpty()) {
91 return Stream.of();
92 }
93
94 return children.stream()
95 .filter(x -> x.isTypeOf(cls))
96 .filter(x -> consumers.containsKey(x.id()));
97 }
98
Naoki Shiotabd1974c2016-04-29 18:44:17 -070099 Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700100 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700101 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700102 .map(Map.Entry::getKey)
103 .map(x -> Resources.discrete(x).resource());
104 }
105}