blob: 555f2509b3c2dc897d54b2a8e8030feae6f24393 [file] [log] [blame]
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZU22fb2832016-05-06 11:44:03 -07003 *
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;
Sacheth Hegde5a91f392018-05-07 17:11:34 -070020import org.onlab.util.Tools;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070021import 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;
Sacheth Hegde5a91f392018-05-07 17:11:34 -070028import org.onosproject.store.service.StorageException;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070029import 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
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070038import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
39
Jordan Halterman6359c832017-03-29 16:53:21 -070040/**
41 * Consistent substore for discrete resources.
42 */
43class ConsistentDiscreteResourceSubStore implements ConsistentResourceSubStore
44 <DiscreteResourceId, DiscreteResource, TransactionalDiscreteResourceSubStore> {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070045 private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070046 private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap;
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070047
Yuta HIGUCHIaaab65e2018-05-08 11:23:54 -070048 @SuppressWarnings("ReturnValueIgnored")
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070049 ConsistentDiscreteResourceSubStore(StorageService service) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070050 this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070051 .withName(MapNames.DISCRETE_CONSUMER_MAP)
52 .withSerializer(SERIALIZER)
53 .build();
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070054 this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070055 .withName(MapNames.DISCRETE_CHILD_MAP)
56 .withSerializer(SERIALIZER)
57 .build();
58
Sacheth Hegde5a91f392018-05-07 17:11:34 -070059 Tools.retryable(
60 () -> childMap.putIfAbsent(Resource.ROOT.id(), DiscreteResources.empty()),
61 StorageException.ConcurrentModification.class,
62 Integer.MAX_VALUE,
63 50
64 ).get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070065 }
66
Jordan Halterman6359c832017-03-29 16:53:21 -070067 @Override
68 public TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070069 return new TransactionalDiscreteResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070070 }
71
72 // computational complexity: O(1)
Jordan Halterman6359c832017-03-29 16:53:21 -070073 @Override
74 public List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070075 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
76 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070077 return ImmutableList.of();
78 }
79
Naoki Shiotabd1974c2016-04-29 18:44:17 -070080 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070081 }
82
Jordan Halterman6359c832017-03-29 16:53:21 -070083 @Override
84 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070085 Versioned<DiscreteResources> children = childMap.get(parent);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070086
87 if (children == null) {
88 return ImmutableSet.of();
89 }
90
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070091 return children.value().values();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070092 }
93
Jordan Halterman6359c832017-03-29 16:53:21 -070094 @Override
95 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070096 Versioned<DiscreteResources> children = childMap.get(parent);
97
98 if (children == null) {
99 return ImmutableSet.of();
100 }
101
102 return children.value().valuesOf(cls);
103 }
104
Jordan Halterman6359c832017-03-29 16:53:21 -0700105 @Override
106 public boolean isAvailable(DiscreteResource resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700107 return getResourceAllocations(resource.id()).isEmpty();
108 }
109
Jordan Halterman6359c832017-03-29 16:53:21 -0700110 @Override
111 public Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700112 Set<DiscreteResource> children = getChildResources(parent);
113 if (children.isEmpty()) {
114 return Stream.of();
115 }
116
117 return children.stream()
118 .filter(x -> x.isTypeOf(cls))
119 .filter(x -> consumers.containsKey(x.id()));
120 }
121
Jordan Halterman6359c832017-03-29 16:53:21 -0700122 @Override
123 public Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700124 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700125 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700126 .map(Map.Entry::getKey)
127 .map(x -> Resources.discrete(x).resource());
128 }
129}