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