blob: 88c3ab6ec26d06e150d1a3227eeec500645134f5 [file] [log] [blame]
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07001/*
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -08002 * Copyright 2015-2016 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 */
16package org.onosproject.net.newresource.impl;
17
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 SHIMIZUdd3750c2016-02-01 11:37:04 -080029import org.onosproject.net.newresource.DiscreteResourceId;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070030import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070031import org.onosproject.net.newresource.ResourceAllocation;
32import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080033import org.onosproject.net.newresource.ResourceEvent;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080034import org.onosproject.net.newresource.ResourceId;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080035import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070036import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080037import org.onosproject.net.newresource.Resource;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070038import org.onosproject.net.newresource.ResourceStore;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080039import org.onosproject.net.newresource.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;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080048import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070049
50/**
51 * An implementation of ResourceService.
52 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070053@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070054@Service
55@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
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 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,
Jonathan Hart56151262016-02-11 09:48:50 -080084 List<Resource> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070085 checkNotNull(consumer);
86 checkNotNull(resources);
87
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070088 boolean success = store.allocate(resources, consumer);
89 if (!success) {
90 return ImmutableList.of();
91 }
92
93 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070094 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070095 .collect(Collectors.toList());
96 }
97
98 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070099 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700100 checkNotNull(allocations);
101
Sho SHIMIZUfc64ffe2016-02-10 20:11:09 -0800102 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700103 }
104
105 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700106 public boolean release(ResourceConsumer consumer) {
107 checkNotNull(consumer);
108
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700109 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700110 return release(ImmutableList.copyOf(allocations));
111 }
112
113 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800114 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
115 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700116
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800117 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700118 }
119
120 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800121 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700122 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700123 checkNotNull(cls);
124
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800125 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800126 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800127 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800128 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800129 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700130 }
131
132 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700133 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700134 checkNotNull(consumer);
135
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800136 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700137 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700138 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700139 .collect(Collectors.toList());
140 }
141
142 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800143 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700144 checkNotNull(parent);
145
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800146 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700147 return children.stream()
148 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800149 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800150 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700151 }
152
153 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800154 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
155 checkNotNull(parent);
156 checkNotNull(cls);
157
158 // naive implementation
159 return getAvailableResources(parent).stream()
160 .filter(resource -> resource.isTypeOf(cls))
161 .collect(Collectors.toSet());
162 }
163
164 @Override
165 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
166 checkNotNull(parent);
167 checkNotNull(cls);
168
169 // naive implementation
170 return getAvailableResources(parent).stream()
171 .flatMap(resource -> Tools.stream(resource.valueAs(cls)))
172 .collect(Collectors.toSet());
173 }
174
175 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800176 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800177 checkNotNull(parent);
178
179 return store.getChildResources(parent);
180 }
181
182 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800183 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700184 checkNotNull(resource);
185
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800186 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700187 }
188
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700189 @Override
Jonathan Hart56151262016-02-11 09:48:50 -0800190 public boolean register(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800191 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700192
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700193 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700194 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700195
196 @Override
Jonathan Hart56151262016-02-11 09:48:50 -0800197 public boolean unregister(List<ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800198 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700199
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800200 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700201 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800202
203 private class InternalStoreDelegate implements ResourceStoreDelegate {
204 @Override
205 public void notify(ResourceEvent event) {
206 post(event);
207 }
208 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700209}