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; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame^] | 39 | import org.onlab.onos.net.resource.DefaultLinkResourceAllocations; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.resource.Lambda; |
| 41 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.resource.LambdaResourceRequest; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 44 | import org.onlab.onos.net.resource.LinkResourceRequest; |
| 45 | import org.onlab.onos.net.resource.LinkResourceService; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.resource.LinkResourceStore; |
Toshio Koide | 5c0a726 | 2014-10-23 14:50:21 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 48 | import org.onlab.onos.net.resource.ResourceRequest; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 49 | import org.onlab.onos.net.resource.ResourceType; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | /** |
| 53 | * Provides basic implementation of link resources allocation. |
| 54 | */ |
| 55 | @Component(immediate = true) |
| 56 | @Service |
| 57 | public class LinkResourceManager implements LinkResourceService { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | private LinkResourceStore store; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 63 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 64 | @Activate |
| 65 | public void activate() { |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | log.info("Stopped"); |
| 72 | } |
| 73 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Returns available lambdas on specified link. |
| 76 | * |
| 77 | * @param link the link |
| 78 | * @return available lambdas on specified link |
| 79 | */ |
| 80 | private Set<Lambda> getAvailableLambdas(Link link) { |
| 81 | checkNotNull(link); |
| 82 | Set<ResourceAllocation> resAllocs = store.getFreeResources(link); |
| 83 | if (resAllocs == null) { |
| 84 | return Collections.emptySet(); |
| 85 | } |
| 86 | Set<Lambda> lambdas = new HashSet<>(); |
| 87 | for (ResourceAllocation res : resAllocs) { |
| 88 | if (res.type() == ResourceType.LAMBDA) { |
| 89 | lambdas.add(((LambdaResourceAllocation) res).lambda()); |
| 90 | } |
| 91 | } |
| 92 | return lambdas; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Returns available lambdas on specified links. |
| 97 | * |
| 98 | * @param links the links |
| 99 | * @return available lambdas on specified links |
| 100 | */ |
| 101 | private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) { |
| 102 | checkNotNull(links); |
| 103 | Iterator<Link> i = links.iterator(); |
| 104 | checkArgument(i.hasNext()); |
| 105 | Set<Lambda> lambdas = new HashSet<>(getAvailableLambdas(i.next())); |
| 106 | while (i.hasNext()) { |
| 107 | lambdas.retainAll(getAvailableLambdas(i.next())); |
| 108 | } |
| 109 | return lambdas; |
| 110 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 111 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 114 | // TODO Concatenate multiple bandwidth requests. |
| 115 | // TODO Support multiple lambda resource requests. |
| 116 | // TODO Throw appropriate exception. |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 117 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 118 | Set<ResourceAllocation> allocs = new HashSet<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 119 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 120 | switch (r.type()) { |
| 121 | case BANDWIDTH: |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 122 | BandwidthResourceRequest br = (BandwidthResourceRequest) r; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 123 | allocs.add(new BandwidthResourceAllocation(br.bandwidth())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 124 | break; |
| 125 | case LAMBDA: |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 126 | Iterator<Lambda> lambdaIterator = |
| 127 | getAvailableLambdas(req.links()).iterator(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 128 | if (lambdaIterator.hasNext()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 129 | allocs.add(new LambdaResourceAllocation(lambdaIterator.next())); |
| 130 | } else { |
| 131 | log.info("Failed to allocate lambda resource."); |
| 132 | return null; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 133 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 134 | break; |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 141 | for (Link link : req.links()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 142 | allocations.put(link, allocs); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 143 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 144 | LinkResourceAllocations result = |
| 145 | new DefaultLinkResourceAllocations(req, allocations); |
| 146 | store.allocateResources(result); |
| 147 | return result; |
| 148 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void releaseResources(LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 153 | store.releaseResources(allocations); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 157 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 158 | LinkResourceAllocations oldAllocations) { |
| 159 | // TODO |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 160 | return null; |
| 161 | } |
| 162 | |
| 163 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 164 | public Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 165 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 169 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 170 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 174 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 175 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 179 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 180 | Set<ResourceAllocation> freeRes = store.getFreeResources(link); |
| 181 | Set<ResourceRequest> result = new HashSet<>(); |
| 182 | for (ResourceAllocation alloc : freeRes) { |
| 183 | switch (alloc.type()) { |
| 184 | case BANDWIDTH: |
| 185 | result.add(new BandwidthResourceRequest( |
| 186 | ((BandwidthResourceAllocation) alloc).bandwidth())); |
| 187 | break; |
| 188 | case LAMBDA: |
| 189 | result.add(new LambdaResourceRequest()); |
| 190 | break; |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 195 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 198 | @Override |
| 199 | public ResourceRequest getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 200 | LinkResourceAllocations allocations) { |
| 201 | // TODO |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 202 | return null; |
| 203 | } |
| 204 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 205 | } |