Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.resource.impl; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 20 | import java.util.ArrayList; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 22 | import java.util.Iterator; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.onos.net.Link; |
| 31 | import org.onlab.onos.net.intent.IntentId; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.resource.BandwidthResourceAllocation; |
| 33 | import org.onlab.onos.net.resource.BandwidthResourceRequest; |
| 34 | import org.onlab.onos.net.resource.Lambda; |
| 35 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
| 36 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 37 | import org.onlab.onos.net.resource.LinkResourceRequest; |
| 38 | import org.onlab.onos.net.resource.LinkResourceService; |
| 39 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 40 | import org.onlab.onos.net.resource.ResourceRequest; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 43 | import com.google.common.collect.Sets; |
| 44 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Provides basic implementation of link resources allocation. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 50 | public class LinkResourceManager implements LinkResourceService { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 54 | LinkResourceAllocations savedAllocations; |
| 55 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 56 | @Activate |
| 57 | public void activate() { |
| 58 | log.info("Started"); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | public void deactivate() { |
| 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 66 | private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) { |
| 67 | return Sets.newHashSet(Lambda.valueOf(7)); |
| 68 | } |
| 69 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 70 | double usedBandwidth = 0.0; |
| 71 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 72 | @Override |
| 73 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 74 | // TODO implement it using a resource data store. |
| 75 | |
| 76 | ResourceAllocation alloc = null; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 77 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 78 | 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 Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 83 | usedBandwidth += br.bandwidth().toDouble(); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 84 | break; |
| 85 | case LAMBDA: |
| 86 | log.info("requestResources() always returns lambda 7"); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 87 | Iterator<Lambda> lambdaIterator = getAvailableLambdas(req.links()).iterator(); |
| 88 | if (lambdaIterator.hasNext()) { |
| 89 | alloc = new LambdaResourceAllocation(lambdaIterator.next()); |
| 90 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 91 | break; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 98 | for (Link link : req.links()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 99 | allocations.put(link, Sets.newHashSet(alloc)); |
| 100 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 101 | savedAllocations = new DefaultLinkResourceAllocations(req, allocations); |
| 102 | return savedAllocations; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void releaseResources(LinkResourceAllocations allocations) { |
| 107 | // TODO Auto-generated method stub |
| 108 | |
| 109 | } |
| 110 | |
| 111 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 112 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
| 113 | LinkResourceAllocations oldAllocations) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 118 | public Iterable<LinkResourceAllocations> getAllocations() { |
| 119 | // TODO Auto-generated method stub |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 124 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 125 | ArrayList<LinkResourceAllocations> retval = new ArrayList<>(0); |
| 126 | if (savedAllocations != null) { |
| 127 | retval.add(savedAllocations); |
| 128 | } |
| 129 | return retval; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 133 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 134 | // TODO Auto-generated method stub |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 139 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 140 | BandwidthResourceRequest bw = new BandwidthResourceRequest(usedBandwidth); |
| 141 | ArrayList<ResourceRequest> result = new ArrayList<>(); |
| 142 | result.add(bw); |
| 143 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public ResourceRequest getAvailableResources(Link link, |
| 148 | LinkResourceAllocations allocations) { |
| 149 | return null; |
| 150 | } |
| 151 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 152 | } |