blob: 4f84b82c1385cae19ada48f5791395017e6172c4 [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
18import static org.slf4j.LoggerFactory.getLogger;
19
Ray Milkeycaa450b2014-10-29 15:54:24 -070020import java.util.ArrayList;
Toshio Koideca0fcff2014-10-23 14:08:36 -070021import java.util.HashMap;
Toshio Koide9be539e2014-10-23 18:43:30 -070022import java.util.Iterator;
Toshio Koideca0fcff2014-10-23 14:08:36 -070023import java.util.Map;
24import java.util.Set;
25
Toshio Koide50df38d2014-10-23 10:36:51 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.onos.net.Link;
31import org.onlab.onos.net.intent.IntentId;
Toshio Koide5c0a7262014-10-23 14:50:21 -070032import org.onlab.onos.net.resource.BandwidthResourceAllocation;
33import org.onlab.onos.net.resource.BandwidthResourceRequest;
34import org.onlab.onos.net.resource.Lambda;
35import org.onlab.onos.net.resource.LambdaResourceAllocation;
36import org.onlab.onos.net.resource.LinkResourceAllocations;
37import org.onlab.onos.net.resource.LinkResourceRequest;
38import org.onlab.onos.net.resource.LinkResourceService;
39import org.onlab.onos.net.resource.ResourceAllocation;
40import org.onlab.onos.net.resource.ResourceRequest;
Toshio Koide50df38d2014-10-23 10:36:51 -070041import org.slf4j.Logger;
42
Toshio Koideca0fcff2014-10-23 14:08:36 -070043import com.google.common.collect.Sets;
44
Toshio Koide50df38d2014-10-23 10:36:51 -070045/**
46 * Provides basic implementation of link resources allocation.
47 */
48@Component(immediate = true)
49@Service
50public class LinkResourceManager implements LinkResourceService {
51
52 private final Logger log = getLogger(getClass());
53
Ray Milkeycaa450b2014-10-29 15:54:24 -070054 LinkResourceAllocations savedAllocations;
55
Toshio Koide50df38d2014-10-23 10:36:51 -070056 @Activate
57 public void activate() {
58 log.info("Started");
59 }
60
61 @Deactivate
62 public void deactivate() {
63 log.info("Stopped");
64 }
65
Toshio Koide9be539e2014-10-23 18:43:30 -070066 private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) {
67 return Sets.newHashSet(Lambda.valueOf(7));
68 }
69
Ray Milkeycaa450b2014-10-29 15:54:24 -070070 double usedBandwidth = 0.0;
71
Toshio Koide50df38d2014-10-23 10:36:51 -070072 @Override
73 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
Toshio Koideca0fcff2014-10-23 14:08:36 -070074 // TODO implement it using a resource data store.
75
76 ResourceAllocation alloc = null;
Toshio Koide9be539e2014-10-23 18:43:30 -070077 for (ResourceRequest r : req.resources()) {
Toshio Koideca0fcff2014-10-23 14:08:36 -070078 switch (r.type()) {
79 case BANDWIDTH:
80 log.info("requestResources() always returns requested bandwidth");
81 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
82 alloc = new BandwidthResourceAllocation(br.bandwidth());
Ray Milkeycaa450b2014-10-29 15:54:24 -070083 usedBandwidth += br.bandwidth().toDouble();
Toshio Koideca0fcff2014-10-23 14:08:36 -070084 break;
85 case LAMBDA:
86 log.info("requestResources() always returns lambda 7");
Toshio Koide9be539e2014-10-23 18:43:30 -070087 Iterator<Lambda> lambdaIterator = getAvailableLambdas(req.links()).iterator();
88 if (lambdaIterator.hasNext()) {
89 alloc = new LambdaResourceAllocation(lambdaIterator.next());
90 }
Toshio Koideca0fcff2014-10-23 14:08:36 -070091 break;
92 default:
93 break;
94 }
95 }
96
97 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
Toshio Koide9be539e2014-10-23 18:43:30 -070098 for (Link link : req.links()) {
Toshio Koideca0fcff2014-10-23 14:08:36 -070099 allocations.put(link, Sets.newHashSet(alloc));
100 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700101 savedAllocations = new DefaultLinkResourceAllocations(req, allocations);
102 return savedAllocations;
Toshio Koide50df38d2014-10-23 10:36:51 -0700103 }
104
105 @Override
106 public void releaseResources(LinkResourceAllocations allocations) {
107 // TODO Auto-generated method stub
108
109 }
110
111 @Override
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700112 public LinkResourceAllocations updateResources(LinkResourceRequest req,
113 LinkResourceAllocations oldAllocations) {
114 return null;
115 }
116
117 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700118 public Iterable<LinkResourceAllocations> getAllocations() {
119 // TODO Auto-generated method stub
120 return null;
121 }
122
123 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700124 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Ray Milkeycaa450b2014-10-29 15:54:24 -0700125 ArrayList<LinkResourceAllocations> retval = new ArrayList<>(0);
126 if (savedAllocations != null) {
127 retval.add(savedAllocations);
128 }
129 return retval;
Toshio Koide50df38d2014-10-23 10:36:51 -0700130 }
131
132 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700133 public LinkResourceAllocations getAllocations(IntentId intentId) {
Toshio Koide50df38d2014-10-23 10:36:51 -0700134 // TODO Auto-generated method stub
135 return null;
136 }
137
138 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700139 public Iterable<ResourceRequest> getAvailableResources(Link link) {
Ray Milkeycaa450b2014-10-29 15:54:24 -0700140 BandwidthResourceRequest bw = new BandwidthResourceRequest(usedBandwidth);
141 ArrayList<ResourceRequest> result = new ArrayList<>();
142 result.add(bw);
143 return result;
Toshio Koide50df38d2014-10-23 10:36:51 -0700144 }
145
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700146 @Override
147 public ResourceRequest getAvailableResources(Link link,
148 LinkResourceAllocations allocations) {
149 return null;
150 }
151
Toshio Koide50df38d2014-10-23 10:36:51 -0700152}