blob: 5fc9d558ace2de1a2329d7ed64d2fc7ee02abe53 [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 SHIMIZUdd3750c2016-02-01 11:37:04 -080028import org.onosproject.net.newresource.DiscreteResourceId;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070029import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070030import org.onosproject.net.newresource.ResourceAllocation;
31import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080032import org.onosproject.net.newresource.ResourceEvent;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080033import org.onosproject.net.newresource.ResourceId;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080034import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070035import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080036import org.onosproject.net.newresource.Resource;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070037import org.onosproject.net.newresource.ResourceStore;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080038import org.onosproject.net.newresource.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;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080047import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070048
49/**
50 * An implementation of ResourceService.
51 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070052@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070053@Service
54@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080055public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
56 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070057
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ResourceStore store;
60
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080061 private final Logger log = getLogger(getClass());
62
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080063 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
64
65 @Activate
66 public void activate() {
67 store.setDelegate(delegate);
68 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080069
70 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080071 }
72
73 @Deactivate
74 public void deactivate() {
75 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080076 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080077
78 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080079 }
80
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070081 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070082 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080083 List<Resource> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070084 checkNotNull(consumer);
85 checkNotNull(resources);
86
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070087 boolean success = store.allocate(resources, consumer);
88 if (!success) {
89 return ImmutableList.of();
90 }
91
92 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070093 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070094 .collect(Collectors.toList());
95 }
96
97 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070099 checkNotNull(allocations);
100
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800101 List<Resource> resources = allocations.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700102 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700103 .collect(Collectors.toList());
104 List<ResourceConsumer> consumers = allocations.stream()
105 .map(ResourceAllocation::consumer)
106 .collect(Collectors.toList());
107
108 return store.release(resources, consumers);
109 }
110
111 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700112 public boolean release(ResourceConsumer consumer) {
113 checkNotNull(consumer);
114
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700115 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700116 return release(ImmutableList.copyOf(allocations));
117 }
118
119 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800120 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
121 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) {
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())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800135 .collect(GuavaCollectors.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) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700140 checkNotNull(consumer);
141
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800142 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700143 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700144 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700145 .collect(Collectors.toList());
146 }
147
148 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800149 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700150 checkNotNull(parent);
151
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800152 Set<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700153 return children.stream()
154 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800155 .filter(store::isAvailable)
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800156 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700157 }
158
159 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800160 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800161 checkNotNull(parent);
162
163 return store.getChildResources(parent);
164 }
165
166 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800167 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700168 checkNotNull(resource);
169
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800170 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700171 }
172
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700173 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800174 public boolean registerResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800175 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700176
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700177 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700178 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700179
180 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800181 public boolean unregisterResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800182 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700183
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800184 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700185 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800186
187 private class InternalStoreDelegate implements ResourceStoreDelegate {
188 @Override
189 public void notify(ResourceEvent event) {
190 post(event);
191 }
192 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700193}