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";
86 * Create Ingress Port Match dpidLong, inPort
88 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
89 * @param dpidLong Long the datapath ID of a switch/node
90 * @param inPort Long ingress port on a switch
91 * @return matchBuilder Map MatchBuilder Object with a match
93 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
95 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
96 LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
97 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
98 matchBuilder.setInPort(ncid);
104 * Create EtherType Match
106 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
107 * @param etherType Long EtherType
108 * @return matchBuilder Map MatchBuilder Object with a match
110 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
112 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
113 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
114 ethTypeBuilder.setType(new EtherType(etherType));
115 ethernetMatch.setEthernetType(ethTypeBuilder.build());
116 matchBuilder.setEthernetMatch(ethernetMatch.build());
122 * Create Ethernet Source Match
124 * @param matchBuilder MatchBuilder Object without a match yet
125 * @param sMacAddr String representing a source MAC
126 * @return matchBuilder Map MatchBuilder Object with a match
128 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
130 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
131 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
132 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
133 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
134 matchBuilder.setEthernetMatch(ethernetMatch.build());
140 * Create Ethernet Destination Match
142 * @param matchBuilder MatchBuilder Object without a match yet
143 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
144 * @return matchBuilder Map MatchBuilder Object with a match
146 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
147 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
148 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
149 vlanIdBuilder.setVlanId(new VlanId(vlanId));
150 vlanIdBuilder.setVlanIdPresent(present);
151 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
152 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
158 * Create Ethernet Destination Match
160 * @param matchBuilder MatchBuilder Object without a match yet
161 * @param dMacAddr String representing a destination MAC
162 * @return matchBuilder Map MatchBuilder Object with a match
164 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
166 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
167 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
168 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
170 ethDestinationBuilder.setMask(mask);
172 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
173 matchBuilder.setEthernetMatch(ethernetMatch.build());
179 * Tunnel ID Match Builder
181 * @param matchBuilder MatchBuilder Object without a match yet
182 * @param tunnelId BigInteger representing a tunnel ID
183 * @return matchBuilder Map MatchBuilder Object with a match
185 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
187 TunnelBuilder tunnelBuilder = new TunnelBuilder();
188 tunnelBuilder.setTunnelId(tunnelId);
189 matchBuilder.setTunnel(tunnelBuilder.build());
195 * Match ICMP code and type
197 * @param matchBuilder MatchBuilder Object
198 * @param type short representing an ICMP type
199 * @param code short representing an ICMP code
200 * @return matchBuilder Map MatchBuilder Object with a match
202 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
204 // Build the IPv4 Match requied per OVS Syntax
205 IpMatchBuilder ipmatch = new IpMatchBuilder();
206 ipmatch.setIpProtocol((short) 1);
207 matchBuilder.setIpMatch(ipmatch.build());
209 // Build the ICMPv4 Match
210 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
211 icmpv4match.setIcmpv4Type(type);
212 icmpv4match.setIcmpv4Code(code);
213 matchBuilder.setIcmpv4Match(icmpv4match.build());
219 * @param matchBuilder MatchBuilder Object without a match yet
220 * @param dstip String containing an IPv4 prefix
221 * @return matchBuilder Map Object with a match
223 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
225 EthernetMatchBuilder eth = new EthernetMatchBuilder();
226 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
227 ethTypeBuilder.setType(new EtherType(0x0800L));
228 eth.setEthernetType(ethTypeBuilder.build());
229 matchBuilder.setEthernetMatch(eth.build());
231 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
232 ipv4match.setIpv4Destination(dstip);
234 matchBuilder.setLayer3Match(ipv4match.build());
241 * @param matchBuilder MatchBuilder Object without a match yet
242 * @param dstip String containing an IPv4 prefix
243 * @return matchBuilder Map Object with a match
245 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
246 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
247 arpDstMatch.setArpTargetTransportAddress(dstip);
248 matchBuilder.setLayer3Match(arpDstMatch.build());
254 * @param matchBuilder MatchBuilder Object without a match yet
255 * @param srcip String containing an IPv4 prefix
256 * @return matchBuilder Map Object with a match
258 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
260 EthernetMatchBuilder eth = new EthernetMatchBuilder();
261 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
262 ethTypeBuilder.setType(new EtherType(0x0800L));
263 eth.setEthernetType(ethTypeBuilder.build());
264 matchBuilder.setEthernetMatch(eth.build());
266 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
267 ipv4match.setIpv4Source(srcip);
268 matchBuilder.setLayer3Match(ipv4match.build());
275 * Create Source TCP Port Match
277 * @param matchBuilder MatchBuilder Object without a match yet
278 * @param tcpport Integer representing a source TCP port
279 * @return matchBuilder Map MatchBuilder Object with a match
281 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
283 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
284 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
285 ethTypeBuilder.setType(new EtherType(0x0800L));
286 ethType.setEthernetType(ethTypeBuilder.build());
287 matchBuilder.setEthernetMatch(ethType.build());
289 IpMatchBuilder ipmatch = new IpMatchBuilder();
290 ipmatch.setIpProtocol((short) 6);
291 matchBuilder.setIpMatch(ipmatch.build());
293 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
294 tcpmatch.setTcpSourcePort(tcpport);
295 matchBuilder.setLayer4Match(tcpmatch.build());
302 * Create Destination TCP Port Match
304 * @param matchBuilder MatchBuilder Object without a match yet
305 * @param tcpDstPort Integer representing a destination TCP port
306 * @return matchBuilder Map MatchBuilder Object with a match
308 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
310 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
311 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
312 ethTypeBuilder.setType(new EtherType(0x0800L));
313 ethType.setEthernetType(ethTypeBuilder.build());
314 matchBuilder.setEthernetMatch(ethType.build());
316 IpMatchBuilder ipmatch = new IpMatchBuilder();
317 ipmatch.setIpProtocol((short) 6);
318 matchBuilder.setIpMatch(ipmatch.build());
320 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
321 tcpmatch.setTcpDestinationPort(tcpDstPort);
322 matchBuilder.setLayer4Match(tcpmatch.build());
328 * Test match for TCP_Flags
330 * @param matchBuilder MatchBuilder Object without a match yet
331 * @param tcpPort PortNumber representing a destination TCP port
332 * @param tcpFlag int representing a tcp_flag
333 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
335 * Defined TCP Flag values in OVS v2.1+
336 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
337 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
338 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
340 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
343 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
344 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
345 ethTypeBuilder.setType(new EtherType(0x0800L));
346 ethernetType.setEthernetType(ethTypeBuilder.build());
347 matchBuilder.setEthernetMatch(ethernetType.build());
349 // TCP Protocol Match
350 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
351 ipMatch.setIpProtocol((short) 6);
352 matchBuilder.setIpMatch(ipMatch.build());
355 PortNumber dstPort = new PortNumber(tcpPort);
356 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
357 tcpMatch.setTcpDestinationPort(dstPort);
358 matchBuilder.setLayer4Match(tcpMatch.build());
360 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
361 tcpFlagMatch.setTcpFlag(tcpFlag);
362 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
367 * @return MatchBuilder containing the metadata match values
369 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
371 // metadata matchbuilder
372 MetadataBuilder metadata = new MetadataBuilder();
373 metadata.setMetadata(metaData);
374 // Optional metadata mask
375 if (metaDataMask != null) {
376 metadata.setMetadataMask(metaDataMask);
378 matchBuilder.setMetadata(metadata.build());
384 * Create TCP Port Match
386 * @param matchBuilder MatchBuilder Object without a match yet
387 * @param ipProtocol Integer representing the IP protocol
388 * @return matchBuilder Map MatchBuilder Object with a match
390 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
392 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
393 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
394 ethTypeBuilder.setType(new EtherType(0x0800L));
395 ethType.setEthernetType(ethTypeBuilder.build());
396 matchBuilder.setEthernetMatch(ethType.build());
398 IpMatchBuilder ipMmatch = new IpMatchBuilder();
399 if (ipProtocol == TCP_SHORT) {
400 ipMmatch.setIpProtocol(TCP_SHORT);
402 else if (ipProtocol == UDP_SHORT) {
403 ipMmatch.setIpProtocol(UDP_SHORT);
405 else if (ipProtocol == ICMP_SHORT) {
406 ipMmatch.setIpProtocol(ICMP_SHORT);
408 matchBuilder.setIpMatch(ipMmatch.build());
413 * Create tcp syn with proto match.
415 * @param matchBuilder the match builder
416 * @return matchBuilder match builder
418 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
421 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
422 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
423 ethTypeBuilder.setType(new EtherType(0x0800L));
424 ethernetType.setEthernetType(ethTypeBuilder.build());
425 matchBuilder.setEthernetMatch(ethernetType.build());
427 // TCP Protocol Match
428 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
429 ipMatch.setIpProtocol((short) 6);
430 matchBuilder.setIpMatch(ipMatch.build());
432 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
433 tcpFlagMatch.setTcpFlag(TCP_SYN);
434 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
439 * Create tcp proto syn match.
441 * @param matchBuilder the match builder
442 * @return matchBuilder match builder
444 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
446 // TCP Protocol Match
447 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
448 ipMatch.setIpProtocol((short) 6);
449 matchBuilder.setIpMatch(ipMatch.build());
451 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
452 tcpFlagMatch.setTcpFlag(TCP_SYN);
453 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
458 * Create dmac tcp port with flag match.
460 * @param matchBuilder the match builder
461 * @param attachedMac the attached mac
462 * @param tcpFlag the tcp flag
463 * @param tunnelID the tunnel iD
464 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
466 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
467 String attachedMac, Integer tcpFlag, String tunnelID) {
468 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
472 * Create dmac ipSa match.
474 * @param matchBuilder the match builder
475 * @param attachedMac the attached mac
476 * @param ipPrefix the src ipPrefix
477 * @param tunnelID the tunnel iD
478 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
480 public static MatchBuilder createDmacIpSaMatch(
481 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
482 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
486 * Create dmac tcp port ipSa with flag match.
488 * @param matchBuilder the match builder
489 * @param attachedMac the attached mac
490 * @param tcpFlag the tcp flag
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 createDmacTcpPortIpSaWithFlagMatch(
496 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
498 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
499 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
500 ethTypeBuilder.setType(new EtherType(0x0800L));
501 ethernetMatch.setEthernetType(ethTypeBuilder.build());
503 if (attachedMac != null) {
504 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
505 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
506 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
507 matchBuilder.setEthernetMatch(ethernetMatch.build());
510 if (tcpFlag != null) {
511 // TCP Protocol Match
512 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
513 ipMatch.setIpProtocol(TCP_SHORT);
514 matchBuilder.setIpMatch(ipMatch.build());
516 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
517 tcpFlagMatch.setTcpFlag(tcpFlag);
518 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
521 if (tunnelID != null) {
522 TunnelBuilder tunnelBuilder = new TunnelBuilder();
523 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
524 matchBuilder.setTunnel(tunnelBuilder.build());
527 if (ipPrefix != null) {
528 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
529 ipv4match.setIpv4Source(ipPrefix);
530 matchBuilder.setLayer3Match(ipv4match.build());
537 * Create dmac tcp syn match.
539 * @param matchBuilder the match builder
540 * @param attachedMac the attached mac
541 * @param tcpPort the tcp port
542 * @param tcpFlag the tcp flag
543 * @param tunnelID the tunnel iD
544 * @return the match builder
546 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
547 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
549 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
550 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
551 ethTypeBuilder.setType(new EtherType(0x0800L));
552 ethernetMatch.setEthernetType(ethTypeBuilder.build());
554 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
555 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
556 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
557 matchBuilder.setEthernetMatch(ethernetMatch.build());
559 // TCP Protocol Match
560 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
561 ipMatch.setIpProtocol((short) 6);
562 matchBuilder.setIpMatch(ipMatch.build());
565 PortNumber dstPort = new PortNumber(tcpPort);
566 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
567 tcpMatch.setTcpDestinationPort(dstPort);
568 matchBuilder.setLayer4Match(tcpMatch.build());
570 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
571 tcpFlagMatch.setTcpFlag(tcpFlag);
572 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
574 TunnelBuilder tunnelBuilder = new TunnelBuilder();
575 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
576 matchBuilder.setTunnel(tunnelBuilder.build());
582 * Create dmac tcp syn dst ip prefix tcp port.
584 * @param matchBuilder the match builder
585 * @param attachedMac the attached mac
586 * @param tcpPort the tcp port
587 * @param tcpFlag the tcp flag
588 * @param segmentationId the segmentation id
589 * @param dstIp the dst ip
590 * @return the match builder
592 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
593 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
596 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
597 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
598 ethTypeBuilder.setType(new EtherType(0x0800L));
599 ethernetMatch.setEthernetType(ethTypeBuilder.build());
601 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
602 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
603 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
605 matchBuilder.setEthernetMatch(ethernetMatch.build());
607 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
608 ipv4match.setIpv4Destination(dstIp);
609 matchBuilder.setLayer3Match(ipv4match.build());
611 // TCP Protocol Match
612 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
613 ipMatch.setIpProtocol(TCP_SHORT);
614 matchBuilder.setIpMatch(ipMatch.build());
617 PortNumber dstPort = new PortNumber(tcpPort);
618 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
619 tcpMatch.setTcpDestinationPort(dstPort);
620 matchBuilder.setLayer4Match(tcpMatch.build());
622 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
623 tcpFlagMatch.setTcpFlag(tcpFlag);
624 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
626 TunnelBuilder tunnelBuilder = new TunnelBuilder();
627 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
628 matchBuilder.setTunnel(tunnelBuilder.build());
634 * Create dmac ip tcp syn match.
636 * @param matchBuilder the match builder
637 * @param dMacAddr the d mac addr
638 * @param mask the mask
639 * @param ipPrefix the ip prefix
640 * @return MatchBuilder containing the metadata match values
642 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
643 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
645 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
646 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
647 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
649 ethDestBuilder.setMask(mask);
651 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
652 matchBuilder.setEthernetMatch(ethernetMatch.build());
654 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
655 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
656 ethTypeBuilder.setType(new EtherType(0x0800L));
657 ethernetType.setEthernetType(ethTypeBuilder.build());
658 matchBuilder.setEthernetMatch(ethernetType.build());
659 if (ipPrefix != null) {
660 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
661 ipv4match.setIpv4Destination(ipPrefix);
662 matchBuilder.setLayer3Match(ipv4match.build());
664 // TCP Protocol Match
665 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
666 ipMatch.setIpProtocol(TCP_SHORT);
667 matchBuilder.setIpMatch(ipMatch.build());
669 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
670 tcpFlagMatch.setTcpFlag(TCP_SYN);
671 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
677 * Create smac tcp syn dst ip prefix tcp port.
679 * @param matchBuilder the match builder
680 * @param attachedMac the attached mac
681 * @param tcpPort the tcp port
682 * @param tcpFlag the tcp flag
683 * @param segmentationId the segmentation id
684 * @param dstIp the dst ip
685 * @return the match builder
687 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
688 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
690 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
691 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
692 ethTypeBuilder.setType(new EtherType(0x0800L));
693 ethernetMatch.setEthernetType(ethTypeBuilder.build());
695 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
696 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
697 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
699 matchBuilder.setEthernetMatch(ethernetMatch.build());
701 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
702 ipv4match.setIpv4Destination(dstIp);
703 matchBuilder.setLayer3Match(ipv4match.build());
705 // TCP Protocol Match
706 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
707 ipMatch.setIpProtocol(TCP_SHORT);
708 matchBuilder.setIpMatch(ipMatch.build());
711 PortNumber dstPort = new PortNumber(tcpPort);
712 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
713 tcpMatch.setTcpDestinationPort(dstPort);
714 matchBuilder.setLayer4Match(tcpMatch.build());
716 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
717 tcpFlagMatch.setTcpFlag(tcpFlag);
718 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
720 TunnelBuilder tunnelBuilder = new TunnelBuilder();
721 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
722 matchBuilder.setTunnel(tunnelBuilder.build());
728 * Create smac tcp port with flag match.
730 * @param matchBuilder the match builder
731 * @param attachedMac the attached mac
732 * @param tcpFlag the tcp flag
733 * @param tunnelID the tunnel iD
734 * @return matchBuilder
736 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
737 Integer tcpFlag, String tunnelID) {
739 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
740 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
741 ethTypeBuilder.setType(new EtherType(0x0800L));
742 ethernetMatch.setEthernetType(ethTypeBuilder.build());
744 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
745 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
746 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
747 matchBuilder.setEthernetMatch(ethernetMatch.build());
749 // TCP Protocol Match
750 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
751 ipMatch.setIpProtocol(TCP_SHORT);
752 matchBuilder.setIpMatch(ipMatch.build());
754 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
755 tcpFlagMatch.setTcpFlag(tcpFlag);
756 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
758 TunnelBuilder tunnelBuilder = new TunnelBuilder();
759 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
760 matchBuilder.setTunnel(tunnelBuilder.build());
766 * Create smac ip tcp syn match.
768 * @param matchBuilder the match builder
769 * @param dMacAddr the d mac addr
770 * @param mask the mask
771 * @param ipPrefix the ip prefix
772 * @return MatchBuilder containing the metadata match values
774 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
775 MacAddress mask, Ipv4Prefix ipPrefix) {
777 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
778 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
779 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
781 ethSrcBuilder.setMask(mask);
783 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
784 matchBuilder.setEthernetMatch(ethernetMatch.build());
786 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
787 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
788 ethTypeBuilder.setType(new EtherType(0x0800L));
789 ethernetType.setEthernetType(ethTypeBuilder.build());
790 matchBuilder.setEthernetMatch(ethernetType.build());
791 if (ipPrefix != null) {
792 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
793 ipv4match.setIpv4Destination(ipPrefix);
794 matchBuilder.setLayer3Match(ipv4match.build());
796 // TCP Protocol Match
797 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
798 ipMatch.setIpProtocol(TCP_SHORT);
799 matchBuilder.setIpMatch(ipMatch.build());
801 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
802 tcpFlagMatch.setTcpFlag(TCP_SYN);
803 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
809 * Create smac tcp syn.
811 * @param matchBuilder the match builder
812 * @param attachedMac the attached mac
813 * @param tcpPort the tcp port
814 * @param tcpFlag the tcp flag
815 * @param tunnelID the tunnel iD
816 * @return the match builder
818 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
819 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
821 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
822 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
823 ethTypeBuilder.setType(new EtherType(0x0800L));
824 ethernetMatch.setEthernetType(ethTypeBuilder.build());
826 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
827 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
828 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
829 matchBuilder.setEthernetMatch(ethernetMatch.build());
831 // TCP Protocol Match
832 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
833 ipMatch.setIpProtocol((short) 6);
834 matchBuilder.setIpMatch(ipMatch.build());
837 PortNumber dstPort = new PortNumber(tcpPort);
838 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
839 tcpMatch.setTcpDestinationPort(dstPort);
840 matchBuilder.setLayer4Match(tcpMatch.build());
843 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
844 tcpFlagMatch.setTcpFlag(tcpFlag);
845 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
847 TunnelBuilder tunnelBuilder = new TunnelBuilder();
848 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
849 matchBuilder.setTunnel(tunnelBuilder.build());
855 * @return MatchBuilder containing the metadata match values
857 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
858 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
860 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
861 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
862 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
864 ethDestinationBuilder.setMask(mask);
866 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
867 matchBuilder.setEthernetMatch(ethernetMatch.build());
869 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
870 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
871 ethTypeBuilder.setType(new EtherType(0x0800L));
872 ethernetType.setEthernetType(ethTypeBuilder.build());
873 matchBuilder.setEthernetMatch(ethernetType.build());
874 if (ipPrefix != null) {
875 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
876 ipv4match.setIpv4Source(ipPrefix);
877 matchBuilder.setLayer3Match(ipv4match.build());
879 // TCP Protocol Match
880 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
881 ipMatch.setIpProtocol(TCP_SHORT);
882 matchBuilder.setIpMatch(ipMatch.build());
884 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
885 tcpFlagMatch.setTcpFlag(TCP_SYN);
886 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
892 * Create a DHCP match with pot provided.
894 * @param matchBuilder the match builder
895 * @param srcPort the source port
896 * @param dstPort the destination port
897 * @return the DHCP match
899 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
900 int srcPort, int dstPort) {
902 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
903 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
904 ethTypeBuilder.setType(new EtherType(0x0800L));
905 ethernetMatch.setEthernetType(ethTypeBuilder.build());
906 matchBuilder.setEthernetMatch(ethernetMatch.build());
908 IpMatchBuilder ipmatch = new IpMatchBuilder();
909 ipmatch.setIpProtocol(UDP_SHORT);
910 matchBuilder.setIpMatch(ipmatch.build());
912 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
913 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
914 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
915 matchBuilder.setLayer4Match(udpmatch.build());
922 * Creates DHCP server packet match with DHCP mac address and port.
924 * @param matchBuilder the matchbuilder
925 * @param dhcpServerMac MAc address of the DHCP server of the subnet
926 * @param srcPort the source port
927 * @param dstPort the destination port
928 * @return the DHCP server match
930 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
933 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
934 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
935 ethTypeBuilder.setType(new EtherType(0x0800L));
936 ethernetMatch.setEthernetType(ethTypeBuilder.build());
937 matchBuilder.setEthernetMatch(ethernetMatch.build());
939 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
940 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
941 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
942 matchBuilder.setEthernetMatch(ethernetMatch.build());
944 IpMatchBuilder ipmatch = new IpMatchBuilder();
945 ipmatch.setIpProtocol(UDP_SHORT);
946 matchBuilder.setIpMatch(ipmatch.build());
948 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
949 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
950 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
951 matchBuilder.setLayer4Match(udpmatch.build());
958 * Creates a Match with src ip address mac address set.
959 * @param matchBuilder MatchBuilder Object
960 * @param srcip String containing an IPv4 prefix
961 * @param srcMac The source macAddress
962 * @return matchBuilder Map Object with a match
964 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
966 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
967 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
968 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
969 ethTypeBuilder.setType(new EtherType(0x0800L));
970 EthernetMatchBuilder eth = new EthernetMatchBuilder();
971 eth.setEthernetType(ethTypeBuilder.build());
972 eth.setEthernetSource(new EthernetSourceBuilder()
976 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
977 matchBuilder.setEthernetMatch(eth.build());
983 * Creates a ether net match with ether type set to 0x0800L.
984 * @param matchBuilder MatchBuilder Object
985 * @param srcMac The source macAddress
986 * @param dstMac The destination mac address
987 * @return matchBuilder Map Object with a match
989 public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
991 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
992 ethTypeBuilder.setType(new EtherType(0x0800L));
993 EthernetMatchBuilder eth = new EthernetMatchBuilder();
994 eth.setEthernetType(ethTypeBuilder.build());
995 if (null != srcMac) {
996 eth.setEthernetSource(new EthernetSourceBuilder()
997 .setAddress(new MacAddress(srcMac)).build());
999 if (null != dstMac) {
1000 eth.setEthernetDestination(new EthernetDestinationBuilder()
1001 .setAddress(new MacAddress(dstMac)).build());
1003 matchBuilder.setEthernetMatch(eth.build());
1004 return matchBuilder;
1007 * Adds remote Ip prefix to existing match.
1008 * @param matchBuilder The match builder
1009 * @param sourceIpPrefix The source IP prefix
1010 * @param destIpPrefix The destination IP prefix
1011 * @return matchBuilder Map Object with a match
1013 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1014 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1015 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1016 if (null != sourceIpPrefix) {
1017 ipv4match.setIpv4Source(sourceIpPrefix);
1019 if (null != destIpPrefix) {
1020 ipv4match.setIpv4Destination(destIpPrefix);
1022 matchBuilder.setLayer3Match(ipv4match.build());
1024 return matchBuilder;
1027 * Add a layer4 match to an existing match
1029 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
1030 * @param protocol The layer4 protocol
1031 * @param srcPort The src port
1032 * @param destPort The destination port
1033 * @return matchBuilder Map Object with a match
1035 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1036 int protocol, int srcPort, int destPort) {
1037 IpMatchBuilder ipmatch = new IpMatchBuilder();
1038 if (TCP_SHORT == protocol) {
1039 ipmatch.setIpProtocol(TCP_SHORT);
1040 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1042 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1044 if (0 != destPort) {
1045 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1047 matchBuilder.setLayer4Match(tcpmatch.build());
1048 } else if (UDP_SHORT == protocol) {
1049 ipmatch.setIpProtocol(UDP_SHORT);
1050 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1052 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1054 if (0 != destPort) {
1055 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1057 matchBuilder.setLayer4Match(udpMatch.build());
1059 matchBuilder.setIpMatch(ipmatch.build());
1061 return matchBuilder;
1065 public static class RegMatch {
1066 final Class<? extends NxmNxReg> reg;
1068 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1073 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1074 return new RegMatch(reg, value);
1078 public static void addNxRegMatch(MatchBuilder match,
1079 RegMatch... matches) {
1080 List<ExtensionList> extensions = new ArrayList<>();
1081 for (RegMatch rm : matches) {
1082 Class<? extends ExtensionKey> key;
1083 if (NxmNxReg0.class.equals(rm.reg)) {
1084 key = NxmNxReg0Key.class;
1085 } else if (NxmNxReg1.class.equals(rm.reg)) {
1086 key = NxmNxReg1Key.class;
1087 } else if (NxmNxReg2.class.equals(rm.reg)) {
1088 key = NxmNxReg2Key.class;
1089 } else if (NxmNxReg3.class.equals(rm.reg)) {
1090 key = NxmNxReg3Key.class;
1091 } else if (NxmNxReg4.class.equals(rm.reg)) {
1092 key = NxmNxReg4Key.class;
1093 } else if (NxmNxReg5.class.equals(rm.reg)) {
1094 key = NxmNxReg5Key.class;
1095 } else if (NxmNxReg6.class.equals(rm.reg)) {
1096 key = NxmNxReg6Key.class;
1098 key = NxmNxReg7Key.class;
1100 NxAugMatchNodesNodeTableFlow am =
1101 new NxAugMatchNodesNodeTableFlowBuilder()
1102 .setNxmNxReg(new NxmNxRegBuilder()
1107 extensions.add(new ExtensionListBuilder()
1108 .setExtensionKey(key)
1109 .setExtension(new ExtensionBuilder()
1110 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1114 GeneralAugMatchNodesNodeTableFlow m =
1115 new GeneralAugMatchNodesNodeTableFlowBuilder()
1116 .setExtensionList(extensions)
1118 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1121 public static void addNxTunIdMatch(MatchBuilder match,
1123 NxAugMatchNodesNodeTableFlow am =
1124 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 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1141 public static void addNxNsp(MatchBuilder match, long nsp) {
1142 org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1143 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1144 .setNxmNxNsp(new NxmNxNspBuilder()
1148 addExtension(match, NxmNxNspKey.class, am);
1151 public static void addNxNsi(MatchBuilder match, short nsi) {
1152 org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1153 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1154 .setNxmNxNsi(new NxmNxNsiBuilder()
1158 addExtension(match, NxmNxNsiKey.class, am);
1161 private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
1162 GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1163 List<ExtensionList> extensions = null;
1164 if (existingAugmentations != null ) {
1165 extensions = existingAugmentations.getExtensionList();
1167 if (extensions == null) {
1168 extensions = Lists.newArrayList();
1171 extensions.add(new ExtensionListBuilder()
1172 .setExtensionKey(extensionKey)
1173 .setExtension(new ExtensionBuilder()
1174 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
1178 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1179 .setExtensionList(extensions)
1181 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1184 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1187 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1188 if (srcMac != null) {
1189 emb.setEthernetSource(new EthernetSourceBuilder()
1193 if (dstMac != null) {
1194 emb.setEthernetDestination(new EthernetDestinationBuilder()
1198 if (etherType != null) {
1199 emb.setEthernetType(new EthernetTypeBuilder()
1200 .setType(new EtherType(etherType))
1207 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1209 * @param ipv4AddressString the ip address, in string format
1210 * @return Ipv4Prefix with ipv4Address and /32 mask
1212 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1213 return new Ipv4Prefix(ipv4AddressString + "/32");
1217 * Return Long that represents OF port for strings where OF is explicitly provided
1219 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1220 * @return the OFport or null
1222 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1223 if (ofPortIdentifier != null) {
1224 String[] pair = ofPortIdentifier.split("\\|");
1225 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1226 return new Long(pair[1]);