blob: abce072a3d1e731f91b31cba54a7bfc485d80dc7 [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
38import static com.google.common.base.Preconditions.checkNotNull;
39
40/**
41 * An implementation of ResourceService.
42 */
43@Component(immediate = true, enabled = false)
44@Service
45@Beta
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -070046public final class ResourceManager implements ResourceService, ResourceAdminService {
47
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected ResourceStore store;
50
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070051 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070052 public List<ResourceAllocation> allocate(ResourceConsumer consumer,
53 List<ResourcePath> resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070054 checkNotNull(consumer);
55 checkNotNull(resources);
56
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070057 // TODO: implement support of resource hierarchy
58 // allocation for a particular resource implies allocations for all of the sub-resources need to be done
59
60 boolean success = store.allocate(resources, consumer);
61 if (!success) {
62 return ImmutableList.of();
63 }
64
65 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070066 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070067 .collect(Collectors.toList());
68 }
69
70 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070071 public boolean release(List<ResourceAllocation> allocations) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070072 checkNotNull(allocations);
73
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070074 List<ResourcePath> resources = allocations.stream()
75 .map(ResourceAllocation::resource)
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070076 .collect(Collectors.toList());
77 List<ResourceConsumer> consumers = allocations.stream()
78 .map(ResourceAllocation::consumer)
79 .collect(Collectors.toList());
80
81 return store.release(resources, consumers);
82 }
83
84 @Override
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070085 public boolean release(ResourceConsumer consumer) {
86 checkNotNull(consumer);
87
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070088 Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070089 return release(ImmutableList.copyOf(allocations));
90 }
91
92 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070093 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
94 checkNotNull(parent);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -070095 checkNotNull(cls);
96
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070097 Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
98 List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
99 for (ResourcePath resource: resources) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700100 // We access store twice in this method, then the store may be updated by others
101 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
102 if (consumer.isPresent()) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700103 allocations.add(new ResourceAllocation(resource, consumer.get()));
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700104 }
105 }
106
107 return allocations;
108 }
109
110 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700111 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700112 checkNotNull(consumer);
113
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700114 Collection<ResourcePath> resources = store.getResources(consumer);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700115 return resources.stream()
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700116 .map(x -> new ResourceAllocation(x, consumer))
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700117 .collect(Collectors.toList());
118 }
119
120 @Override
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700121 public boolean isAvailable(ResourcePath resource) {
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700122 checkNotNull(resource);
123
124 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
125 return !consumer.isPresent();
126 }
127
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -0700128 @Override
Sho SHIMIZUba41fc12015-08-12 15:43:22 -0700129 public <T> boolean registerResources(ResourcePath parent, List<T> children) {
130 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
131 return store.register(parent, resources);
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700132 }
Sho SHIMIZU2d8a13a2015-08-18 22:37:41 -0700133
134 @Override
135 public <T> boolean unregisterResources(ResourcePath parent, List<T> children) {
136 List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
137 return store.unregister(parent, resources);
138 }
Sho SHIMIZU78ee25c2015-07-16 15:54:14 -0700139}