use of org.opendaylight.infrautils.utils.concurrent.NamedLocks in project netvirt by opendaylight.
the class NeutronBgpvpnChangeListener method add.
@Override
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
public void add(InstanceIdentifier<Bgpvpn> identifier, Bgpvpn input) {
LOG.trace("Adding Bgpvpn : key: {}, value={}", identifier, input);
String vpnName = input.getUuid().getValue();
if (!isBgpvpnTypeL3(input.getType())) {
LOG.warn("BGPVPN type for VPN {} is not L3", vpnName);
return;
}
NamedLocks<String> vpnLock = neutronBgpvpnUtils.getVpnLock();
try (AcquireResult lock = vpnLock.tryAcquire(vpnName, NeutronConstants.LOCK_WAIT_TIME, TimeUnit.SECONDS)) {
if (!lock.wasAcquired()) {
LOG.error("Add BGPVPN: add bgpvpn failed for vpn : {} due to failure in acquiring lock", vpnName);
return;
}
// handle route-target(s)
List<String> inputRouteList = input.getRouteTargets();
List<String> inputImportRouteList = input.getImportTargets();
List<String> inputExportRouteList = input.getExportTargets();
Set<String> inputImportRouteSet = new HashSet<>();
Set<String> inputExportRouteSet = new HashSet<>();
if (inputRouteList != null && !inputRouteList.isEmpty()) {
inputImportRouteSet.addAll(inputRouteList);
inputExportRouteSet.addAll(inputRouteList);
}
if (inputImportRouteList != null && !inputImportRouteList.isEmpty()) {
inputImportRouteSet.addAll(inputImportRouteList);
}
if (inputExportRouteList != null && !inputExportRouteList.isEmpty()) {
inputExportRouteSet.addAll(inputExportRouteList);
}
List<String> importRouteTargets = new ArrayList<>(inputImportRouteSet);
List<String> exportRouteTargets = new ArrayList<>(inputExportRouteSet);
boolean rdIrtErtStringsValid;
List<String> rdList = input.getRouteDistinguishers();
if (rdList != null && !rdList.isEmpty()) {
// get the primary RD for vpn instance, if exist
rdIrtErtStringsValid = !(input.getRouteDistinguishers().stream().anyMatch(rdStr -> rdStr.contains(" ")));
rdIrtErtStringsValid = rdIrtErtStringsValid && !(importRouteTargets.stream().anyMatch(irtStr -> irtStr.contains(" ")));
rdIrtErtStringsValid = rdIrtErtStringsValid && !(exportRouteTargets.stream().anyMatch(ertStr -> ertStr.contains(" ")));
if (!rdIrtErtStringsValid) {
LOG.error("Error encountered for BGPVPN {} with RD {} as RD/iRT/eRT contains whitespace " + "characters", vpnName, input.getRouteDistinguishers());
return;
}
String primaryRd = neutronvpnUtils.getVpnRd(vpnName);
if (primaryRd == null) {
primaryRd = rdList.get(0);
}
String[] rdParams = primaryRd.split(":");
if (rdParams[0].trim().equals(adminRDValue)) {
LOG.error("AS specific part of RD should not be same as that defined by DC Admin. Error " + "encountered for BGPVPN {} with RD {}", vpnName, primaryRd);
return;
}
String vpnWithSameRd = neutronvpnUtils.getVpnForRD(primaryRd);
if (vpnWithSameRd != null) {
LOG.error("Creation of L3VPN failed for VPN {} as another VPN {} with the same RD {} " + "is already configured", vpnName, vpnWithSameRd, primaryRd);
return;
}
String existingOperationalVpn = neutronvpnUtils.getExistingOperationalVpn(primaryRd);
if (existingOperationalVpn != null) {
LOG.error("checkVpnCreation: Creation of L3VPN failed for VPN {} as another VPN {} with the " + "same RD {} is still available.", vpnName, existingOperationalVpn, primaryRd);
return;
}
List<Uuid> unpRtrs = neutronBgpvpnUtils.getUnprocessedRoutersForBgpvpn(input.getUuid());
List<Uuid> unpNets = neutronBgpvpnUtils.getUnprocessedNetworksForBgpvpn(input.getUuid());
// TODO: Currently handling routers and networks for backward compatibility. Below logic needs to be
// removed once updated to latest BGPVPN API's.
List<Uuid> inputRouters = input.getRouters();
if (inputRouters != null && !inputRouters.isEmpty()) {
if (unpRtrs != null) {
unpRtrs.addAll(inputRouters);
} else {
unpRtrs = new ArrayList<>(inputRouters);
}
}
if (unpRtrs != null && unpRtrs.size() > NeutronConstants.MAX_ROUTERS_PER_BGPVPN) {
LOG.error("Creation of BGPVPN for rd {} failed: maximum allowed number of associated " + "routers is {}.", rdList, NeutronConstants.MAX_ROUTERS_PER_BGPVPN);
return;
}
List<Uuid> inputNetworks = input.getNetworks();
if (inputNetworks != null && !inputNetworks.isEmpty()) {
if (unpNets != null) {
unpNets.addAll(inputNetworks);
} else {
unpNets = new ArrayList<>(inputNetworks);
}
}
try {
nvpnManager.createVpn(input.getUuid(), input.getName(), input.getTenantId(), rdList, importRouteTargets, exportRouteTargets, unpRtrs, unpNets, false, /* isL2Vpn */
0);
neutronBgpvpnUtils.getUnProcessedRoutersMap().remove(input.getUuid());
neutronBgpvpnUtils.getUnProcessedNetworksMap().remove(input.getUuid());
} catch (Exception e) {
LOG.error("Creation of BGPVPN {} failed with error ", vpnName, e);
}
} else {
LOG.error("add: RD is absent for BGPVPN {}", vpnName);
}
}
}
Aggregations