blob: 96466d59bae84dcaf632f26d3535473ec4db2a8c [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;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070037
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070038import java.util.Collection;
39import java.util.List;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070040import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43
44/**
45 * An implementation of ResourceService.
46 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070047@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070048@Service
49@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080050public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
51 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070052
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected ResourceStore store;
55
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080056 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
57
58 @Activate
59 public void activate() {
60 store.setDelegate(delegate);
61 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
62 }
63
64 @Deactivate
65 public void deactivate() {
66 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080067 eventDispatcher.removeSink(ResourceEvent.class);
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080068 }
69
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070070 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070071 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080072 List<Resource> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070073 checkNotNull(consumer);
74 checkNotNull(resources);
75
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070076 boolean success = store.allocate(resources, consumer);
77 if (!success) {
78 return ImmutableList.of();
79 }
80
81 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070082 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070083 .collect(Collectors.toList());
84 }
85
86 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070087 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070088 checkNotNull(allocations);
89
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080090 List<Resource> resources = allocations.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070091 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070092 .collect(Collectors.toList());
93 List<ResourceConsumer> consumers = allocations.stream()
94 .map(ResourceAllocation::consumer)
95 .collect(Collectors.toList());
96
97 return store.release(resources, consumers);
98 }
99
100 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700101 public boolean release(ResourceConsumer consumer) {
102 checkNotNull(consumer);
103
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700104 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700105 return release(ImmutableList.copyOf(allocations));
106 }
107
108 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800109 public List<ResourceAllocation> getResourceAllocation(Resource resource) {
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700110 checkNotNull(resource);
111
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800112 List<ResourceConsumer> consumers = store.getConsumers(resource);
113 return consumers.stream()
114 .map(x -> new ResourceAllocation(resource, x))
115 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700116 }
117
118 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800119 public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700120 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700121 checkNotNull(cls);
122
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800123 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800124 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800125 return resources.stream()
126 .flatMap(resource -> store.getConsumers(resource).stream()
127 .map(consumer -> new ResourceAllocation(resource, consumer)))
128 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700129 }
130
131 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700132 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700133 checkNotNull(consumer);
134
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800135 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700136 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700137 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700138 .collect(Collectors.toList());
139 }
140
141 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800142 public Collection<Resource> getAvailableResources(Resource parent) {
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700143 checkNotNull(parent);
144
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800145 Collection<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700146 return children.stream()
147 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800148 .filter(store::isAvailable)
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700149 .collect(Collectors.toList());
150 }
151
152 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800153 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700154 checkNotNull(resource);
155
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800156 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700157 }
158
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700159 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800160 public boolean registerResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800161 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700162
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700163 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700164 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700165
166 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800167 public boolean unregisterResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800168 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700169
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800170 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700171 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800172
173 private class InternalStoreDelegate implements ResourceStoreDelegate {
174 @Override
175 public void notify(ResourceEvent event) {
176 post(event);
177 }
178 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700179}