Swap segmentation Id when routing between subnets
[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.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.arp.tpa.grouping.NxmOfArpTpaBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNspKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNsiKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 import com.google.common.collect.ImmutableList;
75 import com.google.common.collect.Lists;
76
77 public class MatchUtils {
78     private static final Logger logger = LoggerFactory.getLogger(MatchUtils.class);
79     public static final short ICMP_SHORT = 1;
80     public static final short TCP_SHORT = 6;
81     public static final short UDP_SHORT = 17;
82     public static final String TCP = "tcp";
83     public static final String UDP = "udp";
84     private static final int TCP_SYN = 0x0002;
85
86     /**
87      * Create Ingress Port Match dpidLong, inPort
88      *
89      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
90      * @param dpidLong     Long the datapath ID of a switch/node
91      * @param inPort       Long ingress port on a switch
92      * @return matchBuilder Map MatchBuilder Object with a match
93      */
94     public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
95
96         NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
97         logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
98         matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
99         matchBuilder.setInPort(ncid);
100
101         return matchBuilder;
102     }
103
104     /**
105      * Create EtherType Match
106      *
107      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
108      * @param etherType    Long EtherType
109      * @return matchBuilder Map MatchBuilder Object with a match
110      */
111     public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
112
113         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
114         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
115         ethTypeBuilder.setType(new EtherType(etherType));
116         ethernetMatch.setEthernetType(ethTypeBuilder.build());
117         matchBuilder.setEthernetMatch(ethernetMatch.build());
118
119         return matchBuilder;
120     }
121
122     /**
123      * Create Ethernet Source Match
124      *
125      * @param matchBuilder MatchBuilder Object without a match yet
126      * @param sMacAddr     String representing a source MAC
127      * @return matchBuilder Map MatchBuilder Object with a match
128      */
129     public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
130
131         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
132         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
133         ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
134         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
135         matchBuilder.setEthernetMatch(ethernetMatch.build());
136
137         return matchBuilder;
138     }
139
140     /**
141      * Create Ethernet Destination Match
142      *
143      * @param matchBuilder MatchBuilder Object without a match yet
144      * @param vlanId       Integer representing a VLAN ID Integer representing a VLAN ID
145      * @return matchBuilder Map MatchBuilder Object with a match
146      */
147     public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
148         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
149         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
150         vlanIdBuilder.setVlanId(new VlanId(vlanId));
151         vlanIdBuilder.setVlanIdPresent(present);
152         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
153         matchBuilder.setVlanMatch(vlanMatchBuilder.build());
154
155         return matchBuilder;
156     }
157
158     /**
159      * Create Ethernet Destination Match
160      *
161      * @param matchBuilder MatchBuilder Object without a match yet
162      * @param dMacAddr     String representing a destination MAC
163      * @return matchBuilder Map MatchBuilder Object with a match
164      */
165     public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
166
167         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
168         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
169         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
170         if (mask != null) {
171             ethDestinationBuilder.setMask(mask);
172         }
173         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
174         matchBuilder.setEthernetMatch(ethernetMatch.build());
175
176         return matchBuilder;
177     }
178
179     /**
180      * Tunnel ID Match Builder
181      *
182      * @param matchBuilder MatchBuilder Object without a match yet
183      * @param tunnelId     BigInteger representing a tunnel ID
184      * @return matchBuilder Map MatchBuilder Object with a match
185      */
186     public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
187
188         TunnelBuilder tunnelBuilder = new TunnelBuilder();
189         tunnelBuilder.setTunnelId(tunnelId);
190         matchBuilder.setTunnel(tunnelBuilder.build());
191
192         return matchBuilder;
193     }
194
195     /**
196      * Match ICMP code and type
197      *
198      * @param matchBuilder MatchBuilder Object without a match yet
199      * @param type         short representing an ICMP type
200      * @param code         short representing an ICMP code
201      * @return matchBuilder Map MatchBuilder Object with a match
202      */
203     public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
204
205         EthernetMatchBuilder eth = new EthernetMatchBuilder();
206         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
207         ethTypeBuilder.setType(new EtherType(0x0800L));
208         eth.setEthernetType(ethTypeBuilder.build());
209         matchBuilder.setEthernetMatch(eth.build());
210
211         // Build the IPv4 Match requied per OVS Syntax
212         IpMatchBuilder ipmatch = new IpMatchBuilder();
213         ipmatch.setIpProtocol((short) 1);
214         matchBuilder.setIpMatch(ipmatch.build());
215
216         // Build the ICMPv4 Match
217         Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
218         icmpv4match.setIcmpv4Type(type);
219         icmpv4match.setIcmpv4Code(code);
220         matchBuilder.setIcmpv4Match(icmpv4match.build());
221
222         return matchBuilder;
223     }
224
225     /**
226      * @param matchBuilder MatchBuilder Object without a match yet
227      * @param dstip        String containing an IPv4 prefix
228      * @return matchBuilder Map Object with a match
229      */
230     public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
231
232         EthernetMatchBuilder eth = new EthernetMatchBuilder();
233         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
234         ethTypeBuilder.setType(new EtherType(0x0800L));
235         eth.setEthernetType(ethTypeBuilder.build());
236         matchBuilder.setEthernetMatch(eth.build());
237
238         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
239         ipv4match.setIpv4Destination(dstip);
240
241         matchBuilder.setLayer3Match(ipv4match.build());
242
243         return matchBuilder;
244
245     }
246
247     /**
248      * @param matchBuilder MatchBuilder Object without a match yet
249      * @param dstip        String containing an IPv4 prefix
250      * @return matchBuilder Map Object with a match
251      */
252     public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
253         ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
254         arpDstMatch.setArpTargetTransportAddress(dstip);
255         matchBuilder.setLayer3Match(arpDstMatch.build());
256
257         return matchBuilder;
258     }
259
260     /**
261      * @param matchBuilder MatchBuilder Object without a match yet
262      * @param srcip        String containing an IPv4 prefix
263      * @return matchBuilder Map Object with a match
264      */
265     public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
266
267         EthernetMatchBuilder eth = new EthernetMatchBuilder();
268         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
269         ethTypeBuilder.setType(new EtherType(0x0800L));
270         eth.setEthernetType(ethTypeBuilder.build());
271         matchBuilder.setEthernetMatch(eth.build());
272
273         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
274         ipv4match.setIpv4Source(srcip);
275         matchBuilder.setLayer3Match(ipv4match.build());
276
277         return matchBuilder;
278
279     }
280
281     /**
282      * Create Source TCP Port Match
283      *
284      * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
285      * @param tcpport      Integer representing a source TCP port
286      * @return matchBuilder Map MatchBuilder Object with a match
287      */
288     public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
289
290         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
291         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
292         ethTypeBuilder.setType(new EtherType(0x0800L));
293         ethType.setEthernetType(ethTypeBuilder.build());
294         matchBuilder.setEthernetMatch(ethType.build());
295
296         IpMatchBuilder ipmatch = new IpMatchBuilder();
297         ipmatch.setIpProtocol((short) 6);
298         matchBuilder.setIpMatch(ipmatch.build());
299
300         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
301         tcpmatch.setTcpSourcePort(tcpport);
302         matchBuilder.setLayer4Match(tcpmatch.build());
303
304         return matchBuilder;
305
306     }
307
308     /**
309      * Create Destination TCP Port Match
310      *
311      * @param matchBuilder MatchBuilder Object without a match yet
312      * @param tcpDstPort   Integer representing a destination TCP port
313      * @return matchBuilder Map MatchBuilder Object with a match
314      */
315     public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
316
317         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
318         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
319         ethTypeBuilder.setType(new EtherType(0x0800L));
320         ethType.setEthernetType(ethTypeBuilder.build());
321         matchBuilder.setEthernetMatch(ethType.build());
322
323         IpMatchBuilder ipmatch = new IpMatchBuilder();
324         ipmatch.setIpProtocol((short) 6);
325         matchBuilder.setIpMatch(ipmatch.build());
326
327         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
328         tcpmatch.setTcpDestinationPort(tcpDstPort);
329         matchBuilder.setLayer4Match(tcpmatch.build());
330
331         return matchBuilder;
332     }
333
334     /**
335      * Test match for TCP_Flags
336      *
337      * @param matchBuilder MatchBuilder Object without a match yet
338      * @param tcpPort  PortNumber representing a destination TCP port
339      * @param tcpFlag  int representing a tcp_flag
340      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
341      * <p/>
342      * Defined TCP Flag values in OVS v2.1+
343      * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
344      * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
345      * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS  0x100
346      */
347     public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
348
349         // Ethertype match
350         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
351         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
352         ethTypeBuilder.setType(new EtherType(0x0800L));
353         ethernetType.setEthernetType(ethTypeBuilder.build());
354         matchBuilder.setEthernetMatch(ethernetType.build());
355
356         // TCP Protocol Match
357         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
358         ipMatch.setIpProtocol((short) 6);
359         matchBuilder.setIpMatch(ipMatch.build());
360
361         // TCP Port Match
362         PortNumber dstPort = new PortNumber(tcpPort);
363         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
364         tcpMatch.setTcpDestinationPort(dstPort);
365         matchBuilder.setLayer4Match(tcpMatch.build());
366
367         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
368         tcpFlagMatch.setTcpFlag(tcpFlag);
369         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
370         return matchBuilder;
371     }
372
373     /**
374      * @return MatchBuilder containing the metadata match values
375      */
376     public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData,  BigInteger metaDataMask) {
377
378         // metadata matchbuilder
379         MetadataBuilder metadata = new MetadataBuilder();
380         metadata.setMetadata(metaData);
381         // Optional metadata mask
382         if (metaDataMask != null) {
383             metadata.setMetadataMask(metaDataMask);
384         }
385         matchBuilder.setMetadata(metadata.build());
386
387         return matchBuilder;
388     }
389
390     /**
391      * Create  TCP Port Match
392      *
393      * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
394      * @param tcpport      Integer representing a source TCP port
395      * @return matchBuilder Map MatchBuilder Object with a match
396      */
397     public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
398
399         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
400         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
401         ethTypeBuilder.setType(new EtherType(0x0800L));
402         ethType.setEthernetType(ethTypeBuilder.build());
403         matchBuilder.setEthernetMatch(ethType.build());
404
405         IpMatchBuilder ipMmatch = new IpMatchBuilder();
406         if (ipProtocol == TCP_SHORT) {
407             ipMmatch.setIpProtocol(TCP_SHORT);
408         }
409         else if (ipProtocol == UDP_SHORT) {
410             ipMmatch.setIpProtocol(UDP_SHORT);
411         }
412         else if (ipProtocol == ICMP_SHORT) {
413             ipMmatch.setIpProtocol(ICMP_SHORT);
414         }
415         matchBuilder.setIpMatch(ipMmatch.build());
416         return matchBuilder;
417     }
418
419     /**
420      * Create tcp syn with proto match.
421      *
422      * @param matchBuilder the match builder
423      * @return matchBuilder match builder
424      */
425     public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
426
427         // Ethertype match
428         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
429         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
430         ethTypeBuilder.setType(new EtherType(0x0800L));
431         ethernetType.setEthernetType(ethTypeBuilder.build());
432         matchBuilder.setEthernetMatch(ethernetType.build());
433
434         // TCP Protocol Match
435         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
436         ipMatch.setIpProtocol((short) 6);
437         matchBuilder.setIpMatch(ipMatch.build());
438
439         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
440         tcpFlagMatch.setTcpFlag(TCP_SYN);
441         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
442         return matchBuilder;
443     }
444
445     /**
446      * Create tcp proto syn match.
447      *
448      * @param matchBuilder the match builder
449      * @return matchBuilder match builder
450      */
451     public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
452
453         // TCP Protocol Match
454         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
455         ipMatch.setIpProtocol((short) 6);
456         matchBuilder.setIpMatch(ipMatch.build());
457
458         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
459         tcpFlagMatch.setTcpFlag(TCP_SYN);
460         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
461         return matchBuilder;
462     }
463
464     /**
465      * Create dmac tcp port with flag match.
466      *
467      * @param matchBuilder the match builder
468      * @param attachedMac the attached mac
469      * @param tcpFlag the tcp flag
470      * @param tunnelID the tunnel iD
471      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
472      */
473     public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
474             String attachedMac, Integer tcpFlag, String tunnelID) {
475
476         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
477         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
478         ethTypeBuilder.setType(new EtherType(0x0800L));
479         ethernetMatch.setEthernetType(ethTypeBuilder.build());
480
481         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
482         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
483         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
484         matchBuilder.setEthernetMatch(ethernetMatch.build());
485
486         // TCP Protocol Match
487         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
488         ipMatch.setIpProtocol(TCP_SHORT);
489         matchBuilder.setIpMatch(ipMatch.build());
490
491         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
492         tcpFlagMatch.setTcpFlag(tcpFlag);
493         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
494
495         TunnelBuilder tunnelBuilder = new TunnelBuilder();
496         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
497         matchBuilder.setTunnel(tunnelBuilder.build());
498
499         return matchBuilder;
500     }
501
502     /**
503      * Create dmac tcp syn match.
504      *
505      * @param matchBuilder the match builder
506      * @param attachedMac the attached mac
507      * @param tcpPort the tcp port
508      * @param tcpFlag the tcp flag
509      * @param tunnelID the tunnel iD
510      * @return the match builder
511      */
512     public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
513             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
514
515         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
516         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
517         ethTypeBuilder.setType(new EtherType(0x0800L));
518         ethernetMatch.setEthernetType(ethTypeBuilder.build());
519
520         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
521         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
522         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
523         matchBuilder.setEthernetMatch(ethernetMatch.build());
524
525         // TCP Protocol Match
526         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
527         ipMatch.setIpProtocol((short) 6);
528         matchBuilder.setIpMatch(ipMatch.build());
529
530         // TCP Port Match
531         PortNumber dstPort = new PortNumber(tcpPort);
532         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
533         tcpMatch.setTcpDestinationPort(dstPort);
534         matchBuilder.setLayer4Match(tcpMatch.build());
535
536         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
537         tcpFlagMatch.setTcpFlag(tcpFlag);
538         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
539
540         TunnelBuilder tunnelBuilder = new TunnelBuilder();
541         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
542         matchBuilder.setTunnel(tunnelBuilder.build());
543
544         return matchBuilder;
545     }
546
547     /**
548      * Create dmac tcp syn dst ip prefix tcp port.
549      *
550      * @param matchBuilder the match builder
551      * @param attachedMac the attached mac
552      * @param tcpPort the tcp port
553      * @param tcpFlag the tcp flag
554      * @param segmentationId the segmentation id
555      * @param dstIp the dst ip
556      * @return the match builder
557      */
558     public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
559             MacAddress attachedMac, PortNumber tcpPort,  Integer tcpFlag, String segmentationId,
560             Ipv4Prefix dstIp) {
561
562         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
563         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
564         ethTypeBuilder.setType(new EtherType(0x0800L));
565         ethernetMatch.setEthernetType(ethTypeBuilder.build());
566
567         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
568         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
569         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
570
571         matchBuilder.setEthernetMatch(ethernetMatch.build());
572
573         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
574         ipv4match.setIpv4Destination(dstIp);
575         matchBuilder.setLayer3Match(ipv4match.build());
576
577         // TCP Protocol Match
578         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
579         ipMatch.setIpProtocol(TCP_SHORT);
580         matchBuilder.setIpMatch(ipMatch.build());
581
582         // TCP Port Match
583         PortNumber dstPort = new PortNumber(tcpPort);
584         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
585         tcpMatch.setTcpDestinationPort(dstPort);
586         matchBuilder.setLayer4Match(tcpMatch.build());
587
588         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
589         tcpFlagMatch.setTcpFlag(tcpFlag);
590         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
591
592         TunnelBuilder tunnelBuilder = new TunnelBuilder();
593         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
594         matchBuilder.setTunnel(tunnelBuilder.build());
595
596         return matchBuilder;
597     }
598
599     /**
600      * Create dmac ip tcp syn match.
601      *
602      * @param matchBuilder the match builder
603      * @param dMacAddr the d mac addr
604      * @param mask the mask
605      * @param ipPrefix the ip prefix
606      * @return MatchBuilder containing the metadata match values
607      */
608     public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
609             MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
610
611         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
612         EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
613         ethDestBuilder.setAddress(new MacAddress(dMacAddr));
614         if (mask != null) {
615             ethDestBuilder.setMask(mask);
616         }
617         ethernetMatch.setEthernetDestination(ethDestBuilder.build());
618         matchBuilder.setEthernetMatch(ethernetMatch.build());
619         // Ethertype match
620         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
621         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
622         ethTypeBuilder.setType(new EtherType(0x0800L));
623         ethernetType.setEthernetType(ethTypeBuilder.build());
624         matchBuilder.setEthernetMatch(ethernetType.build());
625         if (ipPrefix != null) {
626             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
627             ipv4match.setIpv4Destination(ipPrefix);
628             matchBuilder.setLayer3Match(ipv4match.build());
629         }
630         // TCP Protocol Match
631         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
632         ipMatch.setIpProtocol(TCP_SHORT);
633         matchBuilder.setIpMatch(ipMatch.build());
634         // TCP Flag Match
635         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
636         tcpFlagMatch.setTcpFlag(TCP_SYN);
637         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
638
639         return matchBuilder;
640     }
641
642     /**
643      * Create smac tcp syn dst ip prefix tcp port.
644      *
645      * @param matchBuilder the match builder
646      * @param attachedMac the attached mac
647      * @param tcpPort the tcp port
648      * @param tcpFlag the tcp flag
649      * @param segmentationId the segmentation id
650      * @param dstIp the dst ip
651      * @return the match builder
652      */
653     public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
654             PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
655
656         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
657         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
658         ethTypeBuilder.setType(new EtherType(0x0800L));
659         ethernetMatch.setEthernetType(ethTypeBuilder.build());
660
661         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
662         ethSourceBuilder.setAddress(new MacAddress(attachedMac));
663         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
664
665         matchBuilder.setEthernetMatch(ethernetMatch.build());
666
667         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
668         ipv4match.setIpv4Destination(dstIp);
669         matchBuilder.setLayer3Match(ipv4match.build());
670
671         // TCP Protocol Match
672         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
673         ipMatch.setIpProtocol(TCP_SHORT);
674         matchBuilder.setIpMatch(ipMatch.build());
675
676         // TCP Port Match
677         PortNumber dstPort = new PortNumber(tcpPort);
678         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
679         tcpMatch.setTcpDestinationPort(dstPort);
680         matchBuilder.setLayer4Match(tcpMatch.build());
681
682         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
683         tcpFlagMatch.setTcpFlag(tcpFlag);
684         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
685
686         TunnelBuilder tunnelBuilder = new TunnelBuilder();
687         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
688         matchBuilder.setTunnel(tunnelBuilder.build());
689
690         return matchBuilder;
691     }
692
693     /**
694      * Create smac tcp port with flag match.
695      *
696      * @param matchBuilder the match builder
697      * @param attachedMac the attached mac
698      * @param tcpFlag the tcp flag
699      * @param tunnelID the tunnel iD
700      * @return matchBuilder
701      */
702     public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
703             Integer tcpFlag, String tunnelID) {
704
705         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
706         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
707         ethTypeBuilder.setType(new EtherType(0x0800L));
708         ethernetMatch.setEthernetType(ethTypeBuilder.build());
709
710         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
711         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
712         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
713         matchBuilder.setEthernetMatch(ethernetMatch.build());
714
715         // TCP Protocol Match
716         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
717         ipMatch.setIpProtocol(TCP_SHORT);
718         matchBuilder.setIpMatch(ipMatch.build());
719
720         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
721         tcpFlagMatch.setTcpFlag(tcpFlag);
722         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
723
724         TunnelBuilder tunnelBuilder = new TunnelBuilder();
725         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
726         matchBuilder.setTunnel(tunnelBuilder.build());
727
728         return matchBuilder;
729     }
730
731     /**
732      * Create smac ip tcp syn match.
733      *
734      * @param matchBuilder the match builder
735      * @param dMacAddr the d mac addr
736      * @param mask the mask
737      * @param ipPrefix the ip prefix
738      * @return MatchBuilder containing the metadata match values
739      */
740     public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
741             MacAddress mask, Ipv4Prefix ipPrefix) {
742
743         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
744         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
745         ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
746         if (mask != null) {
747             ethSrcBuilder.setMask(mask);
748         }
749         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
750         matchBuilder.setEthernetMatch(ethernetMatch.build());
751         // Ethertype match
752         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
753         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
754         ethTypeBuilder.setType(new EtherType(0x0800L));
755         ethernetType.setEthernetType(ethTypeBuilder.build());
756         matchBuilder.setEthernetMatch(ethernetType.build());
757         if (ipPrefix != null) {
758             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
759             ipv4match.setIpv4Destination(ipPrefix);
760             matchBuilder.setLayer3Match(ipv4match.build());
761         }
762         // TCP Protocol Match
763         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
764         ipMatch.setIpProtocol(TCP_SHORT);
765         matchBuilder.setIpMatch(ipMatch.build());
766         // TCP Flag Match
767         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
768         tcpFlagMatch.setTcpFlag(TCP_SYN);
769         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
770
771         return matchBuilder;
772     }
773
774     /**
775      * Create smac tcp syn.
776      *
777      * @param matchBuilder the match builder
778      * @param attachedMac the attached mac
779      * @param tcpPort the tcp port
780      * @param tcpFlag the tcp flag
781      * @param tunnelID the tunnel iD
782      * @return the match builder
783      */
784     public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
785             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
786
787         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
788         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
789         ethTypeBuilder.setType(new EtherType(0x0800L));
790         ethernetMatch.setEthernetType(ethTypeBuilder.build());
791
792         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
793         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
794         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
795         matchBuilder.setEthernetMatch(ethernetMatch.build());
796
797         // TCP Protocol Match
798         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
799         ipMatch.setIpProtocol((short) 6);
800         matchBuilder.setIpMatch(ipMatch.build());
801
802         // TCP Port Match
803         PortNumber dstPort = new PortNumber(tcpPort);
804         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
805         tcpMatch.setTcpDestinationPort(dstPort);
806         matchBuilder.setLayer4Match(tcpMatch.build());
807
808
809         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
810         tcpFlagMatch.setTcpFlag(tcpFlag);
811         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
812
813         TunnelBuilder tunnelBuilder = new TunnelBuilder();
814         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
815         matchBuilder.setTunnel(tunnelBuilder.build());
816
817         return matchBuilder;
818     }
819
820     /**
821      * @return MatchBuilder containing the metadata match values
822      */
823     public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
824             MacAddress dMacAddr,  MacAddress mask, Ipv4Prefix ipPrefix) {
825
826         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
827         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
828         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
829         if (mask != null) {
830             ethDestinationBuilder.setMask(mask);
831         }
832         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
833         matchBuilder.setEthernetMatch(ethernetMatch.build());
834         // Ethertype match
835         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
836         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
837         ethTypeBuilder.setType(new EtherType(0x0800L));
838         ethernetType.setEthernetType(ethTypeBuilder.build());
839         matchBuilder.setEthernetMatch(ethernetType.build());
840         if (ipPrefix != null) {
841             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
842             ipv4match.setIpv4Source(ipPrefix);
843             matchBuilder.setLayer3Match(ipv4match.build());
844         }
845         // TCP Protocol Match
846         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
847         ipMatch.setIpProtocol(TCP_SHORT);
848         matchBuilder.setIpMatch(ipMatch.build());
849         // TCP Flag Match
850         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
851         tcpFlagMatch.setTcpFlag(TCP_SYN);
852         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
853
854         return matchBuilder;
855     }
856
857     public static class RegMatch {
858         final Class<? extends NxmNxReg> reg;
859         final Long value;
860         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
861             super();
862             this.reg = reg;
863             this.value = value;
864         }
865         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
866             return new RegMatch(reg, value);
867         }
868     }
869
870     public static void addNxRegMatch(MatchBuilder match,
871                                      RegMatch... matches) {
872         ArrayList<ExtensionList> extensions = new ArrayList<>();
873         for (RegMatch rm : matches) {
874             Class<? extends ExtensionKey> key;
875             if (NxmNxReg0.class.equals(rm.reg)) {
876                 key = NxmNxReg0Key.class;
877             } else if (NxmNxReg1.class.equals(rm.reg)) {
878                 key = NxmNxReg1Key.class;
879             } else if (NxmNxReg2.class.equals(rm.reg)) {
880                 key = NxmNxReg2Key.class;
881             } else if (NxmNxReg3.class.equals(rm.reg)) {
882                 key = NxmNxReg3Key.class;
883             } else if (NxmNxReg4.class.equals(rm.reg)) {
884                 key = NxmNxReg4Key.class;
885             } else if (NxmNxReg5.class.equals(rm.reg)) {
886                 key = NxmNxReg5Key.class;
887             } else if (NxmNxReg6.class.equals(rm.reg)) {
888                 key = NxmNxReg6Key.class;
889             } else {
890                 key = NxmNxReg7Key.class;
891             }
892             NxAugMatchNodesNodeTableFlow am =
893                     new NxAugMatchNodesNodeTableFlowBuilder()
894                 .setNxmNxReg(new NxmNxRegBuilder()
895                     .setReg(rm.reg)
896                     .setValue(rm.value)
897                     .build())
898                 .build();
899             extensions.add(new ExtensionListBuilder()
900                 .setExtensionKey(key)
901                 .setExtension(new ExtensionBuilder()
902                      .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
903                      .build())
904                 .build());
905         }
906         GeneralAugMatchNodesNodeTableFlow m =
907                 new GeneralAugMatchNodesNodeTableFlowBuilder()
908             .setExtensionList(extensions)
909             .build();
910         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
911     }
912
913     public static void addNxTunIdMatch(MatchBuilder match,
914                                        int tunId) {
915         NxAugMatchNodesNodeTableFlow am =
916                new NxAugMatchNodesNodeTableFlowBuilder()
917                    .setNxmNxTunId(new NxmNxTunIdBuilder()
918                        .setValue(BigInteger.valueOf(tunId))
919                        .build())
920                    .build();
921         GeneralAugMatchNodesNodeTableFlow m =
922                 new GeneralAugMatchNodesNodeTableFlowBuilder()
923             .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
924                 .setExtensionKey(NxmNxTunIdKey.class)
925                 .setExtension(new ExtensionBuilder()
926                     .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
927                     .build())
928                 .build()))
929             .build();
930         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
931     }
932
933     public static void addNxNsp(MatchBuilder match, long nsp) {
934         org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
935                 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
936                 .setNxmNxNsp(new NxmNxNspBuilder()
937                 .setValue(nsp)
938                 .build())
939                 .build();
940         addExtension(match, NxmNxNspKey.class, am);
941     }
942
943     public static void addNxNsi(MatchBuilder match, short nsi) {
944         org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
945                 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
946                 .setNxmNxNsi(new NxmNxNsiBuilder()
947                 .setNsi(nsi)
948                 .build())
949                 .build();
950         addExtension(match, NxmNxNsiKey.class, am);
951     }
952
953     private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
954         GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
955         List<ExtensionList> extensions = null;
956         if (existingAugmentations != null ) {
957             extensions = existingAugmentations.getExtensionList();
958         }
959         if (extensions == null) {
960             extensions = Lists.newArrayList();
961         }
962
963         extensions.add(new ExtensionListBuilder()
964                            .setExtensionKey(extensionKey)
965                            .setExtension(new ExtensionBuilder()
966                            .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
967                            .build())
968                            .build());
969
970         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
971         .setExtensionList(extensions)
972         .build();
973         match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
974     }
975
976     public static EthernetMatch ethernetMatch(MacAddress srcMac,
977                                               MacAddress dstMac,
978                                               Long etherType) {
979         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
980         if (srcMac != null)
981             emb.setEthernetSource(new EthernetSourceBuilder()
982                 .setAddress(srcMac)
983                 .build());
984         if (dstMac != null)
985             emb.setEthernetDestination(new EthernetDestinationBuilder()
986                 .setAddress(dstMac)
987                 .build());
988         if (etherType != null)
989             emb.setEthernetType(new EthernetTypeBuilder()
990                 .setType(new EtherType(etherType))
991                 .build());
992         return emb.build();
993     }
994 }