blob: 744931ac3b0496e40058b5765ec6ac14abe8221e [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 SHIMIZU84515f32016-02-10 14:42:33 -080083 List<? extends 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 SHIMIZUfc64ffe2016-02-10 20:11:09 -0800101 return store.release(allocations);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700102 }
103
104 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700105 public boolean release(ResourceConsumer consumer) {
106 checkNotNull(consumer);
107
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700108 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700109 return release(ImmutableList.copyOf(allocations));
110 }
111
112 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800113 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
114 checkNotNull(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700115
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800116 return store.getResourceAllocations(id);
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700117 }
118
119 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800120 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700121 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700122 checkNotNull(cls);
123
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800124 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800125 Collection<Resource> resources = store.getAllocatedResources(parent, cls);
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800126 return resources.stream()
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800127 .flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800128 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800142 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700143 checkNotNull(parent);
144
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800145 Set<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 SHIMIZU83258ae2016-01-29 17:39:07 -0800149 .collect(Collectors.toSet());
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700150 }
151
152 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800153 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800154 checkNotNull(parent);
155
156 return store.getChildResources(parent);
157 }
158
159 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800160 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700161 checkNotNull(resource);
162
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800163 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700164 }
165
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700166 @Override
Sho SHIMIZU84515f32016-02-10 14:42:33 -0800167 public boolean register(List<? extends Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800168 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700169
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700170 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700171 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700172
173 @Override
Sho SHIMIZU84515f32016-02-10 14:42:33 -0800174 public boolean unregister(List<? extends ResourceId> ids) {
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800175 checkNotNull(ids);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700176
Sho SHIMIZU72f81b12016-02-09 09:26:17 -0800177 return store.unregister(ids);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700178 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800179
180 private class InternalStoreDelegate implements ResourceStoreDelegate {
181 @Override
182 public void notify(ResourceEvent event) {
183 post(event);
184 }
185 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700186}