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 without a match yet
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 EthernetMatchBuilder eth = new EthernetMatchBuilder();
205 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
206 ethTypeBuilder.setType(new EtherType(0x0800L));
207 eth.setEthernetType(ethTypeBuilder.build());
208 matchBuilder.setEthernetMatch(eth.build());
210 // Build the IPv4 Match requied per OVS Syntax
211 IpMatchBuilder ipmatch = new IpMatchBuilder();
212 ipmatch.setIpProtocol((short) 1);
213 matchBuilder.setIpMatch(ipmatch.build());
215 // Build the ICMPv4 Match
216 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
217 icmpv4match.setIcmpv4Type(type);
218 icmpv4match.setIcmpv4Code(code);
219 matchBuilder.setIcmpv4Match(icmpv4match.build());
225 * @param matchBuilder MatchBuilder Object without a match yet
226 * @param dstip String containing an IPv4 prefix
227 * @return matchBuilder Map Object with a match
229 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
231 EthernetMatchBuilder eth = new EthernetMatchBuilder();
232 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
233 ethTypeBuilder.setType(new EtherType(0x0800L));
234 eth.setEthernetType(ethTypeBuilder.build());
235 matchBuilder.setEthernetMatch(eth.build());
237 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
238 ipv4match.setIpv4Destination(dstip);
240 matchBuilder.setLayer3Match(ipv4match.build());
247 * @param matchBuilder MatchBuilder Object without a match yet
248 * @param dstip String containing an IPv4 prefix
249 * @return matchBuilder Map Object with a match
251 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
252 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
253 arpDstMatch.setArpTargetTransportAddress(dstip);
254 matchBuilder.setLayer3Match(arpDstMatch.build());
260 * @param matchBuilder MatchBuilder Object without a match yet
261 * @param srcip String containing an IPv4 prefix
262 * @return matchBuilder Map Object with a match
264 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
266 EthernetMatchBuilder eth = new EthernetMatchBuilder();
267 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
268 ethTypeBuilder.setType(new EtherType(0x0800L));
269 eth.setEthernetType(ethTypeBuilder.build());
270 matchBuilder.setEthernetMatch(eth.build());
272 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
273 ipv4match.setIpv4Source(srcip);
274 matchBuilder.setLayer3Match(ipv4match.build());
281 * Create Source TCP Port Match
283 * @param matchBuilder MatchBuilder Object without a match yet
284 * @param tcpport Integer representing a source TCP port
285 * @return matchBuilder Map MatchBuilder Object with a match
287 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
289 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
290 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
291 ethTypeBuilder.setType(new EtherType(0x0800L));
292 ethType.setEthernetType(ethTypeBuilder.build());
293 matchBuilder.setEthernetMatch(ethType.build());
295 IpMatchBuilder ipmatch = new IpMatchBuilder();
296 ipmatch.setIpProtocol((short) 6);
297 matchBuilder.setIpMatch(ipmatch.build());
299 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
300 tcpmatch.setTcpSourcePort(tcpport);
301 matchBuilder.setLayer4Match(tcpmatch.build());
308 * Create Destination TCP Port Match
310 * @param matchBuilder MatchBuilder Object without a match yet
311 * @param tcpDstPort Integer representing a destination TCP port
312 * @return matchBuilder Map MatchBuilder Object with a match
314 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
316 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
317 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
318 ethTypeBuilder.setType(new EtherType(0x0800L));
319 ethType.setEthernetType(ethTypeBuilder.build());
320 matchBuilder.setEthernetMatch(ethType.build());
322 IpMatchBuilder ipmatch = new IpMatchBuilder();
323 ipmatch.setIpProtocol((short) 6);
324 matchBuilder.setIpMatch(ipmatch.build());
326 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
327 tcpmatch.setTcpDestinationPort(tcpDstPort);
328 matchBuilder.setLayer4Match(tcpmatch.build());
334 * Test match for TCP_Flags
336 * @param matchBuilder MatchBuilder Object without a match yet
337 * @param tcpPort PortNumber representing a destination TCP port
338 * @param tcpFlag int representing a tcp_flag
339 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
341 * Defined TCP Flag values in OVS v2.1+
342 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
343 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
344 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
346 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
349 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
350 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
351 ethTypeBuilder.setType(new EtherType(0x0800L));
352 ethernetType.setEthernetType(ethTypeBuilder.build());
353 matchBuilder.setEthernetMatch(ethernetType.build());
355 // TCP Protocol Match
356 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
357 ipMatch.setIpProtocol((short) 6);
358 matchBuilder.setIpMatch(ipMatch.build());
361 PortNumber dstPort = new PortNumber(tcpPort);
362 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
363 tcpMatch.setTcpDestinationPort(dstPort);
364 matchBuilder.setLayer4Match(tcpMatch.build());
366 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
367 tcpFlagMatch.setTcpFlag(tcpFlag);
368 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
373 * @return MatchBuilder containing the metadata match values
375 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
377 // metadata matchbuilder
378 MetadataBuilder metadata = new MetadataBuilder();
379 metadata.setMetadata(metaData);
380 // Optional metadata mask
381 if (metaDataMask != null) {
382 metadata.setMetadataMask(metaDataMask);
384 matchBuilder.setMetadata(metadata.build());
390 * Create TCP Port Match
392 * @param matchBuilder MatchBuilder Object without a match yet
393 * @param ipProtocol Integer representing the IP protocol
394 * @return matchBuilder Map MatchBuilder Object with a match
396 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
398 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
399 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
400 ethTypeBuilder.setType(new EtherType(0x0800L));
401 ethType.setEthernetType(ethTypeBuilder.build());
402 matchBuilder.setEthernetMatch(ethType.build());
404 IpMatchBuilder ipMmatch = new IpMatchBuilder();
405 if (ipProtocol == TCP_SHORT) {
406 ipMmatch.setIpProtocol(TCP_SHORT);
408 else if (ipProtocol == UDP_SHORT) {
409 ipMmatch.setIpProtocol(UDP_SHORT);
411 else if (ipProtocol == ICMP_SHORT) {
412 ipMmatch.setIpProtocol(ICMP_SHORT);
414 matchBuilder.setIpMatch(ipMmatch.build());
419 * Create tcp syn with proto match.
421 * @param matchBuilder the match builder
422 * @return matchBuilder match builder
424 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
427 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
428 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
429 ethTypeBuilder.setType(new EtherType(0x0800L));
430 ethernetType.setEthernetType(ethTypeBuilder.build());
431 matchBuilder.setEthernetMatch(ethernetType.build());
433 // TCP Protocol Match
434 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
435 ipMatch.setIpProtocol((short) 6);
436 matchBuilder.setIpMatch(ipMatch.build());
438 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
439 tcpFlagMatch.setTcpFlag(TCP_SYN);
440 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
445 * Create tcp proto syn match.
447 * @param matchBuilder the match builder
448 * @return matchBuilder match builder
450 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
452 // TCP Protocol Match
453 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
454 ipMatch.setIpProtocol((short) 6);
455 matchBuilder.setIpMatch(ipMatch.build());
457 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
458 tcpFlagMatch.setTcpFlag(TCP_SYN);
459 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
464 * Create dmac tcp port with flag match.
466 * @param matchBuilder the match builder
467 * @param attachedMac the attached mac
468 * @param tcpFlag the tcp flag
469 * @param tunnelID the tunnel iD
470 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
472 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
473 String attachedMac, Integer tcpFlag, String tunnelID) {
474 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
478 * Create dmac ipSa match.
480 * @param matchBuilder the match builder
481 * @param attachedMac the attached mac
482 * @param ipPrefix the src ipPrefix
483 * @param tunnelID the tunnel iD
484 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
486 public static MatchBuilder createDmacIpSaMatch(
487 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
488 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
492 * Create dmac tcp port ipSa with flag match.
494 * @param matchBuilder the match builder
495 * @param attachedMac the attached mac
496 * @param tcpFlag the tcp flag
497 * @param ipPrefix the src ipPrefix
498 * @param tunnelID the tunnel iD
499 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
501 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
502 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
504 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
505 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
506 ethTypeBuilder.setType(new EtherType(0x0800L));
507 ethernetMatch.setEthernetType(ethTypeBuilder.build());
509 if (attachedMac != null) {
510 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
511 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
512 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
513 matchBuilder.setEthernetMatch(ethernetMatch.build());
516 if (tcpFlag != null) {
517 // TCP Protocol Match
518 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
519 ipMatch.setIpProtocol(TCP_SHORT);
520 matchBuilder.setIpMatch(ipMatch.build());
522 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
523 tcpFlagMatch.setTcpFlag(tcpFlag);
524 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
527 if (tunnelID != null) {
528 TunnelBuilder tunnelBuilder = new TunnelBuilder();
529 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
530 matchBuilder.setTunnel(tunnelBuilder.build());
533 if (ipPrefix != null) {
534 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
535 ipv4match.setIpv4Source(ipPrefix);
536 matchBuilder.setLayer3Match(ipv4match.build());
543 * Create dmac tcp syn match.
545 * @param matchBuilder the match builder
546 * @param attachedMac the attached mac
547 * @param tcpPort the tcp port
548 * @param tcpFlag the tcp flag
549 * @param tunnelID the tunnel iD
550 * @return the match builder
552 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
553 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
555 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
556 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
557 ethTypeBuilder.setType(new EtherType(0x0800L));
558 ethernetMatch.setEthernetType(ethTypeBuilder.build());
560 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
561 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
562 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
563 matchBuilder.setEthernetMatch(ethernetMatch.build());
565 // TCP Protocol Match
566 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
567 ipMatch.setIpProtocol((short) 6);
568 matchBuilder.setIpMatch(ipMatch.build());
571 PortNumber dstPort = new PortNumber(tcpPort);
572 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
573 tcpMatch.setTcpDestinationPort(dstPort);
574 matchBuilder.setLayer4Match(tcpMatch.build());
576 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
577 tcpFlagMatch.setTcpFlag(tcpFlag);
578 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
580 TunnelBuilder tunnelBuilder = new TunnelBuilder();
581 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
582 matchBuilder.setTunnel(tunnelBuilder.build());
588 * Create dmac tcp syn dst ip prefix tcp port.
590 * @param matchBuilder the match builder
591 * @param attachedMac the attached mac
592 * @param tcpPort the tcp port
593 * @param tcpFlag the tcp flag
594 * @param segmentationId the segmentation id
595 * @param dstIp the dst ip
596 * @return the match builder
598 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
599 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
602 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
603 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
604 ethTypeBuilder.setType(new EtherType(0x0800L));
605 ethernetMatch.setEthernetType(ethTypeBuilder.build());
607 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
608 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
609 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
611 matchBuilder.setEthernetMatch(ethernetMatch.build());
613 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
614 ipv4match.setIpv4Destination(dstIp);
615 matchBuilder.setLayer3Match(ipv4match.build());
617 // TCP Protocol Match
618 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
619 ipMatch.setIpProtocol(TCP_SHORT);
620 matchBuilder.setIpMatch(ipMatch.build());
623 PortNumber dstPort = new PortNumber(tcpPort);
624 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
625 tcpMatch.setTcpDestinationPort(dstPort);
626 matchBuilder.setLayer4Match(tcpMatch.build());
628 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
629 tcpFlagMatch.setTcpFlag(tcpFlag);
630 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
632 TunnelBuilder tunnelBuilder = new TunnelBuilder();
633 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
634 matchBuilder.setTunnel(tunnelBuilder.build());
640 * Create dmac ip tcp syn match.
642 * @param matchBuilder the match builder
643 * @param dMacAddr the d mac addr
644 * @param mask the mask
645 * @param ipPrefix the ip prefix
646 * @return MatchBuilder containing the metadata match values
648 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
649 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
651 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
652 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
653 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
655 ethDestBuilder.setMask(mask);
657 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
658 matchBuilder.setEthernetMatch(ethernetMatch.build());
660 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
661 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
662 ethTypeBuilder.setType(new EtherType(0x0800L));
663 ethernetType.setEthernetType(ethTypeBuilder.build());
664 matchBuilder.setEthernetMatch(ethernetType.build());
665 if (ipPrefix != null) {
666 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
667 ipv4match.setIpv4Destination(ipPrefix);
668 matchBuilder.setLayer3Match(ipv4match.build());
670 // TCP Protocol Match
671 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
672 ipMatch.setIpProtocol(TCP_SHORT);
673 matchBuilder.setIpMatch(ipMatch.build());
675 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
676 tcpFlagMatch.setTcpFlag(TCP_SYN);
677 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
683 * Create smac tcp syn dst ip prefix tcp port.
685 * @param matchBuilder the match builder
686 * @param attachedMac the attached mac
687 * @param tcpPort the tcp port
688 * @param tcpFlag the tcp flag
689 * @param segmentationId the segmentation id
690 * @param dstIp the dst ip
691 * @return the match builder
693 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
694 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
696 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
697 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
698 ethTypeBuilder.setType(new EtherType(0x0800L));
699 ethernetMatch.setEthernetType(ethTypeBuilder.build());
701 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
702 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
703 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
705 matchBuilder.setEthernetMatch(ethernetMatch.build());
707 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
708 ipv4match.setIpv4Destination(dstIp);
709 matchBuilder.setLayer3Match(ipv4match.build());
711 // TCP Protocol Match
712 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
713 ipMatch.setIpProtocol(TCP_SHORT);
714 matchBuilder.setIpMatch(ipMatch.build());
717 PortNumber dstPort = new PortNumber(tcpPort);
718 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
719 tcpMatch.setTcpDestinationPort(dstPort);
720 matchBuilder.setLayer4Match(tcpMatch.build());
722 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
723 tcpFlagMatch.setTcpFlag(tcpFlag);
724 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
726 TunnelBuilder tunnelBuilder = new TunnelBuilder();
727 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
728 matchBuilder.setTunnel(tunnelBuilder.build());
734 * Create smac tcp port with flag match.
736 * @param matchBuilder the match builder
737 * @param attachedMac the attached mac
738 * @param tcpFlag the tcp flag
739 * @param tunnelID the tunnel iD
740 * @return matchBuilder
742 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
743 Integer tcpFlag, String tunnelID) {
745 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
746 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
747 ethTypeBuilder.setType(new EtherType(0x0800L));
748 ethernetMatch.setEthernetType(ethTypeBuilder.build());
750 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
751 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
752 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
753 matchBuilder.setEthernetMatch(ethernetMatch.build());
755 // TCP Protocol Match
756 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
757 ipMatch.setIpProtocol(TCP_SHORT);
758 matchBuilder.setIpMatch(ipMatch.build());
760 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
761 tcpFlagMatch.setTcpFlag(tcpFlag);
762 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
764 TunnelBuilder tunnelBuilder = new TunnelBuilder();
765 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
766 matchBuilder.setTunnel(tunnelBuilder.build());
772 * Create smac ip tcp syn match.
774 * @param matchBuilder the match builder
775 * @param dMacAddr the d mac addr
776 * @param mask the mask
777 * @param ipPrefix the ip prefix
778 * @return MatchBuilder containing the metadata match values
780 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
781 MacAddress mask, Ipv4Prefix ipPrefix) {
783 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
784 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
785 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
787 ethSrcBuilder.setMask(mask);
789 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
790 matchBuilder.setEthernetMatch(ethernetMatch.build());
792 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
793 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
794 ethTypeBuilder.setType(new EtherType(0x0800L));
795 ethernetType.setEthernetType(ethTypeBuilder.build());
796 matchBuilder.setEthernetMatch(ethernetType.build());
797 if (ipPrefix != null) {
798 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
799 ipv4match.setIpv4Destination(ipPrefix);
800 matchBuilder.setLayer3Match(ipv4match.build());
802 // TCP Protocol Match
803 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
804 ipMatch.setIpProtocol(TCP_SHORT);
805 matchBuilder.setIpMatch(ipMatch.build());
807 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
808 tcpFlagMatch.setTcpFlag(TCP_SYN);
809 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
815 * Create smac tcp syn.
817 * @param matchBuilder the match builder
818 * @param attachedMac the attached mac
819 * @param tcpPort the tcp port
820 * @param tcpFlag the tcp flag
821 * @param tunnelID the tunnel iD
822 * @return the match builder
824 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
825 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
827 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
828 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
829 ethTypeBuilder.setType(new EtherType(0x0800L));
830 ethernetMatch.setEthernetType(ethTypeBuilder.build());
832 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
833 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
834 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
835 matchBuilder.setEthernetMatch(ethernetMatch.build());
837 // TCP Protocol Match
838 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
839 ipMatch.setIpProtocol((short) 6);
840 matchBuilder.setIpMatch(ipMatch.build());
843 PortNumber dstPort = new PortNumber(tcpPort);
844 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
845 tcpMatch.setTcpDestinationPort(dstPort);
846 matchBuilder.setLayer4Match(tcpMatch.build());
849 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
850 tcpFlagMatch.setTcpFlag(tcpFlag);
851 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
853 TunnelBuilder tunnelBuilder = new TunnelBuilder();
854 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
855 matchBuilder.setTunnel(tunnelBuilder.build());
861 * @return MatchBuilder containing the metadata match values
863 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
864 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
866 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
867 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
868 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
870 ethDestinationBuilder.setMask(mask);
872 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
873 matchBuilder.setEthernetMatch(ethernetMatch.build());
875 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
876 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
877 ethTypeBuilder.setType(new EtherType(0x0800L));
878 ethernetType.setEthernetType(ethTypeBuilder.build());
879 matchBuilder.setEthernetMatch(ethernetType.build());
880 if (ipPrefix != null) {
881 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
882 ipv4match.setIpv4Source(ipPrefix);
883 matchBuilder.setLayer3Match(ipv4match.build());
885 // TCP Protocol Match
886 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
887 ipMatch.setIpProtocol(TCP_SHORT);
888 matchBuilder.setIpMatch(ipMatch.build());
890 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
891 tcpFlagMatch.setTcpFlag(TCP_SYN);
892 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
898 * Create a DHCP match with pot provided.
900 * @param matchBuilder the match builder
901 * @param srcPort the source port
902 * @param dstPort the destination port
903 * @return the DHCP match
905 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
906 int srcPort, int dstPort) {
908 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
909 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
910 ethTypeBuilder.setType(new EtherType(0x0800L));
911 ethernetMatch.setEthernetType(ethTypeBuilder.build());
912 matchBuilder.setEthernetMatch(ethernetMatch.build());
914 IpMatchBuilder ipmatch = new IpMatchBuilder();
915 ipmatch.setIpProtocol(UDP_SHORT);
916 matchBuilder.setIpMatch(ipmatch.build());
918 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
919 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
920 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
921 matchBuilder.setLayer4Match(udpmatch.build());
928 * Creates DHCP server packet match with DHCP mac address and port.
930 * @param matchBuilder the matchbuilder
931 * @param dhcpServerMac MAc address of the DHCP server of the subnet
932 * @param srcPort the source port
933 * @param dstPort the destination port
934 * @return the DHCP server match
936 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
939 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
940 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
941 ethTypeBuilder.setType(new EtherType(0x0800L));
942 ethernetMatch.setEthernetType(ethTypeBuilder.build());
943 matchBuilder.setEthernetMatch(ethernetMatch.build());
945 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
946 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
947 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
948 matchBuilder.setEthernetMatch(ethernetMatch.build());
950 IpMatchBuilder ipmatch = new IpMatchBuilder();
951 ipmatch.setIpProtocol(UDP_SHORT);
952 matchBuilder.setIpMatch(ipmatch.build());
954 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
955 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
956 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
957 matchBuilder.setLayer4Match(udpmatch.build());
964 * Creates a Match with src ip address mac address set.
965 * @param matchBuilder MatchBuilder Object
966 * @param srcip String containing an IPv4 prefix
967 * @param srcMac The source macAddress
968 * @return matchBuilder Map Object with a match
970 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
972 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
973 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
974 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
975 ethTypeBuilder.setType(new EtherType(0x0800L));
976 EthernetMatchBuilder eth = new EthernetMatchBuilder();
977 eth.setEthernetType(ethTypeBuilder.build());
978 eth.setEthernetSource(new EthernetSourceBuilder()
982 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
983 matchBuilder.setEthernetMatch(eth.build());
989 * Creates a ether net match with ether type set to 0x0800L.
990 * @param matchBuilder MatchBuilder Object
991 * @param srcMac The source macAddress
992 * @param dstMac The destination mac address
993 * @return matchBuilder Map Object with a match
995 public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
997 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
998 ethTypeBuilder.setType(new EtherType(0x0800L));
999 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1000 eth.setEthernetType(ethTypeBuilder.build());
1001 if (null != srcMac) {
1002 eth.setEthernetSource(new EthernetSourceBuilder()
1003 .setAddress(new MacAddress(srcMac)).build());
1005 if (null != dstMac) {
1006 eth.setEthernetDestination(new EthernetDestinationBuilder()
1007 .setAddress(new MacAddress(dstMac)).build());
1009 matchBuilder.setEthernetMatch(eth.build());
1010 return matchBuilder;
1013 * Adds remote Ip prefix to existing match.
1014 * @param matchBuilder The match builder
1015 * @param sourceIpPrefix The source IP prefix
1016 * @param destIpPrefix The destination IP prefix
1017 * @return matchBuilder Map Object with a match
1019 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1020 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1021 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1022 if (null != sourceIpPrefix) {
1023 ipv4match.setIpv4Source(sourceIpPrefix);
1025 if (null != destIpPrefix) {
1026 ipv4match.setIpv4Destination(destIpPrefix);
1028 matchBuilder.setLayer3Match(ipv4match.build());
1030 return matchBuilder;
1033 * Add a layer4 match to an existing match
1035 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
1036 * @param protocol The layer4 protocol
1037 * @param srcPort The src port
1038 * @param destPort The destination port
1039 * @return matchBuilder Map Object with a match
1041 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1042 int protocol, int srcPort, int destPort) {
1043 IpMatchBuilder ipmatch = new IpMatchBuilder();
1044 if (TCP_SHORT == protocol) {
1045 ipmatch.setIpProtocol(TCP_SHORT);
1046 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1048 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1050 if (0 != destPort) {
1051 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1053 matchBuilder.setLayer4Match(tcpmatch.build());
1054 } else if (UDP_SHORT == protocol) {
1055 ipmatch.setIpProtocol(UDP_SHORT);
1056 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1058 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1060 if (0 != destPort) {
1061 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1063 matchBuilder.setLayer4Match(udpMatch.build());
1065 matchBuilder.setIpMatch(ipmatch.build());
1067 return matchBuilder;
1071 public static class RegMatch {
1072 final Class<? extends NxmNxReg> reg;
1074 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1079 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1080 return new RegMatch(reg, value);
1084 public static void addNxRegMatch(MatchBuilder match,
1085 RegMatch... matches) {
1086 List<ExtensionList> extensions = new ArrayList<>();
1087 for (RegMatch rm : matches) {
1088 Class<? extends ExtensionKey> key;
1089 if (NxmNxReg0.class.equals(rm.reg)) {
1090 key = NxmNxReg0Key.class;
1091 } else if (NxmNxReg1.class.equals(rm.reg)) {
1092 key = NxmNxReg1Key.class;
1093 } else if (NxmNxReg2.class.equals(rm.reg)) {
1094 key = NxmNxReg2Key.class;
1095 } else if (NxmNxReg3.class.equals(rm.reg)) {
1096 key = NxmNxReg3Key.class;
1097 } else if (NxmNxReg4.class.equals(rm.reg)) {
1098 key = NxmNxReg4Key.class;
1099 } else if (NxmNxReg5.class.equals(rm.reg)) {
1100 key = NxmNxReg5Key.class;
1101 } else if (NxmNxReg6.class.equals(rm.reg)) {
1102 key = NxmNxReg6Key.class;
1104 key = NxmNxReg7Key.class;
1106 NxAugMatchNodesNodeTableFlow am =
1107 new NxAugMatchNodesNodeTableFlowBuilder()
1108 .setNxmNxReg(new NxmNxRegBuilder()
1113 extensions.add(new ExtensionListBuilder()
1114 .setExtensionKey(key)
1115 .setExtension(new ExtensionBuilder()
1116 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1120 GeneralAugMatchNodesNodeTableFlow m =
1121 new GeneralAugMatchNodesNodeTableFlowBuilder()
1122 .setExtensionList(extensions)
1124 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1127 public static void addNxTunIdMatch(MatchBuilder match,
1129 NxAugMatchNodesNodeTableFlow am =
1130 new NxAugMatchNodesNodeTableFlowBuilder()
1131 .setNxmNxTunId(new NxmNxTunIdBuilder()
1132 .setValue(BigInteger.valueOf(tunId))
1135 GeneralAugMatchNodesNodeTableFlow m =
1136 new GeneralAugMatchNodesNodeTableFlowBuilder()
1137 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1138 .setExtensionKey(NxmNxTunIdKey.class)
1139 .setExtension(new ExtensionBuilder()
1140 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1144 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1147 public static void addNxNsp(MatchBuilder match, long nsp) {
1148 org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1149 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1150 .setNxmNxNsp(new NxmNxNspBuilder()
1154 addExtension(match, NxmNxNspKey.class, am);
1157 public static void addNxNsi(MatchBuilder match, short nsi) {
1158 org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am =
1159 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
1160 .setNxmNxNsi(new NxmNxNsiBuilder()
1164 addExtension(match, NxmNxNsiKey.class, am);
1167 private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
1168 GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1169 List<ExtensionList> extensions = null;
1170 if (existingAugmentations != null ) {
1171 extensions = existingAugmentations.getExtensionList();
1173 if (extensions == null) {
1174 extensions = Lists.newArrayList();
1177 extensions.add(new ExtensionListBuilder()
1178 .setExtensionKey(extensionKey)
1179 .setExtension(new ExtensionBuilder()
1180 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
1184 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1185 .setExtensionList(extensions)
1187 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1190 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1193 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1194 if (srcMac != null) {
1195 emb.setEthernetSource(new EthernetSourceBuilder()
1199 if (dstMac != null) {
1200 emb.setEthernetDestination(new EthernetDestinationBuilder()
1204 if (etherType != null) {
1205 emb.setEthernetType(new EthernetTypeBuilder()
1206 .setType(new EtherType(etherType))
1213 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1215 * @param ipv4AddressString the ip address, in string format
1216 * @return Ipv4Prefix with ipv4Address and /32 mask
1218 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1219 return new Ipv4Prefix(ipv4AddressString + "/32");
1223 * Return Long that represents OF port for strings where OF is explicitly provided
1225 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1226 * @return the OFport or null
1228 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1229 if (ofPortIdentifier != null) {
1230 String[] pair = ofPortIdentifier.split("\\|");
1231 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1232 return new Long(pair[1]);