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; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 33 | import java.util.Collection; |
| 34 | import java.util.List; |
| 35 | import java.util.Optional; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 38 | import static com.google.common.base.Preconditions.checkArgument; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 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 List<ResourceAllocation> allocate(ResourceConsumer consumer, |
| 54 | List<ResourcePath> resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 55 | checkNotNull(consumer); |
| 56 | checkNotNull(resources); |
| 57 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 58 | // TODO: implement support of resource hierarchy |
| 59 | // allocation for a particular resource implies allocations for all of the sub-resources need to be done |
| 60 | |
| 61 | boolean success = store.allocate(resources, consumer); |
| 62 | if (!success) { |
| 63 | return ImmutableList.of(); |
| 64 | } |
| 65 | |
| 66 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 67 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 68 | .collect(Collectors.toList()); |
| 69 | } |
| 70 | |
| 71 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 72 | public boolean release(List<ResourceAllocation> allocations) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 73 | checkNotNull(allocations); |
| 74 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 75 | List<ResourcePath> resources = allocations.stream() |
| 76 | .map(ResourceAllocation::resource) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 77 | .collect(Collectors.toList()); |
| 78 | List<ResourceConsumer> consumers = allocations.stream() |
| 79 | .map(ResourceAllocation::consumer) |
| 80 | .collect(Collectors.toList()); |
| 81 | |
| 82 | return store.release(resources, consumers); |
| 83 | } |
| 84 | |
| 85 | @Override |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 86 | public boolean release(ResourceConsumer consumer) { |
| 87 | checkNotNull(consumer); |
| 88 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 89 | Collection<ResourceAllocation> allocations = getResourceAllocations(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 90 | return release(ImmutableList.copyOf(allocations)); |
| 91 | } |
| 92 | |
| 93 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 94 | public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) { |
| 95 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 96 | checkNotNull(cls); |
| 97 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 98 | Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls); |
| 99 | List<ResourceAllocation> allocations = new ArrayList<>(resources.size()); |
| 100 | for (ResourcePath resource: resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 101 | // We access store twice in this method, then the store may be updated by others |
| 102 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 103 | if (consumer.isPresent()) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 104 | allocations.add(new ResourceAllocation(resource, consumer.get())); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | return allocations; |
| 109 | } |
| 110 | |
| 111 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 112 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 113 | checkNotNull(consumer); |
| 114 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 115 | Collection<ResourcePath> resources = store.getResources(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 116 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 117 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 118 | .collect(Collectors.toList()); |
| 119 | } |
| 120 | |
| 121 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 122 | public boolean isAvailable(ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 123 | checkNotNull(resource); |
| 124 | |
| 125 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 126 | return !consumer.isPresent(); |
| 127 | } |
| 128 | |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 129 | @Override |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 130 | public <T> boolean registerResources(ResourcePath parent, List<T> children) { |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 131 | checkNotNull(parent); |
| 132 | checkNotNull(children); |
| 133 | checkArgument(!children.isEmpty()); |
| 134 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 135 | List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame^] | 136 | return store.register(resources); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 137 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public <T> boolean unregisterResources(ResourcePath parent, List<T> children) { |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 141 | checkNotNull(parent); |
| 142 | checkNotNull(children); |
| 143 | checkArgument(!children.isEmpty()); |
| 144 | |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 145 | List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame^] | 146 | return store.unregister(resources); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 147 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 148 | } |