Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.net.newresource.impl; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame^] | 20 | import com.google.common.collect.Lists; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 25 | import org.onosproject.net.newresource.ResourceAdminService; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 26 | import org.onosproject.net.newresource.ResourceAllocation; |
| 27 | import org.onosproject.net.newresource.ResourceConsumer; |
| 28 | import org.onosproject.net.newresource.ResourceService; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 29 | import org.onosproject.net.newresource.ResourcePath; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 30 | import org.onosproject.net.newresource.ResourceStore; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Collection; |
| 35 | import java.util.List; |
| 36 | import java.util.Optional; |
| 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | |
| 41 | /** |
| 42 | * An implementation of ResourceService. |
| 43 | */ |
| 44 | @Component(immediate = true, enabled = false) |
| 45 | @Service |
| 46 | @Beta |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 47 | public final class ResourceManager implements ResourceService, ResourceAdminService { |
| 48 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected ResourceStore store; |
| 51 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 52 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 53 | public Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 54 | checkNotNull(consumer); |
| 55 | checkNotNull(resource); |
| 56 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 57 | List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource)); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 58 | if (allocations.isEmpty()) { |
| 59 | return Optional.empty(); |
| 60 | } |
| 61 | |
| 62 | assert allocations.size() == 1; |
| 63 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 64 | ResourceAllocation allocation = allocations.get(0); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 65 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 66 | assert allocation.resource().equals(resource); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 67 | |
| 68 | // cast is ensured by the assertions above |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 69 | return Optional.of(allocation); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 73 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, |
| 74 | List<ResourcePath> resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 75 | checkNotNull(consumer); |
| 76 | checkNotNull(resources); |
| 77 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 78 | // TODO: implement support of resource hierarchy |
| 79 | // allocation for a particular resource implies allocations for all of the sub-resources need to be done |
| 80 | |
| 81 | boolean success = store.allocate(resources, consumer); |
| 82 | if (!success) { |
| 83 | return ImmutableList.of(); |
| 84 | } |
| 85 | |
| 86 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 87 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 88 | .collect(Collectors.toList()); |
| 89 | } |
| 90 | |
| 91 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 92 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 93 | checkNotNull(consumer); |
| 94 | checkNotNull(resources); |
| 95 | |
| 96 | return allocate(consumer, Arrays.asList(resources)); |
| 97 | } |
| 98 | |
| 99 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 100 | public boolean release(ResourceAllocation allocation) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 101 | checkNotNull(allocation); |
| 102 | |
| 103 | return release(ImmutableList.of(allocation)); |
| 104 | } |
| 105 | |
| 106 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 107 | public boolean release(List<ResourceAllocation> allocations) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 108 | checkNotNull(allocations); |
| 109 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 110 | List<ResourcePath> resources = allocations.stream() |
| 111 | .map(ResourceAllocation::resource) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 112 | .collect(Collectors.toList()); |
| 113 | List<ResourceConsumer> consumers = allocations.stream() |
| 114 | .map(ResourceAllocation::consumer) |
| 115 | .collect(Collectors.toList()); |
| 116 | |
| 117 | return store.release(resources, consumers); |
| 118 | } |
| 119 | |
| 120 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 121 | public boolean release(ResourceAllocation... allocations) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 122 | checkNotNull(allocations); |
| 123 | |
| 124 | return release(ImmutableList.copyOf(allocations)); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean release(ResourceConsumer consumer) { |
| 129 | checkNotNull(consumer); |
| 130 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 131 | Collection<ResourceAllocation> allocations = getResourceAllocations(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 132 | return release(ImmutableList.copyOf(allocations)); |
| 133 | } |
| 134 | |
| 135 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 136 | public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) { |
| 137 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 138 | checkNotNull(cls); |
| 139 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 140 | Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls); |
| 141 | List<ResourceAllocation> allocations = new ArrayList<>(resources.size()); |
| 142 | for (ResourcePath resource: resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 143 | // We access store twice in this method, then the store may be updated by others |
| 144 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 145 | if (consumer.isPresent()) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 146 | allocations.add(new ResourceAllocation(resource, consumer.get())); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | return allocations; |
| 151 | } |
| 152 | |
| 153 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 154 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 155 | checkNotNull(consumer); |
| 156 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 157 | Collection<ResourcePath> resources = store.getResources(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 158 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 159 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 160 | .collect(Collectors.toList()); |
| 161 | } |
| 162 | |
| 163 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 164 | public boolean isAvailable(ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 165 | checkNotNull(resource); |
| 166 | |
| 167 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 168 | return !consumer.isPresent(); |
| 169 | } |
| 170 | |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 171 | @Override |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame^] | 172 | public <T> boolean registerResources(ResourcePath parent, List<T> children) { |
| 173 | List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); |
| 174 | return store.register(parent, resources); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 175 | } |
| 176 | } |