cbb73c0d2097ba3e9c34a0c17e2fb096916e629a
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / MatchUtils.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.utils.mdsal.openflow;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
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.Ipv6Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfEthDst;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtStateKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtZoneKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpDstKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpSrcKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.state.grouping.NxmNxCtStateBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.zone.grouping.NxmNxCtZoneBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.eth.dst.grouping.NxmOfEthDstBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.src.grouping.NxmOfTcpSrcBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.dst.grouping.NxmOfUdpDstBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.src.grouping.NxmOfUdpSrcBuilder;
88 import org.slf4j.Logger;
89 import org.slf4j.LoggerFactory;
90
91 import com.google.common.collect.ImmutableList;
92 import com.google.common.collect.Lists;
93
94 public class MatchUtils {
95     private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
96     public static final short ICMP_SHORT = 1;
97     public static final short TCP_SHORT = 6;
98     public static final short UDP_SHORT = 17;
99     public static final String TCP = "tcp";
100     public static final String UDP = "udp";
101     private static final int TCP_SYN = 0x0002;
102     public static final String ICMP = "icmp";
103     public static final String ICMPV6 = "icmpv6";
104     public static final short ALL_ICMP = -1;
105
106     /**
107      * Create Ingress Port Match dpidLong, inPort
108      *
109      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
110      * @param dpidLong     Long the datapath ID of a switch/node
111      * @param inPort       Long ingress port on a switch
112      * @return matchBuilder Map MatchBuilder Object with a match
113      */
114     public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
115
116         NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
117         LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
118         matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
119         matchBuilder.setInPort(ncid);
120
121         return matchBuilder;
122     }
123
124     public static MatchBuilder createInPortReservedMatch(MatchBuilder matchBuilder, Long dpidLong, String inPort) {
125
126         NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
127         LOG.debug("createInPortResrevedMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
128                 dpidLong, inPort);
129         matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
130         matchBuilder.setInPort(ncid);
131
132         return matchBuilder;
133     }
134
135     /**
136      * Create EtherType Match
137      *
138      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
139      * @param etherType    Long EtherType
140      * @return matchBuilder Map MatchBuilder Object with a match
141      */
142     public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
143
144         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
145         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
146         ethTypeBuilder.setType(new EtherType(etherType));
147         ethernetMatch.setEthernetType(ethTypeBuilder.build());
148         matchBuilder.setEthernetMatch(ethernetMatch.build());
149
150         return matchBuilder;
151     }
152
153     /**
154      * Create Ethernet Source Match
155      *
156      * @param matchBuilder MatchBuilder Object without a match yet
157      * @param sMacAddr     String representing a source MAC
158      * @return matchBuilder Map MatchBuilder Object with a match
159      */
160     public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
161
162         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
163         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
164         ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
165         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
166         matchBuilder.setEthernetMatch(ethernetMatch.build());
167
168         return matchBuilder;
169     }
170
171     /**
172      * Create Ethernet Destination Match
173      *
174      * @param matchBuilder MatchBuilder Object without a match yet
175      * @param vlanId       Integer representing a VLAN ID Integer representing a VLAN ID
176      * @return matchBuilder Map MatchBuilder Object with a match
177      */
178     public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
179         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
180         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
181         vlanIdBuilder.setVlanId(new VlanId(vlanId));
182         vlanIdBuilder.setVlanIdPresent(present);
183         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
184         matchBuilder.setVlanMatch(vlanMatchBuilder.build());
185
186         return matchBuilder;
187     }
188
189     /**
190      * Create Ethernet Destination Match
191      *
192      * @param matchBuilder MatchBuilder Object without a match yet
193      * @param dMacAddr     String representing a destination MAC
194      * @return matchBuilder Map MatchBuilder Object with a match
195      */
196     public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
197
198         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
199         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
200         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
201         if (mask != null) {
202             ethDestinationBuilder.setMask(mask);
203         }
204         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
205         matchBuilder.setEthernetMatch(ethernetMatch.build());
206
207         return matchBuilder;
208     }
209
210     /**
211      * Tunnel ID Match Builder
212      *
213      * @param matchBuilder MatchBuilder Object without a match yet
214      * @param tunnelId     BigInteger representing a tunnel ID
215      * @return matchBuilder Map MatchBuilder Object with a match
216      */
217     public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
218
219         TunnelBuilder tunnelBuilder = new TunnelBuilder();
220         tunnelBuilder.setTunnelId(tunnelId);
221         matchBuilder.setTunnel(tunnelBuilder.build());
222
223         return matchBuilder;
224     }
225
226     /**
227      * Match ICMP code and type
228      *
229      * @param matchBuilder MatchBuilder Object
230      * @param type         short representing an ICMP type
231      * @param code         short representing an ICMP code
232      * @return matchBuilder Map MatchBuilder Object with a match
233      */
234     public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
235
236         // Build the IPv4 Match requied per OVS Syntax
237         IpMatchBuilder ipmatch = new IpMatchBuilder();
238         ipmatch.setIpProtocol((short) 1);
239         matchBuilder.setIpMatch(ipmatch.build());
240
241         // Build the ICMPv4 Match
242         Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
243         if (type != ALL_ICMP) {
244             icmpv4match.setIcmpv4Type(type);
245         }
246         if (code != ALL_ICMP) {
247             icmpv4match.setIcmpv4Code(code);
248         }
249         matchBuilder.setIcmpv4Match(icmpv4match.build());
250
251         return matchBuilder;
252     }
253
254     /**
255      * Match ICMPv6 code and type
256      *
257      * @param matchBuilder MatchBuilder Object
258      * @param type         short representing an ICMP type
259      * @param code         short representing an ICMP code
260      * @return matchBuilder Map MatchBuilder Object with a match
261      */
262     public static MatchBuilder createICMPv6Match(MatchBuilder matchBuilder, short type, short code) {
263
264         // Build the IPv6 Match required per OVS Syntax
265         IpMatchBuilder ipmatch = new IpMatchBuilder();
266         ipmatch.setIpProtocol((short) 58);
267         matchBuilder.setIpMatch(ipmatch.build());
268
269         // Build the ICMPv6 Match
270         Icmpv6MatchBuilder icmpv6match = new Icmpv6MatchBuilder();
271         if (type != ALL_ICMP || code != ALL_ICMP) {
272             icmpv6match.setIcmpv6Type(type);
273             icmpv6match.setIcmpv6Code(code);
274         }
275         matchBuilder.setIcmpv6Match(icmpv6match.build());
276
277         return matchBuilder;
278     }
279
280     /**
281      * @param matchBuilder MatchBuilder Object without a match yet
282      * @param dstip        String containing an IPv4 prefix
283      * @return matchBuilder Map Object with a match
284      */
285     public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
286
287         EthernetMatchBuilder eth = new EthernetMatchBuilder();
288         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
289         ethTypeBuilder.setType(new EtherType(0x0800L));
290         eth.setEthernetType(ethTypeBuilder.build());
291         matchBuilder.setEthernetMatch(eth.build());
292
293         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
294         ipv4match.setIpv4Destination(dstip);
295
296         matchBuilder.setLayer3Match(ipv4match.build());
297
298         return matchBuilder;
299
300     }
301
302     /**
303      * @param matchBuilder MatchBuilder Object without a match yet
304      * @param dstip        String containing an IPv4 prefix
305      * @return matchBuilder Map Object with a match
306      */
307     public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
308         ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
309         arpDstMatch.setArpTargetTransportAddress(dstip)
310                 .setArpOp(FlowUtils.ARP_OP_REQUEST);
311         matchBuilder.setLayer3Match(arpDstMatch.build());
312
313         return matchBuilder;
314     }
315
316     /**
317      * @param matchBuilder MatchBuilder Object without a match yet
318      * @param srcip        String containing an IPv4 prefix
319      * @return matchBuilder Map Object with a match
320      */
321     public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
322
323         EthernetMatchBuilder eth = new EthernetMatchBuilder();
324         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
325         ethTypeBuilder.setType(new EtherType(0x0800L));
326         eth.setEthernetType(ethTypeBuilder.build());
327         matchBuilder.setEthernetMatch(eth.build());
328
329         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
330         ipv4match.setIpv4Source(srcip);
331         matchBuilder.setLayer3Match(ipv4match.build());
332
333         return matchBuilder;
334
335     }
336
337     /**
338      * Create Source TCP Port Match
339      *
340      * @param matchBuilder MatchBuilder Object without a match yet
341      * @param tcpport      Integer representing a source TCP port
342      * @return matchBuilder Map MatchBuilder Object with a match
343      */
344     public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
345
346         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
347         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
348         ethTypeBuilder.setType(new EtherType(0x0800L));
349         ethType.setEthernetType(ethTypeBuilder.build());
350         matchBuilder.setEthernetMatch(ethType.build());
351
352         IpMatchBuilder ipmatch = new IpMatchBuilder();
353         ipmatch.setIpProtocol((short) 6);
354         matchBuilder.setIpMatch(ipmatch.build());
355
356         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
357         tcpmatch.setTcpSourcePort(tcpport);
358         matchBuilder.setLayer4Match(tcpmatch.build());
359
360         return matchBuilder;
361
362     }
363
364     /**
365      * Create Destination TCP Port Match
366      *
367      * @param matchBuilder MatchBuilder Object without a match yet
368      * @param tcpDstPort   Integer representing a destination TCP port
369      * @return matchBuilder Map MatchBuilder Object with a match
370      */
371     public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
372
373         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
374         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
375         ethTypeBuilder.setType(new EtherType(0x0800L));
376         ethType.setEthernetType(ethTypeBuilder.build());
377         matchBuilder.setEthernetMatch(ethType.build());
378
379         IpMatchBuilder ipmatch = new IpMatchBuilder();
380         ipmatch.setIpProtocol((short) 6);
381         matchBuilder.setIpMatch(ipmatch.build());
382
383         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
384         tcpmatch.setTcpDestinationPort(tcpDstPort);
385         matchBuilder.setLayer4Match(tcpmatch.build());
386
387         return matchBuilder;
388     }
389
390     /**
391      * Test match for TCP_Flags
392      *
393      * @param matchBuilder MatchBuilder Object without a match yet
394      * @param tcpPort  PortNumber representing a destination TCP port
395      * @param tcpFlag  int representing a tcp_flag
396      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
397      * <p>
398      * Defined TCP Flag values in OVS v2.1+
399      * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
400      * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
401      * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS  0x100
402      */
403     public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
404
405         // Ethertype match
406         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
407         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
408         ethTypeBuilder.setType(new EtherType(0x0800L));
409         ethernetType.setEthernetType(ethTypeBuilder.build());
410         matchBuilder.setEthernetMatch(ethernetType.build());
411
412         // TCP Protocol Match
413         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
414         ipMatch.setIpProtocol((short) 6);
415         matchBuilder.setIpMatch(ipMatch.build());
416
417         // TCP Port Match
418         PortNumber dstPort = new PortNumber(tcpPort);
419         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
420         tcpMatch.setTcpDestinationPort(dstPort);
421         matchBuilder.setLayer4Match(tcpMatch.build());
422
423         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
424         tcpFlagMatch.setTcpFlag(tcpFlag);
425         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
426         return matchBuilder;
427     }
428
429     /**
430      * @return MatchBuilder containing the metadata match values
431      */
432     public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData,  BigInteger metaDataMask) {
433
434         // metadata matchbuilder
435         MetadataBuilder metadata = new MetadataBuilder();
436         metadata.setMetadata(metaData);
437         // Optional metadata mask
438         if (metaDataMask != null) {
439             metadata.setMetadataMask(metaDataMask);
440         }
441         matchBuilder.setMetadata(metadata.build());
442
443         return matchBuilder;
444     }
445
446     /**
447      * Create  TCP Port Match
448      *
449      * @param matchBuilder MatchBuilder Object without a match yet
450      * @param ipProtocol   Integer representing the IP protocol
451      * @return matchBuilder Map MatchBuilder Object with a match
452      */
453     public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
454
455         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
456         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
457         ethTypeBuilder.setType(new EtherType(0x0800L));
458         ethType.setEthernetType(ethTypeBuilder.build());
459         matchBuilder.setEthernetMatch(ethType.build());
460
461         IpMatchBuilder ipMmatch = new IpMatchBuilder();
462         if (ipProtocol == TCP_SHORT) {
463             ipMmatch.setIpProtocol(TCP_SHORT);
464         }
465         else if (ipProtocol == UDP_SHORT) {
466             ipMmatch.setIpProtocol(UDP_SHORT);
467         }
468         else if (ipProtocol == ICMP_SHORT) {
469             ipMmatch.setIpProtocol(ICMP_SHORT);
470         }
471         matchBuilder.setIpMatch(ipMmatch.build());
472         return matchBuilder;
473     }
474
475     /**
476      * Create  TCP Port Match
477      *
478      * @param matchBuilder MatchBuilder Object without a match yet
479      * @param ipProtocol   Integer representing the IP protocol
480      * @return matchBuilder Map MatchBuilder Object with a match
481      */
482     public static MatchBuilder createIpv6ProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
483
484         EthernetMatchBuilder ethType = new EthernetMatchBuilder();
485         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
486         ethTypeBuilder.setType(new EtherType(0x86DDL));
487         ethType.setEthernetType(ethTypeBuilder.build());
488         matchBuilder.setEthernetMatch(ethType.build());
489
490         IpMatchBuilder ipMmatch = new IpMatchBuilder();
491         if (ipProtocol == TCP_SHORT) {
492             ipMmatch.setIpProtocol(TCP_SHORT);
493         }
494         else if (ipProtocol == UDP_SHORT) {
495             ipMmatch.setIpProtocol(UDP_SHORT);
496         }
497         else if (ipProtocol == ICMP_SHORT) {
498             ipMmatch.setIpProtocol(ICMP_SHORT);
499         }
500         matchBuilder.setIpMatch(ipMmatch.build());
501         return matchBuilder;
502     }
503
504     /**
505      * Create tcp syn with proto match.
506      *
507      * @param matchBuilder the match builder
508      * @return matchBuilder match builder
509      */
510     public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
511
512         // Ethertype match
513         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
514         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
515         ethTypeBuilder.setType(new EtherType(0x0800L));
516         ethernetType.setEthernetType(ethTypeBuilder.build());
517         matchBuilder.setEthernetMatch(ethernetType.build());
518
519         // TCP Protocol Match
520         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
521         ipMatch.setIpProtocol((short) 6);
522         matchBuilder.setIpMatch(ipMatch.build());
523
524         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
525         tcpFlagMatch.setTcpFlag(TCP_SYN);
526         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
527         return matchBuilder;
528     }
529
530     /**
531      * Create tcp proto syn match.
532      *
533      * @param matchBuilder the match builder
534      * @return matchBuilder match builder
535      */
536     public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
537
538         // TCP Protocol Match
539         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
540         ipMatch.setIpProtocol((short) 6);
541         matchBuilder.setIpMatch(ipMatch.build());
542
543         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
544         tcpFlagMatch.setTcpFlag(TCP_SYN);
545         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
546         return matchBuilder;
547     }
548
549     /**
550      * Create dmac tcp port with flag match.
551      *
552      * @param matchBuilder the match builder
553      * @param attachedMac the attached mac
554      * @param tcpFlag the tcp flag
555      * @param tunnelID the tunnel iD
556      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
557      */
558     public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
559             String attachedMac, Integer tcpFlag, String tunnelID) {
560         return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
561     }
562
563     /**
564      * Create dmac ipSa match.
565      *
566      * @param matchBuilder the match builder
567      * @param attachedMac the attached mac
568      * @param ipPrefix the src ipPrefix
569      * @param tunnelID the tunnel iD
570      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
571      */
572     public static MatchBuilder createDmacIpSaMatch(
573             MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
574         return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
575     }
576
577     /**
578      * Create dmac tcp port ipSa with flag match.
579      *
580      * @param matchBuilder the match builder
581      * @param attachedMac the attached mac
582      * @param tcpFlag the tcp flag
583      * @param ipPrefix the src ipPrefix
584      * @param tunnelID the tunnel iD
585      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
586      */
587     public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
588             MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
589
590         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
591         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
592         ethTypeBuilder.setType(new EtherType(0x0800L));
593         ethernetMatch.setEthernetType(ethTypeBuilder.build());
594
595         if (attachedMac != null) {
596             EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
597             ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
598             ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
599             matchBuilder.setEthernetMatch(ethernetMatch.build());
600         }
601
602         if (tcpFlag != null) {
603             // TCP Protocol Match
604             IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
605             ipMatch.setIpProtocol(TCP_SHORT);
606             matchBuilder.setIpMatch(ipMatch.build());
607
608             TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
609             tcpFlagMatch.setTcpFlag(tcpFlag);
610             matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
611         }
612
613         if (tunnelID != null) {
614             TunnelBuilder tunnelBuilder = new TunnelBuilder();
615             tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
616             matchBuilder.setTunnel(tunnelBuilder.build());
617         }
618
619         if (ipPrefix != null) {
620             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
621             ipv4match.setIpv4Source(ipPrefix);
622             matchBuilder.setLayer3Match(ipv4match.build());
623         }
624
625         return matchBuilder;
626     }
627
628     /**
629      * Create dmac tcp syn match.
630      *
631      * @param matchBuilder the match builder
632      * @param attachedMac the attached mac
633      * @param tcpPort the tcp port
634      * @param tcpFlag the tcp flag
635      * @param tunnelID the tunnel iD
636      * @return the match builder
637      */
638     public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
639             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
640
641         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
642         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
643         ethTypeBuilder.setType(new EtherType(0x0800L));
644         ethernetMatch.setEthernetType(ethTypeBuilder.build());
645
646         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
647         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
648         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
649         matchBuilder.setEthernetMatch(ethernetMatch.build());
650
651         // TCP Protocol Match
652         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
653         ipMatch.setIpProtocol((short) 6);
654         matchBuilder.setIpMatch(ipMatch.build());
655
656         // TCP Port Match
657         PortNumber dstPort = new PortNumber(tcpPort);
658         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
659         tcpMatch.setTcpDestinationPort(dstPort);
660         matchBuilder.setLayer4Match(tcpMatch.build());
661
662         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
663         tcpFlagMatch.setTcpFlag(tcpFlag);
664         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
665
666         TunnelBuilder tunnelBuilder = new TunnelBuilder();
667         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
668         matchBuilder.setTunnel(tunnelBuilder.build());
669
670         return matchBuilder;
671     }
672
673     /**
674      * Create dmac tcp syn dst ip prefix tcp port.
675      *
676      * @param matchBuilder the match builder
677      * @param attachedMac the attached mac
678      * @param tcpPort the tcp port
679      * @param tcpFlag the tcp flag
680      * @param segmentationId the segmentation id
681      * @param dstIp the dst ip
682      * @return the match builder
683      */
684     public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
685             MacAddress attachedMac, PortNumber tcpPort,  Integer tcpFlag, String segmentationId,
686             Ipv4Prefix dstIp) {
687
688         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
689         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
690         ethTypeBuilder.setType(new EtherType(0x0800L));
691         ethernetMatch.setEthernetType(ethTypeBuilder.build());
692
693         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
694         ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
695         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
696
697         matchBuilder.setEthernetMatch(ethernetMatch.build());
698
699         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
700         ipv4match.setIpv4Destination(dstIp);
701         matchBuilder.setLayer3Match(ipv4match.build());
702
703         // TCP Protocol Match
704         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
705         ipMatch.setIpProtocol(TCP_SHORT);
706         matchBuilder.setIpMatch(ipMatch.build());
707
708         // TCP Port Match
709         PortNumber dstPort = new PortNumber(tcpPort);
710         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
711         tcpMatch.setTcpDestinationPort(dstPort);
712         matchBuilder.setLayer4Match(tcpMatch.build());
713
714         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
715         tcpFlagMatch.setTcpFlag(tcpFlag);
716         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
717
718         TunnelBuilder tunnelBuilder = new TunnelBuilder();
719         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
720         matchBuilder.setTunnel(tunnelBuilder.build());
721
722         return matchBuilder;
723     }
724
725     /**
726      * Create dmac ip tcp syn match.
727      *
728      * @param matchBuilder the match builder
729      * @param dMacAddr the d mac addr
730      * @param mask the mask
731      * @param ipPrefix the ip prefix
732      * @return MatchBuilder containing the metadata match values
733      */
734     public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
735             MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
736
737         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
738         EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
739         ethDestBuilder.setAddress(new MacAddress(dMacAddr));
740         if (mask != null) {
741             ethDestBuilder.setMask(mask);
742         }
743         ethernetMatch.setEthernetDestination(ethDestBuilder.build());
744         matchBuilder.setEthernetMatch(ethernetMatch.build());
745         // Ethertype match
746         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
747         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
748         ethTypeBuilder.setType(new EtherType(0x0800L));
749         ethernetType.setEthernetType(ethTypeBuilder.build());
750         matchBuilder.setEthernetMatch(ethernetType.build());
751         if (ipPrefix != null) {
752             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
753             ipv4match.setIpv4Destination(ipPrefix);
754             matchBuilder.setLayer3Match(ipv4match.build());
755         }
756         // TCP Protocol Match
757         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
758         ipMatch.setIpProtocol(TCP_SHORT);
759         matchBuilder.setIpMatch(ipMatch.build());
760         // TCP Flag Match
761         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
762         tcpFlagMatch.setTcpFlag(TCP_SYN);
763         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
764
765         return matchBuilder;
766     }
767
768     /**
769      * Create smac tcp syn dst ip prefix tcp port.
770      *
771      * @param matchBuilder the match builder
772      * @param attachedMac the attached mac
773      * @param tcpPort the tcp port
774      * @param tcpFlag the tcp flag
775      * @param segmentationId the segmentation id
776      * @param dstIp the dst ip
777      * @return the match builder
778      */
779     public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
780             PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
781
782         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
783         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
784         ethTypeBuilder.setType(new EtherType(0x0800L));
785         ethernetMatch.setEthernetType(ethTypeBuilder.build());
786
787         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
788         ethSourceBuilder.setAddress(new MacAddress(attachedMac));
789         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
790
791         matchBuilder.setEthernetMatch(ethernetMatch.build());
792
793         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
794         ipv4match.setIpv4Destination(dstIp);
795         matchBuilder.setLayer3Match(ipv4match.build());
796
797         // TCP Protocol Match
798         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
799         ipMatch.setIpProtocol(TCP_SHORT);
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         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
809         tcpFlagMatch.setTcpFlag(tcpFlag);
810         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
811
812         TunnelBuilder tunnelBuilder = new TunnelBuilder();
813         tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
814         matchBuilder.setTunnel(tunnelBuilder.build());
815
816         return matchBuilder;
817     }
818
819     /**
820      * Create smac tcp port with flag match.
821      *
822      * @param matchBuilder the match builder
823      * @param attachedMac the attached mac
824      * @param tcpFlag the tcp flag
825      * @param tunnelID the tunnel iD
826      * @return matchBuilder
827      */
828     public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
829             Integer tcpFlag, String tunnelID) {
830
831         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
832         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
833         ethTypeBuilder.setType(new EtherType(0x0800L));
834         ethernetMatch.setEthernetType(ethTypeBuilder.build());
835
836         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
837         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
838         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
839         matchBuilder.setEthernetMatch(ethernetMatch.build());
840
841         // TCP Protocol Match
842         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
843         ipMatch.setIpProtocol(TCP_SHORT);
844         matchBuilder.setIpMatch(ipMatch.build());
845
846         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
847         tcpFlagMatch.setTcpFlag(tcpFlag);
848         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
849
850         TunnelBuilder tunnelBuilder = new TunnelBuilder();
851         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
852         matchBuilder.setTunnel(tunnelBuilder.build());
853
854         return matchBuilder;
855     }
856
857     /**
858      * Create smac ip tcp syn match.
859      *
860      * @param matchBuilder the match builder
861      * @param dMacAddr the d mac addr
862      * @param mask the mask
863      * @param ipPrefix the ip prefix
864      * @return MatchBuilder containing the metadata match values
865      */
866     public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
867             MacAddress mask, Ipv4Prefix ipPrefix) {
868
869         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
870         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
871         ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
872         if (mask != null) {
873             ethSrcBuilder.setMask(mask);
874         }
875         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
876         matchBuilder.setEthernetMatch(ethernetMatch.build());
877         // Ethertype match
878         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
879         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
880         ethTypeBuilder.setType(new EtherType(0x0800L));
881         ethernetType.setEthernetType(ethTypeBuilder.build());
882         matchBuilder.setEthernetMatch(ethernetType.build());
883         if (ipPrefix != null) {
884             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
885             ipv4match.setIpv4Destination(ipPrefix);
886             matchBuilder.setLayer3Match(ipv4match.build());
887         }
888         // TCP Protocol Match
889         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
890         ipMatch.setIpProtocol(TCP_SHORT);
891         matchBuilder.setIpMatch(ipMatch.build());
892         // TCP Flag Match
893         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
894         tcpFlagMatch.setTcpFlag(TCP_SYN);
895         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
896
897         return matchBuilder;
898     }
899
900     /**
901      * Create smac tcp syn.
902      *
903      * @param matchBuilder the match builder
904      * @param attachedMac the attached mac
905      * @param tcpPort the tcp port
906      * @param tcpFlag the tcp flag
907      * @param tunnelID the tunnel iD
908      * @return the match builder
909      */
910     public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
911             String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
912
913         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
914         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
915         ethTypeBuilder.setType(new EtherType(0x0800L));
916         ethernetMatch.setEthernetType(ethTypeBuilder.build());
917
918         EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
919         ethSrcBuilder.setAddress(new MacAddress(attachedMac));
920         ethernetMatch.setEthernetSource(ethSrcBuilder.build());
921         matchBuilder.setEthernetMatch(ethernetMatch.build());
922
923         // TCP Protocol Match
924         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
925         ipMatch.setIpProtocol((short) 6);
926         matchBuilder.setIpMatch(ipMatch.build());
927
928         // TCP Port Match
929         PortNumber dstPort = new PortNumber(tcpPort);
930         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
931         tcpMatch.setTcpDestinationPort(dstPort);
932         matchBuilder.setLayer4Match(tcpMatch.build());
933
934
935         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
936         tcpFlagMatch.setTcpFlag(tcpFlag);
937         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
938
939         TunnelBuilder tunnelBuilder = new TunnelBuilder();
940         tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
941         matchBuilder.setTunnel(tunnelBuilder.build());
942
943         return matchBuilder;
944     }
945
946     /**
947      * @return MatchBuilder containing the metadata match values
948      */
949     public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
950             MacAddress dMacAddr,  MacAddress mask, Ipv4Prefix ipPrefix) {
951
952         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
953         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
954         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
955         if (mask != null) {
956             ethDestinationBuilder.setMask(mask);
957         }
958         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
959         matchBuilder.setEthernetMatch(ethernetMatch.build());
960         // Ethertype match
961         EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
962         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
963         ethTypeBuilder.setType(new EtherType(0x0800L));
964         ethernetType.setEthernetType(ethTypeBuilder.build());
965         matchBuilder.setEthernetMatch(ethernetType.build());
966         if (ipPrefix != null) {
967             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
968             ipv4match.setIpv4Source(ipPrefix);
969             matchBuilder.setLayer3Match(ipv4match.build());
970         }
971         // TCP Protocol Match
972         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
973         ipMatch.setIpProtocol(TCP_SHORT);
974         matchBuilder.setIpMatch(ipMatch.build());
975         // TCP Flag Match
976         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
977         tcpFlagMatch.setTcpFlag(TCP_SYN);
978         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
979
980         return matchBuilder;
981     }
982
983     /**
984      * Create a DHCP match with port provided.
985      *
986      * @param matchBuilder the match builder
987      * @param srcPort the source port
988      * @param dstPort the destination port
989      * @return the DHCP match
990      */
991     public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
992                                                int srcPort, int dstPort) {
993
994         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
995         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
996         ethTypeBuilder.setType(new EtherType(0x0800L));
997         ethernetMatch.setEthernetType(ethTypeBuilder.build());
998         matchBuilder.setEthernetMatch(ethernetMatch.build());
999
1000         IpMatchBuilder ipmatch = new IpMatchBuilder();
1001         ipmatch.setIpProtocol(UDP_SHORT);
1002         matchBuilder.setIpMatch(ipmatch.build());
1003
1004         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1005         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1006         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1007         matchBuilder.setLayer4Match(udpmatch.build());
1008
1009         return matchBuilder;
1010
1011     }
1012
1013     /**
1014      * Create a DHCP match with port provided.
1015      *
1016      * @param matchBuilder the match builder
1017      * @param srcPort the source port
1018      * @param dstPort the destination port
1019      * @return the DHCP match
1020      */
1021     public static MatchBuilder createDhcpv6Match(MatchBuilder matchBuilder,
1022                                                  int srcPort, int dstPort) {
1023
1024         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1025         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1026         ethTypeBuilder.setType(new EtherType(0x86DDL));
1027         ethernetMatch.setEthernetType(ethTypeBuilder.build());
1028         matchBuilder.setEthernetMatch(ethernetMatch.build());
1029
1030         IpMatchBuilder ipmatch = new IpMatchBuilder();
1031         ipmatch.setIpProtocol(UDP_SHORT);
1032         matchBuilder.setIpMatch(ipmatch.build());
1033
1034         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1035         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1036         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1037         matchBuilder.setLayer4Match(udpmatch.build());
1038
1039         return matchBuilder;
1040
1041     }
1042
1043     /**
1044      * Creates DHCP server packet match with DHCP mac address and port.
1045      *
1046      * @param matchBuilder the matchbuilder
1047      * @param dhcpServerMac MAc address of the DHCP server of the subnet
1048      * @param srcPort the source port
1049      * @param dstPort the destination port
1050      * @return the DHCP server match
1051      */
1052     public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1053             int dstPort) {
1054
1055         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1056         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1057         ethTypeBuilder.setType(new EtherType(0x0800L));
1058         ethernetMatch.setEthernetType(ethTypeBuilder.build());
1059         matchBuilder.setEthernetMatch(ethernetMatch.build());
1060
1061         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1062         ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1063         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1064         matchBuilder.setEthernetMatch(ethernetMatch.build());
1065
1066         IpMatchBuilder ipmatch = new IpMatchBuilder();
1067         ipmatch.setIpProtocol(UDP_SHORT);
1068         matchBuilder.setIpMatch(ipmatch.build());
1069
1070         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1071         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1072         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1073         matchBuilder.setLayer4Match(udpmatch.build());
1074
1075         return matchBuilder;
1076
1077     }
1078
1079     /**
1080      * Creates DHCPv6 server packet match with DHCP mac address and port.
1081      *
1082      * @param matchBuilder the matchbuilder
1083      * @param dhcpServerMac MAc address of the DHCP server of the subnet
1084      * @param srcPort the source port
1085      * @param dstPort the destination port
1086      * @return the DHCP server match
1087      */
1088     public static MatchBuilder createDhcpv6ServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1089             int dstPort) {
1090
1091         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1092         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1093         ethTypeBuilder.setType(new EtherType(0x86DDL));
1094         ethernetMatch.setEthernetType(ethTypeBuilder.build());
1095         matchBuilder.setEthernetMatch(ethernetMatch.build());
1096
1097         EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1098         ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1099         ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1100         matchBuilder.setEthernetMatch(ethernetMatch.build());
1101
1102         IpMatchBuilder ipmatch = new IpMatchBuilder();
1103         ipmatch.setIpProtocol(UDP_SHORT);
1104         matchBuilder.setIpMatch(ipmatch.build());
1105
1106         UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1107         udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1108         udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1109         matchBuilder.setLayer4Match(udpmatch.build());
1110
1111         return matchBuilder;
1112
1113     }
1114
1115     /**
1116      * Creates a Match with src ip address mac address set.
1117      * @param matchBuilder MatchBuilder Object
1118      * @param srcip String containing an IPv4 prefix
1119      * @param srcMac The source macAddress
1120      * @return matchBuilder Map Object with a match
1121      */
1122     public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
1123
1124         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
1125         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
1126         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1127         ethTypeBuilder.setType(new EtherType(0x0800L));
1128         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1129         eth.setEthernetType(ethTypeBuilder.build());
1130         eth.setEthernetSource(new EthernetSourceBuilder()
1131                 .setAddress(srcMac)
1132                 .build());
1133
1134         matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1135         matchBuilder.setEthernetMatch(eth.build());
1136         return matchBuilder;
1137
1138     }
1139
1140     /**
1141      * Creates a Match with src ip address mac address set.
1142      * @param matchBuilder MatchBuilder Object
1143      * @param srcip String containing an IPv6 prefix
1144      * @param srcMac The source macAddress
1145      * @return matchBuilder Map Object with a match
1146      */
1147     public static MatchBuilder createSrcL3Ipv6MatchWithMac(MatchBuilder matchBuilder, Ipv6Prefix srcip, MacAddress srcMac) {
1148
1149         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
1150         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(srcip));
1151         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1152         ethTypeBuilder.setType(new EtherType(0x86DDL));
1153         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1154         eth.setEthernetType(ethTypeBuilder.build());
1155         eth.setEthernetSource(new EthernetSourceBuilder()
1156                 .setAddress(srcMac)
1157                 .build());
1158
1159         matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1160         matchBuilder.setEthernetMatch(eth.build());
1161         return matchBuilder;
1162
1163     }
1164
1165     /**
1166      * Creates a ether net match with ether type set to 0x0800L.
1167      * @param matchBuilder MatchBuilder Object
1168      * @param srcMac The source macAddress
1169      * @param dstMac The destination mac address
1170      * @return matchBuilder Map Object with a match
1171      */
1172     public static MatchBuilder createV4EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1173     {
1174         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1175         ethTypeBuilder.setType(new EtherType(0x0800L));
1176         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1177         eth.setEthernetType(ethTypeBuilder.build());
1178         if (null != srcMac) {
1179             eth.setEthernetSource(new EthernetSourceBuilder()
1180             .setAddress(new MacAddress(srcMac)).build());
1181         }
1182         if (null != dstMac) {
1183             eth.setEthernetDestination(new EthernetDestinationBuilder()
1184                            .setAddress(new MacAddress(dstMac)).build());
1185         }
1186         matchBuilder.setEthernetMatch(eth.build());
1187         return matchBuilder;
1188     }
1189
1190     /**
1191      * Creates a ether net match with ether type set to 0x86DDL.
1192      * @param matchBuilder MatchBuilder Object
1193      * @param srcMac The source macAddress
1194      * @param dstMac The destination mac address
1195      * @return matchBuilder Map Object with a match
1196      */
1197     public static MatchBuilder createV6EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1198     {
1199         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1200         ethTypeBuilder.setType(new EtherType(0x86DDL));
1201         EthernetMatchBuilder eth = new EthernetMatchBuilder();
1202         eth.setEthernetType(ethTypeBuilder.build());
1203         if (null != srcMac) {
1204             eth.setEthernetSource(new EthernetSourceBuilder()
1205             .setAddress(new MacAddress(srcMac)).build());
1206         }
1207         if (null != dstMac) {
1208             eth.setEthernetDestination(new EthernetDestinationBuilder()
1209                            .setAddress(new MacAddress(dstMac)).build());
1210         }
1211         matchBuilder.setEthernetMatch(eth.build());
1212         return matchBuilder;
1213     }
1214
1215     /**
1216      * Adds remote Ip prefix to existing match.
1217      * @param matchBuilder The match builder
1218      * @param sourceIpPrefix The source IP prefix
1219      * @param destIpPrefix The destination IP prefix
1220      * @return matchBuilder Map Object with a match
1221      */
1222     public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1223                                           Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1224         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1225         if (null != sourceIpPrefix) {
1226             ipv4match.setIpv4Source(sourceIpPrefix);
1227         }
1228         if (null != destIpPrefix) {
1229             ipv4match.setIpv4Destination(destIpPrefix);
1230         }
1231         matchBuilder.setLayer3Match(ipv4match.build());
1232
1233         return matchBuilder;
1234     }
1235
1236     /**
1237      * Adds remote Ipv6 prefix to existing match.
1238      * @param matchBuilder The match builder
1239      * @param sourceIpPrefix The source IP prefix
1240      * @param destIpPrefix The destination IP prefix
1241      * @return matchBuilder Map Object with a match
1242      */
1243     public static MatchBuilder addRemoteIpv6Prefix(MatchBuilder matchBuilder,
1244                                                    Ipv6Prefix sourceIpPrefix,Ipv6Prefix destIpPrefix) {
1245         Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
1246         if (null != sourceIpPrefix) {
1247             ipv6match.setIpv6Source(sourceIpPrefix);
1248         }
1249         if (null != destIpPrefix) {
1250             ipv6match.setIpv6Destination(destIpPrefix);
1251         }
1252         matchBuilder.setLayer3Match(ipv6match.build());
1253
1254         return matchBuilder;
1255     }
1256
1257     /**
1258      * Add a layer4 match to an existing match
1259      *
1260      * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1261      * @param protocol The layer4 protocol
1262      * @param srcPort The src port
1263      * @param destPort The destination port
1264      * @return matchBuilder Map Object with a match
1265      */
1266     public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1267                                               int protocol, int srcPort, int destPort) {
1268         IpMatchBuilder ipmatch = new IpMatchBuilder();
1269         if (TCP_SHORT == protocol) {
1270             ipmatch.setIpProtocol(TCP_SHORT);
1271             TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1272             if (0 != srcPort) {
1273                 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1274             }
1275             if (0 != destPort) {
1276                 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1277             }
1278             matchBuilder.setLayer4Match(tcpmatch.build());
1279         } else if (UDP_SHORT == protocol) {
1280             ipmatch.setIpProtocol(UDP_SHORT);
1281             UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1282             if (0 != srcPort) {
1283                 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1284             }
1285             if (0 != destPort) {
1286                 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1287             }
1288             matchBuilder.setLayer4Match(udpMatch.build());
1289         }
1290         matchBuilder.setIpMatch(ipmatch.build());
1291
1292         return matchBuilder;
1293     }
1294
1295     /**
1296      * Add a layer4 match to an existing match with mask
1297      *
1298      * @param matchBuilder Map matchBuilder MatchBuilder Object with a match.
1299      * @param protocol The layer4 protocol
1300      * @param srcPort The src port
1301      * @param destPort The destination port
1302      * @param mask the mask for the port
1303      * @return matchBuilder Map Object with a match
1304      */
1305     public static MatchBuilder addLayer4MatchWithMask(MatchBuilder matchBuilder,
1306                                                       int protocol, int srcPort, int destPort,int mask) {
1307
1308         IpMatchBuilder ipmatch = new IpMatchBuilder();
1309
1310         NxAugMatchNodesNodeTableFlow nxAugMatch = null;
1311         GeneralAugMatchNodesNodeTableFlow genAugMatch = null;
1312         if (protocol == TCP_SHORT) {
1313             ipmatch.setIpProtocol(TCP_SHORT);
1314             if (0 != srcPort) {
1315                 NxmOfTcpSrcBuilder tcpSrc = new NxmOfTcpSrcBuilder();
1316                 tcpSrc.setPort(new PortNumber(srcPort));
1317                 tcpSrc.setMask(mask);
1318                 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpSrc(tcpSrc.build()).build();
1319                 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1320                 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpSrcKey.class)
1321                                                    .setExtension(new ExtensionBuilder()
1322                                                    .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1323                                                                  .build()).build())).build();
1324             } else if (0 != destPort) {
1325                 NxmOfTcpDstBuilder tcpDst = new NxmOfTcpDstBuilder();
1326                 tcpDst.setPort(new PortNumber(destPort));
1327                 tcpDst.setMask(mask);
1328                 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1329                 .setNxmOfTcpDst(tcpDst.build())
1330                 .build();
1331                 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1332                 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpDstKey.class)
1333                                                    .setExtension(new ExtensionBuilder()
1334                                                    .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1335                                                                  .build()).build())).build();
1336             }
1337
1338         } else if (UDP_SHORT == protocol) {
1339             ipmatch.setIpProtocol(UDP_SHORT);
1340             UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1341             if (0 != srcPort) {
1342                 NxmOfUdpSrcBuilder udpSrc = new NxmOfUdpSrcBuilder();
1343                 udpSrc.setPort(new PortNumber(srcPort));
1344                 udpSrc.setMask(mask);
1345                 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpSrc(udpSrc.build()).build();
1346                 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1347                 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpSrcKey.class)
1348                                                    .setExtension(new ExtensionBuilder()
1349                                                    .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1350                                                                  .build()).build())).build();
1351             } else if (0 != destPort) {
1352                 NxmOfUdpDstBuilder udpDst = new NxmOfUdpDstBuilder();
1353                 udpDst.setPort(new PortNumber(destPort));
1354                 udpDst.setMask(mask);
1355                 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1356                 .setNxmOfUdpDst(udpDst.build())
1357                 .build();
1358                 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1359                 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpDstKey.class)
1360                                                    .setExtension(new ExtensionBuilder()
1361                                                    .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1362                                                                  .build()).build())).build();
1363             }
1364         }
1365         matchBuilder.setIpMatch(ipmatch.build());
1366         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1367         return matchBuilder;
1368     }
1369
1370     public static MatchBuilder addCtState(MatchBuilder matchBuilder,int ct_state, int mask) {
1371         NxmNxCtStateBuilder ctStateBuilder = new NxmNxCtStateBuilder();
1372         ctStateBuilder.setCtState((long)ct_state);
1373         ctStateBuilder.setMask((long)mask);
1374         NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1375         .setNxmNxCtState(ctStateBuilder.build())
1376         .build();
1377         GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1378         .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtStateKey.class)
1379                                            .setExtension(new ExtensionBuilder()
1380                                            .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1381                                                          .build()).build())).build();
1382         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1383         return matchBuilder;
1384     }
1385
1386     public static MatchBuilder addCtZone(MatchBuilder matchBuilder,int ct_zone) {
1387         NxmNxCtZoneBuilder ctZoneBuilder = new NxmNxCtZoneBuilder();
1388         ctZoneBuilder.setCtZone(ct_zone);
1389         NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1390         .setNxmNxCtZone(ctZoneBuilder.build())
1391         .build();
1392         GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1393         .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtZoneKey.class)
1394                                            .setExtension(new ExtensionBuilder()
1395                                            .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1396                                                          .build()).build())).build();
1397         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1398         return matchBuilder;
1399     }
1400
1401     public static class RegMatch {
1402         final Class<? extends NxmNxReg> reg;
1403         final Long value;
1404         public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1405             super();
1406             this.reg = reg;
1407             this.value = value;
1408         }
1409         public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1410             return new RegMatch(reg, value);
1411         }
1412     }
1413
1414     public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1415         List<ExtensionList> extensions = new ArrayList<>();
1416         for (RegMatch rm : matches) {
1417             Class<? extends ExtensionKey> key;
1418             if (NxmNxReg0.class.equals(rm.reg)) {
1419                 key = NxmNxReg0Key.class;
1420             } else if (NxmNxReg1.class.equals(rm.reg)) {
1421                 key = NxmNxReg1Key.class;
1422             } else if (NxmNxReg2.class.equals(rm.reg)) {
1423                 key = NxmNxReg2Key.class;
1424             } else if (NxmNxReg3.class.equals(rm.reg)) {
1425                 key = NxmNxReg3Key.class;
1426             } else if (NxmNxReg4.class.equals(rm.reg)) {
1427                 key = NxmNxReg4Key.class;
1428             } else if (NxmNxReg5.class.equals(rm.reg)) {
1429                 key = NxmNxReg5Key.class;
1430             } else if (NxmNxReg6.class.equals(rm.reg)) {
1431                 key = NxmNxReg6Key.class;
1432             } else {
1433                 key = NxmNxReg7Key.class;
1434             }
1435             NxAugMatchNodesNodeTableFlow am =
1436                     new NxAugMatchNodesNodeTableFlowBuilder()
1437                             .setNxmNxReg(new NxmNxRegBuilder()
1438                                     .setReg(rm.reg)
1439                                     .setValue(rm.value)
1440                                     .build())
1441                             .build();
1442             extensions.add(new ExtensionListBuilder()
1443                     .setExtensionKey(key)
1444                     .setExtension(new ExtensionBuilder()
1445                             .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1446                             .build())
1447                     .build());
1448         }
1449         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1450                 .setExtensionList(extensions)
1451                 .build();
1452         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1453         return matchBuilder;
1454     }
1455
1456     public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1457         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1458                 .setNxmNxTunId(new NxmNxTunIdBuilder()
1459                         .setValue(BigInteger.valueOf(tunId))
1460                         .build())
1461                 .build();
1462         GeneralAugMatchNodesNodeTableFlow m =
1463                 new GeneralAugMatchNodesNodeTableFlowBuilder()
1464                         .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1465                                 .setExtensionKey(NxmNxTunIdKey.class)
1466                                 .setExtension(new ExtensionBuilder()
1467                                         .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1468                                         .build())
1469                                 .build()))
1470                         .build();
1471         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1472         return matchBuilder;
1473     }
1474
1475     public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1476         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1477                 .setNxmNxNsp(new NxmNxNspBuilder()
1478                         .setValue(nsp)
1479                         .build())
1480                 .build();
1481         addExtension(matchBuilder, NxmNxNspKey.class, am);
1482         return matchBuilder;
1483     }
1484
1485     public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1486         NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1487                 .setNxmNxNsi(new NxmNxNsiBuilder()
1488                         .setNsi(nsi)
1489                         .build())
1490                 .build();
1491         addExtension(matchBuilder, NxmNxNsiKey.class, am);
1492         return matchBuilder;
1493     }
1494
1495     private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1496                                      NxAugMatchNodesNodeTableFlow am) {
1497         GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1498                 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1499         List<ExtensionList> extensions = null;
1500         if (existingAugmentations != null ) {
1501             extensions = existingAugmentations.getExtensionList();
1502         }
1503         if (extensions == null) {
1504             extensions = Lists.newArrayList();
1505         }
1506
1507         extensions.add(new ExtensionListBuilder()
1508                 .setExtensionKey(extensionKey)
1509                 .setExtension(new ExtensionBuilder()
1510                         .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1511                         .build())
1512                 .build());
1513
1514         GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1515                 .setExtensionList(extensions)
1516                 .build();
1517         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1518     }
1519
1520     public static EthernetMatch ethernetMatch(MacAddress srcMac,
1521                                               MacAddress dstMac,
1522                                               Long etherType) {
1523         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
1524         if (srcMac != null) {
1525             emb.setEthernetSource(new EthernetSourceBuilder()
1526                 .setAddress(srcMac)
1527                 .build());
1528         }
1529         if (dstMac != null) {
1530             emb.setEthernetDestination(new EthernetDestinationBuilder()
1531                 .setAddress(dstMac)
1532                 .build());
1533         }
1534         if (etherType != null) {
1535             emb.setEthernetType(new EthernetTypeBuilder()
1536                 .setType(new EtherType(etherType))
1537                 .build());
1538         }
1539         return emb.build();
1540     }
1541
1542     /**
1543      * Create ipv4 prefix from ipv4 address, by appending /32 mask
1544      *
1545      * @param ipv4AddressString the ip address, in string format
1546      * @return Ipv4Prefix with ipv4Address and /32 mask
1547      */
1548     public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1549         return new Ipv4Prefix(ipv4AddressString + "/32");
1550     }
1551
1552     /**
1553      * Create ipv6 prefix from ipv6 address, by appending /128 mask
1554      *
1555      * @param ipv6AddressString the ip address, in string format
1556      * @return Ipv6Prefix with ipv6Address and /128 mask
1557      */
1558     public static Ipv6Prefix iPv6PrefixFromIPv6Address(String ipv6AddressString) {
1559         return new Ipv6Prefix(ipv6AddressString + "/128");
1560     }
1561
1562     /**
1563      * Converts port range into a set of masked port ranges.
1564      *
1565      * @param portMin the strating port of the range.
1566      * @param portMax the ending port of the range.
1567      * @return the map contianing the port no and their mask.
1568      *
1569      */
1570     public static Map<Integer,Integer>  getLayer4MaskForRange(int portMin, int portMax) {
1571         int [] offset = {32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
1572         int[] mask = {0x8000,0xC000,0xE000,0xF000,0xF800,0xFC00,0xFE00,0xFF00,
1573             0xFF80,0xFFC0,0xFFE0,0xFFF0,0xFFF8,0xFFFC,0xFFFE,0xFFFF};
1574         int noOfPorts = portMax - portMin + 1;
1575         String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
1576         int medianOffset = 16 - binaryNoOfPorts.length();
1577         int medianLength = offset[medianOffset];
1578         int median = 0;
1579         for (int tempMedian = 0;tempMedian < portMax;) {
1580             tempMedian = medianLength + tempMedian;
1581             if (portMin < tempMedian) {
1582                 median = tempMedian;
1583                 break;
1584             }
1585         }
1586         Map<Integer,Integer> portMap = new HashMap<Integer,Integer>();
1587         int tempMedian = 0;
1588         int currentMedain = median;
1589         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1590             tempMedian = currentMedain - offset[tempMedianOffset];
1591             if (portMin <= tempMedian) {
1592                 for (;portMin <= tempMedian;) {
1593                     portMap.put(tempMedian, mask[tempMedianOffset]);
1594                     currentMedain = tempMedian;
1595                     tempMedian = tempMedian - offset[tempMedianOffset];
1596                 }
1597             }
1598         }
1599         currentMedain = median;
1600         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1601             tempMedian = currentMedain + offset[tempMedianOffset];
1602             if (portMax >= tempMedian - 1) {
1603                 for (;portMax >= tempMedian - 1;) {
1604                     portMap.put(currentMedain, mask[tempMedianOffset]);
1605                     currentMedain = tempMedian;
1606                     tempMedian = tempMedian  + offset[tempMedianOffset];
1607                 }
1608             }
1609         }
1610         return portMap;
1611     }
1612
1613     /**
1614      * Return Long that represents OF port for strings where OF is explicitly provided
1615      *
1616      * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1617      * @return the OFport or null
1618      */
1619     public static Long parseExplicitOFPort(String ofPortIdentifier) {
1620         if (ofPortIdentifier != null) {
1621             String[] pair = ofPortIdentifier.split("\\|");
1622             if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1623                 return new Long(pair[1]);
1624             }
1625         }
1626         return null;
1627     }
1628 }