L3: Add eth to br-ex
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / MatchUtils.java
index 0bd949d4bdbc21b3775b622bf72634df5657f035..dd7222080a038e32243008a9970afb0902104088 100644 (file)
@@ -80,6 +80,7 @@ public class MatchUtils {
     public static final String TCP = "tcp";
     public static final String UDP = "udp";
     private static final int TCP_SYN = 0x0002;
+    public static final String ICMP = "icmp";
 
     /**
      * Create Ingress Port Match dpidLong, inPort
@@ -193,19 +194,13 @@ public class MatchUtils {
     /**
      * Match ICMP code and type
      *
-     * @param matchBuilder MatchBuilder Object without a match yet
+     * @param matchBuilder MatchBuilder Object
      * @param type         short representing an ICMP type
      * @param code         short representing an ICMP code
      * @return matchBuilder Map MatchBuilder Object with a match
      */
     public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
 
-        EthernetMatchBuilder eth = new EthernetMatchBuilder();
-        EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
-        ethTypeBuilder.setType(new EtherType(0x0800L));
-        eth.setEthernetType(ethTypeBuilder.build());
-        matchBuilder.setEthernetMatch(eth.build());
-
         // Build the IPv4 Match requied per OVS Syntax
         IpMatchBuilder ipmatch = new IpMatchBuilder();
         ipmatch.setIpProtocol((short) 1);
@@ -894,14 +889,15 @@ public class MatchUtils {
     }
 
     /**
-     * Create a DHCP match with pot provided
+     * Create a DHCP match with pot provided.
      *
      * @param matchBuilder the match builder
      * @param srcPort the source port
      * @param dstPort the destination port
      * @return the DHCP match
      */
-    public static MatchBuilder createDHCPMatch(MatchBuilder matchBuilder, int srcPort, int dstPort) {
+    public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
+                                               int srcPort, int dstPort) {
 
         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
@@ -931,7 +927,7 @@ public class MatchUtils {
      * @param dstPort the destination port
      * @return the DHCP server match
      */
-    public static MatchBuilder createDHCPServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
+    public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
             int dstPort) {
 
         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
@@ -959,12 +955,13 @@ public class MatchUtils {
     }
 
     /**
+     * Creates a Match with src ip address mac address set.
      * @param matchBuilder MatchBuilder Object
      * @param srcip String containing an IPv4 prefix
      * @param srcMac The source macAddress
      * @return matchBuilder Map Object with a match
      */
-    public static MatchBuilder createSrcL3IPv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
+    public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
 
         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
@@ -982,6 +979,89 @@ public class MatchUtils {
 
     }
 
+    /**
+     * Creates a ether net match with ether type set to 0x0800L.
+     * @param matchBuilder MatchBuilder Object
+     * @param srcMac The source macAddress
+     * @param dstMac The destination mac address
+     * @return matchBuilder Map Object with a match
+     */
+    public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
+    {
+        EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
+        ethTypeBuilder.setType(new EtherType(0x0800L));
+        EthernetMatchBuilder eth = new EthernetMatchBuilder();
+        eth.setEthernetType(ethTypeBuilder.build());
+        if (null != srcMac) {
+            eth.setEthernetSource(new EthernetSourceBuilder()
+            .setAddress(new MacAddress(srcMac)).build());
+        }
+        if (null != dstMac) {
+            eth.setEthernetDestination(new EthernetDestinationBuilder()
+                           .setAddress(new MacAddress(dstMac)).build());
+        }
+        matchBuilder.setEthernetMatch(eth.build());
+        return matchBuilder;
+    }
+    /**
+     * Adds remote Ip prefix to existing match.
+     * @param matchBuilder The match builder
+     * @param sourceIpPrefix The source IP prefix
+     * @param destIpPrefix The destination IP prefix
+     * @return matchBuilder Map Object with a match
+     */
+    public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
+                                          Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
+        Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
+        if (null != sourceIpPrefix) {
+            ipv4match.setIpv4Source(sourceIpPrefix);
+        }
+        if (null != destIpPrefix) {
+            ipv4match.setIpv4Destination(destIpPrefix);
+        }
+        matchBuilder.setLayer3Match(ipv4match.build());
+
+        return matchBuilder;
+    }
+    /**
+     * Add a layer4 match to an existing match
+     *
+     * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
+     * @param protocol The layer4 protocol
+     * @param srcPort The src port
+     * @param destPort The destination port
+     * @return matchBuilder Map Object with a match
+     */
+    public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
+                                              int protocol, int srcPort, int destPort) {
+        IpMatchBuilder ipmatch = new IpMatchBuilder();
+        if (TCP_SHORT == protocol) {
+            ipmatch.setIpProtocol(TCP_SHORT);
+            TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
+            if (0 != srcPort) {
+                tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
+            }
+            if (0 != destPort) {
+                tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
+            }
+            matchBuilder.setLayer4Match(tcpmatch.build());
+        } else if (UDP_SHORT == protocol) {
+            ipmatch.setIpProtocol(UDP_SHORT);
+            UdpMatchBuilder udpMatch = new UdpMatchBuilder();
+            if (0 != srcPort) {
+                udpMatch.setUdpSourcePort(new PortNumber(srcPort));
+            }
+            if (0 != destPort) {
+                udpMatch.setUdpDestinationPort(new PortNumber(destPort));
+            }
+            matchBuilder.setLayer4Match(udpMatch.build());
+        }
+        matchBuilder.setIpMatch(ipmatch.build());
+
+        return matchBuilder;
+    }
+
+
     public static class RegMatch {
         final Class<? extends NxmNxReg> reg;
         final Long value;