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 ...@@ -223,14 +223,25 @@ 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);
231 +
232 + Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
233 + Set<ResourceAllocation> freeL = getFreeLambdaResources(link, lmd, allocations);
234 + free.put(ResourceType.LAMBDA, freeL);
230 235
231 - switch (type) { 236 + Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
232 - case BANDWIDTH: 237 + Set<ResourceAllocation> freeLabel = getFreeLabelResources(link, mpls, allocations);
233 - Set<ResourceAllocation> bw = caps.get(type); 238 + free.put(ResourceType.MPLS_LABEL, freeLabel);
239 +
240 + return free;
241 + }
242 +
243 + private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw,
244 + List<LinkResourceAllocations> allocations) {
234 if (bw == null || bw.isEmpty()) { 245 if (bw == null || bw.isEmpty()) {
235 bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); 246 bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
236 } 247 }
...@@ -239,61 +250,53 @@ public class ConsistentLinkResourceStore extends ...@@ -239,61 +250,53 @@ public class ConsistentLinkResourceStore extends
239 double freeBw = cap.bandwidth().toDouble(); 250 double freeBw = cap.bandwidth().toDouble();
240 251
241 // enumerate current allocations, subtracting resources 252 // enumerate current allocations, subtracting resources
242 - double allocatedBw = ImmutableList.copyOf(allocations).stream() 253 + double allocatedBw = allocations.stream()
243 .flatMap(x -> x.getResourceAllocation(link).stream()) 254 .flatMap(x -> x.getResourceAllocation(link).stream())
244 .filter(x -> x instanceof BandwidthResourceAllocation) 255 .filter(x -> x instanceof BandwidthResourceAllocation)
245 .map(x -> (BandwidthResourceAllocation) x) 256 .map(x -> (BandwidthResourceAllocation) x)
246 .mapToDouble(x -> x.bandwidth().toDouble()) 257 .mapToDouble(x -> x.bandwidth().toDouble())
247 .sum(); 258 .sum();
248 freeBw -= allocatedBw; 259 freeBw -= allocatedBw;
260 + return Sets.newHashSet(
261 + new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
262 + }
249 263
250 - free.put(type, Sets.newHashSet( 264 + private Set<ResourceAllocation> getFreeLambdaResources(Link link, Set<ResourceAllocation> lmd,
251 - new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))))); 265 + List<LinkResourceAllocations> allocations) {
252 - break;
253 - case LAMBDA:
254 - Set<ResourceAllocation> lmd = caps.get(type);
255 if (lmd == null || lmd.isEmpty()) { 266 if (lmd == null || lmd.isEmpty()) {
256 // nothing left 267 // nothing left
257 - break; 268 + return Collections.emptySet();
258 } 269 }
259 Set<ResourceAllocation> freeL = lmd.stream() 270 Set<ResourceAllocation> freeL = lmd.stream()
260 .filter(x -> x instanceof LambdaResourceAllocation) 271 .filter(x -> x instanceof LambdaResourceAllocation)
261 .collect(Collectors.toSet()); 272 .collect(Collectors.toSet());
262 273
263 // enumerate current allocations, removing resources 274 // enumerate current allocations, removing resources
264 - List<ResourceAllocation> allocatedLambda = ImmutableList.copyOf(allocations).stream() 275 + List<ResourceAllocation> allocatedLambda = allocations.stream()
265 .flatMap(x -> x.getResourceAllocation(link).stream()) 276 .flatMap(x -> x.getResourceAllocation(link).stream())
266 .filter(x -> x instanceof LambdaResourceAllocation) 277 .filter(x -> x instanceof LambdaResourceAllocation)
267 .collect(Collectors.toList()); 278 .collect(Collectors.toList());
268 freeL.removeAll(allocatedLambda); 279 freeL.removeAll(allocatedLambda);
280 + return freeL;
281 + }
269 282
270 - free.put(type, freeL); 283 + private Set<ResourceAllocation> getFreeLabelResources(Link link, Set<ResourceAllocation> mpls,
271 - break; 284 + List<LinkResourceAllocations> allocations) {
272 - case MPLS_LABEL:
273 - Set<ResourceAllocation> mpls = caps.get(type);
274 if (mpls == null || mpls.isEmpty()) { 285 if (mpls == null || mpls.isEmpty()) {
275 // nothing left 286 // nothing left
276 - break; 287 + return Collections.emptySet();
277 } 288 }
278 Set<ResourceAllocation> freeLabel = mpls.stream() 289 Set<ResourceAllocation> freeLabel = mpls.stream()
279 .filter(x -> x instanceof MplsLabelResourceAllocation) 290 .filter(x -> x instanceof MplsLabelResourceAllocation)
280 .collect(Collectors.toSet()); 291 .collect(Collectors.toSet());
281 292
282 // enumerate current allocations, removing resources 293 // enumerate current allocations, removing resources
283 - List<ResourceAllocation> allocatedLabel = ImmutableList.copyOf(allocations).stream() 294 + List<ResourceAllocation> allocatedLabel = allocations.stream()
284 .flatMap(x -> x.getResourceAllocation(link).stream()) 295 .flatMap(x -> x.getResourceAllocation(link).stream())
285 .filter(x -> x instanceof MplsLabelResourceAllocation) 296 .filter(x -> x instanceof MplsLabelResourceAllocation)
286 .collect(Collectors.toList()); 297 .collect(Collectors.toList());
287 freeLabel.removeAll(allocatedLabel); 298 freeLabel.removeAll(allocatedLabel);
288 - 299 + return freeLabel;
289 - free.put(type, freeLabel);
290 - break;
291 - default:
292 - log.debug("unsupported ResourceType {}", type);
293 - break;
294 - }
295 - }
296 - return free;
297 } 300 }
298 301
299 @Override 302 @Override
......