blob: a5aae0f3fbcb9177ae7e4aa9754e82957a576832 [file] [log] [blame]
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 SHIMIZUfa62b472015-11-02 17:35:46 -080026import org.onosproject.event.AbstractListenerManager;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070027import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070028import org.onosproject.net.newresource.ResourceAllocation;
29import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080030import org.onosproject.net.newresource.ResourceEvent;
31import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070032import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070033import org.onosproject.net.newresource.ResourcePath;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070034import org.onosproject.net.newresource.ResourceStore;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080035import org.onosproject.net.newresource.ResourceStoreDelegate;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070036
37import java.util.ArrayList;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070038import java.util.Collection;
39import java.util.List;
40import java.util.Optional;
41import java.util.stream.Collectors;
42
43import static com.google.common.base.Preconditions.checkNotNull;
44
45/**
46 * An implementation of ResourceService.
47 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070048@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070049@Service
50@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080051public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
52 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070053
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected ResourceStore store;
56
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080057 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
58
59 @Activate
60 public void activate() {
61 store.setDelegate(delegate);
62 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
63 }
64
65 @Deactivate
66 public void deactivate() {
67 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080068 eventDispatcher.removeSink(ResourceEvent.class);
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080069 }
70
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070071 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070072 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
73 List<ResourcePath> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070074 checkNotNull(consumer);
75 checkNotNull(resources);
76
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070077 boolean success = store.allocate(resources, consumer);
78 if (!success) {
79 return ImmutableList.of();
80 }
81
82 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070083 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070084 .collect(Collectors.toList());
85 }
86
87 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070088 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 checkNotNull(allocations);
90
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070091 List<ResourcePath> resources = allocations.stream()
92 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070093 .collect(Collectors.toList());
94 List<ResourceConsumer> consumers = allocations.stream()
95 .map(ResourceAllocation::consumer)
96 .collect(Collectors.toList());
97
98 return store.release(resources, consumers);
99 }
100
101 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700102 public boolean release(ResourceConsumer consumer) {
103 checkNotNull(consumer);
104
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700105 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700106 return release(ImmutableList.copyOf(allocations));
107 }
108
109 @Override
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700110 public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
111 checkNotNull(resource);
112
113 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
114 return consumer.map(x -> new ResourceAllocation(resource, x));
115 }
116
117 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700118 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
119 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700120 checkNotNull(cls);
121
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700122 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
123 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
124 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700125 // We access store twice in this method, then the store may be updated by others
126 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
127 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700128 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700129 }
130 }
131
132 return allocations;
133 }
134
135 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700136 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700137 checkNotNull(consumer);
138
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700139 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700140 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700141 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700142 .collect(Collectors.toList());
143 }
144
145 @Override
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700146 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
147 checkNotNull(parent);
148
149 Collection<ResourcePath> children = store.getChildResources(parent);
150 return children.stream()
151 // We access store twice in this method, then the store may be updated by others
152 .filter(x -> !store.getConsumer(x).isPresent())
153 .collect(Collectors.toList());
154 }
155
156 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700157 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700158 checkNotNull(resource);
159
160 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
161 return !consumer.isPresent();
162 }
163
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700164 @Override
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800165 public boolean registerResources(List<ResourcePath> resources) {
166 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700167
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700168 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700169 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700170
171 @Override
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800172 public boolean unregisterResources(List<ResourcePath> resources) {
173 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700174
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800175 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700176 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800177
178 private class InternalStoreDelegate implements ResourceStoreDelegate {
179 @Override
180 public void notify(ResourceEvent event) {
181 post(event);
182 }
183 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700184}