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