blob: 7857e40317f60db97064f9aaeb9910592816e3ad [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 SHIMIZU7332fe42016-02-15 14:58:33 -080026import org.onlab.util.Tools;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080027import org.onosproject.event.AbstractListenerManager;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080028import org.onosproject.net.resource.DiscreteResourceId;
29import org.onosproject.net.resource.ResourceAdminService;
30import org.onosproject.net.resource.ResourceAllocation;
31import org.onosproject.net.resource.ResourceConsumer;
32import org.onosproject.net.resource.ResourceEvent;
33import org.onosproject.net.resource.ResourceId;
34import org.onosproject.net.resource.ResourceListener;
35import org.onosproject.net.resource.ResourceService;
36import org.onosproject.net.resource.Resource;
37import org.onosproject.net.resource.ResourceStore;
38import org.onosproject.net.resource.ResourceStoreDelegate;
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;
48import static org.onosproject.security.AppPermission.Type.RESOURCE_WRITE;
49import static org.onosproject.security.AppPermission.Type.RESOURCE_READ;
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 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070055@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070056@Service
57@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080058public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
59 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070060
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected ResourceStore store;
63
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080064 private final Logger log = getLogger(getClass());
65
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080066 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
67
68 @Activate
69 public void activate() {
70 store.setDelegate(delegate);
71 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080072
73 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080074 }
75
76 @Deactivate
77 public void deactivate() {
78 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080079 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080080
81 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080082 }
83
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070084 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070085 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZUef835c92016-08-08 13:51:17 -070086 List<? extends Resource> resources) {
Heedo Kang4a47a302016-02-29 17:40:23 +090087 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070088 checkNotNull(consumer);
89 checkNotNull(resources);
90
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070091 boolean success = store.allocate(resources, consumer);
92 if (!success) {
93 return ImmutableList.of();
94 }
95
96 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070097 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070098 .collect(Collectors.toList());
99 }
100
101 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700102 public boolean release(List<ResourceAllocation> allocations) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900103 checkPermission(RESOURCE_WRITE);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700104 checkNotNull(allocations);
105
Sho SHIMIZUfc64ffe2016-02-10 20:11:09 -0800106 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700107 }
108
109 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700110 public boolean release(ResourceConsumer consumer) {
111 checkNotNull(consumer);
112
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700113 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700114 return release(ImmutableList.copyOf(allocations));
115 }
116
117 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800118 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900119 checkPermission(RESOURCE_READ);
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800120 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700121
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800122 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700123 }
124
125 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800126 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900127 checkPermission(RESOURCE_READ);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700128 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700129 checkNotNull(cls);
130
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800131 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800132 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800133 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800134 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -0700135 .collect(ImmutableList.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700136 }
137
138 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700139 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900140 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700141 checkNotNull(consumer);
142
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800143 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700144 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700145 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700146 .collect(Collectors.toList());
147 }
148
149 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800150 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900151 checkPermission(RESOURCE_READ);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700152 checkNotNull(parent);
153
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800154 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700155 return children.stream()
156 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800157 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800158 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700159 }
160
161 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800162 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900163 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800164 checkNotNull(parent);
165 checkNotNull(cls);
166
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700167 return store.getChildResources(parent, cls).stream()
168 // We access store twice in this method, then the store may be updated by others
169 .filter(store::isAvailable)
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800170 .collect(Collectors.toSet());
171 }
172
173 @Override
174 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900175 checkPermission(RESOURCE_READ);
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800176 checkNotNull(parent);
177 checkNotNull(cls);
178
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700179 return store.getChildResources(parent, cls).stream()
180 // We access store twice in this method, then the store may be updated by others
181 .filter(store::isAvailable)
182 .map(x -> x.valueAs(cls))
183 .flatMap(Tools::stream)
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800184 .collect(Collectors.toSet());
185 }
186
187 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800188 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900189 checkPermission(RESOURCE_READ);
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800190 checkNotNull(parent);
191
192 return store.getChildResources(parent);
193 }
194
195 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800196 public boolean isAvailable(Resource resource) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900197 checkPermission(RESOURCE_READ);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700198 checkNotNull(resource);
199
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800200 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700201 }
202
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700203 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700204 public boolean register(List<? extends Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800205 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700206
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700207 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700208 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700209
210 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700211 public boolean unregister(List<? extends ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800212 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700213
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800214 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700215 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800216
217 private class InternalStoreDelegate implements ResourceStoreDelegate {
218 @Override
219 public void notify(ResourceEvent event) {
220 post(event);
221 }
222 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700223}