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 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 23 | import java.util.HashMap; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 24 | import java.util.HashSet; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 25 | import java.util.Iterator; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Service; |
| 35 | import org.onlab.onos.net.Link; |
| 36 | import org.onlab.onos.net.intent.IntentId; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.resource.BandwidthResourceAllocation; |
| 38 | import org.onlab.onos.net.resource.BandwidthResourceRequest; |
| 39 | import org.onlab.onos.net.resource.Lambda; |
| 40 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.resource.LambdaResourceRequest; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 43 | import org.onlab.onos.net.resource.LinkResourceRequest; |
| 44 | import org.onlab.onos.net.resource.LinkResourceService; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.resource.LinkResourceStore; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 47 | import org.onlab.onos.net.resource.ResourceRequest; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.resource.ResourceType; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
| 51 | /** |
| 52 | * Provides basic implementation of link resources allocation. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | @Service |
| 56 | public class LinkResourceManager implements LinkResourceService { |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | private LinkResourceStore store; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 62 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 63 | @Activate |
| 64 | public void activate() { |
| 65 | log.info("Started"); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
| 70 | log.info("Stopped"); |
| 71 | } |
| 72 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 73 | /** |
| 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 Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 94 | /** |
| 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 Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 110 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 113 | // TODO Concatenate multiple bandwidth requests. |
| 114 | // TODO Support multiple lambda resource requests. |
| 115 | // TODO Throw appropriate exception. |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 116 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 117 | Set<ResourceAllocation> allocs = new HashSet<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 118 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 119 | switch (r.type()) { |
| 120 | case BANDWIDTH: |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 121 | BandwidthResourceRequest br = (BandwidthResourceRequest) r; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 122 | allocs.add(new BandwidthResourceAllocation(br.bandwidth())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 123 | break; |
| 124 | case LAMBDA: |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 125 | Iterator<Lambda> lambdaIterator = |
| 126 | getAvailableLambdas(req.links()).iterator(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 127 | if (lambdaIterator.hasNext()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 128 | allocs.add(new LambdaResourceAllocation(lambdaIterator.next())); |
| 129 | } else { |
| 130 | log.info("Failed to allocate lambda resource."); |
| 131 | return null; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 132 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 140 | for (Link link : req.links()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 141 | allocations.put(link, allocs); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 142 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 143 | LinkResourceAllocations result = |
| 144 | new DefaultLinkResourceAllocations(req, allocations); |
| 145 | store.allocateResources(result); |
| 146 | return result; |
| 147 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void releaseResources(LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 152 | store.releaseResources(allocations); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 156 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 157 | LinkResourceAllocations oldAllocations) { |
| 158 | // TODO |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 159 | return null; |
| 160 | } |
| 161 | |
| 162 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 163 | public Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 164 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 168 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 169 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 173 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 174 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 178 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 179 | 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 Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 194 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public ResourceRequest getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 199 | LinkResourceAllocations allocations) { |
| 200 | // TODO |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 201 | return null; |
| 202 | } |
| 203 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 204 | } |