InterVpnLinkLocator { public List<Uint64> selectSuitableDpns(InterVpnLink interVpnLink) { int numberOfDpns = Integer.getInteger(NBR_OF_DPNS_PROPERTY_NAME, 1); List<Uint64> dpnIdPool = new ArrayList<>(); try { dpnIdPool = NWUtil.getOperativeDPNs(dataBroker).stream().map(dpn -> dpn).collect(Collectors.toList()); } catch (ExecutionException | InterruptedException e) { LOG.error("selectSuitableDpns: Exception while reading Operative DPNs", e); } LOG.trace("selectSuitableDpns for {} with numberOfDpns={} and availableDpns={}", interVpnLink.getName(), numberOfDpns, dpnIdPool); int poolSize = dpnIdPool.size(); if (poolSize <= numberOfDpns) { return dpnIdPool; } List<InterVpnLinkDataComposite> allInterVpnLinks = interVpnLinkCache.getAllInterVpnLinks(); List<Uint64> dpnsWithNoIVL = findDPNsWithNoInterVpnLink(dpnIdPool, allInterVpnLinks); if (dpnsWithNoIVL.size() >= numberOfDpns) { return dpnsWithNoIVL.subList(0, numberOfDpns); } List<Uint64> result = new ArrayList<>(dpnsWithNoIVL); dpnIdPool.removeAll(result); int pendingDPNs = numberOfDpns - result.size(); List<Uint64> dpnsToAvoid = findDpnsWithSimilarIVpnLinks(interVpnLink, allInterVpnLinks); result.addAll(dpnIdPool.stream().filter(dpId -> !dpnsToAvoid.contains(dpId)) .limit(pendingDPNs).collect(Collectors.toList())); int currentNbrOfItems = result.size(); if (currentNbrOfItems < numberOfDpns) { dpnIdPool.removeAll(result); pendingDPNs = numberOfDpns - currentNbrOfItems; result.addAll(dpnIdPool.subList(0, Math.max(dpnIdPool.size(), pendingDPNs))); } return result; } @Inject  InterVpnLinkLocator(final DataBroker dataBroker, final InterVpnLinkCache interVpnLinkCache,
                               final VpnUtil vpnUtil); List<Uint64> selectSuitableDpns(InterVpnLink interVpnLink); List<InterVpnLinkDataComposite> findInterVpnLinksSameGroup(InterVpnLink ivpnLinkToMatch,
                                                                      List<InterVpnLinkDataComposite> interVpnLinks);  }
@Test public void testSelectSuitableDpns_moreDpnsThanIvpnLinks() throws Exception { stubNwUtilsGetOperativeDpns(5); System.setProperty(InterVpnLinkLocator.NBR_OF_DPNS_PROPERTY_NAME, "1"); List<Uint64> vpnLink12Dpns = sut.selectSuitableDpns(I_VPN_LINK_12.getInterVpnLinkConfig()); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_12, true, vpnLink12Dpns); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_12, false, vpnLink12Dpns); interVpnLinkCache.addInterVpnLinkStateToCaches(I_VPN_LINK_12.getInterVpnLinkState()); List<Uint64> vpnLink34Dpns = sut.selectSuitableDpns(I_VPN_LINK_34.getInterVpnLinkConfig()); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_34, true, vpnLink34Dpns); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_34, false, vpnLink34Dpns); interVpnLinkCache.addInterVpnLinkStateToCaches(I_VPN_LINK_34.getInterVpnLinkState()); List<Uint64> vpnLink56Dpns = sut.selectSuitableDpns(I_VPN_LINK_56.getInterVpnLinkConfig()); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_56, true, vpnLink56Dpns); InterVpnLinkTestCatalog.updateEndpointDpns(I_VPN_LINK_56, false, vpnLink56Dpns); interVpnLinkCache.addInterVpnLinkStateToCaches(I_VPN_LINK_56.getInterVpnLinkState()); assertTrue(vpnLink12Dpns.stream().filter(dpn -> vpnLink34Dpns.contains(dpn)).count() == 0); assertTrue(vpnLink12Dpns.stream().filter(dpn -> vpnLink56Dpns.contains(dpn)).count() == 0); assertTrue(vpnLink34Dpns.stream().filter(dpn -> vpnLink56Dpns.contains(dpn)).count() == 0); }