/* * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netvirt.neutronvpn; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.inject.Singleton; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase; import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronConstants; import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.bgpvpns.rev150903.BgpvpnTypeBase; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.bgpvpns.rev150903.BgpvpnTypeL3; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.bgpvpns.rev150903.bgpvpns.attributes.Bgpvpns; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.bgpvpns.rev150903.bgpvpns.attributes.bgpvpns.Bgpvpn; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResult; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Singleton public class NeutronBgpvpnChangeListener extends AsyncDataTreeChangeListenerBase { private static final Logger LOG = LoggerFactory.getLogger(NeutronBgpvpnChangeListener.class); private final DataBroker dataBroker; private final NeutronvpnManager nvpnManager; private final IdManagerService idManager; private final NeutronvpnUtils neutronvpnUtils; private final String adminRDValue; @Inject public NeutronBgpvpnChangeListener(final DataBroker dataBroker, final NeutronvpnManager neutronvpnManager, final IdManagerService idManager, final NeutronvpnUtils neutronvpnUtils) { super(Bgpvpn.class, NeutronBgpvpnChangeListener.class); this.dataBroker = dataBroker; nvpnManager = neutronvpnManager; this.idManager = idManager; this.neutronvpnUtils = neutronvpnUtils; BundleContext bundleContext = FrameworkUtil.getBundle(NeutronBgpvpnChangeListener.class).getBundleContext(); adminRDValue = bundleContext.getProperty(NeutronConstants.RD_PROPERTY_KEY); } @Override @PostConstruct public void init() { LOG.info("{} init", getClass().getSimpleName()); createIdPool(); registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker); } @Override protected InstanceIdentifier getWildCardPath() { return InstanceIdentifier.create(Neutron.class).child(Bgpvpns.class).child(Bgpvpn.class); } @Override protected NeutronBgpvpnChangeListener getDataTreeChangeListener() { return NeutronBgpvpnChangeListener.this; } private boolean isBgpvpnTypeL3(Class bgpvpnType) { if (BgpvpnTypeL3.class.equals(bgpvpnType)) { return true; } else { LOG.warn("CRUD operations supported only for L3 type Bgpvpn"); return false; } } @Override // TODO Clean up the exception handling @SuppressWarnings("checkstyle:IllegalCatch") protected void add(InstanceIdentifier identifier, Bgpvpn input) { LOG.trace("Adding Bgpvpn : key: {}, value={}", identifier, input); String vpnName = input.getUuid().getValue(); if (isBgpvpnTypeL3(input.getType())) { VpnInstance.Type vpnInstanceType = VpnInstance.Type.L3; // handle route-target(s) List inputRouteList = input.getRouteTargets(); List inputImportRouteList = input.getImportTargets(); List inputExportRouteList = input.getExportTargets(); Set inputImportRouteSet = new HashSet<>(); Set 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 importRouteTargets = new ArrayList<>(); List exportRouteTargets = new ArrayList<>(); importRouteTargets.addAll(inputImportRouteSet); exportRouteTargets.addAll(inputExportRouteSet); List rd = input.getRouteDistinguishers(); if (rd == null || rd.isEmpty()) { // generate new RD // TODO - commented out for now to avoid "Dead store to rd" violation. //rd = generateNewRD(input.getUuid()); } else { String[] rdParams = rd.get(0).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, rd.get(0)); return; } List existingRDs = neutronvpnUtils.getExistingRDs(); if (!Collections.disjoint(existingRDs, rd)) { LOG.error("Failed to create VPN {} as another VPN with the same RD {} already exists.", vpnName, rd); return; } Uuid router = null; if (input.getRouters() != null && !input.getRouters().isEmpty()) { // currently only one router router = input.getRouters().get(0); } if (!rd.isEmpty()) { try { nvpnManager.createVpn(input.getUuid(), input.getName(), input.getTenantId(), rd, importRouteTargets, exportRouteTargets, router, input.getNetworks(), vpnInstanceType, 0 /*l3vni*/); } catch (Exception e) { LOG.error("Creation of BGPVPN {} failed", vpnName, e); } } else { LOG.error("Create BgpVPN with id {} failed due to missing RD value", vpnName); } } } else { LOG.warn("BGPVPN type for VPN {} is not L3", vpnName); } } @Override protected void remove(InstanceIdentifier identifier, Bgpvpn input) { LOG.trace("Removing Bgpvpn : key: {}, value={}", identifier, input); if (isBgpvpnTypeL3(input.getType())) { nvpnManager.removeVpn(input.getUuid()); // Release RD Id in pool neutronvpnUtils.releaseRDId(NeutronConstants.RD_IDPOOL_NAME, input.getUuid().toString()); } else { LOG.warn("BGPVPN type for VPN {} is not L3", input.getUuid().getValue()); } } @Override protected void update(InstanceIdentifier identifier, Bgpvpn original, Bgpvpn update) { LOG.trace("Update Bgpvpn : key: {}, value={}", identifier, update); Uuid vpnId = update.getUuid(); if (isBgpvpnTypeL3(update.getType())) { try { handleVpnInstanceUpdate(original.getUuid().getValue(), original.getRouteDistinguishers(), update.getRouteDistinguishers()); } catch (UnsupportedOperationException e) { LOG.error("Error while processing Update Bgpvpn.", e); return; } List oldNetworks = original.getNetworks(); List newNetworks = update.getNetworks(); handleNetworksUpdate(vpnId, oldNetworks, newNetworks); List oldRouters = original.getRouters(); List newRouters = update.getRouters(); handleRoutersUpdate(vpnId, oldRouters, newRouters); } else { LOG.warn("BGPVPN type for VPN {} is not L3", vpnId.getValue()); } } protected void handleVpnInstanceUpdate(String vpnInstanceName,final List originalRds, List updateRDs) throws UnsupportedOperationException { if (updateRDs == null || updateRDs.isEmpty()) { return; } int oldRdsCount = originalRds.size(); for (String rd : originalRds) { //If the existing rd is not present in the updateRds list, not allow to process the updateRDs. if (!updateRDs.contains(rd)) { LOG.error("The existing RD {} not present in the updatedRDsList:{}", rd, updateRDs); throw new UnsupportedOperationException("The existing RD not present in the updatedRDsList"); } } if (updateRDs.size() == oldRdsCount) { LOG.debug("There is no update in the List of Route Distinguisher for the VpnInstance:{}", vpnInstanceName); return; } LOG.debug("update the VpnInstance:{} with the List of RDs: {}", vpnInstanceName, updateRDs); nvpnManager.updateVpnInstanceWithRDs(vpnInstanceName, updateRDs); } protected void handleNetworksUpdate(Uuid vpnId, List oldNetworks, List newNetworks) { if (newNetworks != null && !newNetworks.isEmpty()) { if (oldNetworks != null && !oldNetworks.isEmpty()) { if (oldNetworks != newNetworks) { Iterator iter = newNetworks.iterator(); while (iter.hasNext()) { Uuid net = iter.next(); if (oldNetworks.contains(net)) { oldNetworks.remove(net); iter.remove(); } } //clear removed networks if (!oldNetworks.isEmpty()) { LOG.trace("Removing old networks {} ", oldNetworks); nvpnManager.dissociateNetworksFromVpn(vpnId, oldNetworks); } //add new (Delta) Networks if (!newNetworks.isEmpty()) { LOG.trace("Adding delta New networks {} ", newNetworks); nvpnManager.associateNetworksToVpn(vpnId, newNetworks); } } } else { //add new Networks LOG.trace("Adding New networks {} ", newNetworks); nvpnManager.associateNetworksToVpn(vpnId, newNetworks); } } else if (oldNetworks != null && !oldNetworks.isEmpty()) { LOG.trace("Removing old networks {} ", oldNetworks); nvpnManager.dissociateNetworksFromVpn(vpnId, oldNetworks); } } protected void handleRoutersUpdate(Uuid vpnId, List oldRouters, List newRouters) { if (newRouters != null && !newRouters.isEmpty()) { if (oldRouters != null && !oldRouters.isEmpty()) { if (oldRouters.size() > 1 || newRouters.size() > 1) { VpnMap vpnMap = neutronvpnUtils.getVpnMap(vpnId); if (vpnMap != null && vpnMap.getRouterId() != null) { LOG.warn("Only single router association to a given bgpvpn is allowed. Kindly disassociate " + "router {} from vpn {} before proceeding with associate", vpnMap.getRouterId().getValue(), vpnId.getValue()); } } } else if (validateRouteInfo(newRouters.get(0))) { nvpnManager.associateRouterToVpn(vpnId, newRouters.get(0)); } } else if (oldRouters != null && !oldRouters.isEmpty()) { /* dissociate old router */ Uuid oldRouter = oldRouters.get(0); nvpnManager.dissociateRouterFromVpn(vpnId, oldRouter); } } private void createIdPool() { CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(NeutronConstants.RD_IDPOOL_NAME) .setLow(NeutronConstants.RD_IDPOOL_START) .setHigh(new BigInteger(NeutronConstants.RD_IDPOOL_SIZE).longValue()).build(); try { Future> result = idManager.createIdPool(createPool); if (result != null && result.get().isSuccessful()) { LOG.info("Created IdPool for Bgpvpn RD"); } else { LOG.error("Failed to create ID pool for BGPVPN RD, result future returned {}", result); } } catch (InterruptedException | ExecutionException e) { LOG.error("Failed to create idPool for Bgpvpn RD", e); } } private boolean validateRouteInfo(Uuid routerID) { Uuid assocVPNId; if ((assocVPNId = neutronvpnUtils.getVpnForRouter(routerID, true)) != null) { LOG.warn("VPN router association failed due to router {} already associated to another VPN {}", routerID.getValue(), assocVPNId.getValue()); return false; } return true; } }