Merge "Removed unused fields"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImpl.java
index c94dd639e5556ee73561723ac28986fc8b91fdcc..b471cdab3b1a44a5127aa8e494fccd4b03515c3c 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2013-2014 Ericsson. and others.  All rights reserved.
+ * Copyright (c) 2013, 2015 Ericsson. 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,
@@ -16,6 +16,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import javax.annotation.Nonnull;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
@@ -36,7 +37,6 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
@@ -273,13 +273,16 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     private static final byte[] VLAN_VID_MASK = new byte[]{16, 0};
     private static final short PROTO_TCP = 6;
     private static final short PROTO_UDP = 17;
-    private static final String noIp = "0.0.0.0/0";
+    private static final short PROTO_ICMPV4 = 1;
+    private static final String NO_IP = "0.0.0.0/0";
 
     @Override
     public List<MatchEntry> convert(
             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
         List<MatchEntry> matchEntryList = new ArrayList<>();
-        if (match == null) return matchEntryList;
+        if (match == null) {
+            return matchEntryList;
+        }
         if (match.getInPort() != null) {
             //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
             matchEntryList.add(toOfPort(InPort.class,
@@ -355,7 +358,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
                 VlanVidCaseBuilder vlanVidCaseBuilder = new VlanVidCaseBuilder();
                 VlanVidBuilder vlanVidBuilder = new VlanVidBuilder();
                 boolean setCfiBit = false;
-                Integer vidEntryValue = new Integer(0);
+                Integer vidEntryValue = 0;
                 boolean hasmask = false;
                 if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
                     setCfiBit = true;
@@ -696,7 +699,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
                     ArpThaBuilder arpThaBuilder = new ArpThaBuilder();
                     arpThaBuilder.setMacAddress(arpTargetHardwareAddress.getAddress());
                     boolean hasMask = false;
-                    if (null != arpSourceHardwareAddress.getMask()) {
+                    if (null != arpTargetHardwareAddress.getMask()) {
                         arpThaBuilder.setMask(ByteBufUtils.macAddressToBytes(arpTargetHardwareAddress.getMask().getValue()));
                         hasMask = true;
                     }
@@ -857,7 +860,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private byte[] extractIpv4Mask(boolean hasMask, final Iterator<String> addressParts) {
+    private static byte[] extractIpv4Mask(boolean hasMask, final Iterator<String> addressParts) {
         final int prefix;
         if (addressParts.hasNext()) {
             int potentionalPrefix = Integer.parseInt(addressParts.next());
@@ -884,7 +887,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
      * @return
      * @author avishnoi@in.ibm.com
      */
-    public static Match fromOFMatchV10ToSALMatch(final MatchV10 swMatch, final BigInteger datapathid, final OpenflowVersion ofVersion) {
+    public static MatchBuilder fromOFMatchV10ToSALMatch(@Nonnull final MatchV10 swMatch, @Nonnull final BigInteger datapathid, @Nonnull final OpenflowVersion ofVersion) {
         MatchBuilder matchBuilder = new MatchBuilder();
         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
@@ -916,7 +919,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
         }
         if (!swMatch.getWildcards().isDLVLAN().booleanValue() && swMatch.getDlVlan() != null) {
             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
-            int vlanId = (swMatch.getDlVlan() == ((int) 0xffff)) ? 0 : swMatch.getDlVlan();
+            int vlanId = (swMatch.getDlVlan() == (0xffff)) ? 0 : swMatch.getDlVlan();
             vlanIdBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
             vlanIdBuilder.setVlanIdPresent(vlanId == 0 ? false : true);
             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
@@ -939,7 +942,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
 
                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + "32";
             }
-            if (!ipv4PrefixStr.equals(noIp)) {
+            if (!NO_IP.equals(ipv4PrefixStr)) {
                 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
             }
@@ -956,47 +959,76 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
 
                 ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + "32";
             }
-            if (!ipv4PrefixStr.equals(noIp)) {
+            if (!NO_IP.equals(ipv4PrefixStr)) {
                 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
                 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
             }
         }
         if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() != null) {
-            ipMatchBuilder.setIpProtocol(swMatch.getNwProto());
+            Short nwProto = swMatch.getNwProto();
+            ipMatchBuilder.setIpProtocol(nwProto);
             matchBuilder.setIpMatch(ipMatchBuilder.build());
-        }
-        if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_TCP) {
-            TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
-            if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
-                tcpMatchBuilder
-                        .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
-                                swMatch.getTpSrc()));
-            }
-            if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
-                tcpMatchBuilder
-                        .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
-                                swMatch.getTpDst()));
-            }
 
-            if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
-                matchBuilder.setLayer4Match(tcpMatchBuilder.build());
-            }
-        }
-        if (!swMatch.getWildcards().isNWPROTO().booleanValue() && swMatch.getNwProto() == PROTO_UDP) {
-            UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
-            if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
-                udpMatchBuilder
-                        .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
-                                swMatch.getTpSrc()));
-            }
-            if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
-                udpMatchBuilder
-                        .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
-                                swMatch.getTpDst()));
-            }
+            int proto = nwProto.intValue();
+            if (proto == PROTO_TCP) {
+                TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
+                boolean hasTcp = false;
+                if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
+                    tcpMatchBuilder
+                            .setTcpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
+                                    swMatch.getTpSrc()));
+                    hasTcp = true;
+                }
+                if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
+                    tcpMatchBuilder
+                            .setTcpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
+                                    swMatch.getTpDst()));
+                    hasTcp = true;
+                }
 
-            if (!swMatch.getWildcards().isTPSRC().booleanValue() || !swMatch.getWildcards().isTPDST().booleanValue()) {
-                matchBuilder.setLayer4Match(udpMatchBuilder.build());
+                if (hasTcp) {
+                    matchBuilder.setLayer4Match(tcpMatchBuilder.build());
+                }
+            } else if (proto == PROTO_UDP) {
+                UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
+                boolean hasUdp = false;
+                if (!swMatch.getWildcards().isTPSRC().booleanValue() && swMatch.getTpSrc() != null) {
+                    udpMatchBuilder
+                            .setUdpSourcePort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
+                                    swMatch.getTpSrc()));
+                    hasUdp = true;
+                }
+                if (!swMatch.getWildcards().isTPDST().booleanValue() && swMatch.getTpDst() != null) {
+                    udpMatchBuilder
+                            .setUdpDestinationPort(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber(
+                                    swMatch.getTpDst()));
+                    hasUdp = true;
+                }
+
+                if (hasUdp) {
+                    matchBuilder.setLayer4Match(udpMatchBuilder.build());
+                }
+            } else if (proto == PROTO_ICMPV4) {
+                Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
+                boolean hasIcmpv4 = false;
+                if (!swMatch.getWildcards().isTPSRC().booleanValue()) {
+                    Integer type = swMatch.getTpSrc();
+                    if (type != null) {
+                        icmpv4MatchBuilder.setIcmpv4Type(type.shortValue());
+                        hasIcmpv4 = true;
+                    }
+                }
+                if (!swMatch.getWildcards().isTPDST().booleanValue()) {
+                    Integer code = swMatch.getTpDst();
+                    if (code != null) {
+                        icmpv4MatchBuilder.setIcmpv4Code(code.shortValue());
+                        hasIcmpv4 = true;
+                    }
+                }
+
+                if (hasIcmpv4) {
+                    matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
+                }
             }
         }
         if (!swMatch.getWildcards().isNWTOS().booleanValue() && swMatch.getNwTos() != null) {
@@ -1005,7 +1037,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
             matchBuilder.setIpMatch(ipMatchBuilder.build());
         }
 
-        return matchBuilder.build();
+        return matchBuilder;
     }
 
     /**
@@ -1020,13 +1052,13 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
      * @author avishnoi@in.ibm.com
      */
     public static MatchBuilder fromOFMatchToSALMatch(
-            final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match swMatch,
-            final BigInteger datapathid, final OpenflowVersion ofVersion) {
+            @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match swMatch,
+            @Nonnull final BigInteger datapathid, @Nonnull final OpenflowVersion ofVersion) {
         return OfMatchToSALMatchConvertor(swMatch.getMatchEntry(), datapathid, ofVersion);
     }
 
-    private static MatchBuilder OfMatchToSALMatchConvertor(List<MatchEntry> swMatchList, final BigInteger datapathid,
-                                                           OpenflowVersion ofVersion) {
+    private static MatchBuilder OfMatchToSALMatchConvertor(final List<MatchEntry> swMatchList, final BigInteger datapathid,
+                                                           final OpenflowVersion ofVersion) {
 
         MatchBuilder matchBuilder = new MatchBuilder();
         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
@@ -1414,7 +1446,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
         return matchBuilder;
     }
 
-    private static void setIpv6MatchBuilderFields(final Ipv6MatchBuilder ipv6MatchBuilder, final MatchEntry ofMatch, String ipv6PrefixStr, final byte[] mask) {
+    private static void setIpv6MatchBuilderFields(final Ipv6MatchBuilder ipv6MatchBuilder, final MatchEntry ofMatch, final String ipv6PrefixStr, final byte[] mask) {
         Ipv6Prefix ipv6Prefix;
 
         if (mask != null) {
@@ -1431,7 +1463,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
         }
     }
 
-    private static void setIpv4MatchBuilderFields(final Ipv4MatchBuilder ipv4MatchBuilder, final MatchEntry ofMatch, final byte[] mask, String ipv4PrefixStr) {
+    private static void setIpv4MatchBuilderFields(final Ipv4MatchBuilder ipv4MatchBuilder, final MatchEntry ofMatch, final byte[] mask, final String ipv4PrefixStr) {
         Ipv4Prefix ipv4Prefix;
         if (mask != null) {
             ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr), mask);
@@ -1493,7 +1525,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
         if (mplsBos.shortValue() != 0) {
             isBos = true;
         }
-        mplsBosBuilder.setBos(new Boolean(isBos));
+        mplsBosBuilder.setBos(isBos);
         mplsBosCaseBuilder.setMplsBos(mplsBosBuilder.build());
 
         matchEntryBuilder.setMatchEntryValue(mplsBosCaseBuilder.build());
@@ -1786,22 +1818,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-//    /**
-//     * Method converts OF SetField action to SAL SetFiled action.
-//     *
-//     * @param action
-//     * @param ofVersion current ofp version
-//     * @return
-//     */
-//    public static SetField fromOFSetFieldToSALSetFieldAction(
-//            final Action action, OpenflowVersion ofVersion) {
-//        logger.debug("Converting OF SetField action to SAL SetField action");
-//        SetFieldBuilder setField = new SetFieldBuilder();
-//        OxmFieldsAction oxmFields = action.getAugmentation(OxmFieldsAction.class);
-//        MatchBuilder match = OfMatchToSALMatchConvertor(oxmFields.getMatchEntry(), null, ofVersion);
-//        setField.fieldsFrom(match.build());
-//        return setField.build();
-//    }
+
 
     /**
      * Method converts OF SetField action to SAL SetFiled action.
@@ -1811,7 +1828,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
      * @return
      */
     public static SetField fromOFSetFieldToSALSetFieldAction(
-            final Action action, OpenflowVersion ofVersion) {
+            final Action action, final OpenflowVersion ofVersion) {
         logger.debug("Converting OF SetField action to SAL SetField action");
         SetFieldCase setFieldCase = (SetFieldCase) action.getActionChoice();
         SetFieldAction setFieldAction = setFieldCase.getSetFieldAction();