2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.vpnmanager.intervpnlink;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Objects;
17 import java.util.function.Predicate;
18 import java.util.stream.Collectors;
19 import javax.annotation.Nonnull;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.genius.mdsalutil.NWUtil;
24 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
25 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
26 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnTargets;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpntargets.VpnTarget;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class is responsible for searching the best possible DPN(s) to place
40 public class InterVpnLinkLocator {
42 private static final Logger LOG = LoggerFactory.getLogger(InterVpnLinkLocator.class);
43 protected static final String NBR_OF_DPNS_PROPERTY_NAME = "vpnservice.intervpnlink.number.dpns";
45 private final DataBroker dataBroker;
46 private final InterVpnLinkCache interVpnLinkCache;
47 private final VpnUtil vpnUtil;
50 public InterVpnLinkLocator(final DataBroker dataBroker, final InterVpnLinkCache interVpnLinkCache,
51 final VpnUtil vpnUtil) {
52 this.dataBroker = dataBroker;
53 this.interVpnLinkCache = interVpnLinkCache;
54 this.vpnUtil = vpnUtil;
58 * Retrieves a list of randomly selected DPNs avoiding to select DPNs
59 * where there is already an InterVpnLink of the same group (i.e., an
60 * InterVpnLink that links similar L3VPNs).
62 * @param interVpnLink InterVpnLink to find suitable DPNs for.
63 * @return the list of the selected DPN Ids
65 public List<BigInteger> selectSuitableDpns(InterVpnLink interVpnLink) {
66 int numberOfDpns = Integer.getInteger(NBR_OF_DPNS_PROPERTY_NAME, 1);
67 List<BigInteger> dpnIdPool = NWUtil.getOperativeDPNs(dataBroker);
68 LOG.trace("selectSuitableDpns for {} with numberOfDpns={} and availableDpns={}",
69 interVpnLink.getName(), numberOfDpns, dpnIdPool);
70 int poolSize = dpnIdPool.size();
71 if (poolSize <= numberOfDpns) {
72 // You requested more than there is, I give you all I have.
76 List<InterVpnLinkDataComposite> allInterVpnLinks = interVpnLinkCache.getAllInterVpnLinks();
78 // 1st criteria is to select those DPNs where there is no InterVpnLink at all
79 List<BigInteger> dpnsWithNoIVL = findDPNsWithNoInterVpnLink(dpnIdPool, allInterVpnLinks);
80 if (dpnsWithNoIVL.size() >= numberOfDpns) {
81 return dpnsWithNoIVL.subList(0, numberOfDpns); // Best case scenario
84 // Not enough. 2nd criteria is to avoid DPNs where there are InterVpnLinks of the same group
85 List<BigInteger> result = new ArrayList<>(dpnsWithNoIVL);
86 dpnIdPool.removeAll(result);
87 int pendingDPNs = numberOfDpns - result.size();
89 List<BigInteger> dpnsToAvoid = findDpnsWithSimilarIVpnLinks(interVpnLink, allInterVpnLinks);
90 result.addAll(dpnIdPool.stream().filter(dpId -> !dpnsToAvoid.contains(dpId))
91 .limit(pendingDPNs).collect(Collectors.toList()));
93 int currentNbrOfItems = result.size();
94 if (currentNbrOfItems < numberOfDpns) {
95 // Still not enough. 3rd criteria: whatever is available
96 dpnIdPool.removeAll(result);
97 pendingDPNs = numberOfDpns - currentNbrOfItems;
98 result.addAll(dpnIdPool.subList(0, Math.max(dpnIdPool.size(), pendingDPNs)));
105 * Given a list of Dpn Ids and a list of InterVpnLinks, this method finds
106 * the DPNs in the first list where no InterVpnLink is instantiated there.
108 * @param dpnList A list of DPN IDs
109 * @param interVpnLinks A List of InterVpnLinks to avoid
111 * @return the list of available DPNs among the specified ones
113 private List<BigInteger> findDPNsWithNoInterVpnLink(List<BigInteger> dpnList,
114 List<InterVpnLinkDataComposite> interVpnLinks) {
115 List<BigInteger> occupiedDpns = new ArrayList<>();
116 for (InterVpnLinkDataComposite ivl : interVpnLinks) {
117 if (ivl.isActive()) {
118 occupiedDpns.addAll(ivl.getFirstEndpointDpns());
119 occupiedDpns.addAll(ivl.getSecondEndpointDpns());
123 List<BigInteger> result = new ArrayList<>(dpnList);
124 result.removeAll(occupiedDpns);
129 * Given an InterVpnLink, this method finds those DPNs where there is an
130 * InterVpnLink of the same group. Two InterVpnLinks are in the same group
131 * if they link 2 L3VPNs that are from the same group, and 2 L3VPNs are in
132 * the same group if their iRTs match.
134 * @param interVpnLink InterVpnLink to be checked
135 * @return the list of dpnIds where the specified InterVpnLink should not
139 private List<BigInteger> findDpnsWithSimilarIVpnLinks(InterVpnLink interVpnLink,
140 List<InterVpnLinkDataComposite> allInterVpnLinks) {
141 List<InterVpnLinkDataComposite> sameGroupInterVpnLinks = findInterVpnLinksSameGroup(interVpnLink,
143 Set<BigInteger> resultDpns = new HashSet<>();
144 for (InterVpnLinkDataComposite ivl : sameGroupInterVpnLinks) {
145 resultDpns.addAll(ivl.getFirstEndpointDpns());
146 resultDpns.addAll(ivl.getSecondEndpointDpns());
148 return new ArrayList<>(resultDpns);
151 private List<String> getRts(VpnInstanceOpDataEntry vpnInstance, VpnTarget.VrfRTType rtType) {
152 String name = vpnInstance.getVpnInstanceName();
153 VpnTargets targets = vpnInstance.getVpnTargets();
154 if (targets == null) {
155 LOG.trace("vpn targets not available for {}", name);
156 return new ArrayList<>();
158 List<VpnTarget> vpnTargets = targets.getVpnTarget();
159 if (vpnTargets == null) {
160 LOG.trace("vpnTarget values not available for {}", name);
161 return new ArrayList<>();
163 return vpnTargets.stream()
164 .filter(target -> Objects.equals(target.getVrfRTType(), rtType)
165 || Objects.equals(target.getVrfRTType(), VpnTarget.VrfRTType.Both))
166 .map(VpnTarget::getVrfRTValue)
167 .collect(Collectors.toList());
170 private List<String> getIRTsByVpnName(String vpnName) {
171 String vpn1Rd = vpnUtil.getVpnRd(vpnName);
172 final VpnInstanceOpDataEntry vpnInstance = vpnUtil.getVpnInstanceOpData(vpn1Rd);
173 return getRts(vpnInstance, VpnTarget.VrfRTType.ImportExtcommunity);
176 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_MIGHT_BE_INFEASIBLE")
177 private boolean haveSameIRTs(List<String> irts1, List<String> irts2) {
178 if (irts1 == null && irts2 == null) {
181 if (irts1 == null || irts2 == null) {
185 // FindBugs reports "Possible null pointer dereference of irts1 on branch that might be infeasible" but irts1
186 // can't be null here.
187 if (irts1.size() != irts2.size()) {
190 irts1.sort(/*comparator*/ null);
191 irts2.sort(/*comparator*/ null);
192 return irts1.equals(irts2);
195 public List<InterVpnLinkDataComposite> findInterVpnLinksSameGroup(InterVpnLink ivpnLinkToMatch,
196 List<InterVpnLinkDataComposite> interVpnLinks) {
198 List<String> vpnToMatch1IRTs = getIRTsByVpnName(ivpnLinkToMatch.getFirstEndpoint().getVpnUuid().getValue());
199 List<String> vpnToMatch2IRTs = getIRTsByVpnName(ivpnLinkToMatch.getSecondEndpoint().getVpnUuid().getValue());
201 Predicate<InterVpnLinkDataComposite> areSameGroup = (ivl) -> {
202 if (ivl.getInterVpnLinkName().equals(ivpnLinkToMatch.getName())) {
203 return false; // ivl and ivpnLinlToMatch are the same InterVpnLink
205 String vpn1Name = ivl.getFirstEndpointVpnUuid().orNull();
206 String vpn2Name = ivl.getSecondEndpointVpnUuid().orNull();
207 if (vpn1Name == null) {
210 List<String> vpn1IRTs = getIRTsByVpnName(vpn1Name);
211 List<String> vpn2IRTs = getIRTsByVpnName(vpn2Name);
212 return haveSameIRTs(vpn1IRTs, vpnToMatch1IRTs) && haveSameIRTs(vpn2IRTs, vpnToMatch2IRTs)
213 || haveSameIRTs(vpn1IRTs, vpnToMatch2IRTs) && haveSameIRTs(vpn2IRTs, vpnToMatch1IRTs);
216 return interVpnLinks.stream().filter(areSameGroup).collect(Collectors.toList());