Thrift changes to support IPv6 calls over Quagga BGP stack
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / intervpnlink / InterVpnLinkUtil.java
index f143ebd045fbf0fa27ae865cf783898ec60685c6..af8cf620b2f889e4d9b5b112c03d2cc9fb806e30 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
+ * Copyright (c) 2016, 2017 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,
@@ -10,11 +10,13 @@ package org.opendaylight.netvirt.vpnmanager.intervpnlink;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.ListenableFuture;
+
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
@@ -25,6 +27,7 @@ import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
 import org.opendaylight.genius.utils.ServiceIndex;
 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
+import org.opendaylight.netvirt.fibmanager.api.FibHelper;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
 import org.opendaylight.netvirt.vpnmanager.VpnConstants;
@@ -39,7 +42,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev15033
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.InterVpnLinkStates;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.InterVpnLinks;
@@ -129,19 +131,6 @@ public class InterVpnLinkUtil {
         vpnFootprintService.updateVpnToDpnMapping(dpnId, vpnName, ifaceName, false /* removal */);
     }
 
-    /**
-     * Retrieves the InterVpnLink object searching by its name.
-     *
-     * @param broker dataBroker service reference
-     * @param vpnLinkName Name of the InterVpnLink
-     * @return the InterVpnLink or Optional.absent() if there is no InterVpnLink with the specified name
-     */
-    public static Optional<InterVpnLink> getInterVpnLinkByName(DataBroker broker, String vpnLinkName) {
-        InstanceIdentifier<InterVpnLink> interVpnLinksIid =
-            InstanceIdentifier.builder(InterVpnLinks.class)
-                .child(InterVpnLink.class, new InterVpnLinkKey(vpnLinkName)).build();
-        return VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, interVpnLinksIid);
-    }
 
     /**
      * Updates inter-VPN link state.
@@ -200,7 +189,7 @@ public class InterVpnLinkUtil {
                                                                                Long lportTagOfOtherEndpoint) {
         List<ListenableFuture<Void>> result = new ArrayList<>();
         long vpnId = VpnUtil.getVpnId(broker, vpnUuidOtherEndpoint);
-        for ( BigInteger dpnId : dpnList ) {
+        for (BigInteger dpnId : dpnList) {
             // insert into LPortDispatcher table
             Flow lportDispatcherFlow = buildLPortDispatcherFlow(interVpnLinkName, vpnId,
                                                                 lportTagOfOtherEndpoint.intValue());
@@ -226,11 +215,11 @@ public class InterVpnLinkUtil {
                                 ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME, NwConstants.L3VPN_SERVICE_INDEX)),
                         MetaDataUtil.getMetaDataMaskForLPortDispatcher()));
         String flowRef = getLportDispatcherFlowRef(interVpnLinkName, lportTag);
-        Flow lportDispatcherFlow = MDSALUtil.buildFlowNew(NwConstants.LPORT_DISPATCHER_TABLE, flowRef,
-            VpnConstants.DEFAULT_LPORT_DISPATCHER_FLOW_PRIORITY, flowRef,
-            0, 0, VpnUtil.getCookieL3((int) vpnId), matches,
-            buildLportDispatcherTableInstructions(vpnId));
-        return lportDispatcherFlow;
+
+        return MDSALUtil.buildFlowNew(NwConstants.LPORT_DISPATCHER_TABLE, flowRef,
+                                      VpnConstants.DEFAULT_LPORT_DISPATCHER_FLOW_PRIORITY, flowRef,
+                                      0, 0, VpnUtil.getCookieL3((int) vpnId), matches,
+                                      buildLportDispatcherTableInstructions(vpnId));
     }
 
     /**
@@ -242,15 +231,11 @@ public class InterVpnLinkUtil {
      * @return the flow reference string
      */
     public static String getLportDispatcherFlowRef(String interVpnLinkName, Integer lportTag) {
-        return new StringBuffer()
-            .append(VpnConstants.FLOWID_PREFIX).append("INTERVPNLINK")
-            .append(NwConstants.FLOWID_SEPARATOR).append(interVpnLinkName)
-            .append(NwConstants.FLOWID_SEPARATOR).append(lportTag)
-            .append(NwConstants.FLOWID_SEPARATOR).append(ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME,
-                NwConstants.L3VPN_SERVICE_INDEX))
-            .append(NwConstants.FLOWID_SEPARATOR)
-            .append(VpnConstants.DEFAULT_LPORT_DISPATCHER_FLOW_PRIORITY)
-            .toString();
+        return VpnConstants.FLOWID_PREFIX + "INTERVPNLINK" + NwConstants.FLOWID_SEPARATOR + interVpnLinkName
+             + NwConstants.FLOWID_SEPARATOR + lportTag
+             + NwConstants.FLOWID_SEPARATOR + ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME,
+                                                                    NwConstants.L3VPN_SERVICE_INDEX)
+             + NwConstants.FLOWID_SEPARATOR + VpnConstants.DEFAULT_LPORT_DISPATCHER_FLOW_PRIORITY;
     }
 
 
@@ -293,62 +278,6 @@ public class InterVpnLinkUtil {
         return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, getInterVpnLinkStateIid(interVpnLinkName));
     }
 
-    /**
-     * Checks if the specified InterVpnLink is currently Active.
-     *
-     * @param broker dataBroker service reference
-     * @param interVpnLinkName The name of the InterVpnLink
-     * @return true if the InterVpnLink is Active
-     */
-    public static boolean isInterVpnLinkActive(DataBroker broker, String interVpnLinkName) {
-        Optional<InterVpnLinkState> optIVpnLinkState = getInterVpnLinkState(broker, interVpnLinkName);
-        if (!optIVpnLinkState.isPresent()) {
-            return false;
-        }
-        InterVpnLinkState interVpnLinkState = optIVpnLinkState.get();
-        return interVpnLinkState.getState() == InterVpnLinkState.State.Active;
-    }
-
-    /**
-     * Retrieves an InterVpnLink by searching by one of its endpoint's IP.
-     *
-     * @param broker dataBroker service reference
-     * @param endpointIp IP to serch for.
-     * @return the InterVpnLink or null if no InterVpnLink can be found
-     */
-    public static Optional<InterVpnLink> getInterVpnLinkByEndpointIp(DataBroker broker, String endpointIp) {
-        List<InterVpnLink> allInterVpnLinks = InterVpnLinkUtil.getAllInterVpnLinks(broker);
-        for (InterVpnLink interVpnLink : allInterVpnLinks) {
-            if (interVpnLink.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp)
-                || interVpnLink.getSecondEndpoint().getIpAddress().getValue().equals(endpointIp)) {
-                return Optional.of(interVpnLink);
-            }
-        }
-        return Optional.absent();
-    }
-
-
-    /**
-     * Retrieves the InterVpnLink that has one of its 2 endpoints installed in
-     * the specified DpnId.
-     *
-     * @param broker dataBroker service reference
-     * @param dpnId Id of the DPN
-     * @return The InterVpnLink object if found, Optional.absent() otherwise
-     */
-    public static Optional<InterVpnLink> getInterVpnLinkByDpnId(DataBroker broker, BigInteger dpnId) {
-        List<InterVpnLink> allInterVpnLinks = InterVpnLinkUtil.getAllInterVpnLinks(broker);
-        for (InterVpnLink interVpnLink : allInterVpnLinks) {
-            Optional<InterVpnLinkState> optInterVpnLinkState = getInterVpnLinkState(broker, interVpnLink.getName());
-            if (optInterVpnLinkState.isPresent()
-                && (optInterVpnLinkState.get().getFirstEndpointState().getDpId().contains(dpnId)
-                        || optInterVpnLinkState.get().getSecondEndpointState().getDpId().contains(dpnId))) {
-                return Optional.fromNullable(interVpnLink);
-            }
-        }
-        return Optional.absent();
-    }
-
     /**
      * Retrieves all configured InterVpnLinks.
      *
@@ -365,51 +294,6 @@ public class InterVpnLinkUtil {
             : new ArrayList<>();
     }
 
-    /**
-     * Retrieves the list of DPNs where the endpoint of a VPN in an InterVPNLink was instantiated.
-     *
-     * @param broker dataBroker service reference
-     * @param vpnLinkName the name of the InterVpnLink
-     * @param vpnUuid UUID of the VPN whose endpoint to be checked
-     * @return the list of DPN Ids
-     */
-    public static List<BigInteger> getVpnLinkEndpointDPNs(DataBroker broker, String vpnLinkName, String vpnUuid) {
-        Optional<InterVpnLinkState> interVpnLinkState = getInterVpnLinkState(broker, vpnLinkName);
-        if (interVpnLinkState.isPresent()) {
-            if (interVpnLinkState.get().getFirstEndpointState().getVpnUuid().getValue().equals(vpnUuid)) {
-                return interVpnLinkState.get().getFirstEndpointState().getDpId();
-            } else {
-                return interVpnLinkState.get().getSecondEndpointState().getDpId();
-            }
-        } else {
-            LOG.trace("Could not find InterVpnLinkState for interVpnLink {}", vpnLinkName);
-            return new ArrayList<>();
-        }
-    }
-
-    /**
-     * Retrieves the list of DPNs where the endpoint of a VPN in an InterVPNLink was instantiated.
-     *
-     * @param broker dataBroker service reference
-     * @param endpointIp Ip of the endpoint specified in the InterVpnLink
-     * @return the list of DPN Ids
-     */
-    public static List<BigInteger> getVpnLinkEndpointDPNsByIp(DataBroker broker, String endpointIp) {
-        Optional<InterVpnLink> optIVpnLink = getInterVpnLinkByEndpointIp(broker, endpointIp);
-        if (optIVpnLink.isPresent()) {
-            InterVpnLink interVpnLink = optIVpnLink.get();
-            boolean isFirstEndpoint = interVpnLink.getFirstEndpoint().getIpAddress().getValue().equals(endpointIp);
-            return isFirstEndpoint ? getVpnLinkEndpointDPNs(broker, interVpnLink.getName(),
-                interVpnLink.getFirstEndpoint().getVpnUuid().getValue())
-                : getVpnLinkEndpointDPNs(broker, interVpnLink.getName(),
-                interVpnLink.getSecondEndpoint().getVpnUuid().getValue());
-        } else {
-            LOG.trace("Could not find an InterVpnLink with endpoint IpAddr={}", endpointIp);
-            return new ArrayList<>();
-        }
-    }
-
-
     /**
      * Leaks a route from one VPN to another. By default, the origin for this leaked route is INTERVPN.
      *
@@ -462,10 +346,8 @@ public class InterVpnLinkUtil {
         String endpointIp = destinationIs1stEndpoint ? interVpnLink.getSecondEndpoint().getIpAddress().getValue()
             : interVpnLink.getFirstEndpoint().getIpAddress().getValue();
 
-        VrfEntry newVrfEntry = new VrfEntryBuilder().setKey(new VrfEntryKey(prefix)).setDestPrefix(prefix)
-            .setLabel(label).setNextHopAddressList(Collections.singletonList(endpointIp))
-            .setOrigin(RouteOrigin.INTERVPN.getValue())
-            .build();
+        VrfEntry newVrfEntry = FibHelper.getVrfEntryBuilder(prefix, label, endpointIp, RouteOrigin.INTERVPN,
+                null /* parentVpnRd */).build();
 
         String dstVpnRd = VpnUtil.getVpnRd(broker, dstVpnUuid);
         InstanceIdentifier<VrfEntry> newVrfEntryIid =
@@ -491,7 +373,8 @@ public class InterVpnLinkUtil {
                 LOG.debug("Advertising route in VPN={} [prefix={} label={}  nexthops={}] to DC-GW",
                     dstVpnRd, newVrfEntry.getDestPrefix(), label.intValue(), nexthops);
                 bgpManager.advertisePrefix(dstVpnRd, null /*macAddress*/, newVrfEntry.getDestPrefix(), nexthops,
-                        VrfEntry.EncapType.Mplsgre, label.intValue(), 0 /*l3vni*/, null /*gatewayMacAddress*/);
+                        VrfEntry.EncapType.Mplsgre, label.intValue(), 0 /*l3vni*/, 0 /*l2vni*/,
+                        null /*gatewayMacAddress*/);
             } catch (Exception exc) {
                 LOG.error("Could not advertise prefix {} with label {} to VPN rd={}",
                     newVrfEntry.getDestPrefix(), label.intValue(), dstVpnRd);
@@ -518,7 +401,7 @@ public class InterVpnLinkUtil {
             vpnRd, destination, label, nexthop);
         fibManager.addOrUpdateFibEntry(dataBroker, vpnRd, null /*macAddress*/, destination,
                 Collections.singletonList(nexthop), VrfEntry.EncapType.Mplsgre, label,
-                0 /*l3vni*/, null /*gatewayMacAddress*/, RouteOrigin.STATIC, null /*writeTxn*/);
+                0 /*l3vni*/, null /*gatewayMacAddress*/, null /*parentVpnRd*/, RouteOrigin.STATIC, null /*writeTxn*/);
 
         // Now advertise to BGP. The nexthop that must be advertised to BGP are the IPs of the DPN where the
         // VPN's endpoint have been instantiated
@@ -526,10 +409,11 @@ public class InterVpnLinkUtil {
         List<BigInteger> endpointDpns = interVpnLink.getEndpointDpnsByVpnName(vpnName);
         List<String> nexthopList =
             endpointDpns.stream().map(dpnId -> InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId))
-                .collect(Collectors.toList());
+                        .collect(Collectors.toList());
         LOG.debug("advertising IVpnLink route to BGP:  vpnRd={}, prefix={}, label={}, nexthops={}",
             vpnRd, destination, label, nexthopList);
         bgpManager.advertisePrefix(vpnRd, null /*macAddress*/, destination, nexthopList,
-                VrfEntry.EncapType.Mplsgre, label, 0 /*l3vni*/, null /*gatewayMacAddress*/);
+                VrfEntry.EncapType.Mplsgre, label, 0 /*l3vni*/, 0 /*l2vni*/,
+                null /*gatewayMacAddress*/);
     }
 }