2 * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.utils.mdsal.openflow;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 import com.google.common.collect.ImmutableList;
73 import com.google.common.collect.Lists;
75 public class MatchUtils {
76 private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
77 public static final short ICMP_SHORT = 1;
78 public static final short TCP_SHORT = 6;
79 public static final short UDP_SHORT = 17;
80 public static final String TCP = "tcp";
81 public static final String UDP = "udp";
82 private static final int TCP_SYN = 0x0002;
83 public static final String ICMP = "icmp";
84 public static final short ALL_ICMP = -1;
87 * Create Ingress Port Match dpidLong, inPort
89 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
90 * @param dpidLong Long the datapath ID of a switch/node
91 * @param inPort Long ingress port on a switch
92 * @return matchBuilder Map MatchBuilder Object with a match
94 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
96 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
97 LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
98 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
99 matchBuilder.setInPort(ncid);
104 public static MatchBuilder createInPortReservedMatch(MatchBuilder matchBuilder, Long dpidLong, String inPort) {
106 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
107 LOG.debug("createInPortResrevedMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
109 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
110 matchBuilder.setInPort(ncid);
116 * Create EtherType Match
118 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
119 * @param etherType Long EtherType
120 * @return matchBuilder Map MatchBuilder Object with a match
122 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
124 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
125 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
126 ethTypeBuilder.setType(new EtherType(etherType));
127 ethernetMatch.setEthernetType(ethTypeBuilder.build());
128 matchBuilder.setEthernetMatch(ethernetMatch.build());
134 * Create Ethernet Source Match
136 * @param matchBuilder MatchBuilder Object without a match yet
137 * @param sMacAddr String representing a source MAC
138 * @return matchBuilder Map MatchBuilder Object with a match
140 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
142 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
143 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
144 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
145 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
146 matchBuilder.setEthernetMatch(ethernetMatch.build());
152 * Create Ethernet Destination Match
154 * @param matchBuilder MatchBuilder Object without a match yet
155 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
156 * @return matchBuilder Map MatchBuilder Object with a match
158 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
159 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
160 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
161 vlanIdBuilder.setVlanId(new VlanId(vlanId));
162 vlanIdBuilder.setVlanIdPresent(present);
163 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
164 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
170 * Create Ethernet Destination Match
172 * @param matchBuilder MatchBuilder Object without a match yet
173 * @param dMacAddr String representing a destination MAC
174 * @return matchBuilder Map MatchBuilder Object with a match
176 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
178 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
179 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
180 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
182 ethDestinationBuilder.setMask(mask);
184 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
185 matchBuilder.setEthernetMatch(ethernetMatch.build());
191 * Tunnel ID Match Builder
193 * @param matchBuilder MatchBuilder Object without a match yet
194 * @param tunnelId BigInteger representing a tunnel ID
195 * @return matchBuilder Map MatchBuilder Object with a match
197 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
199 TunnelBuilder tunnelBuilder = new TunnelBuilder();
200 tunnelBuilder.setTunnelId(tunnelId);
201 matchBuilder.setTunnel(tunnelBuilder.build());
207 * Match ICMP code and type
209 * @param matchBuilder MatchBuilder Object
210 * @param type short representing an ICMP type
211 * @param code short representing an ICMP code
212 * @return matchBuilder Map MatchBuilder Object with a match
214 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
216 // Build the IPv4 Match requied per OVS Syntax
217 IpMatchBuilder ipmatch = new IpMatchBuilder();
218 ipmatch.setIpProtocol((short) 1);
219 matchBuilder.setIpMatch(ipmatch.build());
221 // Build the ICMPv4 Match
222 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
223 if (type != ALL_ICMP || code != ALL_ICMP) {
224 icmpv4match.setIcmpv4Type(type);
225 icmpv4match.setIcmpv4Code(code);
227 matchBuilder.setIcmpv4Match(icmpv4match.build());
233 * @param matchBuilder MatchBuilder Object without a match yet
234 * @param dstip String containing an IPv4 prefix
235 * @return matchBuilder Map Object with a match
237 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
239 EthernetMatchBuilder eth = new EthernetMatchBuilder();
240 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
241 ethTypeBuilder.setType(new EtherType(0x0800L));
242 eth.setEthernetType(ethTypeBuilder.build());
243 matchBuilder.setEthernetMatch(eth.build());
245 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
246 ipv4match.setIpv4Destination(dstip);
248 matchBuilder.setLayer3Match(ipv4match.build());
255 * @param matchBuilder MatchBuilder Object without a match yet
256 * @param dstip String containing an IPv4 prefix
257 * @return matchBuilder Map Object with a match
259 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
260 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
261 arpDstMatch.setArpTargetTransportAddress(dstip)
262 .setArpOp(FlowUtils.ARP_OP_REQUEST);
263 matchBuilder.setLayer3Match(arpDstMatch.build());
269 * @param matchBuilder MatchBuilder Object without a match yet
270 * @param srcip String containing an IPv4 prefix
271 * @return matchBuilder Map Object with a match
273 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
275 EthernetMatchBuilder eth = new EthernetMatchBuilder();
276 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
277 ethTypeBuilder.setType(new EtherType(0x0800L));
278 eth.setEthernetType(ethTypeBuilder.build());
279 matchBuilder.setEthernetMatch(eth.build());
281 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
282 ipv4match.setIpv4Source(srcip);
283 matchBuilder.setLayer3Match(ipv4match.build());
290 * Create Source TCP Port Match
292 * @param matchBuilder MatchBuilder Object without a match yet
293 * @param tcpport Integer representing a source TCP port
294 * @return matchBuilder Map MatchBuilder Object with a match
296 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
298 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
299 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
300 ethTypeBuilder.setType(new EtherType(0x0800L));
301 ethType.setEthernetType(ethTypeBuilder.build());
302 matchBuilder.setEthernetMatch(ethType.build());
304 IpMatchBuilder ipmatch = new IpMatchBuilder();
305 ipmatch.setIpProtocol((short) 6);
306 matchBuilder.setIpMatch(ipmatch.build());
308 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
309 tcpmatch.setTcpSourcePort(tcpport);
310 matchBuilder.setLayer4Match(tcpmatch.build());
317 * Create Destination TCP Port Match
319 * @param matchBuilder MatchBuilder Object without a match yet
320 * @param tcpDstPort Integer representing a destination TCP port
321 * @return matchBuilder Map MatchBuilder Object with a match
323 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
325 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
326 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
327 ethTypeBuilder.setType(new EtherType(0x0800L));
328 ethType.setEthernetType(ethTypeBuilder.build());
329 matchBuilder.setEthernetMatch(ethType.build());
331 IpMatchBuilder ipmatch = new IpMatchBuilder();
332 ipmatch.setIpProtocol((short) 6);
333 matchBuilder.setIpMatch(ipmatch.build());
335 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
336 tcpmatch.setTcpDestinationPort(tcpDstPort);
337 matchBuilder.setLayer4Match(tcpmatch.build());
343 * Test match for TCP_Flags
345 * @param matchBuilder MatchBuilder Object without a match yet
346 * @param tcpPort PortNumber representing a destination TCP port
347 * @param tcpFlag int representing a tcp_flag
348 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
350 * Defined TCP Flag values in OVS v2.1+
351 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
352 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
353 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
355 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
358 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
359 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
360 ethTypeBuilder.setType(new EtherType(0x0800L));
361 ethernetType.setEthernetType(ethTypeBuilder.build());
362 matchBuilder.setEthernetMatch(ethernetType.build());
364 // TCP Protocol Match
365 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
366 ipMatch.setIpProtocol((short) 6);
367 matchBuilder.setIpMatch(ipMatch.build());
370 PortNumber dstPort = new PortNumber(tcpPort);
371 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
372 tcpMatch.setTcpDestinationPort(dstPort);
373 matchBuilder.setLayer4Match(tcpMatch.build());
375 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
376 tcpFlagMatch.setTcpFlag(tcpFlag);
377 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
382 * @return MatchBuilder containing the metadata match values
384 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
386 // metadata matchbuilder
387 MetadataBuilder metadata = new MetadataBuilder();
388 metadata.setMetadata(metaData);
389 // Optional metadata mask
390 if (metaDataMask != null) {
391 metadata.setMetadataMask(metaDataMask);
393 matchBuilder.setMetadata(metadata.build());
399 * Create TCP Port Match
401 * @param matchBuilder MatchBuilder Object without a match yet
402 * @param ipProtocol Integer representing the IP protocol
403 * @return matchBuilder Map MatchBuilder Object with a match
405 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
407 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
408 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
409 ethTypeBuilder.setType(new EtherType(0x0800L));
410 ethType.setEthernetType(ethTypeBuilder.build());
411 matchBuilder.setEthernetMatch(ethType.build());
413 IpMatchBuilder ipMmatch = new IpMatchBuilder();
414 if (ipProtocol == TCP_SHORT) {
415 ipMmatch.setIpProtocol(TCP_SHORT);
417 else if (ipProtocol == UDP_SHORT) {
418 ipMmatch.setIpProtocol(UDP_SHORT);
420 else if (ipProtocol == ICMP_SHORT) {
421 ipMmatch.setIpProtocol(ICMP_SHORT);
423 matchBuilder.setIpMatch(ipMmatch.build());
428 * Create tcp syn with proto match.
430 * @param matchBuilder the match builder
431 * @return matchBuilder match builder
433 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
436 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
437 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
438 ethTypeBuilder.setType(new EtherType(0x0800L));
439 ethernetType.setEthernetType(ethTypeBuilder.build());
440 matchBuilder.setEthernetMatch(ethernetType.build());
442 // TCP Protocol Match
443 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
444 ipMatch.setIpProtocol((short) 6);
445 matchBuilder.setIpMatch(ipMatch.build());
447 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
448 tcpFlagMatch.setTcpFlag(TCP_SYN);
449 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
454 * Create tcp proto syn match.
456 * @param matchBuilder the match builder
457 * @return matchBuilder match builder
459 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
461 // TCP Protocol Match
462 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
463 ipMatch.setIpProtocol((short) 6);
464 matchBuilder.setIpMatch(ipMatch.build());
466 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
467 tcpFlagMatch.setTcpFlag(TCP_SYN);
468 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
473 * Create dmac tcp port with flag match.
475 * @param matchBuilder the match builder
476 * @param attachedMac the attached mac
477 * @param tcpFlag the tcp flag
478 * @param tunnelID the tunnel iD
479 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
481 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
482 String attachedMac, Integer tcpFlag, String tunnelID) {
483 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
487 * Create dmac ipSa match.
489 * @param matchBuilder the match builder
490 * @param attachedMac the attached mac
491 * @param ipPrefix the src ipPrefix
492 * @param tunnelID the tunnel iD
493 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
495 public static MatchBuilder createDmacIpSaMatch(
496 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
497 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
501 * Create dmac tcp port ipSa with flag match.
503 * @param matchBuilder the match builder
504 * @param attachedMac the attached mac
505 * @param tcpFlag the tcp flag
506 * @param ipPrefix the src ipPrefix
507 * @param tunnelID the tunnel iD
508 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
510 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
511 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
513 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
514 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
515 ethTypeBuilder.setType(new EtherType(0x0800L));
516 ethernetMatch.setEthernetType(ethTypeBuilder.build());
518 if (attachedMac != null) {
519 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
520 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
521 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
522 matchBuilder.setEthernetMatch(ethernetMatch.build());
525 if (tcpFlag != null) {
526 // TCP Protocol Match
527 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
528 ipMatch.setIpProtocol(TCP_SHORT);
529 matchBuilder.setIpMatch(ipMatch.build());
531 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
532 tcpFlagMatch.setTcpFlag(tcpFlag);
533 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
536 if (tunnelID != null) {
537 TunnelBuilder tunnelBuilder = new TunnelBuilder();
538 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
539 matchBuilder.setTunnel(tunnelBuilder.build());
542 if (ipPrefix != null) {
543 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
544 ipv4match.setIpv4Source(ipPrefix);
545 matchBuilder.setLayer3Match(ipv4match.build());
552 * Create dmac tcp syn match.
554 * @param matchBuilder the match builder
555 * @param attachedMac the attached mac
556 * @param tcpPort the tcp port
557 * @param tcpFlag the tcp flag
558 * @param tunnelID the tunnel iD
559 * @return the match builder
561 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
562 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
564 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
565 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
566 ethTypeBuilder.setType(new EtherType(0x0800L));
567 ethernetMatch.setEthernetType(ethTypeBuilder.build());
569 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
570 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
571 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
572 matchBuilder.setEthernetMatch(ethernetMatch.build());
574 // TCP Protocol Match
575 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
576 ipMatch.setIpProtocol((short) 6);
577 matchBuilder.setIpMatch(ipMatch.build());
580 PortNumber dstPort = new PortNumber(tcpPort);
581 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
582 tcpMatch.setTcpDestinationPort(dstPort);
583 matchBuilder.setLayer4Match(tcpMatch.build());
585 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
586 tcpFlagMatch.setTcpFlag(tcpFlag);
587 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
589 TunnelBuilder tunnelBuilder = new TunnelBuilder();
590 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
591 matchBuilder.setTunnel(tunnelBuilder.build());
597 * Create dmac tcp syn dst ip prefix tcp port.
599 * @param matchBuilder the match builder
600 * @param attachedMac the attached mac
601 * @param tcpPort the tcp port
602 * @param tcpFlag the tcp flag
603 * @param segmentationId the segmentation id
604 * @param dstIp the dst ip
605 * @return the match builder
607 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
608 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
611 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
612 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
613 ethTypeBuilder.setType(new EtherType(0x0800L));
614 ethernetMatch.setEthernetType(ethTypeBuilder.build());
616 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
617 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
618 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
620 matchBuilder.setEthernetMatch(ethernetMatch.build());
622 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
623 ipv4match.setIpv4Destination(dstIp);
624 matchBuilder.setLayer3Match(ipv4match.build());
626 // TCP Protocol Match
627 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
628 ipMatch.setIpProtocol(TCP_SHORT);
629 matchBuilder.setIpMatch(ipMatch.build());
632 PortNumber dstPort = new PortNumber(tcpPort);
633 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
634 tcpMatch.setTcpDestinationPort(dstPort);
635 matchBuilder.setLayer4Match(tcpMatch.build());
637 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
638 tcpFlagMatch.setTcpFlag(tcpFlag);
639 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
641 TunnelBuilder tunnelBuilder = new TunnelBuilder();
642 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
643 matchBuilder.setTunnel(tunnelBuilder.build());
649 * Create dmac ip tcp syn match.
651 * @param matchBuilder the match builder
652 * @param dMacAddr the d mac addr
653 * @param mask the mask
654 * @param ipPrefix the ip prefix
655 * @return MatchBuilder containing the metadata match values
657 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
658 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
660 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
661 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
662 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
664 ethDestBuilder.setMask(mask);
666 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
667 matchBuilder.setEthernetMatch(ethernetMatch.build());
669 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
670 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
671 ethTypeBuilder.setType(new EtherType(0x0800L));
672 ethernetType.setEthernetType(ethTypeBuilder.build());
673 matchBuilder.setEthernetMatch(ethernetType.build());
674 if (ipPrefix != null) {
675 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
676 ipv4match.setIpv4Destination(ipPrefix);
677 matchBuilder.setLayer3Match(ipv4match.build());
679 // TCP Protocol Match
680 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
681 ipMatch.setIpProtocol(TCP_SHORT);
682 matchBuilder.setIpMatch(ipMatch.build());
684 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
685 tcpFlagMatch.setTcpFlag(TCP_SYN);
686 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
692 * Create smac tcp syn dst ip prefix tcp port.
694 * @param matchBuilder the match builder
695 * @param attachedMac the attached mac
696 * @param tcpPort the tcp port
697 * @param tcpFlag the tcp flag
698 * @param segmentationId the segmentation id
699 * @param dstIp the dst ip
700 * @return the match builder
702 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
703 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
705 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
706 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
707 ethTypeBuilder.setType(new EtherType(0x0800L));
708 ethernetMatch.setEthernetType(ethTypeBuilder.build());
710 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
711 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
712 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
714 matchBuilder.setEthernetMatch(ethernetMatch.build());
716 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
717 ipv4match.setIpv4Destination(dstIp);
718 matchBuilder.setLayer3Match(ipv4match.build());
720 // TCP Protocol Match
721 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
722 ipMatch.setIpProtocol(TCP_SHORT);
723 matchBuilder.setIpMatch(ipMatch.build());
726 PortNumber dstPort = new PortNumber(tcpPort);
727 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
728 tcpMatch.setTcpDestinationPort(dstPort);
729 matchBuilder.setLayer4Match(tcpMatch.build());
731 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
732 tcpFlagMatch.setTcpFlag(tcpFlag);
733 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
735 TunnelBuilder tunnelBuilder = new TunnelBuilder();
736 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
737 matchBuilder.setTunnel(tunnelBuilder.build());
743 * Create smac tcp port with flag match.
745 * @param matchBuilder the match builder
746 * @param attachedMac the attached mac
747 * @param tcpFlag the tcp flag
748 * @param tunnelID the tunnel iD
749 * @return matchBuilder
751 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
752 Integer tcpFlag, String tunnelID) {
754 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
755 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
756 ethTypeBuilder.setType(new EtherType(0x0800L));
757 ethernetMatch.setEthernetType(ethTypeBuilder.build());
759 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
760 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
761 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
762 matchBuilder.setEthernetMatch(ethernetMatch.build());
764 // TCP Protocol Match
765 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
766 ipMatch.setIpProtocol(TCP_SHORT);
767 matchBuilder.setIpMatch(ipMatch.build());
769 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
770 tcpFlagMatch.setTcpFlag(tcpFlag);
771 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
773 TunnelBuilder tunnelBuilder = new TunnelBuilder();
774 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
775 matchBuilder.setTunnel(tunnelBuilder.build());
781 * Create smac ip tcp syn match.
783 * @param matchBuilder the match builder
784 * @param dMacAddr the d mac addr
785 * @param mask the mask
786 * @param ipPrefix the ip prefix
787 * @return MatchBuilder containing the metadata match values
789 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
790 MacAddress mask, Ipv4Prefix ipPrefix) {
792 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
793 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
794 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
796 ethSrcBuilder.setMask(mask);
798 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
799 matchBuilder.setEthernetMatch(ethernetMatch.build());
801 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
802 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
803 ethTypeBuilder.setType(new EtherType(0x0800L));
804 ethernetType.setEthernetType(ethTypeBuilder.build());
805 matchBuilder.setEthernetMatch(ethernetType.build());
806 if (ipPrefix != null) {
807 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
808 ipv4match.setIpv4Destination(ipPrefix);
809 matchBuilder.setLayer3Match(ipv4match.build());
811 // TCP Protocol Match
812 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
813 ipMatch.setIpProtocol(TCP_SHORT);
814 matchBuilder.setIpMatch(ipMatch.build());
816 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
817 tcpFlagMatch.setTcpFlag(TCP_SYN);
818 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
824 * Create smac tcp syn.
826 * @param matchBuilder the match builder
827 * @param attachedMac the attached mac
828 * @param tcpPort the tcp port
829 * @param tcpFlag the tcp flag
830 * @param tunnelID the tunnel iD
831 * @return the match builder
833 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
834 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
836 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
837 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
838 ethTypeBuilder.setType(new EtherType(0x0800L));
839 ethernetMatch.setEthernetType(ethTypeBuilder.build());
841 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
842 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
843 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
844 matchBuilder.setEthernetMatch(ethernetMatch.build());
846 // TCP Protocol Match
847 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
848 ipMatch.setIpProtocol((short) 6);
849 matchBuilder.setIpMatch(ipMatch.build());
852 PortNumber dstPort = new PortNumber(tcpPort);
853 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
854 tcpMatch.setTcpDestinationPort(dstPort);
855 matchBuilder.setLayer4Match(tcpMatch.build());
858 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
859 tcpFlagMatch.setTcpFlag(tcpFlag);
860 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
862 TunnelBuilder tunnelBuilder = new TunnelBuilder();
863 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
864 matchBuilder.setTunnel(tunnelBuilder.build());
870 * @return MatchBuilder containing the metadata match values
872 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
873 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
875 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
876 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
877 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
879 ethDestinationBuilder.setMask(mask);
881 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
882 matchBuilder.setEthernetMatch(ethernetMatch.build());
884 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
885 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
886 ethTypeBuilder.setType(new EtherType(0x0800L));
887 ethernetType.setEthernetType(ethTypeBuilder.build());
888 matchBuilder.setEthernetMatch(ethernetType.build());
889 if (ipPrefix != null) {
890 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
891 ipv4match.setIpv4Source(ipPrefix);
892 matchBuilder.setLayer3Match(ipv4match.build());
894 // TCP Protocol Match
895 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
896 ipMatch.setIpProtocol(TCP_SHORT);
897 matchBuilder.setIpMatch(ipMatch.build());
899 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
900 tcpFlagMatch.setTcpFlag(TCP_SYN);
901 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
907 * Create a DHCP match with pot provided.
909 * @param matchBuilder the match builder
910 * @param srcPort the source port
911 * @param dstPort the destination port
912 * @return the DHCP match
914 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
915 int srcPort, int dstPort) {
917 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
918 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
919 ethTypeBuilder.setType(new EtherType(0x0800L));
920 ethernetMatch.setEthernetType(ethTypeBuilder.build());
921 matchBuilder.setEthernetMatch(ethernetMatch.build());
923 IpMatchBuilder ipmatch = new IpMatchBuilder();
924 ipmatch.setIpProtocol(UDP_SHORT);
925 matchBuilder.setIpMatch(ipmatch.build());
927 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
928 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
929 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
930 matchBuilder.setLayer4Match(udpmatch.build());
937 * Creates DHCP server packet match with DHCP mac address and port.
939 * @param matchBuilder the matchbuilder
940 * @param dhcpServerMac MAc address of the DHCP server of the subnet
941 * @param srcPort the source port
942 * @param dstPort the destination port
943 * @return the DHCP server match
945 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
948 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
949 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
950 ethTypeBuilder.setType(new EtherType(0x0800L));
951 ethernetMatch.setEthernetType(ethTypeBuilder.build());
952 matchBuilder.setEthernetMatch(ethernetMatch.build());
954 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
955 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
956 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
957 matchBuilder.setEthernetMatch(ethernetMatch.build());
959 IpMatchBuilder ipmatch = new IpMatchBuilder();
960 ipmatch.setIpProtocol(UDP_SHORT);
961 matchBuilder.setIpMatch(ipmatch.build());
963 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
964 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
965 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
966 matchBuilder.setLayer4Match(udpmatch.build());
973 * Creates a Match with src ip address mac address set.
974 * @param matchBuilder MatchBuilder Object
975 * @param srcip String containing an IPv4 prefix
976 * @param srcMac The source macAddress
977 * @return matchBuilder Map Object with a match
979 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
981 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
982 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
983 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
984 ethTypeBuilder.setType(new EtherType(0x0800L));
985 EthernetMatchBuilder eth = new EthernetMatchBuilder();
986 eth.setEthernetType(ethTypeBuilder.build());
987 eth.setEthernetSource(new EthernetSourceBuilder()
991 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
992 matchBuilder.setEthernetMatch(eth.build());
998 * Creates a ether net match with ether type set to 0x0800L.
999 * @param matchBuilder MatchBuilder Object
1000 * @param srcMac The source macAddress
1001 * @param dstMac The destination mac address
1002 * @return matchBuilder Map Object with a match
1004 public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1006 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1007 ethTypeBuilder.setType(new EtherType(0x0800L));
1008 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1009 eth.setEthernetType(ethTypeBuilder.build());
1010 if (null != srcMac) {
1011 eth.setEthernetSource(new EthernetSourceBuilder()
1012 .setAddress(new MacAddress(srcMac)).build());
1014 if (null != dstMac) {
1015 eth.setEthernetDestination(new EthernetDestinationBuilder()
1016 .setAddress(new MacAddress(dstMac)).build());
1018 matchBuilder.setEthernetMatch(eth.build());
1019 return matchBuilder;
1022 * Adds remote Ip prefix to existing match.
1023 * @param matchBuilder The match builder
1024 * @param sourceIpPrefix The source IP prefix
1025 * @param destIpPrefix The destination IP prefix
1026 * @return matchBuilder Map Object with a match
1028 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1029 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1030 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1031 if (null != sourceIpPrefix) {
1032 ipv4match.setIpv4Source(sourceIpPrefix);
1034 if (null != destIpPrefix) {
1035 ipv4match.setIpv4Destination(destIpPrefix);
1037 matchBuilder.setLayer3Match(ipv4match.build());
1039 return matchBuilder;
1042 * Add a layer4 match to an existing match
1044 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
1045 * @param protocol The layer4 protocol
1046 * @param srcPort The src port
1047 * @param destPort The destination port
1048 * @return matchBuilder Map Object with a match
1050 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1051 int protocol, int srcPort, int destPort) {
1052 IpMatchBuilder ipmatch = new IpMatchBuilder();
1053 if (TCP_SHORT == protocol) {
1054 ipmatch.setIpProtocol(TCP_SHORT);
1055 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1057 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1059 if (0 != destPort) {
1060 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1062 matchBuilder.setLayer4Match(tcpmatch.build());
1063 } else if (UDP_SHORT == protocol) {
1064 ipmatch.setIpProtocol(UDP_SHORT);
1065 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1067 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1069 if (0 != destPort) {
1070 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1072 matchBuilder.setLayer4Match(udpMatch.build());
1074 matchBuilder.setIpMatch(ipmatch.build());
1076 return matchBuilder;
1080 public static class RegMatch {
1081 final Class<? extends NxmNxReg> reg;
1083 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1088 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1089 return new RegMatch(reg, value);
1093 public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1094 List<ExtensionList> extensions = new ArrayList<>();
1095 for (RegMatch rm : matches) {
1096 Class<? extends ExtensionKey> key;
1097 if (NxmNxReg0.class.equals(rm.reg)) {
1098 key = NxmNxReg0Key.class;
1099 } else if (NxmNxReg1.class.equals(rm.reg)) {
1100 key = NxmNxReg1Key.class;
1101 } else if (NxmNxReg2.class.equals(rm.reg)) {
1102 key = NxmNxReg2Key.class;
1103 } else if (NxmNxReg3.class.equals(rm.reg)) {
1104 key = NxmNxReg3Key.class;
1105 } else if (NxmNxReg4.class.equals(rm.reg)) {
1106 key = NxmNxReg4Key.class;
1107 } else if (NxmNxReg5.class.equals(rm.reg)) {
1108 key = NxmNxReg5Key.class;
1109 } else if (NxmNxReg6.class.equals(rm.reg)) {
1110 key = NxmNxReg6Key.class;
1112 key = NxmNxReg7Key.class;
1114 NxAugMatchNodesNodeTableFlow am =
1115 new NxAugMatchNodesNodeTableFlowBuilder()
1116 .setNxmNxReg(new NxmNxRegBuilder()
1121 extensions.add(new ExtensionListBuilder()
1122 .setExtensionKey(key)
1123 .setExtension(new ExtensionBuilder()
1124 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1128 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1129 .setExtensionList(extensions)
1131 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1132 return matchBuilder;
1135 public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1136 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1137 .setNxmNxTunId(new NxmNxTunIdBuilder()
1138 .setValue(BigInteger.valueOf(tunId))
1141 GeneralAugMatchNodesNodeTableFlow m =
1142 new GeneralAugMatchNodesNodeTableFlowBuilder()
1143 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1144 .setExtensionKey(NxmNxTunIdKey.class)
1145 .setExtension(new ExtensionBuilder()
1146 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1150 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1151 return matchBuilder;
1154 public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1155 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1156 .setNxmNxNsp(new NxmNxNspBuilder()
1160 addExtension(matchBuilder, NxmNxNspKey.class, am);
1161 return matchBuilder;
1164 public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1165 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1166 .setNxmNxNsi(new NxmNxNsiBuilder()
1170 addExtension(matchBuilder, NxmNxNsiKey.class, am);
1171 return matchBuilder;
1174 private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1175 NxAugMatchNodesNodeTableFlow am) {
1176 GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1177 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1178 List<ExtensionList> extensions = null;
1179 if (existingAugmentations != null ) {
1180 extensions = existingAugmentations.getExtensionList();
1182 if (extensions == null) {
1183 extensions = Lists.newArrayList();
1186 extensions.add(new ExtensionListBuilder()
1187 .setExtensionKey(extensionKey)
1188 .setExtension(new ExtensionBuilder()
1189 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1193 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1194 .setExtensionList(extensions)
1196 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1199 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1202 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1203 if (srcMac != null) {
1204 emb.setEthernetSource(new EthernetSourceBuilder()
1208 if (dstMac != null) {
1209 emb.setEthernetDestination(new EthernetDestinationBuilder()
1213 if (etherType != null) {
1214 emb.setEthernetType(new EthernetTypeBuilder()
1215 .setType(new EtherType(etherType))
1222 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1224 * @param ipv4AddressString the ip address, in string format
1225 * @return Ipv4Prefix with ipv4Address and /32 mask
1227 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1228 return new Ipv4Prefix(ipv4AddressString + "/32");
1232 * Return Long that represents OF port for strings where OF is explicitly provided
1234 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1235 * @return the OFport or null
1237 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1238 if (ofPortIdentifier != null) {
1239 String[] pair = ofPortIdentifier.split("\\|");
1240 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1241 return new Long(pair[1]);