blob: b8c31a47b2004cffefd86acc84d7d4a7324f3436 [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;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070033import java.util.Collection;
34import java.util.List;
35import java.util.Optional;
36import java.util.stream.Collectors;
37
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -070038import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070039import 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 List<ResourceAllocation> allocate(ResourceConsumer consumer,
54 List<ResourcePath> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070055 checkNotNull(consumer);
56 checkNotNull(resources);
57
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070058 // TODO: implement support of resource hierarchy
59 // allocation for a particular resource implies allocations for all of the sub-resources need to be done
60
61 boolean success = store.allocate(resources, consumer);
62 if (!success) {
63 return ImmutableList.of();
64 }
65
66 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070067 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070068 .collect(Collectors.toList());
69 }
70
71 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070072 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070073 checkNotNull(allocations);
74
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070075 List<ResourcePath> resources = allocations.stream()
76 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070077 .collect(Collectors.toList());
78 List<ResourceConsumer> consumers = allocations.stream()
79 .map(ResourceAllocation::consumer)
80 .collect(Collectors.toList());
81
82 return store.release(resources, consumers);
83 }
84
85 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070086 public boolean release(ResourceConsumer consumer) {
87 checkNotNull(consumer);
88
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070089 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070090 return release(ImmutableList.copyOf(allocations));
91 }
92
93 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070094 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
95 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070096 checkNotNull(cls);
97
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
99 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
100 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700101 // We access store twice in this method, then the store may be updated by others
102 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
103 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700104 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700105 }
106 }
107
108 return allocations;
109 }
110
111 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700112 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700113 checkNotNull(consumer);
114
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700115 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700116 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700117 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700118 .collect(Collectors.toList());
119 }
120
121 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700122 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700123 checkNotNull(resource);
124
125 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
126 return !consumer.isPresent();
127 }
128
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700129 @Override
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700130 public <T> boolean registerResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700131 checkNotNull(parent);
132 checkNotNull(children);
133 checkArgument(!children.isEmpty());
134
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700135 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
136 return store.register(parent, resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700137 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700138
139 @Override
140 public <T> boolean unregisterResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700141 checkNotNull(parent);
142 checkNotNull(children);
143 checkArgument(!children.isEmpty());
144
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700145 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
146 return store.unregister(parent, resources);
147 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700148}