BUG-1994: IP ToS bits match field for OF 1.0
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorV10Impl.java
index df2fed4d5ca0ea7636175c49920a957c0afbf61d..0bf080c5680ee9425b8578a3ddb26b2ad271290a 100644 (file)
@@ -8,8 +8,15 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
 
+import java.math.BigInteger;
+import java.util.Iterator;
+
+import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
+import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
+import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 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.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
@@ -24,15 +31,21 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.matc
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;
 
 /**
- * 
+ *
  */
 public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
-    
+
     /** default MAC */
     public static final MacAddress zeroMac = new MacAddress("00:00:00:00:00:00");
     /** default IPv4 */
     public static final Ipv4Address zeroIPv4 = new Ipv4Address("0.0.0.0");
 
+    /*
+     * The value 0xffff (OFP_VLAN_NONE) is used to indicate
+     * that no VLAN ID is set for OF Flow.
+     */
+    private static final int OFP_VLAN_NONE = 0xffff;
+
     /**
      * Method builds openflow 1.0 specific match (MatchV10) from MD-SAL match.
      * @param match MD-SAL match
@@ -40,9 +53,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @author avishnoi@in.ibm.com
      */
     @Override
-    public MatchV10 convert(Match match) {
+    public MatchV10 convert(final Match match,final BigInteger datapathid) {
         MatchV10Builder matchBuilder = new MatchV10Builder();
-        boolean _aLL = true; // NO MATCHING (all match items are wildcarded) by default
         boolean _dLDST = true;
         boolean _dLSRC = true;
         boolean _dLTYPE = true;
@@ -53,12 +65,12 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
         boolean _nWTOS = true;
         boolean _tPDST = true;
         boolean _tPSRC = true;
-        
+
         matchBuilder.setInPort(0);
         matchBuilder.setDlDst(zeroMac);
         matchBuilder.setDlSrc(zeroMac);
         matchBuilder.setDlType(0);
-        matchBuilder.setDlVlan(0);
+        matchBuilder.setDlVlan(OFP_VLAN_NONE);
         matchBuilder.setDlVlanPcp((short) 0);
         matchBuilder.setNwDst(zeroIPv4);
         matchBuilder.setNwDstMask((short) 0);
@@ -68,61 +80,55 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
         matchBuilder.setNwTos((short) 0);
         matchBuilder.setTpSrc(0);
         matchBuilder.setTpDst(0);
-        
-        EthernetMatch ethernetMatch = match.getEthernetMatch();
-        if(ethernetMatch!= null){
-            _aLL = false;
-            _dLDST = convertEthernetDlDst(matchBuilder, ethernetMatch);
-            _dLSRC = convertEthernetDlSrc(matchBuilder, ethernetMatch);
-            _dLTYPE = convertEthernetDlType(matchBuilder, ethernetMatch);
-        }
-        VlanMatch vlanMatch = match.getVlanMatch();
-        if(vlanMatch!= null){
-            _aLL = false;
-            _dLVLAN = convertDlVlan(matchBuilder, vlanMatch);
-            _dLVLANPCP = convertDlVlanPcp(matchBuilder, vlanMatch);
-        }
-        Long inPort = match.getInPort();
-        if(inPort!=null){
-            _aLL = false;
-            _iNPORT = convertInPortMatch(matchBuilder, inPort);
-        }
-        Layer3Match l3Match = match.getLayer3Match();
-        if(l3Match != null){
-            _aLL = false;
-            if(l3Match instanceof Ipv4Match){
-                Ipv4Match ipv4 = (Ipv4Match)l3Match;
-                _tPSRC = convertL3Ipv4SrcMatch(matchBuilder, ipv4);
-                _tPDST = convertL3Ipv4DstMatch(matchBuilder, ipv4);
+
+        if (match != null) {
+            EthernetMatch ethernetMatch = match.getEthernetMatch();
+            if(ethernetMatch!= null){
+                _dLDST = convertEthernetDlDst(matchBuilder, ethernetMatch);
+                _dLSRC = convertEthernetDlSrc(matchBuilder, ethernetMatch);
+                _dLTYPE = convertEthernetDlType(matchBuilder, ethernetMatch);
             }
-        }
-        IpMatch ipMatch = match.getIpMatch();
-        if(ipMatch!=null){
-            _aLL = false;
-            _nWPROTO = convertNwProto(matchBuilder, ipMatch);
-            _nWTOS = convertNwTos(matchBuilder, ipMatch);
-        }
-        Layer4Match layer4Match = match.getLayer4Match();
-        if (layer4Match != null) {
-            _aLL = false;
-            if (layer4Match instanceof TcpMatch) {
-                TcpMatch tcpMatch = (TcpMatch) layer4Match;
-                _tPSRC = convertL4TpSrcMatch(matchBuilder, tcpMatch);
-                _tPDST = convertL4TpDstMatch(matchBuilder, tcpMatch);
-            } else if (layer4Match instanceof UdpMatch) {
-                UdpMatch udpMatch = (UdpMatch) layer4Match;
-                _tPSRC = convertL4UdpSrcMatch(matchBuilder, udpMatch);
-                _tPDST = convertL4UdpDstMatch(matchBuilder, udpMatch);
+            VlanMatch vlanMatch = match.getVlanMatch();
+            if(vlanMatch!= null){
+                _dLVLAN = convertDlVlan(matchBuilder, vlanMatch);
+                _dLVLANPCP = convertDlVlanPcp(matchBuilder, vlanMatch);
+            }
+            NodeConnectorId inPort = match.getInPort();
+            if(inPort!=null){
+                _iNPORT = convertInPortMatch(matchBuilder, inPort);
+            }
+            Layer3Match l3Match = match.getLayer3Match();
+            if(l3Match != null){
+                if(l3Match instanceof Ipv4Match){
+                    Ipv4Match ipv4 = (Ipv4Match)l3Match;
+                    _tPSRC = convertL3Ipv4SrcMatch(matchBuilder, ipv4);
+                    _tPDST = convertL3Ipv4DstMatch(matchBuilder, ipv4);
+                }
+            }
+            IpMatch ipMatch = match.getIpMatch();
+            if(ipMatch!=null){
+                _nWPROTO = convertNwProto(matchBuilder, ipMatch);
+                _nWTOS = convertNwTos(matchBuilder, ipMatch);
+            }
+            Layer4Match layer4Match = match.getLayer4Match();
+            if (layer4Match != null) {
+                if (layer4Match instanceof TcpMatch) {
+                    TcpMatch tcpMatch = (TcpMatch) layer4Match;
+                    _tPSRC = convertL4TpSrcMatch(matchBuilder, tcpMatch);
+                    _tPDST = convertL4TpDstMatch(matchBuilder, tcpMatch);
+                } else if (layer4Match instanceof UdpMatch) {
+                    UdpMatch udpMatch = (UdpMatch) layer4Match;
+                    _tPSRC = convertL4UdpSrcMatch(matchBuilder, udpMatch);
+                    _tPDST = convertL4UdpDstMatch(matchBuilder, udpMatch);
+                }
             }
         }
-        
-        
+
         FlowWildcardsV10 wildCards = new FlowWildcardsV10(
-                _aLL, _dLDST, _dLSRC, _dLTYPE, _dLVLAN, 
+                _dLDST, _dLSRC, _dLTYPE, _dLVLAN,
                 _dLVLANPCP, _iNPORT, _nWPROTO, _nWTOS, _tPDST, _tPSRC);
-        checkWildCards(wildCards);
         matchBuilder.setWildcards(wildCards);
-        
+
         return matchBuilder.build();
     }
 
@@ -131,8 +137,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param udpMatch
      * @return is wildCard
      */
-    private static boolean convertL4UdpDstMatch(MatchV10Builder matchBuilder,
-            UdpMatch udpMatch) {
+    private static boolean convertL4UdpDstMatch(final MatchV10Builder matchBuilder,
+            final UdpMatch udpMatch) {
         if (udpMatch.getUdpDestinationPort() != null) {
             matchBuilder.setTpDst(udpMatch.getUdpDestinationPort().getValue());
             return false;
@@ -145,8 +151,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param udpMatch
      * @return is wildCard
      */
-    private static boolean convertL4UdpSrcMatch(MatchV10Builder matchBuilder,
-            UdpMatch udpMatch) {
+    private static boolean convertL4UdpSrcMatch(final MatchV10Builder matchBuilder,
+            final UdpMatch udpMatch) {
         if (udpMatch.getUdpSourcePort() != null) {
             matchBuilder.setTpSrc(udpMatch.getUdpSourcePort().getValue());
             return false;
@@ -159,8 +165,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param tcpMatch
      * @return is wildCard
      */
-    private static boolean convertL4TpDstMatch(MatchV10Builder matchBuilder,
-            TcpMatch tcpMatch) {
+    private static boolean convertL4TpDstMatch(final MatchV10Builder matchBuilder,
+            final TcpMatch tcpMatch) {
         if (tcpMatch.getTcpDestinationPort() != null) {
             matchBuilder.setTpDst(tcpMatch.getTcpDestinationPort().getValue());
             return false;
@@ -173,13 +179,13 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param tcpMatch
      * @return is wildCard
      */
-    private static boolean convertL4TpSrcMatch(MatchV10Builder matchBuilder,
-            TcpMatch tcpMatch) {
+    private static boolean convertL4TpSrcMatch(final MatchV10Builder matchBuilder,
+            final TcpMatch tcpMatch) {
         if (tcpMatch.getTcpSourcePort() != null) {
             matchBuilder.setTpSrc(tcpMatch.getTcpSourcePort().getValue());
-            return true;
+            return false;
         }
-        return false;
+        return true;
     }
 
     /**
@@ -187,13 +193,13 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ipMatch
      * @return is wildCard
      */
-    private static boolean convertNwTos(MatchV10Builder matchBuilder,
-            IpMatch ipMatch) {
+    private static boolean convertNwTos(final MatchV10Builder matchBuilder,
+            final IpMatch ipMatch) {
         if (ipMatch.getIpDscp() != null) {
-            matchBuilder.setNwTos(ipMatch.getIpDscp().getValue());
-            return true;
+            matchBuilder.setNwTos(ActionUtil.dscpToTos(ipMatch.getIpDscp().getValue()));
+            return false;
         }
-        return false;
+        return true;
     }
 
     /**
@@ -201,7 +207,7 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ipMatch
      * @return is wildCard
      */
-    private static boolean convertNwProto(MatchV10Builder matchBuilder, IpMatch ipMatch) {
+    private static boolean convertNwProto(final MatchV10Builder matchBuilder, final IpMatch ipMatch) {
         if (ipMatch.getIpProtocol() != null) {
             matchBuilder.setNwProto(ipMatch.getIpProtocol());
             return false;
@@ -214,13 +220,12 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ipv4
      * @return is wildCard
      */
-    private static boolean convertL3Ipv4DstMatch(MatchV10Builder matchBuilder,
-            Ipv4Match ipv4) {
+    private static boolean convertL3Ipv4DstMatch(final MatchV10Builder matchBuilder,
+            final Ipv4Match ipv4) {
         if(ipv4.getIpv4Destination()!=null){
-            String[] addressParts = ipv4.getIpv4Destination().getValue().split("/");
+            Iterator<String> addressParts = MatchConvertorImpl.PREFIX_SPLITTER.split(ipv4.getIpv4Destination().getValue()).iterator();
+            Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
             Integer prefix = buildPrefix(addressParts);
-
-            Ipv4Address ipv4Address = new Ipv4Address(addressParts[0]);
             matchBuilder.setNwDst(ipv4Address);
             matchBuilder.setNwDstMask(prefix.shortValue());
             return false;
@@ -233,13 +238,13 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ipv4
      * @return is wildCard
      */
-    private static boolean convertL3Ipv4SrcMatch(MatchV10Builder matchBuilder,
-            Ipv4Match ipv4) {
+    private static boolean convertL3Ipv4SrcMatch(final MatchV10Builder matchBuilder,
+            final Ipv4Match ipv4) {
         if(ipv4.getIpv4Source()!=null){
-            String[] addressParts = ipv4.getIpv4Source().getValue().split(MatchConvertorImpl.PREFIX_SEPARATOR);
-            Ipv4Address ipv4Address = new Ipv4Address(addressParts[0]);
+            Iterator<String> addressParts = MatchConvertorImpl.PREFIX_SPLITTER.split(ipv4.getIpv4Source().getValue()).iterator();
+            Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
             int prefix = buildPrefix(addressParts);
-            
+
             matchBuilder.setNwSrc(ipv4Address);
             matchBuilder.setNwSrcMask((short) prefix);
             return false;
@@ -251,10 +256,10 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param addressParts
      * @return
      */
-    private static int buildPrefix(String[] addressParts) {
-        int prefix = 0;
-        if (addressParts.length > 1) {
-            prefix = Integer.parseInt(addressParts[1]);
+    private static int buildPrefix(final Iterator<String> addressParts) {
+        int prefix = 32;
+        if (addressParts.hasNext()) {
+            prefix = Integer.parseInt(addressParts.next());
         }
         return prefix;
     }
@@ -262,10 +267,10 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
     /**
      * @param matchBuilder
      * @param vlanMatch
-     * @return 
+     * @return
      */
-    private static boolean convertDlVlanPcp(MatchV10Builder matchBuilder,
-            VlanMatch vlanMatch) {
+    private static boolean convertDlVlanPcp(final MatchV10Builder matchBuilder,
+            final VlanMatch vlanMatch) {
         if (vlanMatch.getVlanPcp() != null) {
             matchBuilder.setDlVlanPcp(vlanMatch.getVlanPcp().getValue());
             return false;
@@ -276,11 +281,12 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
     /**
      * @param matchBuilder
      * @param vlanMatch
-     * @return 
+     * @return
      */
-    private static boolean convertDlVlan(MatchV10Builder matchBuilder, VlanMatch vlanMatch) {
+    private static boolean convertDlVlan(final MatchV10Builder matchBuilder, final VlanMatch vlanMatch) {
         if (vlanMatch.getVlanId() != null) {
-            matchBuilder.setDlVlan(vlanMatch.getVlanId().getVlanId().getValue());
+            int vlanId = vlanMatch.getVlanId().getVlanId().getValue();
+            matchBuilder.setDlVlan((vlanId == 0 ? OFP_VLAN_NONE : vlanId));
             return false;
         }
         return true;
@@ -291,8 +297,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ethernetMatch
      * @return is wildCard
      */
-    private static boolean convertEthernetDlType(MatchV10Builder matchBuilder,
-            EthernetMatch ethernetMatch) {
+    private static boolean convertEthernetDlType(final MatchV10Builder matchBuilder,
+            final EthernetMatch ethernetMatch) {
         if (ethernetMatch.getEthernetType() != null) {
             matchBuilder.setDlType(ethernetMatch.getEthernetType().getType().getValue().intValue());
             return false;
@@ -305,8 +311,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ethernetMatch
      * @return is wildCard
      */
-    private static boolean convertEthernetDlSrc(MatchV10Builder matchBuilder,
-            EthernetMatch ethernetMatch) {
+    private static boolean convertEthernetDlSrc(final MatchV10Builder matchBuilder,
+            final EthernetMatch ethernetMatch) {
         if (ethernetMatch.getEthernetSource() != null) {
             matchBuilder.setDlSrc(ethernetMatch.getEthernetSource().getAddress());
             return false;
@@ -319,8 +325,8 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param ethernetMatch
      * @return is wildCard
      */
-    private static boolean convertEthernetDlDst(MatchV10Builder matchBuilder,
-            EthernetMatch ethernetMatch) {
+    private static boolean convertEthernetDlDst(final MatchV10Builder matchBuilder,
+            final EthernetMatch ethernetMatch) {
         if (ethernetMatch.getEthernetDestination() != null) {
             matchBuilder.setDlDst(ethernetMatch.getEthernetDestination().getAddress());
             return false;
@@ -332,44 +338,12 @@ public class MatchConvertorV10Impl implements MatchConvertor<MatchV10> {
      * @param matchBuilder
      * @param inPort
      */
-    private static boolean convertInPortMatch(MatchV10Builder matchBuilder, Long inPort) {
+    private static boolean convertInPortMatch(final MatchV10Builder matchBuilder, final NodeConnectorId inPort) {
         if (inPort != null) {
-            matchBuilder.setInPort(inPort.intValue());
+            matchBuilder.setInPort(InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF10, inPort).intValue());
             return false;
         }
         return true;
     }
 
-    /**
-     * checks consistency between flag ALL and other flags (true = wildcard is on; false = use match value):
-     * <ul>
-     *   <li>if wc.ALL == true then all other flags must be set to true</li>
-     *   <li>if wc.ALL == false then at least one of other flags must be set to false</li>
-     * </ul>
-     * @param wildCards [wc]
-     */
-    private static void checkWildCards(FlowWildcardsV10 wc) {
-        boolean atLeastOneNonWildCard = false
-                || ! wc.isINPORT()
-                || ! wc.isDLVLAN()
-                || ! wc.isDLSRC()
-                || ! wc.isDLDST()
-                || ! wc.isDLTYPE()
-                || ! wc.isNWPROTO()
-                || ! wc.isTPSRC()
-                || ! wc.isTPDST()
-                || ! wc.isDLVLANPCP()
-                || ! wc.isNWTOS();
-        
-        if (atLeastOneNonWildCard && wc.isALL()) {
-            throw new IllegalStateException(
-                    "[wildcard.all == true] conflicts with at least one non wildcard flag: "+wc);
-        }
-        
-        if (!atLeastOneNonWildCard && !wc.isALL()) {
-            throw new IllegalStateException(
-                    "[wildcard.all == false] conflicts with all set to wildcard flags: "+wc);
-        }
-    }
-
 }