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