blob: 517ee1030bb95b45f4003ff8aecf4e77544bf79d [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 SHIMIZUfa62b472015-11-02 17:35:46 -080027import org.onosproject.event.AbstractListenerManager;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070028import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070029import org.onosproject.net.newresource.ResourceAllocation;
30import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080031import org.onosproject.net.newresource.ResourceEvent;
32import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070033import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080034import org.onosproject.net.newresource.Resource;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070035import org.onosproject.net.newresource.ResourceStore;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080036import org.onosproject.net.newresource.ResourceStoreDelegate;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080037import org.slf4j.Logger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070038
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070039import java.util.Collection;
40import java.util.List;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080041import java.util.Set;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070042import java.util.stream.Collectors;
43
44import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080045import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070046
47/**
48 * An implementation of ResourceService.
49 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070050@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070051@Service
52@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080053public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
54 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070055
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected ResourceStore store;
58
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080059 private final Logger log = getLogger(getClass());
60
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080061 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
62
63 @Activate
64 public void activate() {
65 store.setDelegate(delegate);
66 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080067
68 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080069 }
70
71 @Deactivate
72 public void deactivate() {
73 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080074 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080075
76 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080077 }
78
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070079 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070080 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080081 List<Resource> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070082 checkNotNull(consumer);
83 checkNotNull(resources);
84
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070085 boolean success = store.allocate(resources, consumer);
86 if (!success) {
87 return ImmutableList.of();
88 }
89
90 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070091 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070092 .collect(Collectors.toList());
93 }
94
95 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070096 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070097 checkNotNull(allocations);
98
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080099 List<Resource> resources = allocations.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700100 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700101 .collect(Collectors.toList());
102 List<ResourceConsumer> consumers = allocations.stream()
103 .map(ResourceAllocation::consumer)
104 .collect(Collectors.toList());
105
106 return store.release(resources, consumers);
107 }
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 SHIMIZU4a1e59f2016-01-26 15:27:13 -0800118 public List<ResourceAllocation> getResourceAllocations(Resource resource) {
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700119 checkNotNull(resource);
120
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800121 List<ResourceConsumer> consumers = store.getConsumers(resource);
122 return consumers.stream()
123 .map(x -> new ResourceAllocation(resource, x))
124 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700125 }
126
127 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800128 public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) {
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()
135 .flatMap(resource -> store.getConsumers(resource).stream()
136 .map(consumer -> new ResourceAllocation(resource, consumer)))
137 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700138 }
139
140 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700141 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
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 SHIMIZU83258ae2016-01-29 17:39:07 -0800151 public Set<Resource> getAvailableResources(Resource parent) {
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 SHIMIZU83258ae2016-01-29 17:39:07 -0800162 public Set<Resource> getRegisteredResources(Resource parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800163 checkNotNull(parent);
164
165 return store.getChildResources(parent);
166 }
167
168 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800169 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700170 checkNotNull(resource);
171
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800172 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700173 }
174
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700175 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800176 public boolean registerResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800177 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700178
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700179 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700180 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700181
182 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800183 public boolean unregisterResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800184 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700185
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800186 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700187 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800188
189 private class InternalStoreDelegate implements ResourceStoreDelegate {
190 @Override
191 public void notify(ResourceEvent event) {
192 post(event);
193 }
194 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700195}