2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.resource.impl;
18 import com.google.common.collect.Sets;
19 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate;
22 import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.apache.felix.scr.annotations.Service;
25 import org.onosproject.event.AbstractListenerManager;
26 import org.onosproject.net.Link;
27 import org.onosproject.net.intent.IntentId;
28 import org.onosproject.net.resource.ResourceAllocation;
29 import org.onosproject.net.resource.ResourceRequest;
30 import org.onosproject.net.resource.ResourceType;
31 import org.onosproject.net.resource.link.BandwidthResourceAllocation;
32 import org.onosproject.net.resource.link.BandwidthResourceRequest;
33 import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
34 import org.onosproject.net.resource.link.LambdaResource;
35 import org.onosproject.net.resource.link.LambdaResourceAllocation;
36 import org.onosproject.net.resource.link.LambdaResourceRequest;
37 import org.onosproject.net.resource.link.LinkResourceAllocations;
38 import org.onosproject.net.resource.link.LinkResourceEvent;
39 import org.onosproject.net.resource.link.LinkResourceListener;
40 import org.onosproject.net.resource.link.LinkResourceRequest;
41 import org.onosproject.net.resource.link.LinkResourceService;
42 import org.onosproject.net.resource.link.LinkResourceStore;
43 import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
44 import org.onosproject.net.resource.link.MplsLabel;
45 import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
46 import org.onosproject.net.resource.link.MplsLabelResourceRequest;
47 import org.slf4j.Logger;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.Iterator;
56 import static com.google.common.base.Preconditions.checkArgument;
57 import static com.google.common.base.Preconditions.checkNotNull;
58 import static org.onosproject.security.AppGuard.checkPermission;
59 import static org.slf4j.LoggerFactory.getLogger;
60 import static org.onosproject.security.AppPermission.Type.*;
64 * Provides basic implementation of link resources allocation.
66 @Component(immediate = true)
68 public class LinkResourceManager
69 extends AbstractListenerManager<LinkResourceEvent, LinkResourceListener>
70 implements LinkResourceService {
72 private final Logger log = getLogger(getClass());
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 private LinkResourceStore store;
78 public void activate() {
79 eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry);
84 public void deactivate() {
85 eventDispatcher.removeSink(LinkResourceEvent.class);
90 * Returns available lambdas on specified link.
92 * @param link the link
93 * @return available lambdas on specified link
95 private Set<LambdaResource> getAvailableLambdas(Link link) {
97 Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
98 if (resAllocs == null) {
99 return Collections.emptySet();
101 Set<LambdaResource> lambdas = new HashSet<>();
102 for (ResourceAllocation res : resAllocs) {
103 if (res.type() == ResourceType.LAMBDA) {
104 lambdas.add(((LambdaResourceAllocation) res).lambda());
112 * Returns available lambdas on specified links.
114 * @param links the links
115 * @return available lambdas on specified links
117 private Iterable<LambdaResource> getAvailableLambdas(Iterable<Link> links) {
119 Iterator<Link> i = links.iterator();
120 checkArgument(i.hasNext());
121 Set<LambdaResource> lambdas = new HashSet<>(getAvailableLambdas(i.next()));
122 while (i.hasNext()) {
123 lambdas.retainAll(getAvailableLambdas(i.next()));
130 * Returns available MPLS label on specified link.
132 * @param link the link
133 * @return available MPLS labels on specified link
135 private Iterable<MplsLabel> getAvailableMplsLabels(Link link) {
136 Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
137 if (resAllocs == null) {
138 return Collections.emptySet();
140 Set<MplsLabel> mplsLabels = new HashSet<>();
141 for (ResourceAllocation res : resAllocs) {
142 if (res.type() == ResourceType.MPLS_LABEL) {
144 mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel());
152 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
153 checkPermission(LINK_WRITE);
155 // TODO Concatenate multiple bandwidth requests.
156 // TODO Support multiple lambda resource requests.
157 // TODO Throw appropriate exception.
158 Set<ResourceAllocation> allocs = new HashSet<>();
159 Map<Link, Set<ResourceAllocation>> allocsPerLink = new HashMap<>();
160 for (ResourceRequest r : req.resources()) {
163 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
164 allocs.add(new BandwidthResourceAllocation(br.bandwidth()));
167 Iterator<LambdaResource> lambdaIterator =
168 getAvailableLambdas(req.links()).iterator();
169 if (lambdaIterator.hasNext()) {
170 allocs.add(new LambdaResourceAllocation(lambdaIterator.next()));
172 log.info("Failed to allocate lambda resource.");
177 for (Link link : req.links()) {
178 if (allocsPerLink.get(link) == null) {
179 allocsPerLink.put(link, new HashSet<>());
181 Iterator<MplsLabel> mplsIter = getAvailableMplsLabels(link)
183 if (mplsIter.hasNext()) {
184 allocsPerLink.get(link)
185 .add(new MplsLabelResourceAllocation(mplsIter
188 log.info("Failed to allocate MPLS resource.");
198 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
199 for (Link link : req.links()) {
200 allocations.put(link, new HashSet<>(allocs));
201 Set<ResourceAllocation> linkAllocs = allocsPerLink.get(link);
202 if (linkAllocs != null) {
203 allocations.get(link).addAll(linkAllocs);
206 LinkResourceAllocations result =
207 new DefaultLinkResourceAllocations(req, allocations);
208 store.allocateResources(result);
214 public void releaseResources(LinkResourceAllocations allocations) {
215 checkPermission(LINK_WRITE);
216 final LinkResourceEvent event = store.releaseResources(allocations);
223 public LinkResourceAllocations updateResources(LinkResourceRequest req,
224 LinkResourceAllocations oldAllocations) {
225 checkPermission(LINK_WRITE);
226 releaseResources(oldAllocations);
227 return requestResources(req);
231 public Iterable<LinkResourceAllocations> getAllocations() {
232 checkPermission(LINK_READ);
233 return store.getAllocations();
237 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
238 checkPermission(LINK_READ);
239 return store.getAllocations(link);
243 public LinkResourceAllocations getAllocations(IntentId intentId) {
244 checkPermission(LINK_READ);
245 return store.getAllocations(intentId);
249 public Iterable<ResourceRequest> getAvailableResources(Link link) {
250 checkPermission(LINK_READ);
252 Set<ResourceAllocation> freeRes = store.getFreeResources(link);
253 Set<ResourceRequest> result = new HashSet<>();
254 for (ResourceAllocation alloc : freeRes) {
255 switch (alloc.type()) {
257 result.add(new BandwidthResourceRequest(
258 ((BandwidthResourceAllocation) alloc).bandwidth()));
261 result.add(new LambdaResourceRequest());
264 result.add(new MplsLabelResourceRequest());
274 public Iterable<ResourceRequest> getAvailableResources(Link link,
275 LinkResourceAllocations allocations) {
276 checkPermission(LINK_READ);
278 Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link);
279 Set<ResourceRequest> result = Sets.newHashSet(getAvailableResources(link));
280 result.removeAll(allocatedRes);
285 * Store delegate to re-post events emitted from the store.
287 private class InternalStoreDelegate implements LinkResourceStoreDelegate {
289 public void notify(LinkResourceEvent event) {