blob: d036c5f6b6e03cd60a6b30c353e240cdc91d3d3c [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.DiscreteResource;
21import org.onosproject.net.resource.DiscreteResourceId;
22import org.onosproject.net.resource.Resource;
23import org.onosproject.net.resource.ResourceAllocation;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070024import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070025import org.onosproject.net.resource.Resources;
26import 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
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070031import java.util.List;
32import java.util.Map;
33import java.util.Set;
34import java.util.stream.Stream;
35
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070036import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
37
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070038class ConsistentDiscreteResourceSubStore {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070039 private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070040 private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070041
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070042 ConsistentDiscreteResourceSubStore(StorageService service) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070043 this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070044 .withName(MapNames.DISCRETE_CONSUMER_MAP)
45 .withSerializer(SERIALIZER)
46 .build();
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070047 this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070048 .withName(MapNames.DISCRETE_CHILD_MAP)
49 .withSerializer(SERIALIZER)
50 .build();
51
Madan Jampanic6371882016-06-03 21:30:17 -070052 childMap.put(Resource.ROOT.id(), DiscreteResources.empty());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070053 }
54
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070055 TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) {
56 return new TransactionalDiscreteResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070057 }
58
59 // computational complexity: O(1)
60 List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070061 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
62 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070063 return ImmutableList.of();
64 }
65
Naoki Shiotabd1974c2016-04-29 18:44:17 -070066 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070067 }
68
69 Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070070 Versioned<DiscreteResources> children = childMap.get(parent);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070071
72 if (children == null) {
73 return ImmutableSet.of();
74 }
75
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070076 return children.value().values();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070077 }
78
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070079 <T> Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<T> cls) {
80 Versioned<DiscreteResources> children = childMap.get(parent);
81
82 if (children == null) {
83 return ImmutableSet.of();
84 }
85
86 return children.value().valuesOf(cls);
87 }
88
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070089 boolean isAvailable(DiscreteResource resource) {
90 return getResourceAllocations(resource.id()).isEmpty();
91 }
92
93 <T> Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
94 Set<DiscreteResource> children = getChildResources(parent);
95 if (children.isEmpty()) {
96 return Stream.of();
97 }
98
99 return children.stream()
100 .filter(x -> x.isTypeOf(cls))
101 .filter(x -> consumers.containsKey(x.id()));
102 }
103
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700104 Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700105 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700106 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700107 .map(Map.Entry::getKey)
108 .map(x -> Resources.discrete(x).resource());
109 }
110}