Sonar clean-up: braces for control statements
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / MatchUtils.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  */
9
10 package org.opendaylight.ovsdb.utils.mdsal.openflow;
11
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.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 logger = 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         logger.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     public static class RegMatch {
897         final Class<? extends NxmNxReg> reg;
898         final Long value;
899         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
900             super();
901             this.reg = reg;
902             this.value = value;
903         }
904         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
905             return new RegMatch(reg, value);
906         }
907     }
908
909     public static void addNxRegMatch(MatchBuilder match,
910                                      RegMatch... matches) {
911         ArrayList<ExtensionList> extensions = new ArrayList<>();
912         for (RegMatch rm : matches) {
913             Class<? extends ExtensionKey> key;
914             if (NxmNxReg0.class.equals(rm.reg)) {
915                 key = NxmNxReg0Key.class;
916             } else if (NxmNxReg1.class.equals(rm.reg)) {
917                 key = NxmNxReg1Key.class;
918             } else if (NxmNxReg2.class.equals(rm.reg)) {
919                 key = NxmNxReg2Key.class;
920             } else if (NxmNxReg3.class.equals(rm.reg)) {
921                 key = NxmNxReg3Key.class;
922             } else if (NxmNxReg4.class.equals(rm.reg)) {
923                 key = NxmNxReg4Key.class;
924             } else if (NxmNxReg5.class.equals(rm.reg)) {
925                 key = NxmNxReg5Key.class;
926             } else if (NxmNxReg6.class.equals(rm.reg)) {
927                 key = NxmNxReg6Key.class;
928             } else {
929                 key = NxmNxReg7Key.class;
930             }
931             NxAugMatchNodesNodeTableFlow am =
932                     new NxAugMatchNodesNodeTableFlowBuilder()
933                 .setNxmNxReg(new NxmNxRegBuilder()
934                     .setReg(rm.reg)
935                     .setValue(rm.value)
936                     .build())
937                 .build();
938             extensions.add(new ExtensionListBuilder()
939                 .setExtensionKey(key)
940                 .setExtension(new ExtensionBuilder()
941                      .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
942                      .build())
943                 .build());
944         }
945         GeneralAugMatchNodesNodeTableFlow m =
946                 new GeneralAugMatchNodesNodeTableFlowBuilder()
947             .setExtensionList(extensions)
948             .build();
949         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
950     }
951
952     public static void addNxTunIdMatch(MatchBuilder match,
953                                        int tunId) {
954         NxAugMatchNodesNodeTableFlow am =
955                new NxAugMatchNodesNodeTableFlowBuilder()
956                    .setNxmNxTunId(new NxmNxTunIdBuilder()
957                        .setValue(BigInteger.valueOf(tunId))
958                        .build())
959                    .build();
960         GeneralAugMatchNodesNodeTableFlow m =
961                 new GeneralAugMatchNodesNodeTableFlowBuilder()
962             .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
963                 .setExtensionKey(NxmNxTunIdKey.class)
964                 .setExtension(new ExtensionBuilder()
965                     .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
966                     .build())
967                 .build()))
968             .build();
969         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
970     }
971
972     public static void addNxNsp(MatchBuilder match, long nsp) {
973         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
974                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
975                 .setNxmNxNsp(new NxmNxNspBuilder()
976                 .setValue(nsp)
977                 .build())
978                 .build();
979         addExtension(match, NxmNxNspKey.class, am);
980     }
981
982     public static void addNxNsi(MatchBuilder match, short nsi) {
983         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
984                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
985                 .setNxmNxNsi(new NxmNxNsiBuilder()
986                 .setNsi(nsi)
987                 .build())
988                 .build();
989         addExtension(match, NxmNxNsiKey.class, am);
990     }
991
992     private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
993         GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
994         List<ExtensionList> extensions = null;
995         if (existingAugmentations != null ) {
996             extensions = existingAugmentations.getExtensionList();
997         }
998         if (extensions == null) {
999             extensions = Lists.newArrayList();
1000         }
1001
1002         extensions.add(new ExtensionListBuilder()
1003                            .setExtensionKey(extensionKey)
1004                            .setExtension(new ExtensionBuilder()
1005                            .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
1006                            .build())
1007                            .build());
1008
1009         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1010         .setExtensionList(extensions)
1011         .build();
1012         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1013     }
1014
1015     public static EthernetMatch ethernetMatch(MacAddress srcMac,
1016                                               MacAddress dstMac,
1017                                               Long etherType) {
1018         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
1019         if (srcMac != null) {
1020             emb.setEthernetSource(new EthernetSourceBuilder()
1021                 .setAddress(srcMac)
1022                 .build());
1023         }
1024         if (dstMac != null) {
1025             emb.setEthernetDestination(new EthernetDestinationBuilder()
1026                 .setAddress(dstMac)
1027                 .build());
1028         }
1029         if (etherType != null) {
1030             emb.setEthernetType(new EthernetTypeBuilder()
1031                 .setType(new EtherType(etherType))
1032                 .build());
1033         }
1034         return emb.build();
1035     }
1036
1037     /**
1038      * Create ipv4 prefix from ipv4 address, by appending /32 mask
1039      *
1040      * @param ipv4AddressString the ip address, in string format
1041      * @return Ipv4Prefix with ipv4Address and /32 mask
1042      */
1043     public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1044         return new Ipv4Prefix(ipv4AddressString + "/32");
1045     }
1046
1047     /**
1048      * Return Long that represents OF port for strings where OF is explicitly provided
1049      *
1050      * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1051      * @return the OFport or null
1052      */
1053     public static Long parseExplicitOFPort(String ofPortIdentifier) {
1054         if (ofPortIdentifier != null) {
1055             String[] pair = ofPortIdentifier.split("\\|");
1056             if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1057                 return new Long(pair[1]);
1058             }
1059         }
1060         return null;
1061     }
1062 }