blob: 77a32f451eb95a3b76627b76d83b08dc730c69ca [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 SHIMIZUba41fc12015-08-12 15:43:22 -070020import com.google.common.collect.Lists;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080021import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070022import org.apache.felix.scr.annotations.Component;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080023import org.apache.felix.scr.annotations.Deactivate;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
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 SHIMIZU1f5e5912015-08-10 17:00:00 -070034import org.onosproject.net.newresource.ResourcePath;
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
38import java.util.ArrayList;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070039import java.util.Collection;
40import java.util.List;
41import java.util.Optional;
42import java.util.stream.Collectors;
43
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -070044import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070045import static com.google.common.base.Preconditions.checkNotNull;
46
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
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080059 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
60
61 @Activate
62 public void activate() {
63 store.setDelegate(delegate);
64 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
65 }
66
67 @Deactivate
68 public void deactivate() {
69 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080070 eventDispatcher.removeSink(ResourceEvent.class);
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080071 }
72
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070073 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070074 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
75 List<ResourcePath> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070076 checkNotNull(consumer);
77 checkNotNull(resources);
78
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070079 boolean success = store.allocate(resources, consumer);
80 if (!success) {
81 return ImmutableList.of();
82 }
83
84 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070085 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070086 .collect(Collectors.toList());
87 }
88
89 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070090 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070091 checkNotNull(allocations);
92
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070093 List<ResourcePath> resources = allocations.stream()
94 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070095 .collect(Collectors.toList());
96 List<ResourceConsumer> consumers = allocations.stream()
97 .map(ResourceAllocation::consumer)
98 .collect(Collectors.toList());
99
100 return store.release(resources, consumers);
101 }
102
103 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700104 public boolean release(ResourceConsumer consumer) {
105 checkNotNull(consumer);
106
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700107 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700108 return release(ImmutableList.copyOf(allocations));
109 }
110
111 @Override
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700112 public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
113 checkNotNull(resource);
114
115 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
116 return consumer.map(x -> new ResourceAllocation(resource, x));
117 }
118
119 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700120 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
121 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700122 checkNotNull(cls);
123
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700124 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
125 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
126 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700127 // We access store twice in this method, then the store may be updated by others
128 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
129 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700130 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700131 }
132 }
133
134 return allocations;
135 }
136
137 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700138 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700139 checkNotNull(consumer);
140
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700141 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700142 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700143 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700144 .collect(Collectors.toList());
145 }
146
147 @Override
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700148 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
149 checkNotNull(parent);
150
151 Collection<ResourcePath> children = store.getChildResources(parent);
152 return children.stream()
153 // We access store twice in this method, then the store may be updated by others
154 .filter(x -> !store.getConsumer(x).isPresent())
155 .collect(Collectors.toList());
156 }
157
158 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700159 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700160 checkNotNull(resource);
161
162 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
163 return !consumer.isPresent();
164 }
165
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700166 @Override
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700167 public <T> boolean registerResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700168 checkNotNull(parent);
169 checkNotNull(children);
170 checkArgument(!children.isEmpty());
171
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700172 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700173 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700174 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700175
176 @Override
177 public <T> boolean unregisterResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700178 checkNotNull(parent);
179 checkNotNull(children);
180 checkArgument(!children.isEmpty());
181
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700182 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700183 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700184 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800185
186 private class InternalStoreDelegate implements ResourceStoreDelegate {
187 @Override
188 public void notify(ResourceEvent event) {
189 post(event);
190 }
191 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700192}