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);
105 * Create EtherType Match
107 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
108 * @param etherType Long EtherType
109 * @return matchBuilder Map MatchBuilder Object with a match
111 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
113 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
114 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
115 ethTypeBuilder.setType(new EtherType(etherType));
116 ethernetMatch.setEthernetType(ethTypeBuilder.build());
117 matchBuilder.setEthernetMatch(ethernetMatch.build());
123 * Create Ethernet Source Match
125 * @param matchBuilder MatchBuilder Object without a match yet
126 * @param sMacAddr String representing a source MAC
127 * @return matchBuilder Map MatchBuilder Object with a match
129 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
131 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
132 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
133 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
134 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
135 matchBuilder.setEthernetMatch(ethernetMatch.build());
141 * Create Ethernet Destination Match
143 * @param matchBuilder MatchBuilder Object without a match yet
144 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
145 * @return matchBuilder Map MatchBuilder Object with a match
147 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
148 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
149 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
150 vlanIdBuilder.setVlanId(new VlanId(vlanId));
151 vlanIdBuilder.setVlanIdPresent(present);
152 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
153 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
159 * Create Ethernet Destination Match
161 * @param matchBuilder MatchBuilder Object without a match yet
162 * @param dMacAddr String representing a destination MAC
163 * @return matchBuilder Map MatchBuilder Object with a match
165 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
167 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
168 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
169 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
171 ethDestinationBuilder.setMask(mask);
173 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
174 matchBuilder.setEthernetMatch(ethernetMatch.build());
180 * Tunnel ID Match Builder
182 * @param matchBuilder MatchBuilder Object without a match yet
183 * @param tunnelId BigInteger representing a tunnel ID
184 * @return matchBuilder Map MatchBuilder Object with a match
186 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
188 TunnelBuilder tunnelBuilder = new TunnelBuilder();
189 tunnelBuilder.setTunnelId(tunnelId);
190 matchBuilder.setTunnel(tunnelBuilder.build());
196 * Match ICMP code and type
198 * @param matchBuilder MatchBuilder Object
199 * @param type short representing an ICMP type
200 * @param code short representing an ICMP code
201 * @return matchBuilder Map MatchBuilder Object with a match
203 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
205 // Build the IPv4 Match requied per OVS Syntax
206 IpMatchBuilder ipmatch = new IpMatchBuilder();
207 ipmatch.setIpProtocol((short) 1);
208 matchBuilder.setIpMatch(ipmatch.build());
210 // Build the ICMPv4 Match
211 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
212 if (type != ALL_ICMP || code != ALL_ICMP) {
213 icmpv4match.setIcmpv4Type(type);
214 icmpv4match.setIcmpv4Code(code);
216 matchBuilder.setIcmpv4Match(icmpv4match.build());
222 * @param matchBuilder MatchBuilder Object without a match yet
223 * @param dstip String containing an IPv4 prefix
224 * @return matchBuilder Map Object with a match
226 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
228 EthernetMatchBuilder eth = new EthernetMatchBuilder();
229 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
230 ethTypeBuilder.setType(new EtherType(0x0800L));
231 eth.setEthernetType(ethTypeBuilder.build());
232 matchBuilder.setEthernetMatch(eth.build());
234 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
235 ipv4match.setIpv4Destination(dstip);
237 matchBuilder.setLayer3Match(ipv4match.build());
244 * @param matchBuilder MatchBuilder Object without a match yet
245 * @param dstip String containing an IPv4 prefix
246 * @return matchBuilder Map Object with a match
248 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
249 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
250 arpDstMatch.setArpTargetTransportAddress(dstip);
251 matchBuilder.setLayer3Match(arpDstMatch.build());
257 * @param matchBuilder MatchBuilder Object without a match yet
258 * @param srcip String containing an IPv4 prefix
259 * @return matchBuilder Map Object with a match
261 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
263 EthernetMatchBuilder eth = new EthernetMatchBuilder();
264 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
265 ethTypeBuilder.setType(new EtherType(0x0800L));
266 eth.setEthernetType(ethTypeBuilder.build());
267 matchBuilder.setEthernetMatch(eth.build());
269 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
270 ipv4match.setIpv4Source(srcip);
271 matchBuilder.setLayer3Match(ipv4match.build());
278 * Create Source TCP Port Match
280 * @param matchBuilder MatchBuilder Object without a match yet
281 * @param tcpport Integer representing a source TCP port
282 * @return matchBuilder Map MatchBuilder Object with a match
284 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
286 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
287 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
288 ethTypeBuilder.setType(new EtherType(0x0800L));
289 ethType.setEthernetType(ethTypeBuilder.build());
290 matchBuilder.setEthernetMatch(ethType.build());
292 IpMatchBuilder ipmatch = new IpMatchBuilder();
293 ipmatch.setIpProtocol((short) 6);
294 matchBuilder.setIpMatch(ipmatch.build());
296 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
297 tcpmatch.setTcpSourcePort(tcpport);
298 matchBuilder.setLayer4Match(tcpmatch.build());
305 * Create Destination TCP Port Match
307 * @param matchBuilder MatchBuilder Object without a match yet
308 * @param tcpDstPort Integer representing a destination TCP port
309 * @return matchBuilder Map MatchBuilder Object with a match
311 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
313 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
314 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
315 ethTypeBuilder.setType(new EtherType(0x0800L));
316 ethType.setEthernetType(ethTypeBuilder.build());
317 matchBuilder.setEthernetMatch(ethType.build());
319 IpMatchBuilder ipmatch = new IpMatchBuilder();
320 ipmatch.setIpProtocol((short) 6);
321 matchBuilder.setIpMatch(ipmatch.build());
323 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
324 tcpmatch.setTcpDestinationPort(tcpDstPort);
325 matchBuilder.setLayer4Match(tcpmatch.build());
331 * Test match for TCP_Flags
333 * @param matchBuilder MatchBuilder Object without a match yet
334 * @param tcpPort PortNumber representing a destination TCP port
335 * @param tcpFlag int representing a tcp_flag
336 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
338 * Defined TCP Flag values in OVS v2.1+
339 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
340 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
341 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
343 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
346 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
347 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
348 ethTypeBuilder.setType(new EtherType(0x0800L));
349 ethernetType.setEthernetType(ethTypeBuilder.build());
350 matchBuilder.setEthernetMatch(ethernetType.build());
352 // TCP Protocol Match
353 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
354 ipMatch.setIpProtocol((short) 6);
355 matchBuilder.setIpMatch(ipMatch.build());
358 PortNumber dstPort = new PortNumber(tcpPort);
359 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
360 tcpMatch.setTcpDestinationPort(dstPort);
361 matchBuilder.setLayer4Match(tcpMatch.build());
363 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
364 tcpFlagMatch.setTcpFlag(tcpFlag);
365 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
370 * @return MatchBuilder containing the metadata match values
372 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
374 // metadata matchbuilder
375 MetadataBuilder metadata = new MetadataBuilder();
376 metadata.setMetadata(metaData);
377 // Optional metadata mask
378 if (metaDataMask != null) {
379 metadata.setMetadataMask(metaDataMask);
381 matchBuilder.setMetadata(metadata.build());
387 * Create TCP Port Match
389 * @param matchBuilder MatchBuilder Object without a match yet
390 * @param ipProtocol Integer representing the IP protocol
391 * @return matchBuilder Map MatchBuilder Object with a match
393 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
395 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
396 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
397 ethTypeBuilder.setType(new EtherType(0x0800L));
398 ethType.setEthernetType(ethTypeBuilder.build());
399 matchBuilder.setEthernetMatch(ethType.build());
401 IpMatchBuilder ipMmatch = new IpMatchBuilder();
402 if (ipProtocol == TCP_SHORT) {
403 ipMmatch.setIpProtocol(TCP_SHORT);
405 else if (ipProtocol == UDP_SHORT) {
406 ipMmatch.setIpProtocol(UDP_SHORT);
408 else if (ipProtocol == ICMP_SHORT) {
409 ipMmatch.setIpProtocol(ICMP_SHORT);
411 matchBuilder.setIpMatch(ipMmatch.build());
416 * Create tcp syn with proto match.
418 * @param matchBuilder the match builder
419 * @return matchBuilder match builder
421 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
424 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
425 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
426 ethTypeBuilder.setType(new EtherType(0x0800L));
427 ethernetType.setEthernetType(ethTypeBuilder.build());
428 matchBuilder.setEthernetMatch(ethernetType.build());
430 // TCP Protocol Match
431 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
432 ipMatch.setIpProtocol((short) 6);
433 matchBuilder.setIpMatch(ipMatch.build());
435 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
436 tcpFlagMatch.setTcpFlag(TCP_SYN);
437 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
442 * Create tcp proto syn match.
444 * @param matchBuilder the match builder
445 * @return matchBuilder match builder
447 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
449 // TCP Protocol Match
450 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
451 ipMatch.setIpProtocol((short) 6);
452 matchBuilder.setIpMatch(ipMatch.build());
454 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
455 tcpFlagMatch.setTcpFlag(TCP_SYN);
456 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
461 * Create dmac tcp port with flag match.
463 * @param matchBuilder the match builder
464 * @param attachedMac the attached mac
465 * @param tcpFlag the tcp flag
466 * @param tunnelID the tunnel iD
467 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
469 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
470 String attachedMac, Integer tcpFlag, String tunnelID) {
471 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
475 * Create dmac ipSa match.
477 * @param matchBuilder the match builder
478 * @param attachedMac the attached mac
479 * @param ipPrefix the src ipPrefix
480 * @param tunnelID the tunnel iD
481 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
483 public static MatchBuilder createDmacIpSaMatch(
484 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
485 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
489 * Create dmac tcp port ipSa with flag match.
491 * @param matchBuilder the match builder
492 * @param attachedMac the attached mac
493 * @param tcpFlag the tcp flag
494 * @param ipPrefix the src ipPrefix
495 * @param tunnelID the tunnel iD
496 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
498 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
499 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
501 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
502 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
503 ethTypeBuilder.setType(new EtherType(0x0800L));
504 ethernetMatch.setEthernetType(ethTypeBuilder.build());
506 if (attachedMac != null) {
507 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
508 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
509 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
510 matchBuilder.setEthernetMatch(ethernetMatch.build());
513 if (tcpFlag != null) {
514 // TCP Protocol Match
515 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
516 ipMatch.setIpProtocol(TCP_SHORT);
517 matchBuilder.setIpMatch(ipMatch.build());
519 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
520 tcpFlagMatch.setTcpFlag(tcpFlag);
521 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
524 if (tunnelID != null) {
525 TunnelBuilder tunnelBuilder = new TunnelBuilder();
526 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
527 matchBuilder.setTunnel(tunnelBuilder.build());
530 if (ipPrefix != null) {
531 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
532 ipv4match.setIpv4Source(ipPrefix);
533 matchBuilder.setLayer3Match(ipv4match.build());
540 * Create dmac tcp syn match.
542 * @param matchBuilder the match builder
543 * @param attachedMac the attached mac
544 * @param tcpPort the tcp port
545 * @param tcpFlag the tcp flag
546 * @param tunnelID the tunnel iD
547 * @return the match builder
549 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
550 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
552 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
553 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
554 ethTypeBuilder.setType(new EtherType(0x0800L));
555 ethernetMatch.setEthernetType(ethTypeBuilder.build());
557 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
558 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
559 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
560 matchBuilder.setEthernetMatch(ethernetMatch.build());
562 // TCP Protocol Match
563 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
564 ipMatch.setIpProtocol((short) 6);
565 matchBuilder.setIpMatch(ipMatch.build());
568 PortNumber dstPort = new PortNumber(tcpPort);
569 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
570 tcpMatch.setTcpDestinationPort(dstPort);
571 matchBuilder.setLayer4Match(tcpMatch.build());
573 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
574 tcpFlagMatch.setTcpFlag(tcpFlag);
575 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
577 TunnelBuilder tunnelBuilder = new TunnelBuilder();
578 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
579 matchBuilder.setTunnel(tunnelBuilder.build());
585 * Create dmac tcp syn dst ip prefix tcp port.
587 * @param matchBuilder the match builder
588 * @param attachedMac the attached mac
589 * @param tcpPort the tcp port
590 * @param tcpFlag the tcp flag
591 * @param segmentationId the segmentation id
592 * @param dstIp the dst ip
593 * @return the match builder
595 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
596 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
599 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
600 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
601 ethTypeBuilder.setType(new EtherType(0x0800L));
602 ethernetMatch.setEthernetType(ethTypeBuilder.build());
604 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
605 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
606 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
608 matchBuilder.setEthernetMatch(ethernetMatch.build());
610 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
611 ipv4match.setIpv4Destination(dstIp);
612 matchBuilder.setLayer3Match(ipv4match.build());
614 // TCP Protocol Match
615 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
616 ipMatch.setIpProtocol(TCP_SHORT);
617 matchBuilder.setIpMatch(ipMatch.build());
620 PortNumber dstPort = new PortNumber(tcpPort);
621 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
622 tcpMatch.setTcpDestinationPort(dstPort);
623 matchBuilder.setLayer4Match(tcpMatch.build());
625 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
626 tcpFlagMatch.setTcpFlag(tcpFlag);
627 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
629 TunnelBuilder tunnelBuilder = new TunnelBuilder();
630 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
631 matchBuilder.setTunnel(tunnelBuilder.build());
637 * Create dmac ip tcp syn match.
639 * @param matchBuilder the match builder
640 * @param dMacAddr the d mac addr
641 * @param mask the mask
642 * @param ipPrefix the ip prefix
643 * @return MatchBuilder containing the metadata match values
645 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
646 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
648 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
649 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
650 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
652 ethDestBuilder.setMask(mask);
654 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
655 matchBuilder.setEthernetMatch(ethernetMatch.build());
657 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
658 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
659 ethTypeBuilder.setType(new EtherType(0x0800L));
660 ethernetType.setEthernetType(ethTypeBuilder.build());
661 matchBuilder.setEthernetMatch(ethernetType.build());
662 if (ipPrefix != null) {
663 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
664 ipv4match.setIpv4Destination(ipPrefix);
665 matchBuilder.setLayer3Match(ipv4match.build());
667 // TCP Protocol Match
668 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
669 ipMatch.setIpProtocol(TCP_SHORT);
670 matchBuilder.setIpMatch(ipMatch.build());
672 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
673 tcpFlagMatch.setTcpFlag(TCP_SYN);
674 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
680 * Create smac tcp syn dst ip prefix tcp port.
682 * @param matchBuilder the match builder
683 * @param attachedMac the attached mac
684 * @param tcpPort the tcp port
685 * @param tcpFlag the tcp flag
686 * @param segmentationId the segmentation id
687 * @param dstIp the dst ip
688 * @return the match builder
690 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
691 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
693 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
694 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
695 ethTypeBuilder.setType(new EtherType(0x0800L));
696 ethernetMatch.setEthernetType(ethTypeBuilder.build());
698 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
699 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
700 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
702 matchBuilder.setEthernetMatch(ethernetMatch.build());
704 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
705 ipv4match.setIpv4Destination(dstIp);
706 matchBuilder.setLayer3Match(ipv4match.build());
708 // TCP Protocol Match
709 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
710 ipMatch.setIpProtocol(TCP_SHORT);
711 matchBuilder.setIpMatch(ipMatch.build());
714 PortNumber dstPort = new PortNumber(tcpPort);
715 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
716 tcpMatch.setTcpDestinationPort(dstPort);
717 matchBuilder.setLayer4Match(tcpMatch.build());
719 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
720 tcpFlagMatch.setTcpFlag(tcpFlag);
721 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
723 TunnelBuilder tunnelBuilder = new TunnelBuilder();
724 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
725 matchBuilder.setTunnel(tunnelBuilder.build());
731 * Create smac tcp port with flag match.
733 * @param matchBuilder the match builder
734 * @param attachedMac the attached mac
735 * @param tcpFlag the tcp flag
736 * @param tunnelID the tunnel iD
737 * @return matchBuilder
739 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
740 Integer tcpFlag, String tunnelID) {
742 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
743 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
744 ethTypeBuilder.setType(new EtherType(0x0800L));
745 ethernetMatch.setEthernetType(ethTypeBuilder.build());
747 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
748 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
749 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
750 matchBuilder.setEthernetMatch(ethernetMatch.build());
752 // TCP Protocol Match
753 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
754 ipMatch.setIpProtocol(TCP_SHORT);
755 matchBuilder.setIpMatch(ipMatch.build());
757 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
758 tcpFlagMatch.setTcpFlag(tcpFlag);
759 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
761 TunnelBuilder tunnelBuilder = new TunnelBuilder();
762 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
763 matchBuilder.setTunnel(tunnelBuilder.build());
769 * Create smac ip tcp syn match.
771 * @param matchBuilder the match builder
772 * @param dMacAddr the d mac addr
773 * @param mask the mask
774 * @param ipPrefix the ip prefix
775 * @return MatchBuilder containing the metadata match values
777 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
778 MacAddress mask, Ipv4Prefix ipPrefix) {
780 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
781 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
782 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
784 ethSrcBuilder.setMask(mask);
786 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
787 matchBuilder.setEthernetMatch(ethernetMatch.build());
789 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
790 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
791 ethTypeBuilder.setType(new EtherType(0x0800L));
792 ethernetType.setEthernetType(ethTypeBuilder.build());
793 matchBuilder.setEthernetMatch(ethernetType.build());
794 if (ipPrefix != null) {
795 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
796 ipv4match.setIpv4Destination(ipPrefix);
797 matchBuilder.setLayer3Match(ipv4match.build());
799 // TCP Protocol Match
800 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
801 ipMatch.setIpProtocol(TCP_SHORT);
802 matchBuilder.setIpMatch(ipMatch.build());
804 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
805 tcpFlagMatch.setTcpFlag(TCP_SYN);
806 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
812 * Create smac tcp syn.
814 * @param matchBuilder the match builder
815 * @param attachedMac the attached mac
816 * @param tcpPort the tcp port
817 * @param tcpFlag the tcp flag
818 * @param tunnelID the tunnel iD
819 * @return the match builder
821 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
822 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
824 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
825 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
826 ethTypeBuilder.setType(new EtherType(0x0800L));
827 ethernetMatch.setEthernetType(ethTypeBuilder.build());
829 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
830 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
831 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
832 matchBuilder.setEthernetMatch(ethernetMatch.build());
834 // TCP Protocol Match
835 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
836 ipMatch.setIpProtocol((short) 6);
837 matchBuilder.setIpMatch(ipMatch.build());
840 PortNumber dstPort = new PortNumber(tcpPort);
841 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
842 tcpMatch.setTcpDestinationPort(dstPort);
843 matchBuilder.setLayer4Match(tcpMatch.build());
846 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
847 tcpFlagMatch.setTcpFlag(tcpFlag);
848 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
850 TunnelBuilder tunnelBuilder = new TunnelBuilder();
851 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
852 matchBuilder.setTunnel(tunnelBuilder.build());
858 * @return MatchBuilder containing the metadata match values
860 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
861 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
863 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
864 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
865 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
867 ethDestinationBuilder.setMask(mask);
869 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
870 matchBuilder.setEthernetMatch(ethernetMatch.build());
872 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
873 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
874 ethTypeBuilder.setType(new EtherType(0x0800L));
875 ethernetType.setEthernetType(ethTypeBuilder.build());
876 matchBuilder.setEthernetMatch(ethernetType.build());
877 if (ipPrefix != null) {
878 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
879 ipv4match.setIpv4Source(ipPrefix);
880 matchBuilder.setLayer3Match(ipv4match.build());
882 // TCP Protocol Match
883 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
884 ipMatch.setIpProtocol(TCP_SHORT);
885 matchBuilder.setIpMatch(ipMatch.build());
887 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
888 tcpFlagMatch.setTcpFlag(TCP_SYN);
889 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
895 * Create a DHCP match with pot provided.
897 * @param matchBuilder the match builder
898 * @param srcPort the source port
899 * @param dstPort the destination port
900 * @return the DHCP match
902 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
903 int srcPort, int dstPort) {
905 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
906 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
907 ethTypeBuilder.setType(new EtherType(0x0800L));
908 ethernetMatch.setEthernetType(ethTypeBuilder.build());
909 matchBuilder.setEthernetMatch(ethernetMatch.build());
911 IpMatchBuilder ipmatch = new IpMatchBuilder();
912 ipmatch.setIpProtocol(UDP_SHORT);
913 matchBuilder.setIpMatch(ipmatch.build());
915 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
916 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
917 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
918 matchBuilder.setLayer4Match(udpmatch.build());
925 * Creates DHCP server packet match with DHCP mac address and port.
927 * @param matchBuilder the matchbuilder
928 * @param dhcpServerMac MAc address of the DHCP server of the subnet
929 * @param srcPort the source port
930 * @param dstPort the destination port
931 * @return the DHCP server match
933 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
936 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
937 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
938 ethTypeBuilder.setType(new EtherType(0x0800L));
939 ethernetMatch.setEthernetType(ethTypeBuilder.build());
940 matchBuilder.setEthernetMatch(ethernetMatch.build());
942 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
943 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
944 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
945 matchBuilder.setEthernetMatch(ethernetMatch.build());
947 IpMatchBuilder ipmatch = new IpMatchBuilder();
948 ipmatch.setIpProtocol(UDP_SHORT);
949 matchBuilder.setIpMatch(ipmatch.build());
951 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
952 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
953 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
954 matchBuilder.setLayer4Match(udpmatch.build());
961 * Creates a Match with src ip address mac address set.
962 * @param matchBuilder MatchBuilder Object
963 * @param srcip String containing an IPv4 prefix
964 * @param srcMac The source macAddress
965 * @return matchBuilder Map Object with a match
967 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
969 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
970 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
971 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
972 ethTypeBuilder.setType(new EtherType(0x0800L));
973 EthernetMatchBuilder eth = new EthernetMatchBuilder();
974 eth.setEthernetType(ethTypeBuilder.build());
975 eth.setEthernetSource(new EthernetSourceBuilder()
979 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
980 matchBuilder.setEthernetMatch(eth.build());
986 * Creates a ether net match with ether type set to 0x0800L.
987 * @param matchBuilder MatchBuilder Object
988 * @param srcMac The source macAddress
989 * @param dstMac The destination mac address
990 * @return matchBuilder Map Object with a match
992 public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
994 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
995 ethTypeBuilder.setType(new EtherType(0x0800L));
996 EthernetMatchBuilder eth = new EthernetMatchBuilder();
997 eth.setEthernetType(ethTypeBuilder.build());
998 if (null != srcMac) {
999 eth.setEthernetSource(new EthernetSourceBuilder()
1000 .setAddress(new MacAddress(srcMac)).build());
1002 if (null != dstMac) {
1003 eth.setEthernetDestination(new EthernetDestinationBuilder()
1004 .setAddress(new MacAddress(dstMac)).build());
1006 matchBuilder.setEthernetMatch(eth.build());
1007 return matchBuilder;
1010 * Adds remote Ip prefix to existing match.
1011 * @param matchBuilder The match builder
1012 * @param sourceIpPrefix The source IP prefix
1013 * @param destIpPrefix The destination IP prefix
1014 * @return matchBuilder Map Object with a match
1016 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1017 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1018 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1019 if (null != sourceIpPrefix) {
1020 ipv4match.setIpv4Source(sourceIpPrefix);
1022 if (null != destIpPrefix) {
1023 ipv4match.setIpv4Destination(destIpPrefix);
1025 matchBuilder.setLayer3Match(ipv4match.build());
1027 return matchBuilder;
1030 * Add a layer4 match to an existing match
1032 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
1033 * @param protocol The layer4 protocol
1034 * @param srcPort The src port
1035 * @param destPort The destination port
1036 * @return matchBuilder Map Object with a match
1038 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1039 int protocol, int srcPort, int destPort) {
1040 IpMatchBuilder ipmatch = new IpMatchBuilder();
1041 if (TCP_SHORT == protocol) {
1042 ipmatch.setIpProtocol(TCP_SHORT);
1043 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1045 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1047 if (0 != destPort) {
1048 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1050 matchBuilder.setLayer4Match(tcpmatch.build());
1051 } else if (UDP_SHORT == protocol) {
1052 ipmatch.setIpProtocol(UDP_SHORT);
1053 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1055 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1057 if (0 != destPort) {
1058 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1060 matchBuilder.setLayer4Match(udpMatch.build());
1062 matchBuilder.setIpMatch(ipmatch.build());
1064 return matchBuilder;
1068 public static class RegMatch {
1069 final Class<? extends NxmNxReg> reg;
1071 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1076 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1077 return new RegMatch(reg, value);
1081 public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1082 List<ExtensionList> extensions = new ArrayList<>();
1083 for (RegMatch rm : matches) {
1084 Class<? extends ExtensionKey> key;
1085 if (NxmNxReg0.class.equals(rm.reg)) {
1086 key = NxmNxReg0Key.class;
1087 } else if (NxmNxReg1.class.equals(rm.reg)) {
1088 key = NxmNxReg1Key.class;
1089 } else if (NxmNxReg2.class.equals(rm.reg)) {
1090 key = NxmNxReg2Key.class;
1091 } else if (NxmNxReg3.class.equals(rm.reg)) {
1092 key = NxmNxReg3Key.class;
1093 } else if (NxmNxReg4.class.equals(rm.reg)) {
1094 key = NxmNxReg4Key.class;
1095 } else if (NxmNxReg5.class.equals(rm.reg)) {
1096 key = NxmNxReg5Key.class;
1097 } else if (NxmNxReg6.class.equals(rm.reg)) {
1098 key = NxmNxReg6Key.class;
1100 key = NxmNxReg7Key.class;
1102 NxAugMatchNodesNodeTableFlow am =
1103 new NxAugMatchNodesNodeTableFlowBuilder()
1104 .setNxmNxReg(new NxmNxRegBuilder()
1109 extensions.add(new ExtensionListBuilder()
1110 .setExtensionKey(key)
1111 .setExtension(new ExtensionBuilder()
1112 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1116 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1117 .setExtensionList(extensions)
1119 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1120 return matchBuilder;
1123 public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1124 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1125 .setNxmNxTunId(new NxmNxTunIdBuilder()
1126 .setValue(BigInteger.valueOf(tunId))
1129 GeneralAugMatchNodesNodeTableFlow m =
1130 new GeneralAugMatchNodesNodeTableFlowBuilder()
1131 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1132 .setExtensionKey(NxmNxTunIdKey.class)
1133 .setExtension(new ExtensionBuilder()
1134 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1138 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1139 return matchBuilder;
1142 public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1143 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1144 .setNxmNxNsp(new NxmNxNspBuilder()
1148 addExtension(matchBuilder, NxmNxNspKey.class, am);
1149 return matchBuilder;
1152 public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1153 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1154 .setNxmNxNsi(new NxmNxNsiBuilder()
1158 addExtension(matchBuilder, NxmNxNsiKey.class, am);
1159 return matchBuilder;
1162 private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1163 NxAugMatchNodesNodeTableFlow am) {
1164 GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1165 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1166 List<ExtensionList> extensions = null;
1167 if (existingAugmentations != null ) {
1168 extensions = existingAugmentations.getExtensionList();
1170 if (extensions == null) {
1171 extensions = Lists.newArrayList();
1174 extensions.add(new ExtensionListBuilder()
1175 .setExtensionKey(extensionKey)
1176 .setExtension(new ExtensionBuilder()
1177 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1181 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1182 .setExtensionList(extensions)
1184 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1187 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1190 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1191 if (srcMac != null) {
1192 emb.setEthernetSource(new EthernetSourceBuilder()
1196 if (dstMac != null) {
1197 emb.setEthernetDestination(new EthernetDestinationBuilder()
1201 if (etherType != null) {
1202 emb.setEthernetType(new EthernetTypeBuilder()
1203 .setType(new EtherType(etherType))
1210 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1212 * @param ipv4AddressString the ip address, in string format
1213 * @return Ipv4Prefix with ipv4Address and /32 mask
1215 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1216 return new Ipv4Prefix(ipv4AddressString + "/32");
1220 * Return Long that represents OF port for strings where OF is explicitly provided
1222 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1223 * @return the OFport or null
1225 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1226 if (ofPortIdentifier != null) {
1227 String[] pair = ofPortIdentifier.split("\\|");
1228 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1229 return new Long(pair[1]);