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