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