blob: 56505859c6d9c75175d819969fc00e7add47dfb0 [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
79 boolean isAvailable(DiscreteResource resource) {
80 return getResourceAllocations(resource.id()).isEmpty();
81 }
82
83 <T> Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
84 Set<DiscreteResource> children = getChildResources(parent);
85 if (children.isEmpty()) {
86 return Stream.of();
87 }
88
89 return children.stream()
90 .filter(x -> x.isTypeOf(cls))
91 .filter(x -> consumers.containsKey(x.id()));
92 }
93
Naoki Shiotabd1974c2016-04-29 18:44:17 -070094 Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070095 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -070096 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070097 .map(Map.Entry::getKey)
98 .map(x -> Resources.discrete(x).resource());
99 }
100}