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