Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -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 | */ |
| 16 | package org.onlab.onos.store.resource.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.onlab.onos.net.Link; |
| 23 | import org.onlab.onos.net.intent.IntentId; |
| 24 | import org.onlab.onos.net.resource.Bandwidth; |
| 25 | import org.onlab.onos.net.resource.BandwidthResourceAllocation; |
| 26 | import org.onlab.onos.net.resource.Lambda; |
| 27 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
| 28 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 29 | import org.onlab.onos.net.resource.LinkResourceStore; |
| 30 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 31 | import org.onlab.onos.net.resource.ResourceType; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.Collections; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static com.google.common.base.Preconditions.checkState; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Manages link resources using trivial in-memory structures implementation. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public class DistributedLinkResourceStore implements LinkResourceStore { |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | private Map<IntentId, LinkResourceAllocations> linkResourceAllocationsMap; |
| 52 | private Map<Link, Set<LinkResourceAllocations>> allocatedResources; |
| 53 | private Map<Link, Set<ResourceAllocation>> freeResources; |
| 54 | |
| 55 | @Activate |
| 56 | public void activate() { |
| 57 | linkResourceAllocationsMap = new HashMap<>(); |
| 58 | allocatedResources = new HashMap<>(); |
| 59 | freeResources = new HashMap<>(); |
| 60 | |
| 61 | log.info("Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | log.info("Stopped"); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns free resources for a given link obtaining from topology |
| 71 | * information. |
| 72 | * |
| 73 | * @param link the target link |
| 74 | * @return free resources |
| 75 | */ |
| 76 | private Set<ResourceAllocation> readOriginalFreeResources(Link link) { |
| 77 | // TODO read capacity and lambda resources from topology |
| 78 | Set<ResourceAllocation> allocations = new HashSet<>(); |
| 79 | for (int i = 1; i <= 100; i++) { |
| 80 | allocations.add(new LambdaResourceAllocation(Lambda.valueOf(i))); |
| 81 | } |
| 82 | allocations.add(new BandwidthResourceAllocation(Bandwidth.valueOf(1000000))); |
| 83 | return allocations; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Finds and returns {@link org.onlab.onos.net.resource.BandwidthResourceAllocation} object from a given |
| 88 | * set. |
| 89 | * |
| 90 | * @param freeRes a set of ResourceAllocation object. |
| 91 | * @return {@link org.onlab.onos.net.resource.BandwidthResourceAllocation} object if found, otherwise |
| 92 | * {@link org.onlab.onos.net.resource.BandwidthResourceAllocation} object with 0 bandwidth |
| 93 | * |
| 94 | */ |
| 95 | private BandwidthResourceAllocation getBandwidth(Set<ResourceAllocation> freeRes) { |
| 96 | for (ResourceAllocation res : freeRes) { |
| 97 | if (res.type() == ResourceType.BANDWIDTH) { |
| 98 | return (BandwidthResourceAllocation) res; |
| 99 | } |
| 100 | } |
| 101 | return new BandwidthResourceAllocation(Bandwidth.valueOf(0)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Subtracts given resources from free resources for given link. |
| 106 | * |
| 107 | * @param link the target link |
| 108 | * @param allocations the resources to be subtracted |
| 109 | */ |
| 110 | private void subtractFreeResources(Link link, LinkResourceAllocations allocations) { |
| 111 | // TODO Use lock or version for updating freeResources. |
| 112 | checkNotNull(link); |
| 113 | Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); |
| 114 | Set<ResourceAllocation> subRes = allocations.getResourceAllocation(link); |
| 115 | for (ResourceAllocation res : subRes) { |
| 116 | switch (res.type()) { |
| 117 | case BANDWIDTH: |
| 118 | BandwidthResourceAllocation ba = getBandwidth(freeRes); |
| 119 | double requestedBandwidth = |
| 120 | ((BandwidthResourceAllocation) res).bandwidth().toDouble(); |
| 121 | double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth; |
| 122 | checkState(newBandwidth >= 0.0); |
| 123 | freeRes.remove(ba); |
| 124 | freeRes.add(new BandwidthResourceAllocation( |
| 125 | Bandwidth.valueOf(newBandwidth))); |
| 126 | break; |
| 127 | case LAMBDA: |
| 128 | checkState(freeRes.remove(res)); |
| 129 | break; |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | freeResources.put(link, freeRes); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adds given resources to free resources for given link. |
| 140 | * |
| 141 | * @param link the target link |
| 142 | * @param allocations the resources to be added |
| 143 | */ |
| 144 | private void addFreeResources(Link link, LinkResourceAllocations allocations) { |
| 145 | // TODO Use lock or version for updating freeResources. |
| 146 | Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); |
| 147 | Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link); |
| 148 | for (ResourceAllocation res : addRes) { |
| 149 | switch (res.type()) { |
| 150 | case BANDWIDTH: |
| 151 | BandwidthResourceAllocation ba = getBandwidth(freeRes); |
| 152 | double requestedBandwidth = |
| 153 | ((BandwidthResourceAllocation) res).bandwidth().toDouble(); |
| 154 | double newBandwidth = ba.bandwidth().toDouble() + requestedBandwidth; |
| 155 | freeRes.remove(ba); |
| 156 | freeRes.add(new BandwidthResourceAllocation( |
| 157 | Bandwidth.valueOf(newBandwidth))); |
| 158 | break; |
| 159 | case LAMBDA: |
| 160 | checkState(freeRes.add(res)); |
| 161 | break; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | freeResources.put(link, freeRes); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Set<ResourceAllocation> getFreeResources(Link link) { |
| 171 | checkNotNull(link); |
| 172 | Set<ResourceAllocation> freeRes = freeResources.get(link); |
| 173 | if (freeRes == null) { |
| 174 | freeRes = readOriginalFreeResources(link); |
| 175 | } |
| 176 | |
| 177 | return freeRes; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void allocateResources(LinkResourceAllocations allocations) { |
| 182 | checkNotNull(allocations); |
| 183 | linkResourceAllocationsMap.put(allocations.intendId(), allocations); |
| 184 | for (Link link : allocations.links()) { |
| 185 | subtractFreeResources(link, allocations); |
| 186 | Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link); |
| 187 | if (linkAllocs == null) { |
| 188 | linkAllocs = new HashSet<>(); |
| 189 | } |
| 190 | linkAllocs.add(allocations); |
| 191 | allocatedResources.put(link, linkAllocs); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void releaseResources(LinkResourceAllocations allocations) { |
| 197 | checkNotNull(allocations); |
| 198 | linkResourceAllocationsMap.remove(allocations); |
| 199 | for (Link link : allocations.links()) { |
| 200 | addFreeResources(link, allocations); |
| 201 | Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link); |
| 202 | if (linkAllocs == null) { |
| 203 | log.error("Missing resource allocation."); |
| 204 | } else { |
| 205 | linkAllocs.remove(allocations); |
| 206 | } |
| 207 | allocatedResources.put(link, linkAllocs); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
| 213 | checkNotNull(intentId); |
| 214 | return linkResourceAllocationsMap.get(intentId); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
| 219 | checkNotNull(link); |
| 220 | Set<LinkResourceAllocations> result = allocatedResources.get(link); |
| 221 | if (result == null) { |
| 222 | result = Collections.emptySet(); |
| 223 | } |
| 224 | return Collections.unmodifiableSet(result); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public Iterable<LinkResourceAllocations> getAllocations() { |
| 229 | return Collections.unmodifiableCollection(linkResourceAllocationsMap.values()); |
| 230 | } |
| 231 | |
| 232 | } |