MRI version bumpup for Aluminium
[netvirt.git] / cloud-servicechain / impl / src / main / java / org / opendaylight / netvirt / cloudservicechain / utils / VpnServiceChainUtils.java
index f986ff8aa1b88be8ea192feac381b21bcdf7272b..96ec099c2772754995d93473c8314e97d8233a67 100755 (executable)
@@ -7,15 +7,12 @@
  */
 package org.opendaylight.netvirt.cloudservicechain.utils;
 
-import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 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.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
 import org.opendaylight.genius.mdsalutil.FlowEntity;
@@ -23,6 +20,7 @@ import org.opendaylight.genius.mdsalutil.InstructionInfo;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.MatchInfo;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
+import org.opendaylight.genius.mdsalutil.NWUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.actions.ActionPopMpls;
 import org.opendaylight.genius.mdsalutil.actions.ActionRegLoad;
@@ -34,6 +32,9 @@ import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
 import org.opendaylight.genius.mdsalutil.matches.MatchMplsLabel;
 import org.opendaylight.genius.utils.ServiceIndex;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.ReadFailedException;
 import org.opendaylight.netvirt.cloudservicechain.CloudServiceChainConstants;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
@@ -70,7 +71,9 @@ public final class VpnServiceChainUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(VpnServiceChainUtils.class);
 
-    private VpnServiceChainUtils() { }
+    private VpnServiceChainUtils() {
+
+    }
 
     public static BigInteger getMetadataSCF(long scfTag) { // TODO: Move to a common place
         return new BigInteger("FF", 16).and(BigInteger.valueOf(scfTag)).shiftLeft(32);
@@ -313,13 +316,14 @@ public final class VpnServiceChainUtils {
      * @param lportTag Pseudo Logical Port tag
      * @return the FlowEntity
      */
-    public static FlowEntity buildLFibVpnPseudoPortFlow(BigInteger dpId, Long label, String nextHop, int lportTag) {
+    public static FlowEntity buildLFibVpnPseudoPortFlow(BigInteger dpId, Long label, int etherType, String nextHop,
+                                                        int lportTag) {
 
         List<MatchInfo> matches = new ArrayList<>();
         matches.add(MatchEthernetType.MPLS_UNICAST);
         matches.add(new MatchMplsLabel(label));
 
-        List<ActionInfo> actionsInfos = Collections.singletonList(new ActionPopMpls());
+        List<ActionInfo> actionsInfos = Collections.singletonList(new ActionPopMpls(etherType));
         List<InstructionInfo> instructions = new ArrayList<>();
         instructions.add(new InstructionWriteMetadata(
                        MetaDataUtil.getMetaDataForLPortDispatcher(lportTag,
@@ -352,16 +356,22 @@ public final class VpnServiceChainUtils {
                     .forEach(routePath -> {
                         Long label = routePath.getLabel();
                         String nextHop = routePath.getNexthopAddress();
-                        FlowEntity flowEntity = buildLFibVpnPseudoPortFlow(dpId, label, nextHop, lportTag);
-                        if (addOrRemove == NwConstants.ADD_FLOW) {
-                            mdsalMgr.installFlow(flowEntity);
-                        } else {
-                            mdsalMgr.removeFlow(flowEntity);
+                        try {
+                            int etherType = NWUtil.getEtherTypeFromIpPrefix(vrfEntry.getDestPrefix());
+                            FlowEntity flowEntity = buildLFibVpnPseudoPortFlow(dpId, label, etherType, nextHop,
+                                    lportTag);
+                            if (addOrRemove == NwConstants.ADD_FLOW) {
+                                mdsalMgr.installFlow(flowEntity);
+                            } else {
+                                mdsalMgr.removeFlow(flowEntity);
+                            }
+                            LOG.debug(
+                                    "LFIBEntry for label={}, destination={}, nexthop={} {} successfully in dpn={}",
+                                    label, vrfEntry.getDestPrefix(), nextHop,
+                                    addOrRemove == NwConstants.DEL_FLOW ? "removed" : "installed", dpId);
+                        } catch (IllegalArgumentException ex) {
+                            LOG.warn("Unable to get etherType for IP Prefix {}", vrfEntry.getDestPrefix());
                         }
-                        LOG.debug(
-                                "LFIBEntry for label={}, destination={}, nexthop={} {} successfully in dpn={}",
-                                label, vrfEntry.getDestPrefix(), nextHop,
-                                addOrRemove == NwConstants.DEL_FLOW ? "removed" : "installed", dpId);
                     }));
         }
     }
@@ -467,7 +477,8 @@ public final class VpnServiceChainUtils {
      * this kind of flows.
      */
     public static String getL3VpnToScfLportDispatcherFlowRef(Integer lportTag) {
-        return new StringBuffer(64).append(CloudServiceChainConstants.VPN_PSEUDO_VPN2SCF_FLOWID_PREFIX).append(lportTag)
+        return new StringBuilder(64).append(CloudServiceChainConstants.VPN_PSEUDO_VPN2SCF_FLOWID_PREFIX)
+                                   .append(lportTag)
                                    .append(NwConstants.FLOWID_SEPARATOR)
                                    .append(ServiceIndex.getIndex(NwConstants.SCF_SERVICE_NAME,
                                                                  NwConstants.SCF_SERVICE_INDEX))
@@ -482,7 +493,7 @@ public final class VpnServiceChainUtils {
      *
      */
     public static String getScfToL3VpnLportDispatcherFlowRef(Integer lportTag) {
-        return new StringBuffer().append(CloudServiceChainConstants.VPN_PSEUDO_SCF2VPN_FLOWID_PREFIX).append(lportTag)
+        return new StringBuilder().append(CloudServiceChainConstants.VPN_PSEUDO_SCF2VPN_FLOWID_PREFIX).append(lportTag)
                                  .append(NwConstants.FLOWID_SEPARATOR)
                                  .append(ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME,
                                                                NwConstants.L3VPN_SERVICE_INDEX))
@@ -514,7 +525,7 @@ public final class VpnServiceChainUtils {
                             .flatMap(dpn -> dpn.getVpnInterfaces().stream())
                             .map(VpnInterfaces::getInterfaceName)
                             .collect(Collectors.toList());
-        } catch (ReadFailedException e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.warn("getAllVpnInterfaces for vpn {}: Failure on read operation", vpnName, e);
             return Collections.emptyList();
         }