blob: 81af96d0316768a4a7bd4e838362fbd98f39294e [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;
30import org.onosproject.net.resource.ResourceService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080031import org.onosproject.net.resource.ResourceStore;
32import org.onosproject.net.resource.ResourceStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Component;
35import org.osgi.service.component.annotations.Deactivate;
36import org.osgi.service.component.annotations.Reference;
37import org.osgi.service.component.annotations.ReferenceCardinality;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080038import org.slf4j.Logger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070039
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070040import java.util.Collection;
41import java.util.List;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080042import java.util.Set;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070043import java.util.stream.Collectors;
44
45import static com.google.common.base.Preconditions.checkNotNull;
Heedo Kang4a47a302016-02-29 17:40:23 +090046import static org.onosproject.security.AppGuard.checkPermission;
Heedo Kang4a47a302016-02-29 17:40:23 +090047import static org.onosproject.security.AppPermission.Type.RESOURCE_READ;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048import static org.onosproject.security.AppPermission.Type.RESOURCE_WRITE;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080049import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070050
51/**
52 * An implementation of ResourceService.
53 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054@Component(immediate = true, service = {ResourceService.class, ResourceAdminService.class})
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070055@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080056public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
57 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070058
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070060 protected ResourceStore store;
61
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080062 private final Logger log = getLogger(getClass());
63
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080064 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
65
66 @Activate
67 public void activate() {
68 store.setDelegate(delegate);
69 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080070
71 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080072 }
73
74 @Deactivate
75 public void deactivate() {
76 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080077 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080078
79 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080080 }
81
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070082 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070083 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZUef835c92016-08-08 13:51:17 -070084 List<? extends Resource> resources) {
Heedo Kang4a47a302016-02-29 17:40:23 +090085 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070086 checkNotNull(consumer);
87 checkNotNull(resources);
88
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 boolean success = store.allocate(resources, consumer);
90 if (!success) {
91 return ImmutableList.of();
92 }
93
94 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070095 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070096 .collect(Collectors.toList());
97 }
98
99 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700100 public boolean release(List<ResourceAllocation> allocations) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900101 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700102 checkNotNull(allocations);
103
Sho SHIMIZUfc64ffe2016-02-10 20:11:09 -0800104 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700105 }
106
107 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700108 public boolean release(ResourceConsumer consumer) {
109 checkNotNull(consumer);
110
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700111 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700112 return release(ImmutableList.copyOf(allocations));
113 }
114
115 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800116 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900117 checkPermission(RESOURCE_READ);
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800118 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700119
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800120 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700121 }
122
123 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800124 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900125 checkPermission(RESOURCE_READ);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700126 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700127 checkNotNull(cls);
128
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800129 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800130 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800131 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800132 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -0700133 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700134 }
135
136 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700137 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900138 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700139 checkNotNull(consumer);
140
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800141 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700142 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700143 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700144 .collect(Collectors.toList());
145 }
146
147 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800148 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900149 checkPermission(RESOURCE_READ);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700150 checkNotNull(parent);
151
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800152 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700153 return children.stream()
154 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800155 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800156 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700157 }
158
159 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800160 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900161 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800162 checkNotNull(parent);
163 checkNotNull(cls);
164
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700165 return store.getChildResources(parent, cls).stream()
166 // We access store twice in this method, then the store may be updated by others
167 .filter(store::isAvailable)
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800168 .collect(Collectors.toSet());
169 }
170
171 @Override
172 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900173 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800174 checkNotNull(parent);
175 checkNotNull(cls);
176
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700177 return store.getChildResources(parent, cls).stream()
178 // We access store twice in this method, then the store may be updated by others
179 .filter(store::isAvailable)
180 .map(x -> x.valueAs(cls))
181 .flatMap(Tools::stream)
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800182 .collect(Collectors.toSet());
183 }
184
185 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800186 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900187 checkPermission(RESOURCE_READ);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800188 checkNotNull(parent);
189
190 return store.getChildResources(parent);
191 }
192
193 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800194 public boolean isAvailable(Resource resource) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900195 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700196 checkNotNull(resource);
197
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800198 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700199 }
200
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700201 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700202 public boolean register(List<? extends Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800203 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700204
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700205 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700206 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700207
208 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700209 public boolean unregister(List<? extends ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800210 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700211
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800212 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700213 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800214
215 private class InternalStoreDelegate implements ResourceStoreDelegate {
216 @Override
217 public void notify(ResourceEvent event) {
218 post(event);
219 }
220 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700221}