Fix javadoc JDK8 compatibility
[ovsdb.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / MatchUtils.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  */
9
10 package org.opendaylight.ovsdb.utils.mdsal.openflow;
11
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.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
474         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
475         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
476         ethTypeBuilder.setType(new EtherType(0x0800L));
477         ethernetMatch.setEthernetType(ethTypeBuilder.build());
478
479         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
480         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
481         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
482         matchBuilder.setEthernetMatch(ethernetMatch.build());
483
484         // TCP Protocol Match
485         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
486         ipMatch.setIpProtocol(TCP_SHORT);
487         matchBuilder.setIpMatch(ipMatch.build());
488
489         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
490         tcpFlagMatch.setTcpFlag(tcpFlag);
491         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
492
493         TunnelBuilder tunnelBuilder = new TunnelBuilder();
494         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
495         matchBuilder.setTunnel(tunnelBuilder.build());
496
497         return matchBuilder;
498     }
499
500     /**
501      * Create dmac tcp syn match.
502      *
503      * @param matchBuilder the match builder
504      * @param attachedMac the attached mac
505      * @param tcpPort the tcp port
506      * @param tcpFlag the tcp flag
507      * @param tunnelID the tunnel iD
508      * @return the match builder
509      */
510     public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
511             String attachedMac, PortNumber tcpPort, Integer tcpFlag, 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         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
519         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
520         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
521         matchBuilder.setEthernetMatch(ethernetMatch.build());
522
523         // TCP Protocol Match
524         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
525         ipMatch.setIpProtocol((short) 6);
526         matchBuilder.setIpMatch(ipMatch.build());
527
528         // TCP Port Match
529         PortNumber dstPort = new PortNumber(tcpPort);
530         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
531         tcpMatch.setTcpDestinationPort(dstPort);
532         matchBuilder.setLayer4Match(tcpMatch.build());
533
534         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
535         tcpFlagMatch.setTcpFlag(tcpFlag);
536         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
537
538         TunnelBuilder tunnelBuilder = new TunnelBuilder();
539         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
540         matchBuilder.setTunnel(tunnelBuilder.build());
541
542         return matchBuilder;
543     }
544
545     /**
546      * Create dmac tcp syn dst ip prefix tcp port.
547      *
548      * @param matchBuilder the match builder
549      * @param attachedMac the attached mac
550      * @param tcpPort the tcp port
551      * @param tcpFlag the tcp flag
552      * @param segmentationId the segmentation id
553      * @param dstIp the dst ip
554      * @return the match builder
555      */
556     public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
557             MacAddress attachedMac, PortNumber tcpPort,  Integer tcpFlag, String segmentationId,
558             Ipv4Prefix dstIp) {
559
560         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
561         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
562         ethTypeBuilder.setType(new EtherType(0x0800L));
563         ethernetMatch.setEthernetType(ethTypeBuilder.build());
564
565         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
566         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
567         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
568
569         matchBuilder.setEthernetMatch(ethernetMatch.build());
570
571         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
572         ipv4match.setIpv4Destination(dstIp);
573         matchBuilder.setLayer3Match(ipv4match.build());
574
575         // TCP Protocol Match
576         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
577         ipMatch.setIpProtocol(TCP_SHORT);
578         matchBuilder.setIpMatch(ipMatch.build());
579
580         // TCP Port Match
581         PortNumber dstPort = new PortNumber(tcpPort);
582         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
583         tcpMatch.setTcpDestinationPort(dstPort);
584         matchBuilder.setLayer4Match(tcpMatch.build());
585
586         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
587         tcpFlagMatch.setTcpFlag(tcpFlag);
588         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
589
590         TunnelBuilder tunnelBuilder = new TunnelBuilder();
591         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
592         matchBuilder.setTunnel(tunnelBuilder.build());
593
594         return matchBuilder;
595     }
596
597     /**
598      * Create dmac ip tcp syn match.
599      *
600      * @param matchBuilder the match builder
601      * @param dMacAddr the d mac addr
602      * @param mask the mask
603      * @param ipPrefix the ip prefix
604      * @return MatchBuilder containing the metadata match values
605      */
606     public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
607             MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
608
609         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
610         EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
611         ethDestBuilder.setAddress(new MacAddress(dMacAddr));
612         if (mask != null) {
613             ethDestBuilder.setMask(mask);
614         }
615         ethernetMatch.setEthernetDestination(ethDestBuilder.build());
616         matchBuilder.setEthernetMatch(ethernetMatch.build());
617         // Ethertype match
618         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
619         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
620         ethTypeBuilder.setType(new EtherType(0x0800L));
621         ethernetType.setEthernetType(ethTypeBuilder.build());
622         matchBuilder.setEthernetMatch(ethernetType.build());
623         if (ipPrefix != null) {
624             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
625             ipv4match.setIpv4Destination(ipPrefix);
626             matchBuilder.setLayer3Match(ipv4match.build());
627         }
628         // TCP Protocol Match
629         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
630         ipMatch.setIpProtocol(TCP_SHORT);
631         matchBuilder.setIpMatch(ipMatch.build());
632         // TCP Flag Match
633         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
634         tcpFlagMatch.setTcpFlag(TCP_SYN);
635         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
636
637         return matchBuilder;
638     }
639
640     /**
641      * Create smac tcp syn dst ip prefix tcp port.
642      *
643      * @param matchBuilder the match builder
644      * @param attachedMac the attached mac
645      * @param tcpPort the tcp port
646      * @param tcpFlag the tcp flag
647      * @param segmentationId the segmentation id
648      * @param dstIp the dst ip
649      * @return the match builder
650      */
651     public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
652             PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
653
654         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
655         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
656         ethTypeBuilder.setType(new EtherType(0x0800L));
657         ethernetMatch.setEthernetType(ethTypeBuilder.build());
658
659         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
660         ethSourceBuilder.setAddress(new MacAddress(attachedMac));
661         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
662
663         matchBuilder.setEthernetMatch(ethernetMatch.build());
664
665         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
666         ipv4match.setIpv4Destination(dstIp);
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
674         // TCP Port Match
675         PortNumber dstPort = new PortNumber(tcpPort);
676         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
677         tcpMatch.setTcpDestinationPort(dstPort);
678         matchBuilder.setLayer4Match(tcpMatch.build());
679
680         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
681         tcpFlagMatch.setTcpFlag(tcpFlag);
682         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
683
684         TunnelBuilder tunnelBuilder = new TunnelBuilder();
685         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
686         matchBuilder.setTunnel(tunnelBuilder.build());
687
688         return matchBuilder;
689     }
690
691     /**
692      * Create smac tcp port with flag match.
693      *
694      * @param matchBuilder the match builder
695      * @param attachedMac the attached mac
696      * @param tcpFlag the tcp flag
697      * @param tunnelID the tunnel iD
698      * @return matchBuilder
699      */
700     public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
701             Integer tcpFlag, String tunnelID) {
702
703         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
704         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
705         ethTypeBuilder.setType(new EtherType(0x0800L));
706         ethernetMatch.setEthernetType(ethTypeBuilder.build());
707
708         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
709         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
710         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
711         matchBuilder.setEthernetMatch(ethernetMatch.build());
712
713         // TCP Protocol Match
714         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
715         ipMatch.setIpProtocol(TCP_SHORT);
716         matchBuilder.setIpMatch(ipMatch.build());
717
718         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
719         tcpFlagMatch.setTcpFlag(tcpFlag);
720         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
721
722         TunnelBuilder tunnelBuilder = new TunnelBuilder();
723         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
724         matchBuilder.setTunnel(tunnelBuilder.build());
725
726         return matchBuilder;
727     }
728
729     /**
730      * Create smac ip tcp syn match.
731      *
732      * @param matchBuilder the match builder
733      * @param dMacAddr the d mac addr
734      * @param mask the mask
735      * @param ipPrefix the ip prefix
736      * @return MatchBuilder containing the metadata match values
737      */
738     public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
739             MacAddress mask, Ipv4Prefix ipPrefix) {
740
741         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
742         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
743         ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
744         if (mask != null) {
745             ethSrcBuilder.setMask(mask);
746         }
747         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
748         matchBuilder.setEthernetMatch(ethernetMatch.build());
749         // Ethertype match
750         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
751         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
752         ethTypeBuilder.setType(new EtherType(0x0800L));
753         ethernetType.setEthernetType(ethTypeBuilder.build());
754         matchBuilder.setEthernetMatch(ethernetType.build());
755         if (ipPrefix != null) {
756             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
757             ipv4match.setIpv4Destination(ipPrefix);
758             matchBuilder.setLayer3Match(ipv4match.build());
759         }
760         // TCP Protocol Match
761         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
762         ipMatch.setIpProtocol(TCP_SHORT);
763         matchBuilder.setIpMatch(ipMatch.build());
764         // TCP Flag Match
765         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
766         tcpFlagMatch.setTcpFlag(TCP_SYN);
767         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
768
769         return matchBuilder;
770     }
771
772     /**
773      * Create smac tcp syn.
774      *
775      * @param matchBuilder the match builder
776      * @param attachedMac the attached mac
777      * @param tcpPort the tcp port
778      * @param tcpFlag the tcp flag
779      * @param tunnelID the tunnel iD
780      * @return the match builder
781      */
782     public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
783             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
784
785         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
786         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
787         ethTypeBuilder.setType(new EtherType(0x0800L));
788         ethernetMatch.setEthernetType(ethTypeBuilder.build());
789
790         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
791         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
792         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
793         matchBuilder.setEthernetMatch(ethernetMatch.build());
794
795         // TCP Protocol Match
796         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
797         ipMatch.setIpProtocol((short) 6);
798         matchBuilder.setIpMatch(ipMatch.build());
799
800         // TCP Port Match
801         PortNumber dstPort = new PortNumber(tcpPort);
802         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
803         tcpMatch.setTcpDestinationPort(dstPort);
804         matchBuilder.setLayer4Match(tcpMatch.build());
805
806
807         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
808         tcpFlagMatch.setTcpFlag(tcpFlag);
809         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
810
811         TunnelBuilder tunnelBuilder = new TunnelBuilder();
812         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
813         matchBuilder.setTunnel(tunnelBuilder.build());
814
815         return matchBuilder;
816     }
817
818     /**
819      * @return MatchBuilder containing the metadata match values
820      */
821     public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
822             MacAddress dMacAddr,  MacAddress mask, Ipv4Prefix ipPrefix) {
823
824         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
825         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
826         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
827         if (mask != null) {
828             ethDestinationBuilder.setMask(mask);
829         }
830         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
831         matchBuilder.setEthernetMatch(ethernetMatch.build());
832         // Ethertype match
833         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
834         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
835         ethTypeBuilder.setType(new EtherType(0x0800L));
836         ethernetType.setEthernetType(ethTypeBuilder.build());
837         matchBuilder.setEthernetMatch(ethernetType.build());
838         if (ipPrefix != null) {
839             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
840             ipv4match.setIpv4Source(ipPrefix);
841             matchBuilder.setLayer3Match(ipv4match.build());
842         }
843         // TCP Protocol Match
844         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
845         ipMatch.setIpProtocol(TCP_SHORT);
846         matchBuilder.setIpMatch(ipMatch.build());
847         // TCP Flag Match
848         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
849         tcpFlagMatch.setTcpFlag(TCP_SYN);
850         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
851
852         return matchBuilder;
853     }
854
855     public static class RegMatch {
856         final Class<? extends NxmNxReg> reg;
857         final Long value;
858         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
859             super();
860             this.reg = reg;
861             this.value = value;
862         }
863         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
864             return new RegMatch(reg, value);
865         }
866     }
867
868     public static void addNxRegMatch(MatchBuilder match,
869                                      RegMatch... matches) {
870         ArrayList<ExtensionList> extensions = new ArrayList<>();
871         for (RegMatch rm : matches) {
872             Class<? extends ExtensionKey> key;
873             if (NxmNxReg0.class.equals(rm.reg)) {
874                 key = NxmNxReg0Key.class;
875             } else if (NxmNxReg1.class.equals(rm.reg)) {
876                 key = NxmNxReg1Key.class;
877             } else if (NxmNxReg2.class.equals(rm.reg)) {
878                 key = NxmNxReg2Key.class;
879             } else if (NxmNxReg3.class.equals(rm.reg)) {
880                 key = NxmNxReg3Key.class;
881             } else if (NxmNxReg4.class.equals(rm.reg)) {
882                 key = NxmNxReg4Key.class;
883             } else if (NxmNxReg5.class.equals(rm.reg)) {
884                 key = NxmNxReg5Key.class;
885             } else if (NxmNxReg6.class.equals(rm.reg)) {
886                 key = NxmNxReg6Key.class;
887             } else {
888                 key = NxmNxReg7Key.class;
889             }
890             NxAugMatchNodesNodeTableFlow am =
891                     new NxAugMatchNodesNodeTableFlowBuilder()
892                 .setNxmNxReg(new NxmNxRegBuilder()
893                     .setReg(rm.reg)
894                     .setValue(rm.value)
895                     .build())
896                 .build();
897             extensions.add(new ExtensionListBuilder()
898                 .setExtensionKey(key)
899                 .setExtension(new ExtensionBuilder()
900                      .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
901                      .build())
902                 .build());
903         }
904         GeneralAugMatchNodesNodeTableFlow m =
905                 new GeneralAugMatchNodesNodeTableFlowBuilder()
906             .setExtensionList(extensions)
907             .build();
908         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
909     }
910
911     public static void addNxTunIdMatch(MatchBuilder match,
912                                        int tunId) {
913         NxAugMatchNodesNodeTableFlow am =
914                new NxAugMatchNodesNodeTableFlowBuilder()
915                    .setNxmNxTunId(new NxmNxTunIdBuilder()
916                        .setValue(BigInteger.valueOf(tunId))
917                        .build())
918                    .build();
919         GeneralAugMatchNodesNodeTableFlow m =
920                 new GeneralAugMatchNodesNodeTableFlowBuilder()
921             .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
922                 .setExtensionKey(NxmNxTunIdKey.class)
923                 .setExtension(new ExtensionBuilder()
924                     .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
925                     .build())
926                 .build()))
927             .build();
928         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
929     }
930
931     public static void addNxNsp(MatchBuilder match, long nsp) {
932         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
933                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
934                 .setNxmNxNsp(new NxmNxNspBuilder()
935                 .setValue(nsp)
936                 .build())
937                 .build();
938         addExtension(match, NxmNxNspKey.class, am);
939     }
940
941     public static void addNxNsi(MatchBuilder match, short nsi) {
942         org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
943                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
944                 .setNxmNxNsi(new NxmNxNsiBuilder()
945                 .setNsi(nsi)
946                 .build())
947                 .build();
948         addExtension(match, NxmNxNsiKey.class, am);
949     }
950
951     private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
952         GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
953         List<ExtensionList> extensions = null;
954         if (existingAugmentations != null ) {
955             extensions = existingAugmentations.getExtensionList();
956         }
957         if (extensions == null) {
958             extensions = Lists.newArrayList();
959         }
960
961         extensions.add(new ExtensionListBuilder()
962                            .setExtensionKey(extensionKey)
963                            .setExtension(new ExtensionBuilder()
964                            .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
965                            .build())
966                            .build());
967
968         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
969         .setExtensionList(extensions)
970         .build();
971         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
972     }
973
974     public static EthernetMatch ethernetMatch(MacAddress srcMac,
975                                               MacAddress dstMac,
976                                               Long etherType) {
977         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
978         if (srcMac != null)
979             emb.setEthernetSource(new EthernetSourceBuilder()
980                 .setAddress(srcMac)
981                 .build());
982         if (dstMac != null)
983             emb.setEthernetDestination(new EthernetDestinationBuilder()
984                 .setAddress(dstMac)
985                 .build());
986         if (etherType != null)
987             emb.setEthernetType(new EthernetTypeBuilder()
988                 .setType(new EtherType(etherType))
989                 .build());
990         return emb.build();
991     }
992
993     /**
994      * Create ipv4 prefix from ipv4 address, by appending /32 mask
995      *
996      * @param ipv4AddressString the ip address, in string format
997      * @return Ipv4Prefix with ipv4Address and /32 mask
998      */
999     public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1000         return new Ipv4Prefix(ipv4AddressString + "/32");
1001     }
1002 }