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.HashMap;
14 import java.util.List;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpDstKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpSrcKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.src.grouping.NxmOfTcpSrcBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.dst.grouping.NxmOfUdpDstBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.src.grouping.NxmOfUdpSrcBuilder;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
82 import com.google.common.collect.ImmutableList;
83 import com.google.common.collect.Lists;
85 public class MatchUtils {
86 private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
87 public static final short ICMP_SHORT = 1;
88 public static final short TCP_SHORT = 6;
89 public static final short UDP_SHORT = 17;
90 public static final String TCP = "tcp";
91 public static final String UDP = "udp";
92 private static final int TCP_SYN = 0x0002;
93 public static final String ICMP = "icmp";
94 public static final short ALL_ICMP = -1;
97 * Create Ingress Port Match dpidLong, inPort
99 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
100 * @param dpidLong Long the datapath ID of a switch/node
101 * @param inPort Long ingress port on a switch
102 * @return matchBuilder Map MatchBuilder Object with a match
104 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
106 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
107 LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
108 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
109 matchBuilder.setInPort(ncid);
114 public static MatchBuilder createInPortReservedMatch(MatchBuilder matchBuilder, Long dpidLong, String inPort) {
116 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
117 LOG.debug("createInPortResrevedMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
119 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
120 matchBuilder.setInPort(ncid);
126 * Create EtherType Match
128 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
129 * @param etherType Long EtherType
130 * @return matchBuilder Map MatchBuilder Object with a match
132 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
134 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
135 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
136 ethTypeBuilder.setType(new EtherType(etherType));
137 ethernetMatch.setEthernetType(ethTypeBuilder.build());
138 matchBuilder.setEthernetMatch(ethernetMatch.build());
144 * Create Ethernet Source Match
146 * @param matchBuilder MatchBuilder Object without a match yet
147 * @param sMacAddr String representing a source MAC
148 * @return matchBuilder Map MatchBuilder Object with a match
150 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
152 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
153 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
154 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
155 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
156 matchBuilder.setEthernetMatch(ethernetMatch.build());
162 * Create Ethernet Destination Match
164 * @param matchBuilder MatchBuilder Object without a match yet
165 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
166 * @return matchBuilder Map MatchBuilder Object with a match
168 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
169 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
170 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
171 vlanIdBuilder.setVlanId(new VlanId(vlanId));
172 vlanIdBuilder.setVlanIdPresent(present);
173 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
174 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
180 * Create Ethernet Destination Match
182 * @param matchBuilder MatchBuilder Object without a match yet
183 * @param dMacAddr String representing a destination MAC
184 * @return matchBuilder Map MatchBuilder Object with a match
186 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
188 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
189 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
190 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
192 ethDestinationBuilder.setMask(mask);
194 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
195 matchBuilder.setEthernetMatch(ethernetMatch.build());
201 * Tunnel ID Match Builder
203 * @param matchBuilder MatchBuilder Object without a match yet
204 * @param tunnelId BigInteger representing a tunnel ID
205 * @return matchBuilder Map MatchBuilder Object with a match
207 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
209 TunnelBuilder tunnelBuilder = new TunnelBuilder();
210 tunnelBuilder.setTunnelId(tunnelId);
211 matchBuilder.setTunnel(tunnelBuilder.build());
217 * Match ICMP code and type
219 * @param matchBuilder MatchBuilder Object
220 * @param type short representing an ICMP type
221 * @param code short representing an ICMP code
222 * @return matchBuilder Map MatchBuilder Object with a match
224 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
226 // Build the IPv4 Match requied per OVS Syntax
227 IpMatchBuilder ipmatch = new IpMatchBuilder();
228 ipmatch.setIpProtocol((short) 1);
229 matchBuilder.setIpMatch(ipmatch.build());
231 // Build the ICMPv4 Match
232 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
233 if (type != ALL_ICMP || code != ALL_ICMP) {
234 icmpv4match.setIcmpv4Type(type);
235 icmpv4match.setIcmpv4Code(code);
237 matchBuilder.setIcmpv4Match(icmpv4match.build());
243 * @param matchBuilder MatchBuilder Object without a match yet
244 * @param dstip String containing an IPv4 prefix
245 * @return matchBuilder Map Object with a match
247 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
249 EthernetMatchBuilder eth = new EthernetMatchBuilder();
250 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
251 ethTypeBuilder.setType(new EtherType(0x0800L));
252 eth.setEthernetType(ethTypeBuilder.build());
253 matchBuilder.setEthernetMatch(eth.build());
255 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
256 ipv4match.setIpv4Destination(dstip);
258 matchBuilder.setLayer3Match(ipv4match.build());
265 * @param matchBuilder MatchBuilder Object without a match yet
266 * @param dstip String containing an IPv4 prefix
267 * @return matchBuilder Map Object with a match
269 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
270 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
271 arpDstMatch.setArpTargetTransportAddress(dstip)
272 .setArpOp(FlowUtils.ARP_OP_REQUEST);
273 matchBuilder.setLayer3Match(arpDstMatch.build());
279 * @param matchBuilder MatchBuilder Object without a match yet
280 * @param srcip String containing an IPv4 prefix
281 * @return matchBuilder Map Object with a match
283 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
285 EthernetMatchBuilder eth = new EthernetMatchBuilder();
286 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
287 ethTypeBuilder.setType(new EtherType(0x0800L));
288 eth.setEthernetType(ethTypeBuilder.build());
289 matchBuilder.setEthernetMatch(eth.build());
291 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
292 ipv4match.setIpv4Source(srcip);
293 matchBuilder.setLayer3Match(ipv4match.build());
300 * Create Source TCP Port Match
302 * @param matchBuilder MatchBuilder Object without a match yet
303 * @param tcpport Integer representing a source TCP port
304 * @return matchBuilder Map MatchBuilder Object with a match
306 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
308 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
309 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
310 ethTypeBuilder.setType(new EtherType(0x0800L));
311 ethType.setEthernetType(ethTypeBuilder.build());
312 matchBuilder.setEthernetMatch(ethType.build());
314 IpMatchBuilder ipmatch = new IpMatchBuilder();
315 ipmatch.setIpProtocol((short) 6);
316 matchBuilder.setIpMatch(ipmatch.build());
318 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
319 tcpmatch.setTcpSourcePort(tcpport);
320 matchBuilder.setLayer4Match(tcpmatch.build());
327 * Create Destination TCP Port Match
329 * @param matchBuilder MatchBuilder Object without a match yet
330 * @param tcpDstPort Integer representing a destination TCP port
331 * @return matchBuilder Map MatchBuilder Object with a match
333 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
335 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
336 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
337 ethTypeBuilder.setType(new EtherType(0x0800L));
338 ethType.setEthernetType(ethTypeBuilder.build());
339 matchBuilder.setEthernetMatch(ethType.build());
341 IpMatchBuilder ipmatch = new IpMatchBuilder();
342 ipmatch.setIpProtocol((short) 6);
343 matchBuilder.setIpMatch(ipmatch.build());
345 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
346 tcpmatch.setTcpDestinationPort(tcpDstPort);
347 matchBuilder.setLayer4Match(tcpmatch.build());
353 * Test match for TCP_Flags
355 * @param matchBuilder MatchBuilder Object without a match yet
356 * @param tcpPort PortNumber representing a destination TCP port
357 * @param tcpFlag int representing a tcp_flag
358 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
360 * Defined TCP Flag values in OVS v2.1+
361 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
362 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
363 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
365 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
368 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
369 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
370 ethTypeBuilder.setType(new EtherType(0x0800L));
371 ethernetType.setEthernetType(ethTypeBuilder.build());
372 matchBuilder.setEthernetMatch(ethernetType.build());
374 // TCP Protocol Match
375 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
376 ipMatch.setIpProtocol((short) 6);
377 matchBuilder.setIpMatch(ipMatch.build());
380 PortNumber dstPort = new PortNumber(tcpPort);
381 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
382 tcpMatch.setTcpDestinationPort(dstPort);
383 matchBuilder.setLayer4Match(tcpMatch.build());
385 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
386 tcpFlagMatch.setTcpFlag(tcpFlag);
387 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
392 * @return MatchBuilder containing the metadata match values
394 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
396 // metadata matchbuilder
397 MetadataBuilder metadata = new MetadataBuilder();
398 metadata.setMetadata(metaData);
399 // Optional metadata mask
400 if (metaDataMask != null) {
401 metadata.setMetadataMask(metaDataMask);
403 matchBuilder.setMetadata(metadata.build());
409 * Create TCP Port Match
411 * @param matchBuilder MatchBuilder Object without a match yet
412 * @param ipProtocol Integer representing the IP protocol
413 * @return matchBuilder Map MatchBuilder Object with a match
415 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
417 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
418 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
419 ethTypeBuilder.setType(new EtherType(0x0800L));
420 ethType.setEthernetType(ethTypeBuilder.build());
421 matchBuilder.setEthernetMatch(ethType.build());
423 IpMatchBuilder ipMmatch = new IpMatchBuilder();
424 if (ipProtocol == TCP_SHORT) {
425 ipMmatch.setIpProtocol(TCP_SHORT);
427 else if (ipProtocol == UDP_SHORT) {
428 ipMmatch.setIpProtocol(UDP_SHORT);
430 else if (ipProtocol == ICMP_SHORT) {
431 ipMmatch.setIpProtocol(ICMP_SHORT);
433 matchBuilder.setIpMatch(ipMmatch.build());
438 * Create tcp syn with proto match.
440 * @param matchBuilder the match builder
441 * @return matchBuilder match builder
443 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
446 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
447 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
448 ethTypeBuilder.setType(new EtherType(0x0800L));
449 ethernetType.setEthernetType(ethTypeBuilder.build());
450 matchBuilder.setEthernetMatch(ethernetType.build());
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 tcp proto syn match.
466 * @param matchBuilder the match builder
467 * @return matchBuilder match builder
469 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
471 // TCP Protocol Match
472 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
473 ipMatch.setIpProtocol((short) 6);
474 matchBuilder.setIpMatch(ipMatch.build());
476 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
477 tcpFlagMatch.setTcpFlag(TCP_SYN);
478 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
483 * Create dmac tcp port with flag match.
485 * @param matchBuilder the match builder
486 * @param attachedMac the attached mac
487 * @param tcpFlag the tcp flag
488 * @param tunnelID the tunnel iD
489 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
491 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
492 String attachedMac, Integer tcpFlag, String tunnelID) {
493 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
497 * Create dmac ipSa match.
499 * @param matchBuilder the match builder
500 * @param attachedMac the attached mac
501 * @param ipPrefix the src ipPrefix
502 * @param tunnelID the tunnel iD
503 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
505 public static MatchBuilder createDmacIpSaMatch(
506 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
507 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
511 * Create dmac tcp port ipSa with flag match.
513 * @param matchBuilder the match builder
514 * @param attachedMac the attached mac
515 * @param tcpFlag the tcp flag
516 * @param ipPrefix the src ipPrefix
517 * @param tunnelID the tunnel iD
518 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
520 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
521 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
523 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
524 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
525 ethTypeBuilder.setType(new EtherType(0x0800L));
526 ethernetMatch.setEthernetType(ethTypeBuilder.build());
528 if (attachedMac != null) {
529 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
530 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
531 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
532 matchBuilder.setEthernetMatch(ethernetMatch.build());
535 if (tcpFlag != null) {
536 // TCP Protocol Match
537 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
538 ipMatch.setIpProtocol(TCP_SHORT);
539 matchBuilder.setIpMatch(ipMatch.build());
541 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
542 tcpFlagMatch.setTcpFlag(tcpFlag);
543 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
546 if (tunnelID != null) {
547 TunnelBuilder tunnelBuilder = new TunnelBuilder();
548 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
549 matchBuilder.setTunnel(tunnelBuilder.build());
552 if (ipPrefix != null) {
553 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
554 ipv4match.setIpv4Source(ipPrefix);
555 matchBuilder.setLayer3Match(ipv4match.build());
562 * Create dmac tcp syn match.
564 * @param matchBuilder the match builder
565 * @param attachedMac the attached mac
566 * @param tcpPort the tcp port
567 * @param tcpFlag the tcp flag
568 * @param tunnelID the tunnel iD
569 * @return the match builder
571 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
572 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
574 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
575 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
576 ethTypeBuilder.setType(new EtherType(0x0800L));
577 ethernetMatch.setEthernetType(ethTypeBuilder.build());
579 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
580 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
581 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
582 matchBuilder.setEthernetMatch(ethernetMatch.build());
584 // TCP Protocol Match
585 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
586 ipMatch.setIpProtocol((short) 6);
587 matchBuilder.setIpMatch(ipMatch.build());
590 PortNumber dstPort = new PortNumber(tcpPort);
591 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
592 tcpMatch.setTcpDestinationPort(dstPort);
593 matchBuilder.setLayer4Match(tcpMatch.build());
595 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
596 tcpFlagMatch.setTcpFlag(tcpFlag);
597 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
599 TunnelBuilder tunnelBuilder = new TunnelBuilder();
600 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
601 matchBuilder.setTunnel(tunnelBuilder.build());
607 * Create dmac tcp syn dst ip prefix tcp port.
609 * @param matchBuilder the match builder
610 * @param attachedMac the attached mac
611 * @param tcpPort the tcp port
612 * @param tcpFlag the tcp flag
613 * @param segmentationId the segmentation id
614 * @param dstIp the dst ip
615 * @return the match builder
617 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
618 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
621 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
622 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
623 ethTypeBuilder.setType(new EtherType(0x0800L));
624 ethernetMatch.setEthernetType(ethTypeBuilder.build());
626 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
627 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
628 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
630 matchBuilder.setEthernetMatch(ethernetMatch.build());
632 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
633 ipv4match.setIpv4Destination(dstIp);
634 matchBuilder.setLayer3Match(ipv4match.build());
636 // TCP Protocol Match
637 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
638 ipMatch.setIpProtocol(TCP_SHORT);
639 matchBuilder.setIpMatch(ipMatch.build());
642 PortNumber dstPort = new PortNumber(tcpPort);
643 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
644 tcpMatch.setTcpDestinationPort(dstPort);
645 matchBuilder.setLayer4Match(tcpMatch.build());
647 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
648 tcpFlagMatch.setTcpFlag(tcpFlag);
649 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
651 TunnelBuilder tunnelBuilder = new TunnelBuilder();
652 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
653 matchBuilder.setTunnel(tunnelBuilder.build());
659 * Create dmac ip tcp syn match.
661 * @param matchBuilder the match builder
662 * @param dMacAddr the d mac addr
663 * @param mask the mask
664 * @param ipPrefix the ip prefix
665 * @return MatchBuilder containing the metadata match values
667 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
668 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
670 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
671 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
672 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
674 ethDestBuilder.setMask(mask);
676 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
677 matchBuilder.setEthernetMatch(ethernetMatch.build());
679 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
680 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
681 ethTypeBuilder.setType(new EtherType(0x0800L));
682 ethernetType.setEthernetType(ethTypeBuilder.build());
683 matchBuilder.setEthernetMatch(ethernetType.build());
684 if (ipPrefix != null) {
685 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
686 ipv4match.setIpv4Destination(ipPrefix);
687 matchBuilder.setLayer3Match(ipv4match.build());
689 // TCP Protocol Match
690 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
691 ipMatch.setIpProtocol(TCP_SHORT);
692 matchBuilder.setIpMatch(ipMatch.build());
694 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
695 tcpFlagMatch.setTcpFlag(TCP_SYN);
696 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
702 * Create smac tcp syn dst ip prefix tcp port.
704 * @param matchBuilder the match builder
705 * @param attachedMac the attached mac
706 * @param tcpPort the tcp port
707 * @param tcpFlag the tcp flag
708 * @param segmentationId the segmentation id
709 * @param dstIp the dst ip
710 * @return the match builder
712 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
713 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
715 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
716 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
717 ethTypeBuilder.setType(new EtherType(0x0800L));
718 ethernetMatch.setEthernetType(ethTypeBuilder.build());
720 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
721 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
722 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
724 matchBuilder.setEthernetMatch(ethernetMatch.build());
726 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
727 ipv4match.setIpv4Destination(dstIp);
728 matchBuilder.setLayer3Match(ipv4match.build());
730 // TCP Protocol Match
731 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
732 ipMatch.setIpProtocol(TCP_SHORT);
733 matchBuilder.setIpMatch(ipMatch.build());
736 PortNumber dstPort = new PortNumber(tcpPort);
737 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
738 tcpMatch.setTcpDestinationPort(dstPort);
739 matchBuilder.setLayer4Match(tcpMatch.build());
741 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
742 tcpFlagMatch.setTcpFlag(tcpFlag);
743 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
745 TunnelBuilder tunnelBuilder = new TunnelBuilder();
746 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
747 matchBuilder.setTunnel(tunnelBuilder.build());
753 * Create smac tcp port with flag match.
755 * @param matchBuilder the match builder
756 * @param attachedMac the attached mac
757 * @param tcpFlag the tcp flag
758 * @param tunnelID the tunnel iD
759 * @return matchBuilder
761 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
762 Integer tcpFlag, String tunnelID) {
764 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
765 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
766 ethTypeBuilder.setType(new EtherType(0x0800L));
767 ethernetMatch.setEthernetType(ethTypeBuilder.build());
769 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
770 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
771 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
772 matchBuilder.setEthernetMatch(ethernetMatch.build());
774 // TCP Protocol Match
775 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
776 ipMatch.setIpProtocol(TCP_SHORT);
777 matchBuilder.setIpMatch(ipMatch.build());
779 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
780 tcpFlagMatch.setTcpFlag(tcpFlag);
781 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
783 TunnelBuilder tunnelBuilder = new TunnelBuilder();
784 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
785 matchBuilder.setTunnel(tunnelBuilder.build());
791 * Create smac ip tcp syn match.
793 * @param matchBuilder the match builder
794 * @param dMacAddr the d mac addr
795 * @param mask the mask
796 * @param ipPrefix the ip prefix
797 * @return MatchBuilder containing the metadata match values
799 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
800 MacAddress mask, Ipv4Prefix ipPrefix) {
802 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
803 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
804 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
806 ethSrcBuilder.setMask(mask);
808 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
809 matchBuilder.setEthernetMatch(ethernetMatch.build());
811 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
812 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
813 ethTypeBuilder.setType(new EtherType(0x0800L));
814 ethernetType.setEthernetType(ethTypeBuilder.build());
815 matchBuilder.setEthernetMatch(ethernetType.build());
816 if (ipPrefix != null) {
817 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
818 ipv4match.setIpv4Destination(ipPrefix);
819 matchBuilder.setLayer3Match(ipv4match.build());
821 // TCP Protocol Match
822 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
823 ipMatch.setIpProtocol(TCP_SHORT);
824 matchBuilder.setIpMatch(ipMatch.build());
826 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
827 tcpFlagMatch.setTcpFlag(TCP_SYN);
828 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
834 * Create smac tcp syn.
836 * @param matchBuilder the match builder
837 * @param attachedMac the attached mac
838 * @param tcpPort the tcp port
839 * @param tcpFlag the tcp flag
840 * @param tunnelID the tunnel iD
841 * @return the match builder
843 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
844 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
846 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
847 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
848 ethTypeBuilder.setType(new EtherType(0x0800L));
849 ethernetMatch.setEthernetType(ethTypeBuilder.build());
851 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
852 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
853 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
854 matchBuilder.setEthernetMatch(ethernetMatch.build());
856 // TCP Protocol Match
857 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
858 ipMatch.setIpProtocol((short) 6);
859 matchBuilder.setIpMatch(ipMatch.build());
862 PortNumber dstPort = new PortNumber(tcpPort);
863 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
864 tcpMatch.setTcpDestinationPort(dstPort);
865 matchBuilder.setLayer4Match(tcpMatch.build());
868 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
869 tcpFlagMatch.setTcpFlag(tcpFlag);
870 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
872 TunnelBuilder tunnelBuilder = new TunnelBuilder();
873 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
874 matchBuilder.setTunnel(tunnelBuilder.build());
880 * @return MatchBuilder containing the metadata match values
882 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
883 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
885 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
886 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
887 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
889 ethDestinationBuilder.setMask(mask);
891 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
892 matchBuilder.setEthernetMatch(ethernetMatch.build());
894 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
895 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
896 ethTypeBuilder.setType(new EtherType(0x0800L));
897 ethernetType.setEthernetType(ethTypeBuilder.build());
898 matchBuilder.setEthernetMatch(ethernetType.build());
899 if (ipPrefix != null) {
900 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
901 ipv4match.setIpv4Source(ipPrefix);
902 matchBuilder.setLayer3Match(ipv4match.build());
904 // TCP Protocol Match
905 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
906 ipMatch.setIpProtocol(TCP_SHORT);
907 matchBuilder.setIpMatch(ipMatch.build());
909 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
910 tcpFlagMatch.setTcpFlag(TCP_SYN);
911 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
917 * Create a DHCP match with pot provided.
919 * @param matchBuilder the match builder
920 * @param srcPort the source port
921 * @param dstPort the destination port
922 * @return the DHCP match
924 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
925 int srcPort, int dstPort) {
927 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
928 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
929 ethTypeBuilder.setType(new EtherType(0x0800L));
930 ethernetMatch.setEthernetType(ethTypeBuilder.build());
931 matchBuilder.setEthernetMatch(ethernetMatch.build());
933 IpMatchBuilder ipmatch = new IpMatchBuilder();
934 ipmatch.setIpProtocol(UDP_SHORT);
935 matchBuilder.setIpMatch(ipmatch.build());
937 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
938 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
939 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
940 matchBuilder.setLayer4Match(udpmatch.build());
947 * Creates DHCP server packet match with DHCP mac address and port.
949 * @param matchBuilder the matchbuilder
950 * @param dhcpServerMac MAc address of the DHCP server of the subnet
951 * @param srcPort the source port
952 * @param dstPort the destination port
953 * @return the DHCP server match
955 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
958 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
959 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
960 ethTypeBuilder.setType(new EtherType(0x0800L));
961 ethernetMatch.setEthernetType(ethTypeBuilder.build());
962 matchBuilder.setEthernetMatch(ethernetMatch.build());
964 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
965 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
966 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
967 matchBuilder.setEthernetMatch(ethernetMatch.build());
969 IpMatchBuilder ipmatch = new IpMatchBuilder();
970 ipmatch.setIpProtocol(UDP_SHORT);
971 matchBuilder.setIpMatch(ipmatch.build());
973 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
974 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
975 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
976 matchBuilder.setLayer4Match(udpmatch.build());
983 * Creates a Match with src ip address mac address set.
984 * @param matchBuilder MatchBuilder Object
985 * @param srcip String containing an IPv4 prefix
986 * @param srcMac The source macAddress
987 * @return matchBuilder Map Object with a match
989 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
991 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
992 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
993 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
994 ethTypeBuilder.setType(new EtherType(0x0800L));
995 EthernetMatchBuilder eth = new EthernetMatchBuilder();
996 eth.setEthernetType(ethTypeBuilder.build());
997 eth.setEthernetSource(new EthernetSourceBuilder()
1001 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1002 matchBuilder.setEthernetMatch(eth.build());
1003 return matchBuilder;
1008 * Creates a ether net match with ether type set to 0x0800L.
1009 * @param matchBuilder MatchBuilder Object
1010 * @param srcMac The source macAddress
1011 * @param dstMac The destination mac address
1012 * @return matchBuilder Map Object with a match
1014 public static MatchBuilder createEtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1016 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1017 ethTypeBuilder.setType(new EtherType(0x0800L));
1018 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1019 eth.setEthernetType(ethTypeBuilder.build());
1020 if (null != srcMac) {
1021 eth.setEthernetSource(new EthernetSourceBuilder()
1022 .setAddress(new MacAddress(srcMac)).build());
1024 if (null != dstMac) {
1025 eth.setEthernetDestination(new EthernetDestinationBuilder()
1026 .setAddress(new MacAddress(dstMac)).build());
1028 matchBuilder.setEthernetMatch(eth.build());
1029 return matchBuilder;
1032 * Adds remote Ip prefix to existing match.
1033 * @param matchBuilder The match builder
1034 * @param sourceIpPrefix The source IP prefix
1035 * @param destIpPrefix The destination IP prefix
1036 * @return matchBuilder Map Object with a match
1038 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1039 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1040 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1041 if (null != sourceIpPrefix) {
1042 ipv4match.setIpv4Source(sourceIpPrefix);
1044 if (null != destIpPrefix) {
1045 ipv4match.setIpv4Destination(destIpPrefix);
1047 matchBuilder.setLayer3Match(ipv4match.build());
1049 return matchBuilder;
1052 * Add a layer4 match to an existing match
1054 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1055 * @param protocol The layer4 protocol
1056 * @param srcPort The src port
1057 * @param destPort The destination port
1058 * @return matchBuilder Map Object with a match
1060 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1061 int protocol, int srcPort, int destPort) {
1062 IpMatchBuilder ipmatch = new IpMatchBuilder();
1063 if (TCP_SHORT == protocol) {
1064 ipmatch.setIpProtocol(TCP_SHORT);
1065 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1067 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1069 if (0 != destPort) {
1070 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1072 matchBuilder.setLayer4Match(tcpmatch.build());
1073 } else if (UDP_SHORT == protocol) {
1074 ipmatch.setIpProtocol(UDP_SHORT);
1075 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1077 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1079 if (0 != destPort) {
1080 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1082 matchBuilder.setLayer4Match(udpMatch.build());
1084 matchBuilder.setIpMatch(ipmatch.build());
1086 return matchBuilder;
1090 * Add a layer4 match to an existing match with mask
1092 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match.
1093 * @param protocol The layer4 protocol
1094 * @param srcPort The src port
1095 * @param destPort The destination port
1096 * @param mask the mask for the port
1097 * @return matchBuilder Map Object with a match
1099 public static MatchBuilder addLayer4MatchWithMask(MatchBuilder matchBuilder,
1100 int protocol, int srcPort, int destPort,int mask) {
1102 IpMatchBuilder ipmatch = new IpMatchBuilder();
1104 NxAugMatchNodesNodeTableFlow nxAugMatch = null;
1105 GeneralAugMatchNodesNodeTableFlow genAugMatch = null;
1106 if (protocol == TCP_SHORT) {
1107 ipmatch.setIpProtocol(TCP_SHORT);
1109 NxmOfTcpSrcBuilder tcpSrc = new NxmOfTcpSrcBuilder();
1110 tcpSrc.setPort(new PortNumber(srcPort));
1111 tcpSrc.setMask(mask);
1112 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpSrc(tcpSrc.build()).build();
1113 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1114 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpSrcKey.class)
1115 .setExtension(new ExtensionBuilder()
1116 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1117 .build()).build())).build();
1118 } else if (0 != destPort) {
1119 NxmOfTcpDstBuilder tcpDst = new NxmOfTcpDstBuilder();
1120 tcpDst.setPort(new PortNumber(destPort));
1121 tcpDst.setMask(mask);
1122 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1123 .setNxmOfTcpDst(tcpDst.build())
1125 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1126 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpDstKey.class)
1127 .setExtension(new ExtensionBuilder()
1128 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1129 .build()).build())).build();
1132 } else if (UDP_SHORT == protocol) {
1133 ipmatch.setIpProtocol(UDP_SHORT);
1134 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1136 NxmOfUdpSrcBuilder udpSrc = new NxmOfUdpSrcBuilder();
1137 udpSrc.setPort(new PortNumber(srcPort));
1138 udpSrc.setMask(mask);
1139 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpSrc(udpSrc.build()).build();
1140 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1141 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpSrcKey.class)
1142 .setExtension(new ExtensionBuilder()
1143 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1144 .build()).build())).build();
1145 } else if (0 != destPort) {
1146 NxmOfUdpDstBuilder udpDst = new NxmOfUdpDstBuilder();
1147 udpDst.setPort(new PortNumber(destPort));
1148 udpDst.setMask(mask);
1149 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1150 .setNxmOfUdpDst(udpDst.build())
1152 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1153 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpDstKey.class)
1154 .setExtension(new ExtensionBuilder()
1155 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1156 .build()).build())).build();
1159 matchBuilder.setIpMatch(ipmatch.build());
1160 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1161 return matchBuilder;
1164 public static class RegMatch {
1165 final Class<? extends NxmNxReg> reg;
1167 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1172 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1173 return new RegMatch(reg, value);
1177 public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1178 List<ExtensionList> extensions = new ArrayList<>();
1179 for (RegMatch rm : matches) {
1180 Class<? extends ExtensionKey> key;
1181 if (NxmNxReg0.class.equals(rm.reg)) {
1182 key = NxmNxReg0Key.class;
1183 } else if (NxmNxReg1.class.equals(rm.reg)) {
1184 key = NxmNxReg1Key.class;
1185 } else if (NxmNxReg2.class.equals(rm.reg)) {
1186 key = NxmNxReg2Key.class;
1187 } else if (NxmNxReg3.class.equals(rm.reg)) {
1188 key = NxmNxReg3Key.class;
1189 } else if (NxmNxReg4.class.equals(rm.reg)) {
1190 key = NxmNxReg4Key.class;
1191 } else if (NxmNxReg5.class.equals(rm.reg)) {
1192 key = NxmNxReg5Key.class;
1193 } else if (NxmNxReg6.class.equals(rm.reg)) {
1194 key = NxmNxReg6Key.class;
1196 key = NxmNxReg7Key.class;
1198 NxAugMatchNodesNodeTableFlow am =
1199 new NxAugMatchNodesNodeTableFlowBuilder()
1200 .setNxmNxReg(new NxmNxRegBuilder()
1205 extensions.add(new ExtensionListBuilder()
1206 .setExtensionKey(key)
1207 .setExtension(new ExtensionBuilder()
1208 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1212 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1213 .setExtensionList(extensions)
1215 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1216 return matchBuilder;
1219 public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1220 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1221 .setNxmNxTunId(new NxmNxTunIdBuilder()
1222 .setValue(BigInteger.valueOf(tunId))
1225 GeneralAugMatchNodesNodeTableFlow m =
1226 new GeneralAugMatchNodesNodeTableFlowBuilder()
1227 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1228 .setExtensionKey(NxmNxTunIdKey.class)
1229 .setExtension(new ExtensionBuilder()
1230 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1234 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1235 return matchBuilder;
1238 public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1239 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1240 .setNxmNxNsp(new NxmNxNspBuilder()
1244 addExtension(matchBuilder, NxmNxNspKey.class, am);
1245 return matchBuilder;
1248 public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1249 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1250 .setNxmNxNsi(new NxmNxNsiBuilder()
1254 addExtension(matchBuilder, NxmNxNsiKey.class, am);
1255 return matchBuilder;
1258 private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1259 NxAugMatchNodesNodeTableFlow am) {
1260 GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1261 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1262 List<ExtensionList> extensions = null;
1263 if (existingAugmentations != null ) {
1264 extensions = existingAugmentations.getExtensionList();
1266 if (extensions == null) {
1267 extensions = Lists.newArrayList();
1270 extensions.add(new ExtensionListBuilder()
1271 .setExtensionKey(extensionKey)
1272 .setExtension(new ExtensionBuilder()
1273 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1277 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1278 .setExtensionList(extensions)
1280 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1283 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1286 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1287 if (srcMac != null) {
1288 emb.setEthernetSource(new EthernetSourceBuilder()
1292 if (dstMac != null) {
1293 emb.setEthernetDestination(new EthernetDestinationBuilder()
1297 if (etherType != null) {
1298 emb.setEthernetType(new EthernetTypeBuilder()
1299 .setType(new EtherType(etherType))
1306 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1308 * @param ipv4AddressString the ip address, in string format
1309 * @return Ipv4Prefix with ipv4Address and /32 mask
1311 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1312 return new Ipv4Prefix(ipv4AddressString + "/32");
1316 * Converts port range into a set of masked port ranges.
1318 * @param portMin the strating port of the range.
1319 * @param portMax the ending port of the range.
1320 * @return the map contianing the port no and their mask.
1323 public static Map<Integer,Integer> getLayer4MaskForRange(int portMin, int portMax) {
1324 int [] offset = {32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
1325 int[] mask = {0x8000,0xC000,0xE000,0xF000,0xF800,0xFC00,0xFE00,0xFF00,
1326 0xFF80,0xFFC0,0xFFE0,0xFFF0,0xFFF8,0xFFFC,0xFFFE,0xFFFF};
1327 int noOfPorts = portMax - portMin + 1;
1328 String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
1329 int medianOffset = 16 - binaryNoOfPorts.length();
1330 int medianLength = offset[medianOffset];
1332 for (int tempMedian = 0;tempMedian < portMax;) {
1333 tempMedian = medianLength + tempMedian;
1334 if (portMin < tempMedian) {
1335 median = tempMedian;
1339 Map<Integer,Integer> portMap = new HashMap<Integer,Integer>();
1341 int currentMedain = median;
1342 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1343 tempMedian = currentMedain - offset[tempMedianOffset];
1344 if (portMin <= tempMedian) {
1345 for (;portMin <= tempMedian;) {
1346 portMap.put(tempMedian, mask[tempMedianOffset]);
1347 currentMedain = tempMedian;
1348 tempMedian = tempMedian - offset[tempMedianOffset];
1352 currentMedain = median;
1353 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1354 tempMedian = currentMedain + offset[tempMedianOffset];
1355 if (portMax >= tempMedian - 1) {
1356 for (;portMax >= tempMedian - 1;) {
1357 portMap.put(currentMedain, mask[tempMedianOffset]);
1358 currentMedain = tempMedian;
1359 tempMedian = tempMedian + offset[tempMedianOffset];
1367 * Return Long that represents OF port for strings where OF is explicitly provided
1369 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1370 * @return the OFport or null
1372 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1373 if (ofPortIdentifier != null) {
1374 String[] pair = ofPortIdentifier.split("\\|");
1375 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1376 return new Long(pair[1]);