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