Committed by
Ray Milkey
Refactor: Reduce depth of indent
Inline enumerating over enum values Change-Id: Icff35340d91490b67b05d521f7addc6519d2b47a
Showing
1 changed file
with
70 additions
and
67 deletions
| ... | @@ -223,79 +223,82 @@ public class ConsistentLinkResourceStore extends | ... | @@ -223,79 +223,82 @@ public class ConsistentLinkResourceStore extends |
| 223 | 223 | ||
| 224 | Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>(); | 224 | Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>(); |
| 225 | final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link); | 225 | final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link); |
| 226 | - final Iterable<LinkResourceAllocations> allocations = getAllocations(tx, link); | 226 | + final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link)); |
| 227 | 227 | ||
| 228 | - for (ResourceType type : ResourceType.values()) { | 228 | + Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH); |
| 229 | - // there should be class/category of resources | 229 | + Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations); |
| 230 | + free.put(ResourceType.BANDWIDTH, value); | ||
| 230 | 231 | ||
| 231 | - switch (type) { | 232 | + Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA); |
| 232 | - case BANDWIDTH: | 233 | + Set<ResourceAllocation> freeL = getFreeLambdaResources(link, lmd, allocations); |
| 233 | - Set<ResourceAllocation> bw = caps.get(type); | 234 | + free.put(ResourceType.LAMBDA, freeL); |
| 234 | - if (bw == null || bw.isEmpty()) { | 235 | + |
| 235 | - bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); | 236 | + Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL); |
| 236 | - } | 237 | + Set<ResourceAllocation> freeLabel = getFreeLabelResources(link, mpls, allocations); |
| 238 | + free.put(ResourceType.MPLS_LABEL, freeLabel); | ||
| 237 | 239 | ||
| 238 | - BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next(); | ||
| 239 | - double freeBw = cap.bandwidth().toDouble(); | ||
| 240 | - | ||
| 241 | - // enumerate current allocations, subtracting resources | ||
| 242 | - double allocatedBw = ImmutableList.copyOf(allocations).stream() | ||
| 243 | - .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 244 | - .filter(x -> x instanceof BandwidthResourceAllocation) | ||
| 245 | - .map(x -> (BandwidthResourceAllocation) x) | ||
| 246 | - .mapToDouble(x -> x.bandwidth().toDouble()) | ||
| 247 | - .sum(); | ||
| 248 | - freeBw -= allocatedBw; | ||
| 249 | - | ||
| 250 | - free.put(type, Sets.newHashSet( | ||
| 251 | - new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))))); | ||
| 252 | - break; | ||
| 253 | - case LAMBDA: | ||
| 254 | - Set<ResourceAllocation> lmd = caps.get(type); | ||
| 255 | - if (lmd == null || lmd.isEmpty()) { | ||
| 256 | - // nothing left | ||
| 257 | - break; | ||
| 258 | - } | ||
| 259 | - Set<ResourceAllocation> freeL = lmd.stream() | ||
| 260 | - .filter(x -> x instanceof LambdaResourceAllocation) | ||
| 261 | - .collect(Collectors.toSet()); | ||
| 262 | - | ||
| 263 | - // enumerate current allocations, removing resources | ||
| 264 | - List<ResourceAllocation> allocatedLambda = ImmutableList.copyOf(allocations).stream() | ||
| 265 | - .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 266 | - .filter(x -> x instanceof LambdaResourceAllocation) | ||
| 267 | - .collect(Collectors.toList()); | ||
| 268 | - freeL.removeAll(allocatedLambda); | ||
| 269 | - | ||
| 270 | - free.put(type, freeL); | ||
| 271 | - break; | ||
| 272 | - case MPLS_LABEL: | ||
| 273 | - Set<ResourceAllocation> mpls = caps.get(type); | ||
| 274 | - if (mpls == null || mpls.isEmpty()) { | ||
| 275 | - // nothing left | ||
| 276 | - break; | ||
| 277 | - } | ||
| 278 | - Set<ResourceAllocation> freeLabel = mpls.stream() | ||
| 279 | - .filter(x -> x instanceof MplsLabelResourceAllocation) | ||
| 280 | - .collect(Collectors.toSet()); | ||
| 281 | - | ||
| 282 | - // enumerate current allocations, removing resources | ||
| 283 | - List<ResourceAllocation> allocatedLabel = ImmutableList.copyOf(allocations).stream() | ||
| 284 | - .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 285 | - .filter(x -> x instanceof MplsLabelResourceAllocation) | ||
| 286 | - .collect(Collectors.toList()); | ||
| 287 | - freeLabel.removeAll(allocatedLabel); | ||
| 288 | - | ||
| 289 | - free.put(type, freeLabel); | ||
| 290 | - break; | ||
| 291 | - default: | ||
| 292 | - log.debug("unsupported ResourceType {}", type); | ||
| 293 | - break; | ||
| 294 | - } | ||
| 295 | - } | ||
| 296 | return free; | 240 | return free; |
| 297 | } | 241 | } |
| 298 | 242 | ||
| 243 | + private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw, | ||
| 244 | + List<LinkResourceAllocations> allocations) { | ||
| 245 | + if (bw == null || bw.isEmpty()) { | ||
| 246 | + bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next(); | ||
| 250 | + double freeBw = cap.bandwidth().toDouble(); | ||
| 251 | + | ||
| 252 | + // enumerate current allocations, subtracting resources | ||
| 253 | + double allocatedBw = allocations.stream() | ||
| 254 | + .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 255 | + .filter(x -> x instanceof BandwidthResourceAllocation) | ||
| 256 | + .map(x -> (BandwidthResourceAllocation) x) | ||
| 257 | + .mapToDouble(x -> x.bandwidth().toDouble()) | ||
| 258 | + .sum(); | ||
| 259 | + freeBw -= allocatedBw; | ||
| 260 | + return Sets.newHashSet( | ||
| 261 | + new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))); | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + private Set<ResourceAllocation> getFreeLambdaResources(Link link, Set<ResourceAllocation> lmd, | ||
| 265 | + List<LinkResourceAllocations> allocations) { | ||
| 266 | + if (lmd == null || lmd.isEmpty()) { | ||
| 267 | + // nothing left | ||
| 268 | + return Collections.emptySet(); | ||
| 269 | + } | ||
| 270 | + Set<ResourceAllocation> freeL = lmd.stream() | ||
| 271 | + .filter(x -> x instanceof LambdaResourceAllocation) | ||
| 272 | + .collect(Collectors.toSet()); | ||
| 273 | + | ||
| 274 | + // enumerate current allocations, removing resources | ||
| 275 | + List<ResourceAllocation> allocatedLambda = allocations.stream() | ||
| 276 | + .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 277 | + .filter(x -> x instanceof LambdaResourceAllocation) | ||
| 278 | + .collect(Collectors.toList()); | ||
| 279 | + freeL.removeAll(allocatedLambda); | ||
| 280 | + return freeL; | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + private Set<ResourceAllocation> getFreeLabelResources(Link link, Set<ResourceAllocation> mpls, | ||
| 284 | + List<LinkResourceAllocations> allocations) { | ||
| 285 | + if (mpls == null || mpls.isEmpty()) { | ||
| 286 | + // nothing left | ||
| 287 | + return Collections.emptySet(); | ||
| 288 | + } | ||
| 289 | + Set<ResourceAllocation> freeLabel = mpls.stream() | ||
| 290 | + .filter(x -> x instanceof MplsLabelResourceAllocation) | ||
| 291 | + .collect(Collectors.toSet()); | ||
| 292 | + | ||
| 293 | + // enumerate current allocations, removing resources | ||
| 294 | + List<ResourceAllocation> allocatedLabel = allocations.stream() | ||
| 295 | + .flatMap(x -> x.getResourceAllocation(link).stream()) | ||
| 296 | + .filter(x -> x instanceof MplsLabelResourceAllocation) | ||
| 297 | + .collect(Collectors.toList()); | ||
| 298 | + freeLabel.removeAll(allocatedLabel); | ||
| 299 | + return freeLabel; | ||
| 300 | + } | ||
| 301 | + | ||
| 299 | @Override | 302 | @Override |
| 300 | public void allocateResources(LinkResourceAllocations allocations) { | 303 | public void allocateResources(LinkResourceAllocations allocations) { |
| 301 | checkNotNull(allocations); | 304 | checkNotNull(allocations); | ... | ... |
-
Please register or login to post a comment