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