blob: 52ea71d9b528cbd3c69f0ab1d598b3728380c5da [file] [log] [blame]
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 SHIMIZUfa62b472015-11-02 17:35:46 -080020import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070021import org.apache.felix.scr.annotations.Component;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080022import org.apache.felix.scr.annotations.Deactivate;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080026import org.onlab.util.GuavaCollectors;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080027import org.onlab.util.Tools;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080028import org.onosproject.event.AbstractListenerManager;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080029import org.onosproject.net.resource.DiscreteResourceId;
30import org.onosproject.net.resource.ResourceAdminService;
31import org.onosproject.net.resource.ResourceAllocation;
32import org.onosproject.net.resource.ResourceConsumer;
33import org.onosproject.net.resource.ResourceEvent;
34import org.onosproject.net.resource.ResourceId;
35import org.onosproject.net.resource.ResourceListener;
36import org.onosproject.net.resource.ResourceService;
37import org.onosproject.net.resource.Resource;
38import org.onosproject.net.resource.ResourceStore;
39import org.onosproject.net.resource.ResourceStoreDelegate;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080040import org.slf4j.Logger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070041
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070042import java.util.Collection;
43import java.util.List;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080044import java.util.Set;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070045import java.util.stream.Collectors;
46
47import static com.google.common.base.Preconditions.checkNotNull;
Heedo Kang4a47a302016-02-29 17:40:23 +090048import static org.onosproject.security.AppGuard.checkPermission;
49import static org.onosproject.security.AppPermission.Type.RESOURCE_WRITE;
50import static org.onosproject.security.AppPermission.Type.RESOURCE_READ;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080051import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070052
53/**
54 * An implementation of ResourceService.
55 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070056@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070057@Service
58@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080059public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
60 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070061
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ResourceStore store;
64
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080065 private final Logger log = getLogger(getClass());
66
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080067 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
68
69 @Activate
70 public void activate() {
71 store.setDelegate(delegate);
72 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080073
74 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080075 }
76
77 @Deactivate
78 public void deactivate() {
79 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080080 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080081
82 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080083 }
84
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070085 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070086 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Jonathan Hart56151262016-02-11 09:48:50 -080087 List<Resource> resources) {
Heedo Kang4a47a302016-02-29 17:40:23 +090088 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 checkNotNull(consumer);
90 checkNotNull(resources);
91
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070092 boolean success = store.allocate(resources, consumer);
93 if (!success) {
94 return ImmutableList.of();
95 }
96
97 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070099 .collect(Collectors.toList());
100 }
101
102 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700103 public boolean release(List<ResourceAllocation> allocations) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900104 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700105 checkNotNull(allocations);
106
Sho SHIMIZUfc64ffe2016-02-10 20:11:09 -0800107 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700108 }
109
110 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700111 public boolean release(ResourceConsumer consumer) {
112 checkNotNull(consumer);
113
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700114 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700115 return release(ImmutableList.copyOf(allocations));
116 }
117
118 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800119 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900120 checkPermission(RESOURCE_READ);
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800121 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700122
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800123 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700124 }
125
126 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800127 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900128 checkPermission(RESOURCE_READ);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700129 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700130 checkNotNull(cls);
131
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800132 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800133 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800134 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800135 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800136 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700137 }
138
139 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700140 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900141 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700142 checkNotNull(consumer);
143
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800144 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700145 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700146 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700147 .collect(Collectors.toList());
148 }
149
150 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800151 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900152 checkPermission(RESOURCE_READ);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700153 checkNotNull(parent);
154
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800155 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700156 return children.stream()
157 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800158 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800159 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700160 }
161
162 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800163 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900164 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800165 checkNotNull(parent);
166 checkNotNull(cls);
167
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700168 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 SHIMIZU7332fe42016-02-15 14:58:33 -0800171 .collect(Collectors.toSet());
172 }
173
174 @Override
175 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900176 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800177 checkNotNull(parent);
178 checkNotNull(cls);
179
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700180 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 SHIMIZU7332fe42016-02-15 14:58:33 -0800185 .collect(Collectors.toSet());
186 }
187
188 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800189 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900190 checkPermission(RESOURCE_READ);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800191 checkNotNull(parent);
192
193 return store.getChildResources(parent);
194 }
195
196 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800197 public boolean isAvailable(Resource resource) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900198 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700199 checkNotNull(resource);
200
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800201 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700202 }
203
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700204 @Override
Jonathan Hart56151262016-02-11 09:48:50 -0800205 public boolean register(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800206 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700207
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700208 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700209 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700210
211 @Override
Jonathan Hart56151262016-02-11 09:48:50 -0800212 public boolean unregister(List<ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800213 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700214
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800215 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700216 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800217
218 private class InternalStoreDelegate implements ResourceStoreDelegate {
219 @Override
220 public void notify(ResourceEvent event) {
221 post(event);
222 }
223 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700224}