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 | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame^] | 94 | public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) { |
| 95 | checkNotNull(resource); |
| 96 | |
| 97 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 98 | return consumer.map(x -> new ResourceAllocation(resource, x)); |
| 99 | } |
| 100 | |
| 101 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 102 | public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) { |
| 103 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 104 | checkNotNull(cls); |
| 105 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 106 | Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls); |
| 107 | List<ResourceAllocation> allocations = new ArrayList<>(resources.size()); |
| 108 | for (ResourcePath resource: resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 109 | // We access store twice in this method, then the store may be updated by others |
| 110 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 111 | if (consumer.isPresent()) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 112 | allocations.add(new ResourceAllocation(resource, consumer.get())); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | return allocations; |
| 117 | } |
| 118 | |
| 119 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 120 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 121 | checkNotNull(consumer); |
| 122 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 123 | Collection<ResourcePath> resources = store.getResources(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 124 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 125 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 126 | .collect(Collectors.toList()); |
| 127 | } |
| 128 | |
| 129 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 130 | public boolean isAvailable(ResourcePath resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 131 | checkNotNull(resource); |
| 132 | |
| 133 | Optional<ResourceConsumer> consumer = store.getConsumer(resource); |
| 134 | return !consumer.isPresent(); |
| 135 | } |
| 136 | |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 137 | @Override |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 138 | public <T> boolean registerResources(ResourcePath parent, List<T> children) { |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 139 | checkNotNull(parent); |
| 140 | checkNotNull(children); |
| 141 | checkArgument(!children.isEmpty()); |
| 142 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 143 | List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 144 | return store.register(resources); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 145 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 146 | |
| 147 | @Override |
| 148 | public <T> boolean unregisterResources(ResourcePath parent, List<T> children) { |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 149 | checkNotNull(parent); |
| 150 | checkNotNull(children); |
| 151 | checkArgument(!children.isEmpty()); |
| 152 | |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 153 | List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 154 | return store.unregister(resources); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 155 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 156 | } |