BUG 2280 - mac address mask translation.
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImpl.java
index 74701163cacade026bd62def819d3c5cac9e21c7..dba2e92a3f420dc54e1cd79cf5da33610e6e13e7 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2013 Ericsson. and others.  All rights reserved.
+ * Copyright (c) 2013-2014 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,
@@ -8,14 +8,23 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
 
+import static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Splitter;
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
-import org.opendaylight.openflowplugin.openflow.md.OFConstants;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
+import org.opendaylight.openflowplugin.extension.api.ConverterExtensionKey;
+import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
+import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionResolvers;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
@@ -67,6 +76,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
@@ -123,7 +134,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcMatchEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcpFlagMatchEntry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TcpFlagMatchEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpMatchEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
@@ -163,7 +173,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Meta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsLabel;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsTc;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Nxm1Class;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.SctpDst;
@@ -172,6 +181,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpD
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpFlag;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TcpSrc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Dst;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelIpv4Src;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpDst;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.UdpSrc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanPcp;
@@ -179,11 +190,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Vlan
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralExtensionListGrouping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Splitter;
-
 /**
  * Utility class for converting a MD-SAL Flow into the OF flow mod
  */
@@ -191,7 +203,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
     private static final Logger logger = LoggerFactory.getLogger(MatchConvertorImpl.class);
     static final String PREFIX_SEPARATOR = "/";
     static final Splitter PREFIX_SPLITTER = Splitter.on('/');
-    private static final byte[] VLAN_VID_MASK = new byte[] { 16, 0 };
+    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";
@@ -200,15 +212,17 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
     public List<MatchEntries> convert(
             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
         List<MatchEntries> matchEntriesList = new ArrayList<>();
-
+        if (match == null) return matchEntriesList;
         if (match.getInPort() != null) {
+            //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
             matchEntriesList.add(toOfPort(InPort.class,
-                    InventoryDataServiceUtil.portNumberfromNodeConnectorId(match.getInPort())));
+                    InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPort())));
         }
 
         if (match.getInPhyPort() != null) {
+            //TODO: currently this matchconverter is mapped to OF1.3 in MatchReactorMappingFactory. Will need to revisit during 1.4+
             matchEntriesList.add(toOfPort(InPhyPort.class,
-                    InventoryDataServiceUtil.portNumberfromNodeConnectorId(match.getInPhyPort())));
+                    InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF13, match.getInPhyPort())));
         }
 
         org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata metadata = match
@@ -327,6 +341,15 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 if (ipv4Match.getIpv4Destination() != null) {
                     matchEntriesList.add(toOfIpv4Prefix(Ipv4Dst.class, ipv4Match.getIpv4Destination()));
                 }
+            }
+            if (layer3Match instanceof TunnelIpv4Match) {
+                TunnelIpv4Match tunnelIpv4Src = (TunnelIpv4Match) layer3Match;
+                if (tunnelIpv4Src.getTunnelIpv4Source() != null) {
+                    matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Src.class, tunnelIpv4Src.getTunnelIpv4Source()));
+                }
+                if (tunnelIpv4Src.getTunnelIpv4Destination() != null) {
+                    matchEntriesList.add(NxmExtensionsConvertor.toNxmIpv4Tunnel(TunnelIpv4Dst.class, tunnelIpv4Src.getTunnelIpv4Destination()));
+                }
             } else if (layer3Match instanceof ArpMatch) {
                 ArpMatch arpMatch = (ArpMatch) layer3Match;
                 if (arpMatch.getArpOp() != null) {
@@ -352,9 +375,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     matchEntriesList.add(toOfMacAddress(ArpTha.class, arpTargetHardwareAddress.getAddress(),
                             arpTargetHardwareAddress.getMask()));
                 }
-            }
-
-            else if (layer3Match instanceof Ipv6Match) {
+            } else if (layer3Match instanceof Ipv6Match) {
                 Ipv6Match ipv6Match = (Ipv6Match) layer3Match;
                 if (ipv6Match.getIpv6Source() != null) {
                     matchEntriesList.add(toOfIpv6Prefix(Ipv6Src.class, ipv6Match.getIpv6Source()));
@@ -406,10 +427,11 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         }
 
         //FIXME: move to extensible support
+        // TODO: Move to seperate bundle as soon as OF extensibility is supported by ofplugin/java
         TcpFlagMatch tcpFlagMatch = match.getTcpFlagMatch();
         if (tcpFlagMatch != null) {
             if (tcpFlagMatch.getTcpFlag() != null) {
-                matchEntriesList.add(toOfTcpFlag(tcpFlagMatch.getTcpFlag()));
+                matchEntriesList.add(NxmExtensionsConvertor.toNxmTcpFlag(tcpFlagMatch.getTcpFlag()));
             }
         }
 
@@ -419,6 +441,24 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
             matchEntriesList.add(toOfMetadata(TunnelId.class, tunnel.getTunnelId(), tunnel.getTunnelMask()));
         }
 
+
+        /**
+         * TODO: EXTENSION PROPOSAL (match, MD-SAL to OFJava)
+         * - we might need version for conversion and for key
+         * - sanitize NPE
+         */
+        Optional<GeneralExtensionListGrouping> extensionListOpt = ExtensionResolvers.getMatchExtensionResolver().getExtension(match);
+        if (extensionListOpt.isPresent()) {
+            for (ExtensionList extensionItem : extensionListOpt.get().getExtensionList()) {
+                // TODO: get real version
+                ConverterExtensionKey<? extends ExtensionKey> key = new ConverterExtensionKey<>(extensionItem.getExtensionKey(), OFConstants.OFP_VERSION_1_3);
+                ConvertorToOFJava<MatchEntries> convertor =
+                        OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
+                MatchEntries ofMatch = convertor.convert(extensionItem.getExtension());
+                matchEntriesList.add(ofMatch);
+            }
+        }
+
         return matchEntriesList;
     }
 
@@ -430,7 +470,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
      * @return
      * @author avishnoi@in.ibm.com
      */
-    public static Match fromOFMatchV10ToSALMatch(final MatchV10 swMatch, final BigInteger datapathid) {
+    public static Match fromOFMatchV10ToSALMatch(final MatchV10 swMatch, final BigInteger datapathid, final OpenflowVersion ofVersion) {
         MatchBuilder matchBuilder = new MatchBuilder();
         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
@@ -438,7 +478,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
         if (!swMatch.getWildcards().isINPORT().booleanValue() && swMatch.getInPort() != null) {
             matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
-                    (long) swMatch.getInPort()));
+                    (long) swMatch.getInPort(), ofVersion));
         }
 
         if (!swMatch.getWildcards().isDLSRC().booleanValue() && swMatch.getDlSrc() != null) {
@@ -464,7 +504,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
             int vlanId = (swMatch.getDlVlan() == ((int) 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);
+            vlanIdBuilder.setVlanIdPresent(vlanId == 0 ? false : true);
             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
         }
@@ -477,6 +517,13 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
             String ipv4PrefixStr = swMatch.getNwSrc().getValue();
             if (swMatch.getNwSrcMask() != null) {
                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwSrcMask();
+            } else {
+                //Openflow Spec : 1.3.2
+                //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
+                // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
+                // statistics response.
+
+                ipv4PrefixStr += PREFIX_SEPARATOR + "32";
             }
             if (!ipv4PrefixStr.equals(noIp)) {
                 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
@@ -487,6 +534,13 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
             String ipv4PrefixStr = swMatch.getNwDst().getValue();
             if (swMatch.getNwDstMask() != null) {
                 ipv4PrefixStr += PREFIX_SEPARATOR + swMatch.getNwDstMask();
+            } else {
+                //Openflow Spec : 1.3.2
+                //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
+                // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
+                // statistics response.
+
+                ipv4PrefixStr += PREFIX_SEPARATOR + "32";
             }
             if (!ipv4PrefixStr.equals(noIp)) {
                 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
@@ -532,12 +586,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
             }
         }
         if (!swMatch.getWildcards().isNWTOS().booleanValue() && swMatch.getNwTos() != null) {
-            // DSCP default value is 0 from the library but controller side it
-            // is null.
-            // look if there better solution
-            if (0 != swMatch.getNwTos()) {
-                ipMatchBuilder.setIpDscp(new Dscp(swMatch.getNwTos()));
-            }
+            Short dscp = ActionUtil.tosToDscp(swMatch.getNwTos().shortValue());
+            ipMatchBuilder.setIpDscp(new Dscp(dscp));
             matchBuilder.setIpMatch(ipMatchBuilder.build());
         }
 
@@ -549,19 +599,21 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
      * match
      *
      * @param match
-     * @return
+     * @param swMatch
+     * @param datapathid
+     * @param ofVersion
+     * @return md-sal match instance
      * @author avishnoi@in.ibm.com
      */
-    public static Match fromOFMatchToSALMatch(
+    public static MatchBuilder fromOFMatchToSALMatch(
             final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match swMatch,
-            final BigInteger datapathid) {
-        
-        return OfMatchToSALMatchConvertor(swMatch.getMatchEntries(), datapathid);
-
+            final BigInteger datapathid, final OpenflowVersion ofVersion) {
+        return OfMatchToSALMatchConvertor(swMatch.getMatchEntries(), datapathid, ofVersion);
     }
 
-    private static Match OfMatchToSALMatchConvertor(List<MatchEntries> swMatchList, final BigInteger datapathid){
-        
+    private static MatchBuilder OfMatchToSALMatchConvertor(List<MatchEntries> swMatchList, final BigInteger datapathid,
+                                                           OpenflowVersion ofVersion) {
+
         MatchBuilder matchBuilder = new MatchBuilder();
         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
@@ -576,17 +628,20 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
         ProtocolMatchFieldsBuilder protocolMatchFieldsBuilder = new ProtocolMatchFieldsBuilder();
         TcpFlagMatchBuilder tcpFlagMatchBuilder = new TcpFlagMatchBuilder();
+        TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
 
         for (MatchEntries ofMatch : swMatchList) {
 
             if (ofMatch.getOxmMatchField().equals(InPort.class)) {
                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
-                matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
-                        portNumber.getPortNumber().getValue()));
+                if (portNumber != null) {
+                    Long portNo = portNumber.getPortNumber().getValue();
+                    matchBuilder.setInPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid, portNo, ofVersion));
+                }
             } else if (ofMatch.getOxmMatchField().equals(InPhyPort.class)) {
                 PortNumberMatchEntry portNumber = ofMatch.getAugmentation(PortNumberMatchEntry.class);
                 matchBuilder.setInPhyPort(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathid,
-                        portNumber.getPortNumber().getValue()));
+                        portNumber.getPortNumber().getValue(), ofVersion));
             } else if (ofMatch.getOxmMatchField().equals(Metadata.class)) {
                 MetadataBuilder metadataBuilder = new MetadataBuilder();
                 MetadataMatchEntry metadataMatchEntry = ofMatch.getAugmentation(MetadataMatchEntry.class);
@@ -601,17 +656,25 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 }
             } else if (ofMatch.getOxmMatchField().equals(EthSrc.class)) {
                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
+                final MaskMatchEntry sourceMask = ofMatch.getAugmentation(MaskMatchEntry.class);
                 if (macAddressMatchEntry != null) {
                     EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
                     ethSourceBuilder.setAddress(macAddressMatchEntry.getMacAddress());
+                    if (sourceMask != null) {
+                        ethSourceBuilder.setMask(new MacAddress(macAddressToString(sourceMask.getMask())));
+                    }
                     ethMatchBuilder.setEthernetSource(ethSourceBuilder.build());
                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
                 }
             } else if (ofMatch.getOxmMatchField().equals(EthDst.class)) {
                 MacAddressMatchEntry macAddressMatchEntry = ofMatch.getAugmentation(MacAddressMatchEntry.class);
+                final MaskMatchEntry destinationMask = ofMatch.getAugmentation(MaskMatchEntry.class);
                 if (macAddressMatchEntry != null) {
                     EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
                     ethDestinationBuilder.setAddress(macAddressMatchEntry.getMacAddress());
+                    if (destinationMask != null) {
+                        ethDestinationBuilder.setMask(new MacAddress(macAddressToString(destinationMask.getMask())));
+                    }
                     ethMatchBuilder.setEthernetDestination(ethDestinationBuilder.build());
                     matchBuilder.setEthernetMatch(ethMatchBuilder.build());
                 }
@@ -630,7 +693,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 if (vlanVidMatchEntry != null) {
                     VlanIdBuilder vlanBuilder = new VlanIdBuilder();
                     vlanBuilder.setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(
-                            vlanVidMatchEntry.getVlanVid()));
+                            vlanVidMatchEntry.getVlanVid()))
+                            .setVlanIdPresent(vlanVidMatchEntry.isCfiBit());
                     vlanMatchBuilder.setVlanId(vlanBuilder.build());
                     matchBuilder.setVlanMatch(vlanMatchBuilder.build());
                 }
@@ -728,20 +792,14 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
                     if (maskMatchEntry != null) {
-                        int receivedMask = ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
-                        int shiftCount=0;
-                        while(receivedMask != 0xffffffff){
-                            receivedMask = receivedMask >> 1;
-                            shiftCount++;
-                        }
-                        ipv4PrefixStr += PREFIX_SEPARATOR + (32-shiftCount);
-                    }else{
+                        ipv4PrefixStr += MatchConvertorUtil.getIpv4Mask(maskMatchEntry);
+                    } else {
                         //Openflow Spec : 1.3.2
                         //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
                         // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
                         // statistics response.
 
-                        ipv4PrefixStr+=PREFIX_SEPARATOR + "32";
+                        ipv4PrefixStr += PREFIX_SEPARATOR + "32";
                     }
                     if (ofMatch.getOxmMatchField().equals(Ipv4Src.class)) {
                         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
@@ -751,6 +809,30 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     }
                     matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
                 }
+            } else if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)
+                    || ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
+                Ipv4AddressMatchEntry ipv4AddressMatchEntry = ofMatch.getAugmentation(Ipv4AddressMatchEntry.class);
+                if (ipv4AddressMatchEntry != null) {
+                    String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
+                    MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
+                    if (maskMatchEntry != null) {
+                        ipv4PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
+                    } else {
+                        //Openflow Spec : 1.3.2
+                        //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
+                        // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
+                        // statistics response.
+
+                        ipv4PrefixStr += PREFIX_SEPARATOR + "32";
+                    }
+                    if (ofMatch.getOxmMatchField().equals(TunnelIpv4Dst.class)) {
+                        tunnelIpv4MatchBuilder.setTunnelIpv4Destination(new Ipv4Prefix(ipv4PrefixStr));
+                    }
+                    if (ofMatch.getOxmMatchField().equals(TunnelIpv4Src.class)) {
+                        tunnelIpv4MatchBuilder.setTunnelIpv4Source(new Ipv4Prefix(ipv4PrefixStr));
+                    }
+                    matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
+                }
             } else if (ofMatch.getOxmMatchField().equals(ArpOp.class)) {
                 OpCodeMatchEntry opCodeMatchEntry = ofMatch.getAugmentation(OpCodeMatchEntry.class);
                 if (opCodeMatchEntry != null) {
@@ -764,7 +846,14 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     String ipv4PrefixStr = ipv4AddressMatchEntry.getIpv4Address().getValue();
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
                     if (maskMatchEntry != null) {
-                        ipv4PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
+                        ipv4PrefixStr += MatchConvertorUtil.getIpv4Mask(maskMatchEntry);
+                    } else {
+                        //Openflow Spec : 1.3.2
+                        //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
+                        // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
+                        // statistics response.
+
+                        ipv4PrefixStr += PREFIX_SEPARATOR + "32";
                     }
                     if (ofMatch.getOxmMatchField().equals(ArpSpa.class)) {
                         arpMatchBuilder.setArpSourceTransportAddress(new Ipv4Prefix(ipv4PrefixStr));
@@ -807,11 +896,11 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 if (ipv6AddressMatchEntry != null) {
                     String ipv6PrefixStr = ipv6AddressMatchEntry.getIpv6Address().getValue();
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
-                    if(maskMatchEntry != null){
+                    if (maskMatchEntry != null) {
                         ipv6PrefixStr += PREFIX_SEPARATOR
                                 + MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskMatchEntry.getMask());
                     }
-                        
+
                     if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
                         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(ipv6PrefixStr));
                     }
@@ -827,7 +916,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     ipv6LabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(ipv6FlabelMatchEntry.getIpv6Flabel()));
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
                     if (maskMatchEntry != null) {
-                        ipv6LabelBuilder.setFlabelMask(new Ipv6FlowLabel(new Long(ByteUtil
+                        ipv6LabelBuilder.setFlabelMask(new Ipv6FlowLabel(Long.valueOf(ByteUtil
                                 .bytesToUnsignedInt(maskMatchEntry.getMask()))));
                     }
                     ipv6MatchBuilder.setIpv6Label(ipv6LabelBuilder.build());
@@ -892,7 +981,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     pbbBuilder.setPbbIsid(isidMatchEntry.getIsid());
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
                     if (maskMatchEntry != null) {
-                        pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedInt(maskMatchEntry.getMask()));
+                        pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedMedium(maskMatchEntry.getMask()));
                     }
                     protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
@@ -918,8 +1007,10 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 }
             }
         }
-        return matchBuilder.build();
+        return matchBuilder;
     }
+
+
     private static MatchEntries toOfMplsPbb(final Pbb pbb) {
         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
         boolean hasmask = false;
@@ -930,7 +1021,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
         if (pbb.getPbbMask() != null) {
             hasmask = true;
-            addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(pbb.getPbbMask()));
+            addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedMediumToBytes(pbb.getPbbMask()));
         }
         matchEntriesBuilder.setHasMask(hasmask);
         return matchEntriesBuilder.build();
@@ -1027,7 +1118,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
     }
 
     private static MatchEntries toOfMetadata(final Class<? extends MatchField> field, final BigInteger metadata,
-            final BigInteger metadataMask) {
+                                             final BigInteger metadataMask) {
         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
         boolean hasmask = false;
         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
@@ -1043,8 +1134,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
     }
 
     public static MatchEntries toOfMacAddress(final Class<? extends MatchField> field,
-            final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress macAddress,
-            final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress mask) {
+                                              final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress macAddress,
+                                              final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress mask) {
         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
         boolean hasmask = false;
         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
@@ -1070,7 +1161,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
     }
 
     private static MatchEntries toOfLayer3Port(final Class<? extends MatchField> field,
-            final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber) {
+                                               final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber portNumber) {
         MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
         matchEntriesBuilder.setHasMask(false);
@@ -1236,18 +1327,6 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         return matchEntriesBuilder.build();
     }
 
-    //FIXME: move to extensible support
-    private static MatchEntries toOfTcpFlag(Integer tcpFlag) {
-        MatchEntriesBuilder matchBuilder = new MatchEntriesBuilder();
-        matchBuilder.setOxmClass(Nxm1Class.class);
-        matchBuilder.setHasMask(false);
-        matchBuilder.setOxmMatchField(TcpFlag.class);
-        TcpFlagMatchEntryBuilder tcpFlagBuilder = new TcpFlagMatchEntryBuilder();
-        tcpFlagBuilder.setTcpFlag(tcpFlag);
-        matchBuilder.addAugmentation(TcpFlagMatchEntry.class, tcpFlagBuilder.build());
-        return matchBuilder.build();
-    }
-
     private static void addMaskAugmentation(final MatchEntriesBuilder builder, final byte[] mask) {
         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
         maskBuilder.setMask(mask);
@@ -1301,7 +1380,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
 
     /**
      * @return true if Ipv4Prefix contains prefix (and it is used in mask),
-     *         false otherwise
+     * false otherwise
      */
     private static boolean addIpv4PrefixAugmentation(final MatchEntriesBuilder builder, final Ipv4Prefix address) {
         boolean hasMask = false;
@@ -1310,7 +1389,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
 
         final int prefix;
         if (addressParts.hasNext()) {
-            prefix = Integer.parseInt(addressParts.next());
+            int potentionalPrefix = Integer.parseInt(addressParts.next());
+            prefix = potentionalPrefix < 32 ? potentionalPrefix : 0;
         } else {
             prefix = 0;
         }
@@ -1320,8 +1400,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         builder.addAugmentation(Ipv4AddressMatchEntry.class, ipv4AddressBuilder.build());
         if (prefix != 0) {
             int mask = 0xffffffff << (32 - prefix);
-            byte[] maskBytes = new byte[] { (byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
-                    (byte) mask };
+            byte[] maskBytes = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
+                    (byte) mask};
             addMaskAugmentation(builder, maskBytes);
             hasMask = true;
         }
@@ -1338,15 +1418,16 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
      * Method converts OF SetField action to SAL SetFiled action.
      *
      * @param action
+     * @param ofVersion current ofp version
      * @return
      */
     public static SetField fromOFSetFieldToSALSetFieldAction(
-            final Action action) {
+            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);
-        Match match = OfMatchToSALMatchConvertor(oxmFields.getMatchEntries(), null);
-        setField.fieldsFrom(match);
+        MatchBuilder match = OfMatchToSALMatchConvertor(oxmFields.getMatchEntries(), null, ofVersion);
+        setField.fieldsFrom(match.build());
         return setField.build();
     }
 }