blob: d2fe4a7361ac84ffff34d57d309bfa64523bf951 [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 SHIMIZU78ee25c2015-07-16 15:54:14 -070021import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070025import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070026import org.onosproject.net.newresource.ResourceAllocation;
27import org.onosproject.net.newresource.ResourceConsumer;
28import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070029import org.onosproject.net.newresource.ResourcePath;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070030import org.onosproject.net.newresource.ResourceStore;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Collection;
35import java.util.List;
36import java.util.Optional;
37import java.util.stream.Collectors;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40
41/**
42 * An implementation of ResourceService.
43 */
44@Component(immediate = true, enabled = false)
45@Service
46@Beta
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070047public final class ResourceManager implements ResourceService, ResourceAdminService {
48
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected ResourceStore store;
51
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070052 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070053 public Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070054 checkNotNull(consumer);
55 checkNotNull(resource);
56
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070057 List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070058 if (allocations.isEmpty()) {
59 return Optional.empty();
60 }
61
62 assert allocations.size() == 1;
63
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070064 ResourceAllocation allocation = allocations.get(0);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070065
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070066 assert allocation.resource().equals(resource);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070067
68 // cast is ensured by the assertions above
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070069 return Optional.of(allocation);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070070 }
71
72 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070073 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
74 List<ResourcePath> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070075 checkNotNull(consumer);
76 checkNotNull(resources);
77
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070078 // TODO: implement support of resource hierarchy
79 // allocation for a particular resource implies allocations for all of the sub-resources need to be done
80
81 boolean success = store.allocate(resources, consumer);
82 if (!success) {
83 return ImmutableList.of();
84 }
85
86 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070087 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070088 .collect(Collectors.toList());
89 }
90
91 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070092 public List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070093 checkNotNull(consumer);
94 checkNotNull(resources);
95
96 return allocate(consumer, Arrays.asList(resources));
97 }
98
99 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700100 public boolean release(ResourceAllocation allocation) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700101 checkNotNull(allocation);
102
103 return release(ImmutableList.of(allocation));
104 }
105
106 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700107 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700108 checkNotNull(allocations);
109
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700110 List<ResourcePath> resources = allocations.stream()
111 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700112 .collect(Collectors.toList());
113 List<ResourceConsumer> consumers = allocations.stream()
114 .map(ResourceAllocation::consumer)
115 .collect(Collectors.toList());
116
117 return store.release(resources, consumers);
118 }
119
120 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700121 public boolean release(ResourceAllocation... allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700122 checkNotNull(allocations);
123
124 return release(ImmutableList.copyOf(allocations));
125 }
126
127 @Override
128 public boolean release(ResourceConsumer consumer) {
129 checkNotNull(consumer);
130
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700131 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700132 return release(ImmutableList.copyOf(allocations));
133 }
134
135 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700136 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
137 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700138 checkNotNull(cls);
139
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700140 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
141 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
142 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700143 // We access store twice in this method, then the store may be updated by others
144 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
145 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700146 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700147 }
148 }
149
150 return allocations;
151 }
152
153 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700154 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700155 checkNotNull(consumer);
156
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700157 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700158 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700159 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700160 .collect(Collectors.toList());
161 }
162
163 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700164 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700165 checkNotNull(resource);
166
167 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
168 return !consumer.isPresent();
169 }
170
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700171 @Override
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700172 public <T> boolean registerResources(ResourcePath parent, List<T> children) {
173 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
174 return store.register(parent, resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700175 }
176}