blob: 7f01d37559a4829e82cfb2f7b3514d65a3145ac8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Toshio Koide5c0a7262014-10-23 14:50:21 -070016package org.onlab.onos.net.resource.impl;
Toshio Koide50df38d2014-10-23 10:36:51 -070017
Toshio Koide106d4492014-10-28 11:22:02 -070018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
Toshio Koide50df38d2014-10-23 10:36:51 -070020import static org.slf4j.LoggerFactory.getLogger;
21
Toshio Koide106d4492014-10-28 11:22:02 -070022import java.util.Collections;
Toshio Koideca0fcff2014-10-23 14:08:36 -070023import java.util.HashMap;
Toshio Koide106d4492014-10-28 11:22:02 -070024import java.util.HashSet;
Toshio Koide9be539e2014-10-23 18:43:30 -070025import java.util.Iterator;
Toshio Koideca0fcff2014-10-23 14:08:36 -070026import java.util.Map;
27import java.util.Set;
28
Toshio Koide50df38d2014-10-23 10:36:51 -070029import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
Toshio Koide106d4492014-10-28 11:22:02 -070032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
Toshio Koide50df38d2014-10-23 10:36:51 -070034import org.apache.felix.scr.annotations.Service;
35import org.onlab.onos.net.Link;
36import org.onlab.onos.net.intent.IntentId;
Toshio Koide5c0a7262014-10-23 14:50:21 -070037import org.onlab.onos.net.resource.BandwidthResourceAllocation;
38import org.onlab.onos.net.resource.BandwidthResourceRequest;
39import org.onlab.onos.net.resource.Lambda;
40import org.onlab.onos.net.resource.LambdaResourceAllocation;
Toshio Koide106d4492014-10-28 11:22:02 -070041import org.onlab.onos.net.resource.LambdaResourceRequest;
Toshio Koide5c0a7262014-10-23 14:50:21 -070042import org.onlab.onos.net.resource.LinkResourceAllocations;
43import org.onlab.onos.net.resource.LinkResourceRequest;
44import org.onlab.onos.net.resource.LinkResourceService;
Toshio Koide106d4492014-10-28 11:22:02 -070045import org.onlab.onos.net.resource.LinkResourceStore;
Toshio Koide5c0a7262014-10-23 14:50:21 -070046import org.onlab.onos.net.resource.ResourceAllocation;
47import org.onlab.onos.net.resource.ResourceRequest;
Toshio Koide106d4492014-10-28 11:22:02 -070048import org.onlab.onos.net.resource.ResourceType;
Toshio Koide50df38d2014-10-23 10:36:51 -070049import org.slf4j.Logger;
50
51/**
52 * Provides basic implementation of link resources allocation.
53 */
54@Component(immediate = true)
55@Service
56public class LinkResourceManager implements LinkResourceService {
57
58 private final Logger log = getLogger(getClass());
59
Toshio Koide106d4492014-10-28 11:22:02 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 private LinkResourceStore store;
Ray Milkeycaa450b2014-10-29 15:54:24 -070062
Toshio Koide50df38d2014-10-23 10:36:51 -070063 @Activate
64 public void activate() {
65 log.info("Started");
66 }
67
68 @Deactivate
69 public void deactivate() {
70 log.info("Stopped");
71 }
72
Toshio Koide106d4492014-10-28 11:22:02 -070073 /**
74 * Returns available lambdas on specified link.
75 *
76 * @param link the link
77 * @return available lambdas on specified link
78 */
79 private Set<Lambda> getAvailableLambdas(Link link) {
80 checkNotNull(link);
81 Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
82 if (resAllocs == null) {
83 return Collections.emptySet();
84 }
85 Set<Lambda> lambdas = new HashSet<>();
86 for (ResourceAllocation res : resAllocs) {
87 if (res.type() == ResourceType.LAMBDA) {
88 lambdas.add(((LambdaResourceAllocation) res).lambda());
89 }
90 }
91 return lambdas;
Toshio Koide9be539e2014-10-23 18:43:30 -070092 }
93
Toshio Koide106d4492014-10-28 11:22:02 -070094 /**
95 * Returns available lambdas on specified links.
96 *
97 * @param links the links
98 * @return available lambdas on specified links
99 */
100 private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) {
101 checkNotNull(links);
102 Iterator<Link> i = links.iterator();
103 checkArgument(i.hasNext());
104 Set<Lambda> lambdas = new HashSet<>(getAvailableLambdas(i.next()));
105 while (i.hasNext()) {
106 lambdas.retainAll(getAvailableLambdas(i.next()));
107 }
108 return lambdas;
109 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700110
Toshio Koide50df38d2014-10-23 10:36:51 -0700111 @Override
112 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
Toshio Koide106d4492014-10-28 11:22:02 -0700113 // TODO Concatenate multiple bandwidth requests.
114 // TODO Support multiple lambda resource requests.
115 // TODO Throw appropriate exception.
Toshio Koideca0fcff2014-10-23 14:08:36 -0700116
Toshio Koide106d4492014-10-28 11:22:02 -0700117 Set<ResourceAllocation> allocs = new HashSet<>();
Toshio Koide9be539e2014-10-23 18:43:30 -0700118 for (ResourceRequest r : req.resources()) {
Toshio Koideca0fcff2014-10-23 14:08:36 -0700119 switch (r.type()) {
120 case BANDWIDTH:
Toshio Koideca0fcff2014-10-23 14:08:36 -0700121 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
Toshio Koide106d4492014-10-28 11:22:02 -0700122 allocs.add(new BandwidthResourceAllocation(br.bandwidth()));
Toshio Koideca0fcff2014-10-23 14:08:36 -0700123 break;
124 case LAMBDA:
Toshio Koide106d4492014-10-28 11:22:02 -0700125 Iterator<Lambda> lambdaIterator =
126 getAvailableLambdas(req.links()).iterator();
Toshio Koide9be539e2014-10-23 18:43:30 -0700127 if (lambdaIterator.hasNext()) {
Toshio Koide106d4492014-10-28 11:22:02 -0700128 allocs.add(new LambdaResourceAllocation(lambdaIterator.next()));
129 } else {
130 log.info("Failed to allocate lambda resource.");
131 return null;
Toshio Koide9be539e2014-10-23 18:43:30 -0700132 }
Toshio Koideca0fcff2014-10-23 14:08:36 -0700133 break;
134 default:
135 break;
136 }
137 }
138
139 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
Toshio Koide9be539e2014-10-23 18:43:30 -0700140 for (Link link : req.links()) {
Toshio Koide106d4492014-10-28 11:22:02 -0700141 allocations.put(link, allocs);
Toshio Koideca0fcff2014-10-23 14:08:36 -0700142 }
Toshio Koide106d4492014-10-28 11:22:02 -0700143 LinkResourceAllocations result =
144 new DefaultLinkResourceAllocations(req, allocations);
145 store.allocateResources(result);
146 return result;
147
Toshio Koide50df38d2014-10-23 10:36:51 -0700148 }
149
150 @Override
151 public void releaseResources(LinkResourceAllocations allocations) {
Toshio Koide106d4492014-10-28 11:22:02 -0700152 store.releaseResources(allocations);
Toshio Koide50df38d2014-10-23 10:36:51 -0700153 }
154
155 @Override
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700156 public LinkResourceAllocations updateResources(LinkResourceRequest req,
Toshio Koide106d4492014-10-28 11:22:02 -0700157 LinkResourceAllocations oldAllocations) {
158 // TODO
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700159 return null;
160 }
161
162 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700163 public Iterable<LinkResourceAllocations> getAllocations() {
Toshio Koide106d4492014-10-28 11:22:02 -0700164 return store.getAllocations();
Toshio Koide50df38d2014-10-23 10:36:51 -0700165 }
166
167 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700168 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Toshio Koide106d4492014-10-28 11:22:02 -0700169 return store.getAllocations(link);
Toshio Koide50df38d2014-10-23 10:36:51 -0700170 }
171
172 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700173 public LinkResourceAllocations getAllocations(IntentId intentId) {
Toshio Koide106d4492014-10-28 11:22:02 -0700174 return store.getAllocations(intentId);
Toshio Koide50df38d2014-10-23 10:36:51 -0700175 }
176
177 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700178 public Iterable<ResourceRequest> getAvailableResources(Link link) {
Toshio Koide106d4492014-10-28 11:22:02 -0700179 Set<ResourceAllocation> freeRes = store.getFreeResources(link);
180 Set<ResourceRequest> result = new HashSet<>();
181 for (ResourceAllocation alloc : freeRes) {
182 switch (alloc.type()) {
183 case BANDWIDTH:
184 result.add(new BandwidthResourceRequest(
185 ((BandwidthResourceAllocation) alloc).bandwidth()));
186 break;
187 case LAMBDA:
188 result.add(new LambdaResourceRequest());
189 break;
190 default:
191 break;
192 }
193 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700194 return result;
Toshio Koide50df38d2014-10-23 10:36:51 -0700195 }
196
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700197 @Override
198 public ResourceRequest getAvailableResources(Link link,
Toshio Koide106d4492014-10-28 11:22:02 -0700199 LinkResourceAllocations allocations) {
200 // TODO
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700201 return null;
202 }
203
Toshio Koide50df38d2014-10-23 10:36:51 -0700204}