Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 20 | import org.onosproject.net.resource.DiscreteResource; |
| 21 | import org.onosproject.net.resource.DiscreteResourceId; |
| 22 | import org.onosproject.net.resource.Resource; |
| 23 | import org.onosproject.net.resource.ResourceAllocation; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 24 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 25 | import org.onosproject.net.resource.Resources; |
| 26 | import org.onosproject.store.service.ConsistentMap; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 27 | import org.onosproject.store.service.StorageService; |
| 28 | import org.onosproject.store.service.TransactionContext; |
| 29 | import org.onosproject.store.service.Versioned; |
| 30 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.Stream; |
| 35 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 36 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
| 37 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 38 | /** |
| 39 | * Consistent substore for discrete resources. |
| 40 | */ |
| 41 | class ConsistentDiscreteResourceSubStore implements ConsistentResourceSubStore |
| 42 | <DiscreteResourceId, DiscreteResource, TransactionalDiscreteResourceSubStore> { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 43 | private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers; |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 44 | private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 45 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 46 | ConsistentDiscreteResourceSubStore(StorageService service) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 47 | this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 48 | .withName(MapNames.DISCRETE_CONSUMER_MAP) |
| 49 | .withSerializer(SERIALIZER) |
| 50 | .build(); |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 51 | this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder() |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 52 | .withName(MapNames.DISCRETE_CHILD_MAP) |
| 53 | .withSerializer(SERIALIZER) |
| 54 | .build(); |
| 55 | |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 56 | childMap.put(Resource.ROOT.id(), DiscreteResources.empty()); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 59 | @Override |
| 60 | public TransactionalDiscreteResourceSubStore transactional(TransactionContext tx) { |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 61 | return new TransactionalDiscreteResourceSubStore(tx); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // computational complexity: O(1) |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 65 | @Override |
| 66 | public List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) { |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 67 | Versioned<ResourceConsumerId> consumerId = consumers.get(resource); |
| 68 | if (consumerId == null) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 69 | return ImmutableList.of(); |
| 70 | } |
| 71 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 72 | return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value())); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 75 | @Override |
| 76 | public Set<DiscreteResource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 77 | Versioned<DiscreteResources> children = childMap.get(parent); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 78 | |
| 79 | if (children == null) { |
| 80 | return ImmutableSet.of(); |
| 81 | } |
| 82 | |
Sho SHIMIZU | b85000d | 2016-05-17 14:00:05 -0700 | [diff] [blame] | 83 | return children.value().values(); |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 86 | @Override |
| 87 | public Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 88 | 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 Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 97 | @Override |
| 98 | public boolean isAvailable(DiscreteResource resource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 99 | return getResourceAllocations(resource.id()).isEmpty(); |
| 100 | } |
| 101 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 102 | @Override |
| 103 | public Stream<DiscreteResource> getAllocatedResources(DiscreteResourceId parent, Class<?> cls) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 104 | 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 Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame^] | 114 | @Override |
| 115 | public Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 116 | return consumers.entrySet().stream() |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 117 | .filter(x -> x.getValue().value().equals(consumerId)) |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 118 | .map(Map.Entry::getKey) |
| 119 | .map(x -> Resources.discrete(x).resource()); |
| 120 | } |
| 121 | } |