blob: 5226967ffb69d0107edeb7c9c31b12c804ff051f [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 SHIMIZUf853b0e2015-09-29 15:15:32 -070094 public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
95 checkNotNull(resource);
96
97 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
98 return consumer.map(x -> new ResourceAllocation(resource, x));
99 }
100
101 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700102 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
103 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700104 checkNotNull(cls);
105
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700106 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
107 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
108 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700109 // We access store twice in this method, then the store may be updated by others
110 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
111 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700112 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700113 }
114 }
115
116 return allocations;
117 }
118
119 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700120 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700121 checkNotNull(consumer);
122
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700123 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700124 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700125 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700126 .collect(Collectors.toList());
127 }
128
129 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700130 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700131 checkNotNull(resource);
132
133 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
134 return !consumer.isPresent();
135 }
136
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700137 @Override
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700138 public <T> boolean registerResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700139 checkNotNull(parent);
140 checkNotNull(children);
141 checkArgument(!children.isEmpty());
142
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700143 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700144 return store.register(resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700145 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700146
147 @Override
148 public <T> boolean unregisterResources(ResourcePath parent, List<T> children) {
Sho SHIMIZU7b1ef132015-08-19 18:18:13 -0700149 checkNotNull(parent);
150 checkNotNull(children);
151 checkArgument(!children.isEmpty());
152
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700153 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
Sho SHIMIZU83e17a02015-08-20 14:07:05 -0700154 return store.unregister(resources);
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700155 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700156}