package org.opendaylight.netvirt.vpnmanager.intervpnlink;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Objects;
import java.util.Set;
+import java.util.concurrent.ExecutionException;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.genius.mdsalutil.NWUtil;
+import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.netvirt.vpnmanager.VpnUtil;
import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnTargets;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpntargets.VpnTarget;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpntargets.VpnTargetKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
+import org.opendaylight.yangtools.yang.common.Uint64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final DataBroker dataBroker;
private final InterVpnLinkCache interVpnLinkCache;
+ private final VpnUtil vpnUtil;
@Inject
- public InterVpnLinkLocator(final DataBroker dataBroker, final InterVpnLinkCache interVpnLinkCache) {
+ public InterVpnLinkLocator(final DataBroker dataBroker, final InterVpnLinkCache interVpnLinkCache,
+ final VpnUtil vpnUtil) {
this.dataBroker = dataBroker;
this.interVpnLinkCache = interVpnLinkCache;
+ this.vpnUtil = vpnUtil;
}
/**
* @param interVpnLink InterVpnLink to find suitable DPNs for.
* @return the list of the selected DPN Ids
*/
- public List<BigInteger> selectSuitableDpns(InterVpnLink interVpnLink) {
+ public List<Uint64> selectSuitableDpns(InterVpnLink interVpnLink) {
int numberOfDpns = Integer.getInteger(NBR_OF_DPNS_PROPERTY_NAME, 1);
- List<BigInteger> dpnIdPool = NWUtil.getOperativeDPNs(dataBroker);
+ 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();
List<InterVpnLinkDataComposite> allInterVpnLinks = interVpnLinkCache.getAllInterVpnLinks();
// 1st criteria is to select those DPNs where there is no InterVpnLink at all
- List<BigInteger> dpnsWithNoIVL = findDPNsWithNoInterVpnLink(dpnIdPool, allInterVpnLinks);
+ List<Uint64> dpnsWithNoIVL = findDPNsWithNoInterVpnLink(dpnIdPool, allInterVpnLinks);
if (dpnsWithNoIVL.size() >= numberOfDpns) {
return dpnsWithNoIVL.subList(0, numberOfDpns); // Best case scenario
}
// Not enough. 2nd criteria is to avoid DPNs where there are InterVpnLinks of the same group
- List<BigInteger> result = new ArrayList<>(dpnsWithNoIVL);
+ List<Uint64> result = new ArrayList<>(dpnsWithNoIVL);
dpnIdPool.removeAll(result);
int pendingDPNs = numberOfDpns - result.size();
- List<BigInteger> dpnsToAvoid = findDpnsWithSimilarIVpnLinks(interVpnLink, allInterVpnLinks);
- result.addAll(dpnIdPool.stream().filter(dpId -> dpnsToAvoid == null || !dpnsToAvoid.contains(dpId))
+ List<Uint64> dpnsToAvoid = findDpnsWithSimilarIVpnLinks(interVpnLink, allInterVpnLinks);
+ result.addAll(dpnIdPool.stream().filter(dpId -> !dpnsToAvoid.contains(dpId))
.limit(pendingDPNs).collect(Collectors.toList()));
int currentNbrOfItems = result.size();
*
* @return the list of available DPNs among the specified ones
*/
- private List<BigInteger> findDPNsWithNoInterVpnLink(List<BigInteger> dpnList,
+ private List<Uint64> findDPNsWithNoInterVpnLink(List<Uint64> dpnList,
List<InterVpnLinkDataComposite> interVpnLinks) {
- List<BigInteger> occupiedDpns = new ArrayList<>();
+ List<Uint64> occupiedDpns = new ArrayList<>();
for (InterVpnLinkDataComposite ivl : interVpnLinks) {
if (ivl.isActive()) {
occupiedDpns.addAll(ivl.getFirstEndpointDpns());
}
}
- List<BigInteger> result = new ArrayList<>(dpnList);
+ List<Uint64> result = new ArrayList<>(dpnList);
result.removeAll(occupiedDpns);
return result;
}
* @return the list of dpnIds where the specified InterVpnLink should not
* be installed
*/
- private List<BigInteger> findDpnsWithSimilarIVpnLinks(InterVpnLink interVpnLink,
+ @NonNull
+ private List<Uint64> findDpnsWithSimilarIVpnLinks(InterVpnLink interVpnLink,
List<InterVpnLinkDataComposite> allInterVpnLinks) {
List<InterVpnLinkDataComposite> sameGroupInterVpnLinks = findInterVpnLinksSameGroup(interVpnLink,
allInterVpnLinks);
- Set<BigInteger> resultDpns = new HashSet<>();
+ Set<Uint64> resultDpns = new HashSet<>();
for (InterVpnLinkDataComposite ivl : sameGroupInterVpnLinks) {
resultDpns.addAll(ivl.getFirstEndpointDpns());
resultDpns.addAll(ivl.getSecondEndpointDpns());
LOG.trace("vpn targets not available for {}", name);
return new ArrayList<>();
}
- List<VpnTarget> vpnTargets = targets.getVpnTarget();
+ Map<VpnTargetKey, VpnTarget> vpnTargets = targets.getVpnTarget();
if (vpnTargets == null) {
LOG.trace("vpnTarget values not available for {}", name);
return new ArrayList<>();
}
- return vpnTargets.stream()
- .filter(target -> target.getVrfRTType().equals(rtType)
- || target.getVrfRTType().equals(VpnTarget.VrfRTType.Both))
+ return vpnTargets.values().stream()
+ .filter(target -> Objects.equals(target.getVrfRTType(), rtType)
+ || Objects.equals(target.getVrfRTType(), VpnTarget.VrfRTType.Both))
.map(VpnTarget::getVrfRTValue)
.collect(Collectors.toList());
}
private List<String> getIRTsByVpnName(String vpnName) {
- String vpn1Rd = VpnUtil.getVpnRd(dataBroker, vpnName);
- final VpnInstanceOpDataEntry vpnInstance = VpnUtil.getVpnInstanceOpData(dataBroker, vpn1Rd);
+ String vpn1Rd = vpnUtil.getVpnRd(vpnName);
+ final VpnInstanceOpDataEntry vpnInstance = vpnUtil.getVpnInstanceOpData(vpn1Rd);
return getRts(vpnInstance, VpnTarget.VrfRTType.ImportExtcommunity);
}
if (irts1 == null && irts2 == null) {
return true;
}
- if (irts1 == null && irts2 != null || irts1 != null && irts2 == null) {
+ if (irts1 == null || irts2 == null) {
return false;
}
if (ivl.getInterVpnLinkName().equals(ivpnLinkToMatch.getName())) {
return false; // ivl and ivpnLinlToMatch are the same InterVpnLink
}
- String vpn1Name = ivl.getFirstEndpointVpnUuid().orNull();
- String vpn2Name = ivl.getSecondEndpointVpnUuid().orNull();
+ String vpn1Name = ivl.getFirstEndpointVpnUuid().orElse(null);
+ String vpn2Name = ivl.getSecondEndpointVpnUuid().orElse(null);
if (vpn1Name == null) {
return false;
}