blob: 83ae6e0058f80cb99768ee2c87e7d1991242cbc7 [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;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080037import org.slf4j.Logger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070038
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070039import java.util.Collection;
40import java.util.List;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070041import java.util.stream.Collectors;
42
43import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080044import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070045
46/**
47 * An implementation of ResourceService.
48 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070049@Component(immediate = true)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070050@Service
51@Beta
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080052public final class ResourceManager extends AbstractListenerManager<ResourceEvent, ResourceListener>
53 implements ResourceService, ResourceAdminService {
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070054
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected ResourceStore store;
57
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080058 private final Logger log = getLogger(getClass());
59
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080060 private final ResourceStoreDelegate delegate = new InternalStoreDelegate();
61
62 @Activate
63 public void activate() {
64 store.setDelegate(delegate);
65 eventDispatcher.addSink(ResourceEvent.class, listenerRegistry);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080066
67 log.info("Started");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080068 }
69
70 @Deactivate
71 public void deactivate() {
72 store.unsetDelegate(delegate);
Madan Jampania85c6942015-11-09 14:41:48 -080073 eventDispatcher.removeSink(ResourceEvent.class);
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080074
75 log.info("Stopped");
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080076 }
77
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070078 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070079 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080080 List<Resource> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070081 checkNotNull(consumer);
82 checkNotNull(resources);
83
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070084 boolean success = store.allocate(resources, consumer);
85 if (!success) {
86 return ImmutableList.of();
87 }
88
89 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070090 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070091 .collect(Collectors.toList());
92 }
93
94 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070095 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070096 checkNotNull(allocations);
97
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080098 List<Resource> resources = allocations.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070099 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700100 .collect(Collectors.toList());
101 List<ResourceConsumer> consumers = allocations.stream()
102 .map(ResourceAllocation::consumer)
103 .collect(Collectors.toList());
104
105 return store.release(resources, consumers);
106 }
107
108 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700109 public boolean release(ResourceConsumer consumer) {
110 checkNotNull(consumer);
111
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700112 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700113 return release(ImmutableList.copyOf(allocations));
114 }
115
116 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800117 public List<ResourceAllocation> getResourceAllocation(Resource resource) {
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700118 checkNotNull(resource);
119
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800120 List<ResourceConsumer> consumers = store.getConsumers(resource);
121 return consumers.stream()
122 .map(x -> new ResourceAllocation(resource, x))
123 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZUf853b0e2015-09-29 15:15:32 -0700124 }
125
126 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800127 public <T> Collection<ResourceAllocation> getResourceAllocations(Resource 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()
134 .flatMap(resource -> store.getConsumers(resource).stream()
135 .map(consumer -> new ResourceAllocation(resource, consumer)))
136 .collect(GuavaCollectors.toImmutableList());
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700137 }
138
139 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700140 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700141 checkNotNull(consumer);
142
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800143 Collection<Resource> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700144 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700145 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700146 .collect(Collectors.toList());
147 }
148
149 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800150 public Collection<Resource> getAvailableResources(Resource parent) {
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700151 checkNotNull(parent);
152
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800153 Collection<Resource> children = store.getChildResources(parent);
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700154 return children.stream()
155 // We access store twice in this method, then the store may be updated by others
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800156 .filter(store::isAvailable)
Sho SHIMIZUe7f4f3f2015-10-13 16:27:25 -0700157 .collect(Collectors.toList());
158 }
159
160 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800161 public boolean isAvailable(Resource resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700162 checkNotNull(resource);
163
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800164 return store.isAvailable(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700165 }
166
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700167 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800168 public boolean registerResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800169 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700170
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700171 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700172 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700173
174 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800175 public boolean unregisterResources(List<Resource> resources) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800176 checkNotNull(resources);
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700177
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800178 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700179 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800180
181 private class InternalStoreDelegate implements ResourceStoreDelegate {
182 @Override
183 public void notify(ResourceEvent event) {
184 post(event);
185 }
186 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700187}