blob: 9135230121bbe3eae12e966bbe2f77649e9cea8e [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
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070048 ConsistentDiscreteResourceSubStore(StorageService service) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070049 this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070050 .withName(MapNames.DISCRETE_CONSUMER_MAP)
51 .withSerializer(SERIALIZER)
52 .build();
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070053 this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder()
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070054 .withName(MapNames.DISCRETE_CHILD_MAP)
55 .withSerializer(SERIALIZER)
56 .build();
57
Sacheth Hegde5a91f392018-05-07 17:11:34 -070058 Tools.retryable(
59 () -> childMap.putIfAbsent(Resource.ROOT.id(), DiscreteResources.empty()),
60 StorageException.ConcurrentModification.class,
61 Integer.MAX_VALUE,
62 50
63 ).get();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070064 }
65
Jordan Halterman6359c832017-03-29 16:53:21 -070066 @Override
67 public TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070068 return new TransactionalDiscreteResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070069 }
70
71 // computational complexity: O(1)
Jordan Halterman6359c832017-03-29 16:53:21 -070072 @Override
73 public List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070074 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
75 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070076 return ImmutableList.of();
77 }
78
Naoki Shiotabd1974c2016-04-29 18:44:17 -070079 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070080 }
81
Jordan Halterman6359c832017-03-29 16:53:21 -070082 @Override
83 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070084 Versioned<DiscreteResources> children = childMap.get(parent);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070085
86 if (children == null) {
87 return ImmutableSet.of();
88 }
89
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070090 return children.value().values();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070091 }
92
Jordan Halterman6359c832017-03-29 16:53:21 -070093 @Override
94 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070095 Versioned<DiscreteResources> children = childMap.get(parent);
96
97 if (children == null) {
98 return ImmutableSet.of();
99 }
100
101 return children.value().valuesOf(cls);
102 }
103
Jordan Halterman6359c832017-03-29 16:53:21 -0700104 @Override
105 public boolean isAvailable(DiscreteResource resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700106 return getResourceAllocations(resource.id()).isEmpty();
107 }
108
Jordan Halterman6359c832017-03-29 16:53:21 -0700109 @Override
110 public Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700111 Set<DiscreteResource> children = getChildResources(parent);
112 if (children.isEmpty()) {
113 return Stream.of();
114 }
115
116 return children.stream()
117 .filter(x -> x.isTypeOf(cls))
118 .filter(x -> consumers.containsKey(x.id()));
119 }
120
Jordan Halterman6359c832017-03-29 16:53:21 -0700121 @Override
122 public Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700123 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700124 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700125 .map(Map.Entry::getKey)
126 .map(x -> Resources.discrete(x).resource());
127 }
128}