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