8b9952edb5352ce178bb6f35930870decde4e1a6
[onosfw.git] /
1 /*
2  * Copyright 2014-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  */
16 package org.onosproject.net.resource.impl;
17
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;
48
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.Iterator;
53 import java.util.Map;
54 import java.util.Set;
55
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.*;
61
62
63 /**
64  * Provides basic implementation of link resources allocation.
65  */
66 @Component(immediate = true)
67 @Service
68 public class LinkResourceManager
69         extends AbstractListenerManager<LinkResourceEvent, LinkResourceListener>
70         implements LinkResourceService {
71
72     private final Logger log = getLogger(getClass());
73
74     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75     private LinkResourceStore store;
76
77     @Activate
78     public void activate() {
79         eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry);
80         log.info("Started");
81     }
82
83     @Deactivate
84     public void deactivate() {
85         eventDispatcher.removeSink(LinkResourceEvent.class);
86         log.info("Stopped");
87     }
88
89     /**
90      * Returns available lambdas on specified link.
91      *
92      * @param link the link
93      * @return available lambdas on specified link
94      */
95     private Set<LambdaResource> getAvailableLambdas(Link link) {
96         checkNotNull(link);
97         Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
98         if (resAllocs == null) {
99             return Collections.emptySet();
100         }
101         Set<LambdaResource> lambdas = new HashSet<>();
102         for (ResourceAllocation res : resAllocs) {
103             if (res.type() == ResourceType.LAMBDA) {
104                 lambdas.add(((LambdaResourceAllocation) res).lambda());
105             }
106         }
107         return lambdas;
108     }
109
110
111     /**
112      * Returns available lambdas on specified links.
113      *
114      * @param links the links
115      * @return available lambdas on specified links
116      */
117     private Iterable<LambdaResource> getAvailableLambdas(Iterable<Link> links) {
118         checkNotNull(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()));
124         }
125         return lambdas;
126     }
127
128
129     /**
130      * Returns available MPLS label on specified link.
131      *
132      * @param link the link
133      * @return available MPLS labels on specified link
134      */
135     private Iterable<MplsLabel> getAvailableMplsLabels(Link link) {
136         Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
137         if (resAllocs == null) {
138             return Collections.emptySet();
139         }
140         Set<MplsLabel> mplsLabels = new HashSet<>();
141         for (ResourceAllocation res : resAllocs) {
142             if (res.type() == ResourceType.MPLS_LABEL) {
143
144                 mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel());
145             }
146         }
147
148         return mplsLabels;
149     }
150
151     @Override
152     public LinkResourceAllocations requestResources(LinkResourceRequest req) {
153         checkPermission(LINK_WRITE);
154
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()) {
161             switch (r.type()) {
162             case BANDWIDTH:
163                 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
164                 allocs.add(new BandwidthResourceAllocation(br.bandwidth()));
165                 break;
166             case LAMBDA:
167                 Iterator<LambdaResource> lambdaIterator =
168                         getAvailableLambdas(req.links()).iterator();
169                 if (lambdaIterator.hasNext()) {
170                     allocs.add(new LambdaResourceAllocation(lambdaIterator.next()));
171                 } else {
172                     log.info("Failed to allocate lambda resource.");
173                     return null;
174                 }
175                 break;
176             case MPLS_LABEL:
177                 for (Link link : req.links()) {
178                     if (allocsPerLink.get(link) == null) {
179                         allocsPerLink.put(link, new HashSet<>());
180                     }
181                     Iterator<MplsLabel> mplsIter = getAvailableMplsLabels(link)
182                             .iterator();
183                     if (mplsIter.hasNext()) {
184                         allocsPerLink.get(link)
185                                 .add(new MplsLabelResourceAllocation(mplsIter
186                                              .next()));
187                     } else {
188                         log.info("Failed to allocate MPLS resource.");
189                         break;
190                     }
191                 }
192                 break;
193             default:
194                 break;
195             }
196         }
197
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);
204             }
205         }
206         LinkResourceAllocations result =
207                 new DefaultLinkResourceAllocations(req, allocations);
208         store.allocateResources(result);
209         return result;
210
211     }
212
213     @Override
214     public void releaseResources(LinkResourceAllocations allocations) {
215         checkPermission(LINK_WRITE);
216         final LinkResourceEvent event = store.releaseResources(allocations);
217         if (event != null) {
218             post(event);
219         }
220     }
221
222     @Override
223     public LinkResourceAllocations updateResources(LinkResourceRequest req,
224             LinkResourceAllocations oldAllocations) {
225         checkPermission(LINK_WRITE);
226         releaseResources(oldAllocations);
227          return requestResources(req);
228     }
229
230     @Override
231     public Iterable<LinkResourceAllocations> getAllocations() {
232         checkPermission(LINK_READ);
233         return store.getAllocations();
234     }
235
236     @Override
237     public Iterable<LinkResourceAllocations> getAllocations(Link link) {
238         checkPermission(LINK_READ);
239         return store.getAllocations(link);
240     }
241
242     @Override
243     public LinkResourceAllocations getAllocations(IntentId intentId) {
244         checkPermission(LINK_READ);
245         return store.getAllocations(intentId);
246     }
247
248     @Override
249     public Iterable<ResourceRequest> getAvailableResources(Link link) {
250         checkPermission(LINK_READ);
251
252         Set<ResourceAllocation> freeRes = store.getFreeResources(link);
253         Set<ResourceRequest> result = new HashSet<>();
254         for (ResourceAllocation alloc : freeRes) {
255             switch (alloc.type()) {
256             case BANDWIDTH:
257                 result.add(new BandwidthResourceRequest(
258                         ((BandwidthResourceAllocation) alloc).bandwidth()));
259                 break;
260             case LAMBDA:
261                 result.add(new LambdaResourceRequest());
262                 break;
263             case MPLS_LABEL:
264                 result.add(new MplsLabelResourceRequest());
265                 break;
266             default:
267                 break;
268             }
269         }
270         return result;
271     }
272
273     @Override
274     public Iterable<ResourceRequest> getAvailableResources(Link link,
275             LinkResourceAllocations allocations) {
276         checkPermission(LINK_READ);
277
278         Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link);
279         Set<ResourceRequest> result = Sets.newHashSet(getAvailableResources(link));
280         result.removeAll(allocatedRes);
281         return result;
282     }
283
284     /**
285      * Store delegate to re-post events emitted from the store.
286      */
287     private class InternalStoreDelegate implements LinkResourceStoreDelegate {
288         @Override
289         public void notify(LinkResourceEvent event) {
290             post(event);
291         }
292     }
293 }