blob: 3ebbdc02d0c6c8e3f8ec8b73ed8bc866b35cd626 [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;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24import org.onosproject.net.newresource.DefaultResource;
25import org.onosproject.net.newresource.DefaultResourceAllocation;
26import org.onosproject.net.newresource.Resource;
27import org.onosproject.net.newresource.ResourceAllocation;
28import org.onosproject.net.newresource.ResourceConsumer;
29import org.onosproject.net.newresource.ResourceService;
30import 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
47public final class ResourceManager implements ResourceService {
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected ResourceStore store;
51
52 @SuppressWarnings("unchecked")
53 @Override
54 public <S, T> Optional<ResourceAllocation<S, T>> allocate(ResourceConsumer consumer, Resource<S, T> resource) {
55 checkNotNull(consumer);
56 checkNotNull(resource);
57
58 List<ResourceAllocation<?, ?>> allocations = allocate(consumer, ImmutableList.of(resource));
59 if (allocations.isEmpty()) {
60 return Optional.empty();
61 }
62
63 assert allocations.size() == 1;
64
65 ResourceAllocation<?, ?> allocation = allocations.get(0);
66
67 assert allocation.subject().getClass() == resource.subject().getClass();
68 assert allocation.resource().getClass() == resource.resource().getClass();
69
70 // cast is ensured by the assertions above
71 return Optional.of((ResourceAllocation<S, T>) allocation);
72 }
73
74 @Override
75 public List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer,
76 List<? extends Resource<?, ?>> resources) {
77 checkNotNull(consumer);
78 checkNotNull(resources);
79
80 if (resources.stream().anyMatch(x -> !isValid(x))) {
81 return ImmutableList.of();
82 }
83
84 // TODO: implement support of resource hierarchy
85 // allocation for a particular resource implies allocations for all of the sub-resources need to be done
86
87 boolean success = store.allocate(resources, consumer);
88 if (!success) {
89 return ImmutableList.of();
90 }
91
92 return resources.stream()
93 .map(x -> new DefaultResourceAllocation<>(x.subject(), x.resource(), consumer))
94 .collect(Collectors.toList());
95 }
96
97 @Override
98 public List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, Resource<?, ?>... resources) {
99 checkNotNull(consumer);
100 checkNotNull(resources);
101
102 return allocate(consumer, Arrays.asList(resources));
103 }
104
105 @Override
106 public <S, T> boolean release(ResourceAllocation<S, T> allocation) {
107 checkNotNull(allocation);
108
109 return release(ImmutableList.of(allocation));
110 }
111
112 @Override
113 public boolean release(List<? extends ResourceAllocation<?, ?>> allocations) {
114 checkNotNull(allocations);
115
116 List<DefaultResource<?, ?>> resources = allocations.stream()
117 .map(x -> new DefaultResource<>(x.subject(), x.resource()))
118 .collect(Collectors.toList());
119 List<ResourceConsumer> consumers = allocations.stream()
120 .map(ResourceAllocation::consumer)
121 .collect(Collectors.toList());
122
123 return store.release(resources, consumers);
124 }
125
126 @Override
127 public boolean release(ResourceAllocation<?, ?>... allocations) {
128 checkNotNull(allocations);
129
130 return release(ImmutableList.copyOf(allocations));
131 }
132
133 @Override
134 public boolean release(ResourceConsumer consumer) {
135 checkNotNull(consumer);
136
137 Collection<ResourceAllocation<?, ?>> allocations = getResourceAllocations(consumer);
138 return release(ImmutableList.copyOf(allocations));
139 }
140
141 @Override
142 public <S, T> Collection<ResourceAllocation<S, T>> getResourceAllocations(S subject, Class<T> cls) {
143 checkNotNull(subject);
144 checkNotNull(cls);
145
146 Collection<Resource<S, T>> resources = store.getAllocatedResources(subject, cls);
147 List<ResourceAllocation<S, T>> allocations = new ArrayList<>(resources.size());
148 for (Resource<S, T> resource: resources) {
149 // We access store twice in this method, then the store may be updated by others
150 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
151 if (consumer.isPresent()) {
152 allocations.add(
153 new DefaultResourceAllocation<>(resource.subject(), resource.resource(), consumer.get()));
154 }
155 }
156
157 return allocations;
158 }
159
160 @Override
161 public Collection<ResourceAllocation<?, ?>> getResourceAllocations(ResourceConsumer consumer) {
162 checkNotNull(consumer);
163
164 Collection<Resource<?, ?>> resources = store.getResources(consumer);
165 return resources.stream()
166 .map(x -> new DefaultResourceAllocation<>(x.subject(), x.resource(), consumer))
167 .collect(Collectors.toList());
168 }
169
170 @Override
171 public <S, T> boolean isAvailable(Resource<S, T> resource) {
172 checkNotNull(resource);
173
174 Optional<ResourceConsumer> consumer = store.getConsumer(resource);
175 return !consumer.isPresent();
176 }
177
178 /**
179 * Returns if the specified resource is in the resource range.
180 * E.g. VLAN ID against a link must be within 12 bit address space.
181 *
182 * @param resource resource to be checked if it is within the resource range
183 * @param <S> type of the subject
184 * @param <T> type of the resource
185 * @return true if the resource within the range, false otherwise
186 */
187 private <S, T> boolean isValid(Resource<S, T> resource) {
188 // TODO: implement
189 return true;
190 }
191}