Sho SHIMIZU
Committed by Ray Milkey

Refactor: Reduce depth of indent

Inline enumerating over enum values

Change-Id: Icff35340d91490b67b05d521f7addc6519d2b47a
......@@ -223,14 +223,25 @@ public class ConsistentLinkResourceStore extends
Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>();
final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link);
final Iterable<LinkResourceAllocations> allocations = getAllocations(tx, link);
final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link));
for (ResourceType type : ResourceType.values()) {
// there should be class/category of resources
Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH);
Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations);
free.put(ResourceType.BANDWIDTH, value);
Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
Set<ResourceAllocation> freeL = getFreeLambdaResources(link, lmd, allocations);
free.put(ResourceType.LAMBDA, freeL);
switch (type) {
case BANDWIDTH:
Set<ResourceAllocation> bw = caps.get(type);
Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
Set<ResourceAllocation> freeLabel = getFreeLabelResources(link, mpls, allocations);
free.put(ResourceType.MPLS_LABEL, freeLabel);
return free;
}
private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw,
List<LinkResourceAllocations> allocations) {
if (bw == null || bw.isEmpty()) {
bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
}
......@@ -239,61 +250,53 @@ public class ConsistentLinkResourceStore extends
double freeBw = cap.bandwidth().toDouble();
// enumerate current allocations, subtracting resources
double allocatedBw = ImmutableList.copyOf(allocations).stream()
double allocatedBw = allocations.stream()
.flatMap(x -> x.getResourceAllocation(link).stream())
.filter(x -> x instanceof BandwidthResourceAllocation)
.map(x -> (BandwidthResourceAllocation) x)
.mapToDouble(x -> x.bandwidth().toDouble())
.sum();
freeBw -= allocatedBw;
return Sets.newHashSet(
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
}
free.put(type, Sets.newHashSet(
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))));
break;
case LAMBDA:
Set<ResourceAllocation> lmd = caps.get(type);
private Set<ResourceAllocation> getFreeLambdaResources(Link link, Set<ResourceAllocation> lmd,
List<LinkResourceAllocations> allocations) {
if (lmd == null || lmd.isEmpty()) {
// nothing left
break;
return Collections.emptySet();
}
Set<ResourceAllocation> freeL = lmd.stream()
.filter(x -> x instanceof LambdaResourceAllocation)
.collect(Collectors.toSet());
// enumerate current allocations, removing resources
List<ResourceAllocation> allocatedLambda = ImmutableList.copyOf(allocations).stream()
List<ResourceAllocation> allocatedLambda = allocations.stream()
.flatMap(x -> x.getResourceAllocation(link).stream())
.filter(x -> x instanceof LambdaResourceAllocation)
.collect(Collectors.toList());
freeL.removeAll(allocatedLambda);
return freeL;
}
free.put(type, freeL);
break;
case MPLS_LABEL:
Set<ResourceAllocation> mpls = caps.get(type);
private Set<ResourceAllocation> getFreeLabelResources(Link link, Set<ResourceAllocation> mpls,
List<LinkResourceAllocations> allocations) {
if (mpls == null || mpls.isEmpty()) {
// nothing left
break;
return Collections.emptySet();
}
Set<ResourceAllocation> freeLabel = mpls.stream()
.filter(x -> x instanceof MplsLabelResourceAllocation)
.collect(Collectors.toSet());
// enumerate current allocations, removing resources
List<ResourceAllocation> allocatedLabel = ImmutableList.copyOf(allocations).stream()
List<ResourceAllocation> allocatedLabel = allocations.stream()
.flatMap(x -> x.getResourceAllocation(link).stream())
.filter(x -> x instanceof MplsLabelResourceAllocation)
.collect(Collectors.toList());
freeLabel.removeAll(allocatedLabel);
free.put(type, freeLabel);
break;
default:
log.debug("unsupported ResourceType {}", type);
break;
}
}
return free;
return freeLabel;
}
@Override
......