Sho SHIMIZU

Reduce duplicated code

Change-Id: Iac8f2c0ec7336d936425701976e090b413186d8d
...@@ -230,11 +230,13 @@ public class ConsistentLinkResourceStore extends ...@@ -230,11 +230,13 @@ public class ConsistentLinkResourceStore extends
230 free.put(ResourceType.BANDWIDTH, value); 230 free.put(ResourceType.BANDWIDTH, value);
231 231
232 Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA); 232 Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
233 - Set<ResourceAllocation> freeL = getFreeLambdaResources(link, lmd, allocations); 233 + Set<ResourceAllocation> freeL = getFreeResources(link, lmd, allocations,
234 + LambdaResourceAllocation.class);
234 free.put(ResourceType.LAMBDA, freeL); 235 free.put(ResourceType.LAMBDA, freeL);
235 236
236 Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL); 237 Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
237 - Set<ResourceAllocation> freeLabel = getFreeLabelResources(link, mpls, allocations); 238 + Set<ResourceAllocation> freeLabel = getFreeResources(link, mpls, allocations,
239 + MplsLabelResourceAllocation.class);
238 free.put(ResourceType.MPLS_LABEL, freeLabel); 240 free.put(ResourceType.MPLS_LABEL, freeLabel);
239 241
240 return free; 242 return free;
...@@ -261,44 +263,27 @@ public class ConsistentLinkResourceStore extends ...@@ -261,44 +263,27 @@ public class ConsistentLinkResourceStore extends
261 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))); 263 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
262 } 264 }
263 265
264 - private Set<ResourceAllocation> getFreeLambdaResources(Link link, Set<ResourceAllocation> lmd, 266 + private Set<ResourceAllocation> getFreeResources(Link link,
265 - List<LinkResourceAllocations> allocations) { 267 + Set<ResourceAllocation> resources,
266 - if (lmd == null || lmd.isEmpty()) { 268 + List<LinkResourceAllocations> allocations,
269 + Class<? extends ResourceAllocation> cls) {
270 + if (resources == null || resources.isEmpty()) {
267 // nothing left 271 // nothing left
268 return Collections.emptySet(); 272 return Collections.emptySet();
269 } 273 }
270 - Set<ResourceAllocation> freeL = lmd.stream() 274 + Set<ResourceAllocation> freeL = resources.stream()
271 - .filter(x -> x instanceof LambdaResourceAllocation) 275 + .filter(cls::isInstance)
272 .collect(Collectors.toSet()); 276 .collect(Collectors.toSet());
273 277
274 // enumerate current allocations, removing resources 278 // enumerate current allocations, removing resources
275 - List<ResourceAllocation> allocatedLambda = allocations.stream() 279 + List<ResourceAllocation> allocated = allocations.stream()
276 .flatMap(x -> x.getResourceAllocation(link).stream()) 280 .flatMap(x -> x.getResourceAllocation(link).stream())
277 - .filter(x -> x instanceof LambdaResourceAllocation) 281 + .filter(cls::isInstance)
278 .collect(Collectors.toList()); 282 .collect(Collectors.toList());
279 - freeL.removeAll(allocatedLambda); 283 + freeL.removeAll(allocated);
280 return freeL; 284 return freeL;
281 } 285 }
282 286
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 -
302 @Override 287 @Override
303 public void allocateResources(LinkResourceAllocations allocations) { 288 public void allocateResources(LinkResourceAllocations allocations) {
304 checkNotNull(allocations); 289 checkNotNull(allocations);
......