L3: Add eth to br-ex
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / MatchUtils.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.utils.mdsal.openflow;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 import com.google.common.collect.ImmutableList;
73 import com.google.common.collect.Lists;
74
75 public class MatchUtils {
76     private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
77     public static final short ICMP_SHORT = 1;
78     public static final short TCP_SHORT = 6;
79     public static final short UDP_SHORT = 17;
80     public static final String TCP = "tcp";
81     public static final String UDP = "udp";
82     private static final int TCP_SYN = 0x0002;
83     public static final String ICMP = "icmp";
84
85     /**
86      * Create Ingress Port Match dpidLong, inPort
87      *
88      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
89      * @param dpidLong     Long the datapath ID of a switch/node
90      * @param inPort       Long ingress port on a switch
91      * @return matchBuilder Map MatchBuilder Object with a match
92      */
93     public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
94
95         NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
96         LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
97         matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
98         matchBuilder.setInPort(ncid);
99
100         return matchBuilder;
101     }
102
103     /**
104      * Create EtherType Match
105      *
106      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
107      * @param etherType    Long EtherType
108      * @return matchBuilder Map MatchBuilder Object with a match
109      */
110     public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
111
112         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
113         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
114         ethTypeBuilder.setType(new EtherType(etherType));
115         ethernetMatch.setEthernetType(ethTypeBuilder.build());
116         matchBuilder.setEthernetMatch(ethernetMatch.build());
117
118         return matchBuilder;
119     }
120
121     /**
122      * Create Ethernet Source Match
123      *
124      * @param matchBuilder MatchBuilder Object without a match yet
125      * @param sMacAddr     String representing a source MAC
126      * @return matchBuilder Map MatchBuilder Object with a match
127      */
128     public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
129
130         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
131         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
132         ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
133         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
134         matchBuilder.setEthernetMatch(ethernetMatch.build());
135
136         return matchBuilder;
137     }
138
139     /**
140      * Create Ethernet Destination Match
141      *
142      * @param matchBuilder MatchBuilder Object without a match yet
143      * @param vlanId       Integer representing a VLAN ID Integer representing a VLAN ID
144      * @return matchBuilder Map MatchBuilder Object with a match
145      */
146     public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
147         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
148         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
149         vlanIdBuilder.setVlanId(new VlanId(vlanId));
150         vlanIdBuilder.setVlanIdPresent(present);
151         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
152         matchBuilder.setVlanMatch(vlanMatchBuilder.build());
153
154         return matchBuilder;
155     }
156
157     /**
158      * Create Ethernet Destination Match
159      *
160      * @param matchBuilder MatchBuilder Object without a match yet
161      * @param dMacAddr     String representing a destination MAC
162      * @return matchBuilder Map MatchBuilder Object with a match
163      */
164     public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
165
166         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
167         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
168         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
169         if (mask != null) {
170             ethDestinationBuilder.setMask(mask);
171         }
172         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
173         matchBuilder.setEthernetMatch(ethernetMatch.build());
174
175         return matchBuilder;
176     }
177
178     /**
179      * Tunnel ID Match Builder
180      *
181      * @param matchBuilder MatchBuilder Object without a match yet
182      * @param tunnelId     BigInteger representing a tunnel ID
183      * @return matchBuilder Map MatchBuilder Object with a match
184      */
185     public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
186
187         TunnelBuilder tunnelBuilder = new TunnelBuilder();
188         tunnelBuilder.setTunnelId(tunnelId);
189         matchBuilder.setTunnel(tunnelBuilder.build());
190
191         return matchBuilder;
192     }
193
194     /**
195      * Match ICMP code and type
196      *
197      * @param matchBuilder MatchBuilder Object
198      * @param type         short representing an ICMP type
199      * @param code         short representing an ICMP code
200      * @return matchBuilder Map MatchBuilder Object with a match
201      */
202     public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
203
204         // Build the IPv4 Match requied per OVS Syntax
205         IpMatchBuilder ipmatch = new IpMatchBuilder();
206         ipmatch.setIpProtocol((short) 1);
207         matchBuilder.setIpMatch(ipmatch.build());
208
209         // Build the ICMPv4 Match
210         Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
211         icmpv4match.setIcmpv4Type(type);
212         icmpv4match.setIcmpv4Code(code);
213         matchBuilder.setIcmpv4Match(icmpv4match.build());
214
215         return matchBuilder;
216     }
217
218     /**
219      * @param matchBuilder MatchBuilder Object without a match yet
220      * @param dstip        String containing an IPv4 prefix
221      * @return matchBuilder Map Object with a match
222      */
223     public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
224
225         EthernetMatchBuilder eth = new EthernetMatchBuilder();
226         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
227         ethTypeBuilder.setType(new EtherType(0x0800L));
228         eth.setEthernetType(ethTypeBuilder.build());
229         matchBuilder.setEthernetMatch(eth.build());
230
231         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
232         ipv4match.setIpv4Destination(dstip);
233
234         matchBuilder.setLayer3Match(ipv4match.build());
235
236         return matchBuilder;
237
238     }
239
240     /**
241      * @param matchBuilder MatchBuilder Object without a match yet
242      * @param dstip        String containing an IPv4 prefix
243      * @return matchBuilder Map Object with a match
244      */
245     public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
246         ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
247         arpDstMatch.setArpTargetTransportAddress(dstip);
248         matchBuilder.setLayer3Match(arpDstMatch.build());
249
250         return matchBuilder;
251     }
252
253     /**
254      * @param matchBuilder MatchBuilder Object without a match yet
255      * @param srcip        String containing an IPv4 prefix
256      * @return matchBuilder Map Object with a match
257      */
258     public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
259
260         EthernetMatchBuilder eth = new EthernetMatchBuilder();
261         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
262         ethTypeBuilder.setType(new EtherType(0x0800L));
263         eth.setEthernetType(ethTypeBuilder.build());
264         matchBuilder.setEthernetMatch(eth.build());
265
266         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
267         ipv4match.setIpv4Source(srcip);
268         matchBuilder.setLayer3Match(ipv4match.build());
269
270         return matchBuilder;
271
272     }
273
274     /**
275      * Create Source TCP Port Match
276      *
277      * @param matchBuilder MatchBuilder Object without a match yet
278      * @param tcpport      Integer representing a source TCP port
279      * @return matchBuilder Map MatchBuilder Object with a match
280      */
281     public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
282
283         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
284         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
285         ethTypeBuilder.setType(new EtherType(0x0800L));
286         ethType.setEthernetType(ethTypeBuilder.build());
287         matchBuilder.setEthernetMatch(ethType.build());
288
289         IpMatchBuilder ipmatch = new IpMatchBuilder();
290         ipmatch.setIpProtocol((short) 6);
291         matchBuilder.setIpMatch(ipmatch.build());
292
293         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
294         tcpmatch.setTcpSourcePort(tcpport);
295         matchBuilder.setLayer4Match(tcpmatch.build());
296
297         return matchBuilder;
298
299     }
300
301     /**
302      * Create Destination TCP Port Match
303      *
304      * @param matchBuilder MatchBuilder Object without a match yet
305      * @param tcpDstPort   Integer representing a destination TCP port
306      * @return matchBuilder Map MatchBuilder Object with a match
307      */
308     public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
309
310         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
311         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
312         ethTypeBuilder.setType(new EtherType(0x0800L));
313         ethType.setEthernetType(ethTypeBuilder.build());
314         matchBuilder.setEthernetMatch(ethType.build());
315
316         IpMatchBuilder ipmatch = new IpMatchBuilder();
317         ipmatch.setIpProtocol((short) 6);
318         matchBuilder.setIpMatch(ipmatch.build());
319
320         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
321         tcpmatch.setTcpDestinationPort(tcpDstPort);
322         matchBuilder.setLayer4Match(tcpmatch.build());
323
324         return matchBuilder;
325     }
326
327     /**
328      * Test match for TCP_Flags
329      *
330      * @param matchBuilder MatchBuilder Object without a match yet
331      * @param tcpPort  PortNumber representing a destination TCP port
332      * @param tcpFlag  int representing a tcp_flag
333      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
334      * <p>
335      * Defined TCP Flag values in OVS v2.1+
336      * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
337      * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
338      * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS  0x100
339      */
340     public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
341
342         // Ethertype match
343         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
344         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
345         ethTypeBuilder.setType(new EtherType(0x0800L));
346         ethernetType.setEthernetType(ethTypeBuilder.build());
347         matchBuilder.setEthernetMatch(ethernetType.build());
348
349         // TCP Protocol Match
350         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
351         ipMatch.setIpProtocol((short) 6);
352         matchBuilder.setIpMatch(ipMatch.build());
353
354         // TCP Port Match
355         PortNumber dstPort = new PortNumber(tcpPort);
356         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
357         tcpMatch.setTcpDestinationPort(dstPort);
358         matchBuilder.setLayer4Match(tcpMatch.build());
359
360         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
361         tcpFlagMatch.setTcpFlag(tcpFlag);
362         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
363         return matchBuilder;
364     }
365
366     /**
367      * @return MatchBuilder containing the metadata match values
368      */
369     public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData,  BigInteger metaDataMask) {
370
371         // metadata matchbuilder
372         MetadataBuilder metadata = new MetadataBuilder();
373         metadata.setMetadata(metaData);
374         // Optional metadata mask
375         if (metaDataMask != null) {
376             metadata.setMetadataMask(metaDataMask);
377         }
378         matchBuilder.setMetadata(metadata.build());
379
380         return matchBuilder;
381     }
382
383     /**
384      * Create  TCP Port Match
385      *
386      * @param matchBuilder MatchBuilder Object without a match yet
387      * @param ipProtocol   Integer representing the IP protocol
388      * @return matchBuilder Map MatchBuilder Object with a match
389      */
390     public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
391
392         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
393         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
394         ethTypeBuilder.setType(new EtherType(0x0800L));
395         ethType.setEthernetType(ethTypeBuilder.build());
396         matchBuilder.setEthernetMatch(ethType.build());
397
398         IpMatchBuilder ipMmatch = new IpMatchBuilder();
399         if (ipProtocol == TCP_SHORT) {
400             ipMmatch.setIpProtocol(TCP_SHORT);
401         }
402         else if (ipProtocol == UDP_SHORT) {
403             ipMmatch.setIpProtocol(UDP_SHORT);
404         }
405         else if (ipProtocol == ICMP_SHORT) {
406             ipMmatch.setIpProtocol(ICMP_SHORT);
407         }
408         matchBuilder.setIpMatch(ipMmatch.build());
409         return matchBuilder;
410     }
411
412     /**
413      * Create tcp syn with proto match.
414      *
415      * @param matchBuilder the match builder
416      * @return matchBuilder match builder
417      */
418     public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
419
420         // Ethertype match
421         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
422         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
423         ethTypeBuilder.setType(new EtherType(0x0800L));
424         ethernetType.setEthernetType(ethTypeBuilder.build());
425         matchBuilder.setEthernetMatch(ethernetType.build());
426
427         // TCP Protocol Match
428         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
429         ipMatch.setIpProtocol((short) 6);
430         matchBuilder.setIpMatch(ipMatch.build());
431
432         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
433         tcpFlagMatch.setTcpFlag(TCP_SYN);
434         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
435         return matchBuilder;
436     }
437
438     /**
439      * Create tcp proto syn match.
440      *
441      * @param matchBuilder the match builder
442      * @return matchBuilder match builder
443      */
444     public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
445
446         // TCP Protocol Match
447         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
448         ipMatch.setIpProtocol((short) 6);
449         matchBuilder.setIpMatch(ipMatch.build());
450
451         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
452         tcpFlagMatch.setTcpFlag(TCP_SYN);
453         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
454         return matchBuilder;
455     }
456
457     /**
458      * Create dmac tcp port with flag match.
459      *
460      * @param matchBuilder the match builder
461      * @param attachedMac the attached mac
462      * @param tcpFlag the tcp flag
463      * @param tunnelID the tunnel iD
464      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
465      */
466     public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
467             String attachedMac, Integer tcpFlag, String tunnelID) {
468         return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
469     }
470
471     /**
472      * Create dmac ipSa match.
473      *
474      * @param matchBuilder the match builder
475      * @param attachedMac the attached mac
476      * @param ipPrefix the src ipPrefix
477      * @param tunnelID the tunnel iD
478      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
479      */
480     public static MatchBuilder createDmacIpSaMatch(
481             MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
482         return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
483     }
484
485     /**
486      * Create dmac tcp port ipSa with flag match.
487      *
488      * @param matchBuilder the match builder
489      * @param attachedMac the attached mac
490      * @param tcpFlag the tcp flag
491      * @param ipPrefix the src ipPrefix
492      * @param tunnelID the tunnel iD
493      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
494      */
495     public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
496             MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
497
498         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
499         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
500         ethTypeBuilder.setType(new EtherType(0x0800L));
501         ethernetMatch.setEthernetType(ethTypeBuilder.build());
502
503         if (attachedMac != null) {
504             EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
505             ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
506             ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
507             matchBuilder.setEthernetMatch(ethernetMatch.build());
508         }
509
510         if (tcpFlag != null) {
511             // TCP Protocol Match
512             IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
513             ipMatch.setIpProtocol(TCP_SHORT);
514             matchBuilder.setIpMatch(ipMatch.build());
515
516             TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
517             tcpFlagMatch.setTcpFlag(tcpFlag);
518             matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
519         }
520
521         if (tunnelID != null) {
522             TunnelBuilder tunnelBuilder = new TunnelBuilder();
523             tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
524             matchBuilder.setTunnel(tunnelBuilder.build());
525         }
526
527         if (ipPrefix != null) {
528             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
529             ipv4match.setIpv4Source(ipPrefix);
530             matchBuilder.setLayer3Match(ipv4match.build());
531         }
532
533         return matchBuilder;
534     }
535
536     /**
537      * Create dmac tcp syn match.
538      *
539      * @param matchBuilder the match builder
540      * @param attachedMac the attached mac
541      * @param tcpPort the tcp port
542      * @param tcpFlag the tcp flag
543      * @param tunnelID the tunnel iD
544      * @return the match builder
545      */
546     public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
547             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
548
549         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
550         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
551         ethTypeBuilder.setType(new EtherType(0x0800L));
552         ethernetMatch.setEthernetType(ethTypeBuilder.build());
553
554         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
555         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
556         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
557         matchBuilder.setEthernetMatch(ethernetMatch.build());
558
559         // TCP Protocol Match
560         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
561         ipMatch.setIpProtocol((short) 6);
562         matchBuilder.setIpMatch(ipMatch.build());
563
564         // TCP Port Match
565         PortNumber dstPort = new PortNumber(tcpPort);
566         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
567         tcpMatch.setTcpDestinationPort(dstPort);
568         matchBuilder.setLayer4Match(tcpMatch.build());
569
570         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
571         tcpFlagMatch.setTcpFlag(tcpFlag);
572         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
573
574         TunnelBuilder tunnelBuilder = new TunnelBuilder();
575         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
576         matchBuilder.setTunnel(tunnelBuilder.build());
577
578         return matchBuilder;
579     }
580
581     /**
582      * Create dmac tcp syn dst ip prefix tcp port.
583      *
584      * @param matchBuilder the match builder
585      * @param attachedMac the attached mac
586      * @param tcpPort the tcp port
587      * @param tcpFlag the tcp flag
588      * @param segmentationId the segmentation id
589      * @param dstIp the dst ip
590      * @return the match builder
591      */
592     public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
593             MacAddress attachedMac, PortNumber tcpPort,  Integer tcpFlag, String segmentationId,
594             Ipv4Prefix dstIp) {
595
596         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
597         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
598         ethTypeBuilder.setType(new EtherType(0x0800L));
599         ethernetMatch.setEthernetType(ethTypeBuilder.build());
600
601         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
602         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
603         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
604
605         matchBuilder.setEthernetMatch(ethernetMatch.build());
606
607         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
608         ipv4match.setIpv4Destination(dstIp);
609         matchBuilder.setLayer3Match(ipv4match.build());
610
611         // TCP Protocol Match
612         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
613         ipMatch.setIpProtocol(TCP_SHORT);
614         matchBuilder.setIpMatch(ipMatch.build());
615
616         // TCP Port Match
617         PortNumber dstPort = new PortNumber(tcpPort);
618         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
619         tcpMatch.setTcpDestinationPort(dstPort);
620         matchBuilder.setLayer4Match(tcpMatch.build());
621
622         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
623         tcpFlagMatch.setTcpFlag(tcpFlag);
624         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
625
626         TunnelBuilder tunnelBuilder = new TunnelBuilder();
627         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
628         matchBuilder.setTunnel(tunnelBuilder.build());
629
630         return matchBuilder;
631     }
632
633     /**
634      * Create dmac ip tcp syn match.
635      *
636      * @param matchBuilder the match builder
637      * @param dMacAddr the d mac addr
638      * @param mask the mask
639      * @param ipPrefix the ip prefix
640      * @return MatchBuilder containing the metadata match values
641      */
642     public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
643             MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
644
645         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
646         EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
647         ethDestBuilder.setAddress(new MacAddress(dMacAddr));
648         if (mask != null) {
649             ethDestBuilder.setMask(mask);
650         }
651         ethernetMatch.setEthernetDestination(ethDestBuilder.build());
652         matchBuilder.setEthernetMatch(ethernetMatch.build());
653         // Ethertype match
654         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
655         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
656         ethTypeBuilder.setType(new EtherType(0x0800L));
657         ethernetType.setEthernetType(ethTypeBuilder.build());
658         matchBuilder.setEthernetMatch(ethernetType.build());
659         if (ipPrefix != null) {
660             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
661             ipv4match.setIpv4Destination(ipPrefix);
662             matchBuilder.setLayer3Match(ipv4match.build());
663         }
664         // TCP Protocol Match
665         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
666         ipMatch.setIpProtocol(TCP_SHORT);
667         matchBuilder.setIpMatch(ipMatch.build());
668         // TCP Flag Match
669         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
670         tcpFlagMatch.setTcpFlag(TCP_SYN);
671         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
672
673         return matchBuilder;
674     }
675
676     /**
677      * Create smac tcp syn dst ip prefix tcp port.
678      *
679      * @param matchBuilder the match builder
680      * @param attachedMac the attached mac
681      * @param tcpPort the tcp port
682      * @param tcpFlag the tcp flag
683      * @param segmentationId the segmentation id
684      * @param dstIp the dst ip
685      * @return the match builder
686      */
687     public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
688             PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
689
690         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
691         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
692         ethTypeBuilder.setType(new EtherType(0x0800L));
693         ethernetMatch.setEthernetType(ethTypeBuilder.build());
694
695         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
696         ethSourceBuilder.setAddress(new MacAddress(attachedMac));
697         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
698
699         matchBuilder.setEthernetMatch(ethernetMatch.build());
700
701         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
702         ipv4match.setIpv4Destination(dstIp);
703         matchBuilder.setLayer3Match(ipv4match.build());
704
705         // TCP Protocol Match
706         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
707         ipMatch.setIpProtocol(TCP_SHORT);
708         matchBuilder.setIpMatch(ipMatch.build());
709
710         // TCP Port Match
711         PortNumber dstPort = new PortNumber(tcpPort);
712         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
713         tcpMatch.setTcpDestinationPort(dstPort);
714         matchBuilder.setLayer4Match(tcpMatch.build());
715
716         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
717         tcpFlagMatch.setTcpFlag(tcpFlag);
718         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
719
720         TunnelBuilder tunnelBuilder = new TunnelBuilder();
721         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
722         matchBuilder.setTunnel(tunnelBuilder.build());
723
724         return matchBuilder;
725     }
726
727     /**
728      * Create smac tcp port with flag match.
729      *
730      * @param matchBuilder the match builder
731      * @param attachedMac the attached mac
732      * @param tcpFlag the tcp flag
733      * @param tunnelID the tunnel iD
734      * @return matchBuilder
735      */
736     public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
737             Integer tcpFlag, String tunnelID) {
738
739         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
740         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
741         ethTypeBuilder.setType(new EtherType(0x0800L));
742         ethernetMatch.setEthernetType(ethTypeBuilder.build());
743
744         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
745         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
746         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
747         matchBuilder.setEthernetMatch(ethernetMatch.build());
748
749         // TCP Protocol Match
750         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
751         ipMatch.setIpProtocol(TCP_SHORT);
752         matchBuilder.setIpMatch(ipMatch.build());
753
754         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
755         tcpFlagMatch.setTcpFlag(tcpFlag);
756         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
757
758         TunnelBuilder tunnelBuilder = new TunnelBuilder();
759         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
760         matchBuilder.setTunnel(tunnelBuilder.build());
761
762         return matchBuilder;
763     }
764
765     /**
766      * Create smac ip tcp syn match.
767      *
768      * @param matchBuilder the match builder
769      * @param dMacAddr the d mac addr
770      * @param mask the mask
771      * @param ipPrefix the ip prefix
772      * @return MatchBuilder containing the metadata match values
773      */
774     public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
775             MacAddress mask, Ipv4Prefix ipPrefix) {
776
777         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
778         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
779         ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
780         if (mask != null) {
781             ethSrcBuilder.setMask(mask);
782         }
783         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
784         matchBuilder.setEthernetMatch(ethernetMatch.build());
785         // Ethertype match
786         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
787         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
788         ethTypeBuilder.setType(new EtherType(0x0800L));
789         ethernetType.setEthernetType(ethTypeBuilder.build());
790         matchBuilder.setEthernetMatch(ethernetType.build());
791         if (ipPrefix != null) {
792             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
793             ipv4match.setIpv4Destination(ipPrefix);
794             matchBuilder.setLayer3Match(ipv4match.build());
795         }
796         // TCP Protocol Match
797         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
798         ipMatch.setIpProtocol(TCP_SHORT);
799         matchBuilder.setIpMatch(ipMatch.build());
800         // TCP Flag Match
801         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
802         tcpFlagMatch.setTcpFlag(TCP_SYN);
803         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
804
805         return matchBuilder;
806     }
807
808     /**
809      * Create smac tcp syn.
810      *
811      * @param matchBuilder the match builder
812      * @param attachedMac the attached mac
813      * @param tcpPort the tcp port
814      * @param tcpFlag the tcp flag
815      * @param tunnelID the tunnel iD
816      * @return the match builder
817      */
818     public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
819             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
820
821         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
822         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
823         ethTypeBuilder.setType(new EtherType(0x0800L));
824         ethernetMatch.setEthernetType(ethTypeBuilder.build());
825
826         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
827         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
828         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
829         matchBuilder.setEthernetMatch(ethernetMatch.build());
830
831         // TCP Protocol Match
832         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
833         ipMatch.setIpProtocol((short) 6);
834         matchBuilder.setIpMatch(ipMatch.build());
835
836         // TCP Port Match
837         PortNumber dstPort = new PortNumber(tcpPort);
838         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
839         tcpMatch.setTcpDestinationPort(dstPort);
840         matchBuilder.setLayer4Match(tcpMatch.build());
841
842
843         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
844         tcpFlagMatch.setTcpFlag(tcpFlag);
845         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
846
847         TunnelBuilder tunnelBuilder = new TunnelBuilder();
848         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
849         matchBuilder.setTunnel(tunnelBuilder.build());
850
851         return matchBuilder;
852     }
853
854     /**
855      * @return MatchBuilder containing the metadata match values
856      */
857     public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
858             MacAddress dMacAddr,  MacAddress mask, Ipv4Prefix ipPrefix) {
859
860         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
861         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
862         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
863         if (mask != null) {
864             ethDestinationBuilder.setMask(mask);
865         }
866         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
867         matchBuilder.setEthernetMatch(ethernetMatch.build());
868         // Ethertype match
869         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
870         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
871         ethTypeBuilder.setType(new EtherType(0x0800L));
872         ethernetType.setEthernetType(ethTypeBuilder.build());
873         matchBuilder.setEthernetMatch(ethernetType.build());
874         if (ipPrefix != null) {
875             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
876             ipv4match.setIpv4Source(ipPrefix);
877             matchBuilder.setLayer3Match(ipv4match.build());
878         }
879         // TCP Protocol Match
880         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
881         ipMatch.setIpProtocol(TCP_SHORT);
882         matchBuilder.setIpMatch(ipMatch.build());
883         // TCP Flag Match
884         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
885         tcpFlagMatch.setTcpFlag(TCP_SYN);
886         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
887
888         return matchBuilder;
889     }
890
891     /**
892      * Create a DHCP match with pot provided.
893      *
894      * @param matchBuilder the match builder
895      * @param srcPort the source port
896      * @param dstPort the destination port
897      * @return the DHCP match
898      */
899     public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
900                                                int srcPort, int dstPort) {
901
902         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
903         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
904         ethTypeBuilder.setType(new EtherType(0x0800L));
905         ethernetMatch.setEthernetType(ethTypeBuilder.build());
906         matchBuilder.setEthernetMatch(ethernetMatch.build());
907
908         IpMatchBuilder ipmatch = new IpMatchBuilder();
909         ipmatch.setIpProtocol(UDP_SHORT);
910         matchBuilder.setIpMatch(ipmatch.build());
911
912         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
913         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
914         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
915         matchBuilder.setLayer4Match(udpmatch.build());
916
917         return matchBuilder;
918
919     }
920
921     /**
922      * Creates DHCP server packet match with DHCP mac address and port.
923      *
924      * @param matchBuilder the matchbuilder
925      * @param dhcpServerMac MAc address of the DHCP server of the subnet
926      * @param srcPort the source port
927      * @param dstPort the destination port
928      * @return the DHCP server match
929      */
930     public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
931             int dstPort) {
932
933         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
934         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
935         ethTypeBuilder.setType(new EtherType(0x0800L));
936         ethernetMatch.setEthernetType(ethTypeBuilder.build());
937         matchBuilder.setEthernetMatch(ethernetMatch.build());
938
939         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
940         ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
941         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
942         matchBuilder.setEthernetMatch(ethernetMatch.build());
943
944         IpMatchBuilder ipmatch = new IpMatchBuilder();
945         ipmatch.setIpProtocol(UDP_SHORT);
946         matchBuilder.setIpMatch(ipmatch.build());
947
948         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
949         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
950         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
951         matchBuilder.setLayer4Match(udpmatch.build());
952
953         return matchBuilder;
954
955     }
956
957     /**
958      * Creates a Match with src ip address mac address set.
959      * @param matchBuilder MatchBuilder Object
960      * @param srcip String containing an IPv4 prefix
961      * @param srcMac The source macAddress
962      * @return matchBuilder Map Object with a match
963      */
964     public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
965
966         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
967         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
968         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
969         ethTypeBuilder.setType(new EtherType(0x0800L));
970         EthernetMatchBuilder eth = new EthernetMatchBuilder();
971         eth.setEthernetType(ethTypeBuilder.build());
972         eth.setEthernetSource(new EthernetSourceBuilder()
973                 .setAddress(srcMac)
974                 .build());
975
976         matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
977         matchBuilder.setEthernetMatch(eth.build());
978         return matchBuilder;
979
980     }
981
982     /**
983      * Creates a ether net match with ether type set to 0x0800L.
984      * @param matchBuilder MatchBuilder Object
985      * @param srcMac The source macAddress
986      * @param dstMac The destination mac address
987      * @return matchBuilder Map Object with a match
988      */
989     public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
990     {
991         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
992         ethTypeBuilder.setType(new EtherType(0x0800L));
993         EthernetMatchBuilder eth = new EthernetMatchBuilder();
994         eth.setEthernetType(ethTypeBuilder.build());
995         if (null != srcMac) {
996             eth.setEthernetSource(new EthernetSourceBuilder()
997             .setAddress(new MacAddress(srcMac)).build());
998         }
999         if (null != dstMac) {
1000             eth.setEthernetDestination(new EthernetDestinationBuilder()
1001                            .setAddress(new MacAddress(dstMac)).build());
1002         }
1003         matchBuilder.setEthernetMatch(eth.build());
1004         return matchBuilder;
1005     }
1006     /**
1007      * Adds remote Ip prefix to existing match.
1008      * @param matchBuilder The match builder
1009      * @param sourceIpPrefix The source IP prefix
1010      * @param destIpPrefix The destination IP prefix
1011      * @return matchBuilder Map Object with a match
1012      */
1013     public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1014                                           Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1015         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1016         if (null != sourceIpPrefix) {
1017             ipv4match.setIpv4Source(sourceIpPrefix);
1018         }
1019         if (null != destIpPrefix) {
1020             ipv4match.setIpv4Destination(destIpPrefix);
1021         }
1022         matchBuilder.setLayer3Match(ipv4match.build());
1023
1024         return matchBuilder;
1025     }
1026     /**
1027      * Add a layer4 match to an existing match
1028      *
1029      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
1030      * @param protocol The layer4 protocol
1031      * @param srcPort The src port
1032      * @param destPort The destination port
1033      * @return matchBuilder Map Object with a match
1034      */
1035     public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1036                                               int protocol, int srcPort, int destPort) {
1037         IpMatchBuilder ipmatch = new IpMatchBuilder();
1038         if (TCP_SHORT == protocol) {
1039             ipmatch.setIpProtocol(TCP_SHORT);
1040             TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1041             if (0 != srcPort) {
1042                 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1043             }
1044             if (0 != destPort) {
1045                 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1046             }
1047             matchBuilder.setLayer4Match(tcpmatch.build());
1048         } else if (UDP_SHORT == protocol) {
1049             ipmatch.setIpProtocol(UDP_SHORT);
1050             UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1051             if (0 != srcPort) {
1052                 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1053             }
1054             if (0 != destPort) {
1055                 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1056             }
1057             matchBuilder.setLayer4Match(udpMatch.build());
1058         }
1059         matchBuilder.setIpMatch(ipmatch.build());
1060
1061         return matchBuilder;
1062     }
1063
1064
1065     public static class RegMatch {
1066         final Class<? extends NxmNxReg> reg;
1067         final Long value;
1068         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1069             super();
1070             this.reg = reg;
1071             this.value = value;
1072         }
1073         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1074             return new RegMatch(reg, value);
1075         }
1076     }
1077
1078     public static void addNxRegMatch(MatchBuilder match,
1079                                      RegMatch... matches) {
1080         List<ExtensionList> extensions = new ArrayList<>();
1081         for (RegMatch rm : matches) {
1082             Class<? extends ExtensionKey> key;
1083             if (NxmNxReg0.class.equals(rm.reg)) {
1084                 key = NxmNxReg0Key.class;
1085             } else if (NxmNxReg1.class.equals(rm.reg)) {
1086                 key = NxmNxReg1Key.class;
1087             } else if (NxmNxReg2.class.equals(rm.reg)) {
1088                 key = NxmNxReg2Key.class;
1089             } else if (NxmNxReg3.class.equals(rm.reg)) {
1090                 key = NxmNxReg3Key.class;
1091             } else if (NxmNxReg4.class.equals(rm.reg)) {
1092                 key = NxmNxReg4Key.class;
1093             } else if (NxmNxReg5.class.equals(rm.reg)) {
1094                 key = NxmNxReg5Key.class;
1095             } else if (NxmNxReg6.class.equals(rm.reg)) {
1096                 key = NxmNxReg6Key.class;
1097             } else {
1098                 key = NxmNxReg7Key.class;
1099             }
1100             NxAugMatchNodesNodeTableFlow am =
1101                     new NxAugMatchNodesNodeTableFlowBuilder()
1102                 .setNxmNxReg(new NxmNxRegBuilder()
1103                     .setReg(rm.reg)
1104                     .setValue(rm.value)
1105                     .build())
1106                 .build();
1107             extensions.add(new ExtensionListBuilder()
1108                 .setExtensionKey(key)
1109                 .setExtension(new ExtensionBuilder()
1110                      .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1111                      .build())
1112                 .build());
1113         }
1114         GeneralAugMatchNodesNodeTableFlow m =
1115                 new GeneralAugMatchNodesNodeTableFlowBuilder()
1116             .setExtensionList(extensions)
1117             .build();
1118         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1119     }
1120
1121     public static void addNxTunIdMatch(MatchBuilder match,
1122                                        int tunId) {
1123         NxAugMatchNodesNodeTableFlow am =
1124                new NxAugMatchNodesNodeTableFlowBuilder()
1125                    .setNxmNxTunId(new NxmNxTunIdBuilder()
1126                        .setValue(BigInteger.valueOf(tunId))
1127                        .build())
1128                    .build();
1129         GeneralAugMatchNodesNodeTableFlow m =
1130                 new GeneralAugMatchNodesNodeTableFlowBuilder()
1131             .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1132                 .setExtensionKey(NxmNxTunIdKey.class)
1133                 .setExtension(new ExtensionBuilder()
1134                     .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1135                     .build())
1136                 .build()))
1137             .build();
1138         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1139     }
1140
1141     public static void addNxNsp(MatchBuilder match, long nsp) {
1142         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1143                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1144                 .setNxmNxNsp(new NxmNxNspBuilder()
1145                 .setValue(nsp)
1146                 .build())
1147                 .build();
1148         addExtension(match, NxmNxNspKey.class, am);
1149     }
1150
1151     public static void addNxNsi(MatchBuilder match, short nsi) {
1152         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1153                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1154                 .setNxmNxNsi(new NxmNxNsiBuilder()
1155                 .setNsi(nsi)
1156                 .build())
1157                 .build();
1158         addExtension(match, NxmNxNsiKey.class, am);
1159     }
1160
1161     private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
1162         GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1163         List<ExtensionList> extensions = null;
1164         if (existingAugmentations != null ) {
1165             extensions = existingAugmentations.getExtensionList();
1166         }
1167         if (extensions == null) {
1168             extensions = Lists.newArrayList();
1169         }
1170
1171         extensions.add(new ExtensionListBuilder()
1172                            .setExtensionKey(extensionKey)
1173                            .setExtension(new ExtensionBuilder()
1174                            .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
1175                            .build())
1176                            .build());
1177
1178         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1179         .setExtensionList(extensions)
1180         .build();
1181         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1182     }
1183
1184     public static EthernetMatch ethernetMatch(MacAddress srcMac,
1185                                               MacAddress dstMac,
1186                                               Long etherType) {
1187         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
1188         if (srcMac != null) {
1189             emb.setEthernetSource(new EthernetSourceBuilder()
1190                 .setAddress(srcMac)
1191                 .build());
1192         }
1193         if (dstMac != null) {
1194             emb.setEthernetDestination(new EthernetDestinationBuilder()
1195                 .setAddress(dstMac)
1196                 .build());
1197         }
1198         if (etherType != null) {
1199             emb.setEthernetType(new EthernetTypeBuilder()
1200                 .setType(new EtherType(etherType))
1201                 .build());
1202         }
1203         return emb.build();
1204     }
1205
1206     /**
1207      * Create ipv4 prefix from ipv4 address, by appending /32 mask
1208      *
1209      * @param ipv4AddressString the ip address, in string format
1210      * @return Ipv4Prefix with ipv4Address and /32 mask
1211      */
1212     public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1213         return new Ipv4Prefix(ipv4AddressString + "/32");
1214     }
1215
1216     /**
1217      * Return Long that represents OF port for strings where OF is explicitly provided
1218      *
1219      * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1220      * @return the OFport or null
1221      */
1222     public static Long parseExplicitOFPort(String ofPortIdentifier) {
1223         if (ofPortIdentifier != null) {
1224             String[] pair = ofPortIdentifier.split("\\|");
1225             if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1226                 return new Long(pair[1]);
1227             }
1228         }
1229         return null;
1230     }
1231 }