blob: 1cdab853ecf51326e67496adf7c8c680b3603fe1 [file] [log] [blame]
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07003 *
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 SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource.impl;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070017
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableList;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080020import org.onlab.util.Tools;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080021import org.onosproject.event.AbstractListenerManager;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080022import org.onosproject.net.resource.DiscreteResourceId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.onosproject.net.resource.Resource;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080024import org.onosproject.net.resource.ResourceAdminService;
25import org.onosproject.net.resource.ResourceAllocation;
26import org.onosproject.net.resource.ResourceConsumer;
27import org.onosproject.net.resource.ResourceEvent;
28import org.onosproject.net.resource.ResourceId;
29import org.onosproject.net.resource.ResourceListener;
Ray Milkeye453edc2019-02-19 13:42:29 -080030import org.onosproject.net.resource.ResourceQueryService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080031import org.onosproject.net.resource.ResourceService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080032import org.onosproject.net.resource.ResourceStore;
33import org.onosproject.net.resource.ResourceStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.osgi.service.component.annotations.Reference;
38import org.osgi.service.component.annotations.ReferenceCardinality;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080039import org.slf4j.Logger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070040
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070041import java.util.Collection;
42import java.util.List;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080043import java.util.Set;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070044import java.util.stream.Collectors;
45
46import static com.google.common.base.Preconditions.checkNotNull;
Heedo Kang4a47a302016-02-29 17:40:23 +090047import static org.onosproject.security.AppGuard.checkPermission;
Heedo Kang4a47a302016-02-29 17:40:23 +090048import static org.onosproject.security.AppPermission.Type.RESOURCE_READ;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049import static org.onosproject.security.AppPermission.Type.RESOURCE_WRITE;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080050import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070051
52/**
53 * An implementation of ResourceService.
54 */
Ray Milkeye453edc2019-02-19 13:42:29 -080055@Component(immediate = true,
56 service = {
57 ResourceService.class,
58 ResourceAdminService.class,
59 ResourceQueryService.class
60 }
61)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070062@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080063public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
64 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070065
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070067 protected ResourceStore store;
68
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080069 private final Logger log = getLogger(getClass());
70
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080071 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
72
73 @Activate
74 public void activate() {
75 store.setDelegate(delegate);
76 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080077
78 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080079 }
80
81 @Deactivate
82 public void deactivate() {
83 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080084 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080085
86 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080087 }
88
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070090 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZUef835c92016-08-08 13:51:17 -070091 List<? extends Resource> resources) {
Heedo Kang4a47a302016-02-29 17:40:23 +090092 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070093 checkNotNull(consumer);
94 checkNotNull(resources);
95
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070096 boolean success = store.allocate(resources, consumer);
97 if (!success) {
98 return ImmutableList.of();
99 }
100
101 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700102 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700103 .collect(Collectors.toList());
104 }
105
106 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700107 public boolean release(List<ResourceAllocation> allocations) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900108 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700109 checkNotNull(allocations);
110
Sho SHIMIZUfc64ffe2016-02-10 20:11:09 -0800111 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700112 }
113
114 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700115 public boolean release(ResourceConsumer consumer) {
116 checkNotNull(consumer);
117
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700118 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700119 return release(ImmutableList.copyOf(allocations));
120 }
121
122 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800123 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900124 checkPermission(RESOURCE_READ);
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800125 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700126
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800127 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700128 }
129
130 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800131 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900132 checkPermission(RESOURCE_READ);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700133 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700134 checkNotNull(cls);
135
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800136 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800137 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800138 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800139 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -0700140 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700141 }
142
143 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700144 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900145 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700146 checkNotNull(consumer);
147
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800148 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700149 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700150 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700151 .collect(Collectors.toList());
152 }
153
154 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800155 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900156 checkPermission(RESOURCE_READ);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700157 checkNotNull(parent);
158
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800159 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700160 return children.stream()
161 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800162 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800163 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700164 }
165
166 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800167 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900168 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800169 checkNotNull(parent);
170 checkNotNull(cls);
171
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700172 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 SHIMIZU7332fe42016-02-15 14:58:33 -0800175 .collect(Collectors.toSet());
176 }
177
178 @Override
179 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900180 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800181 checkNotNull(parent);
182 checkNotNull(cls);
183
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700184 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 SHIMIZU7332fe42016-02-15 14:58:33 -0800189 .collect(Collectors.toSet());
190 }
191
192 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800193 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900194 checkPermission(RESOURCE_READ);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800195 checkNotNull(parent);
196
197 return store.getChildResources(parent);
198 }
199
200 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800201 public boolean isAvailable(Resource resource) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900202 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700203 checkNotNull(resource);
204
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800205 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700206 }
207
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700208 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700209 public boolean register(List<? extends Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800210 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700211
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700212 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700213 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700214
215 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700216 public boolean unregister(List<? extends ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800217 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700218
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800219 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700220 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800221
222 private class InternalStoreDelegate implements ResourceStoreDelegate {
223 @Override
224 public void notify(ResourceEvent event) {
225 post(event);
226 }
227 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700228}