Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 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 | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Component; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Deactivate; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 26 | import org.onlab.util.GuavaCollectors; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 27 | import org.onosproject.event.AbstractListenerManager; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 28 | import org.onosproject.net.newresource.DiscreteResourceId; |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 29 | import org.onosproject.net.newresource.ResourceAdminService; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 30 | import org.onosproject.net.newresource.ResourceAllocation; |
| 31 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 32 | import org.onosproject.net.newresource.ResourceEvent; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 33 | import org.onosproject.net.newresource.ResourceId; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 34 | import org.onosproject.net.newresource.ResourceListener; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 35 | import org.onosproject.net.newresource.ResourceService; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 36 | import org.onosproject.net.newresource.Resource; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 37 | import org.onosproject.net.newresource.ResourceStore; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 38 | import org.onosproject.net.newresource.ResourceStoreDelegate; |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 39 | import org.slf4j.Logger; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 40 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 41 | import java.util.Collection; |
| 42 | import java.util.List; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 43 | import java.util.Set; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 44 | import java.util.stream.Collectors; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * An implementation of ResourceService. |
| 51 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 52 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 53 | @Service |
| 54 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 55 | public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener> |
| 56 | implements ResourceService, ResourceAdminService { |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 57 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected ResourceStore store; |
| 60 | |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 63 | private final ResourceStoreDelegate delegate = new InternalStoreDelegate(); |
| 64 | |
| 65 | @Activate |
| 66 | public void activate() { |
| 67 | store.setDelegate(delegate); |
| 68 | eventDispatcher.addSink(ResourceEvent.class, listenerRegistry); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 69 | |
| 70 | log.info("Started"); |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Deactivate |
| 74 | public void deactivate() { |
| 75 | store.unsetDelegate(delegate); |
Madan Jampani | a85c694 | 2015-11-09 14:41:48 -0800 | [diff] [blame] | 76 | eventDispatcher.removeSink(ResourceEvent.class); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 77 | |
| 78 | log.info("Stopped"); |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 81 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 82 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 83 | List<Resource> resources) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 84 | checkNotNull(consumer); |
| 85 | checkNotNull(resources); |
| 86 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 87 | boolean success = store.allocate(resources, consumer); |
| 88 | if (!success) { |
| 89 | return ImmutableList.of(); |
| 90 | } |
| 91 | |
| 92 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 93 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 94 | .collect(Collectors.toList()); |
| 95 | } |
| 96 | |
| 97 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 98 | public boolean release(List<ResourceAllocation> allocations) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 99 | checkNotNull(allocations); |
| 100 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 101 | List<Resource> resources = allocations.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 102 | .map(ResourceAllocation::resource) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 103 | .collect(Collectors.toList()); |
| 104 | List<ResourceConsumer> consumers = allocations.stream() |
| 105 | .map(ResourceAllocation::consumer) |
| 106 | .collect(Collectors.toList()); |
| 107 | |
| 108 | return store.release(resources, consumers); |
| 109 | } |
| 110 | |
| 111 | @Override |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 112 | public boolean release(ResourceConsumer consumer) { |
| 113 | checkNotNull(consumer); |
| 114 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 115 | Collection<ResourceAllocation> allocations = getResourceAllocations(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 116 | return release(ImmutableList.copyOf(allocations)); |
| 117 | } |
| 118 | |
| 119 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 120 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 121 | checkNotNull(id); |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 122 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 123 | return store.getResourceAllocations(id); |
Sho SHIMIZU | f853b0e | 2015-09-29 15:15:32 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 127 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 128 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 129 | checkNotNull(cls); |
| 130 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 131 | // We access store twice in this method, then the store may be updated by others |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 132 | Collection<Resource> resources = store.getAllocatedResources(parent, cls); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 133 | return resources.stream() |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 134 | .flatMap(resource -> store.getResourceAllocations(resource.id()).stream()) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 135 | .collect(GuavaCollectors.toImmutableList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 139 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 140 | checkNotNull(consumer); |
| 141 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 142 | Collection<Resource> resources = store.getResources(consumer); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 143 | return resources.stream() |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 144 | .map(x -> new ResourceAllocation(x, consumer)) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 145 | .collect(Collectors.toList()); |
| 146 | } |
| 147 | |
| 148 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 149 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 150 | checkNotNull(parent); |
| 151 | |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 152 | Set<Resource> children = store.getChildResources(parent); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 153 | return children.stream() |
| 154 | // We access store twice in this method, then the store may be updated by others |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 155 | .filter(store::isAvailable) |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 156 | .collect(Collectors.toSet()); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 160 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 161 | checkNotNull(parent); |
| 162 | |
| 163 | return store.getChildResources(parent); |
| 164 | } |
| 165 | |
| 166 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 167 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 168 | checkNotNull(resource); |
| 169 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 170 | return store.isAvailable(resource); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Sho SHIMIZU | 70ee1ee | 2015-08-06 11:11:52 -0700 | [diff] [blame] | 173 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 174 | public boolean registerResources(List<Resource> resources) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 175 | checkNotNull(resources); |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 176 | |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 177 | return store.register(resources); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 178 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 179 | |
| 180 | @Override |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame^] | 181 | public boolean unregisterResources(List<ResourceId> ids) { |
| 182 | checkNotNull(ids); |
Sho SHIMIZU | 7b1ef13 | 2015-08-19 18:18:13 -0700 | [diff] [blame] | 183 | |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame^] | 184 | return store.unregister(ids); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 185 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 186 | |
| 187 | private class InternalStoreDelegate implements ResourceStoreDelegate { |
| 188 | @Override |
| 189 | public void notify(ResourceEvent event) { |
| 190 | post(event); |
| 191 | } |
| 192 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 193 | } |