blob: d6eb37674ddf8acd595d781c4d6e266ee03a7664 [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
Jordan Halterman6359c832017-03-29 16:53:21 -070038/**
39 * Consistent substore for discrete resources.
40 */
41class ConsistentDiscreteResourceSubStore implements ConsistentResourceSubStore
42 <DiscreteResourceId, DiscreteResource, TransactionalDiscreteResourceSubStore> {
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
Yuta HIGUCHIab9fa372017-07-25 18:06:43 -070056 childMap.putIfAbsent(Resource.ROOT.id(), DiscreteResources.empty());
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070057 }
58
Jordan Halterman6359c832017-03-29 16:53:21 -070059 @Override
60 public TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) {
Sho SHIMIZU7ecf5ea2016-05-13 15:28:59 -070061 return new TransactionalDiscreteResourceSubStore(tx);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070062 }
63
64 // computational complexity: O(1)
Jordan Halterman6359c832017-03-29 16:53:21 -070065 @Override
66 public List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070067 Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
68 if (consumerId == null) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070069 return ImmutableList.of();
70 }
71
Naoki Shiotabd1974c2016-04-29 18:44:17 -070072 return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070073 }
74
Jordan Halterman6359c832017-03-29 16:53:21 -070075 @Override
76 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070077 Versioned<DiscreteResources> children = childMap.get(parent);
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070078
79 if (children == null) {
80 return ImmutableSet.of();
81 }
82
Sho SHIMIZUb85000d2016-05-17 14:00:05 -070083 return children.value().values();
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070084 }
85
Jordan Halterman6359c832017-03-29 16:53:21 -070086 @Override
87 public Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -070088 Versioned<DiscreteResources> children = childMap.get(parent);
89
90 if (children == null) {
91 return ImmutableSet.of();
92 }
93
94 return children.value().valuesOf(cls);
95 }
96
Jordan Halterman6359c832017-03-29 16:53:21 -070097 @Override
98 public boolean isAvailable(DiscreteResource resource) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -070099 return getResourceAllocations(resource.id()).isEmpty();
100 }
101
Jordan Halterman6359c832017-03-29 16:53:21 -0700102 @Override
103 public Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700104 Set<DiscreteResource> children = getChildResources(parent);
105 if (children.isEmpty()) {
106 return Stream.of();
107 }
108
109 return children.stream()
110 .filter(x -> x.isTypeOf(cls))
111 .filter(x -> consumers.containsKey(x.id()));
112 }
113
Jordan Halterman6359c832017-03-29 16:53:21 -0700114 @Override
115 public Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700116 return consumers.entrySet().stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700117 .filter(x -> x.getValue().value().equals(consumerId))
Sho SHIMIZU22fb2832016-05-06 11:44:03 -0700118 .map(Map.Entry::getKey)
119 .map(x -> Resources.discrete(x).resource());
120 }
121}