2 * Copyright (c) 2013 - 2016 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.netvirt.utils.mdsal.openflow;
11 import com.google.common.collect.ImmutableList;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagsMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtStateKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtZoneKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpDstKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpSrcKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.state.grouping.NxmNxCtStateBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.zone.grouping.NxmNxCtZoneBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.src.grouping.NxmOfTcpSrcBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.dst.grouping.NxmOfUdpDstBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.src.grouping.NxmOfUdpSrcBuilder;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
93 public class MatchUtils {
94 private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
95 public static final short ICMP_SHORT = 1;
96 public static final short TCP_SHORT = 6;
97 public static final short UDP_SHORT = 17;
98 public static final short SCTP_SHORT = 132;
99 public static final String TCP = "tcp";
100 public static final String UDP = "udp";
101 public static final String ICMP_PROTOCOL = "1";
102 public static final String TCP_PROTOCOL = "6";
103 public static final String UDP_PROTOCOL = "17";
104 public static final String ANY_PROTOCOL = "0";
105 private static final int TCP_SYN = 0x0002;
106 public static final String ICMP = "icmp";
107 public static final String ICMPV6 = "icmpv6";
108 public static final short ALL_ICMP = -1;
109 public static final long ETHERTYPE_IPV4 = 0x0800;
110 public static final long ETHERTYPE_IPV6 = 0x86dd;
111 public static final long ETHERTYPE_ARP = 0x0806L;
112 public static final int UNTRACKED_CT_STATE = 0x00;
113 public static final int UNTRACKED_CT_STATE_MASK = 0x20;
114 public static final int TRACKED_EST_CT_STATE = 0x22;
115 public static final int TRACKED_REL_CT_STATE = 0x24;
116 public static final int TRACKED_NEW_CT_STATE = 0x21;
117 public static final int TRACKED_INV_CT_STATE = 0x30;
118 public static final int TRACKED_INV_CT_STATE_MASK = 0x30;
119 public static final int TRACKED_CT_STATE_MASK = 0x37;
120 public static final int TRACKED_NEW_CT_STATE_MASK = 0x21;
123 * Create Ingress Port Match dpidLong, inPort
125 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
126 * @param dpidLong Long the datapath ID of a switch/node
127 * @param inPort Long ingress port on a switch
128 * @return matchBuilder Map MatchBuilder Object with a match
130 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
132 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
133 LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
134 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
135 matchBuilder.setInPort(ncid);
140 public static MatchBuilder createInPortReservedMatch(MatchBuilder matchBuilder, Long dpidLong, String inPort) {
142 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
143 LOG.debug("createInPortResrevedMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
145 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
146 matchBuilder.setInPort(ncid);
152 * Create EtherType Match
154 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
155 * @param etherType Long EtherType
156 * @return matchBuilder Map MatchBuilder Object with a match
158 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
160 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
161 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
162 ethTypeBuilder.setType(new EtherType(etherType));
163 ethernetMatch.setEthernetType(ethTypeBuilder.build());
164 matchBuilder.setEthernetMatch(ethernetMatch.build());
169 public static MatchBuilder createEthSrcDstMatch(MatchBuilder matchBuilder, MacAddress srcMac, MacAddress dstMac) {
170 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
171 if (srcMac != null) {
172 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
173 ethSourceBuilder.setAddress(srcMac);
174 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
176 if (dstMac != null) {
177 EthernetDestinationBuilder ethDestinationBuild = new EthernetDestinationBuilder();
178 ethDestinationBuild.setAddress(dstMac);
179 ethernetMatch.setEthernetDestination(ethDestinationBuild.build());
181 if (matchBuilder.getEthernetMatch() != null && matchBuilder.getEthernetMatch().getEthernetType() != null) {
182 ethernetMatch.setEthernetType(matchBuilder.getEthernetMatch().getEthernetType());
185 matchBuilder.setEthernetMatch(ethernetMatch.build());
191 * Create Ethernet Source Match
193 * @param matchBuilder MatchBuilder Object without a match yet
194 * @param sMacAddr String representing a source MAC
195 * @return matchBuilder Map MatchBuilder Object with a match
197 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
199 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
200 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
201 ethSourceBuilder.setAddress(sMacAddr);
202 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
203 matchBuilder.setEthernetMatch(ethernetMatch.build());
209 * Create Ethernet Destination Match
211 * @param matchBuilder MatchBuilder Object without a match yet
212 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
213 * @return matchBuilder Map MatchBuilder Object with a match
215 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
216 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
217 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
218 vlanIdBuilder.setVlanId(new VlanId(vlanId));
219 vlanIdBuilder.setVlanIdPresent(present);
220 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
221 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
227 * Create Ethernet Destination Match
229 * @param matchBuilder MatchBuilder Object without a match yet
230 * @param dMacAddr String representing a destination MAC
231 * @return matchBuilder Map MatchBuilder Object with a match
233 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
235 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
236 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
237 ethDestinationBuilder.setAddress(dMacAddr);
239 ethDestinationBuilder.setMask(mask);
241 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
242 matchBuilder.setEthernetMatch(ethernetMatch.build());
248 * Tunnel ID Match Builder
250 * @param matchBuilder MatchBuilder Object without a match yet
251 * @param tunnelId BigInteger representing a tunnel ID
252 * @return matchBuilder Map MatchBuilder Object with a match
254 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
256 TunnelBuilder tunnelBuilder = new TunnelBuilder();
257 tunnelBuilder.setTunnelId(tunnelId);
258 matchBuilder.setTunnel(tunnelBuilder.build());
264 * Match ICMP code and type
266 * @param matchBuilder MatchBuilder Object
267 * @param type short representing an ICMP type
268 * @param code short representing an ICMP code
269 * @return matchBuilder Map MatchBuilder Object with a match
271 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
273 // Build the IPv4 Match requied per OVS Syntax
274 IpMatchBuilder ipmatch = new IpMatchBuilder();
275 ipmatch.setIpProtocol((short) 1);
276 matchBuilder.setIpMatch(ipmatch.build());
278 // Build the ICMPv4 Match
279 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
280 if (type != ALL_ICMP) {
281 icmpv4match.setIcmpv4Type(type);
283 if (code != ALL_ICMP) {
284 icmpv4match.setIcmpv4Code(code);
286 matchBuilder.setIcmpv4Match(icmpv4match.build());
292 * Match ICMPv6 code and type
294 * @param matchBuilder MatchBuilder Object
295 * @param type short representing an ICMP type
296 * @param code short representing an ICMP code
297 * @return matchBuilder Map MatchBuilder Object with a match
299 public static MatchBuilder createICMPv6Match(MatchBuilder matchBuilder, short type, short code) {
301 // Build the IPv6 Match required per OVS Syntax
302 IpMatchBuilder ipmatch = new IpMatchBuilder();
303 ipmatch.setIpProtocol((short) 58);
304 matchBuilder.setIpMatch(ipmatch.build());
306 // Build the ICMPv6 Match
307 Icmpv6MatchBuilder icmpv6match = new Icmpv6MatchBuilder();
308 if (type != ALL_ICMP || code != ALL_ICMP) {
309 icmpv6match.setIcmpv6Type(type);
310 icmpv6match.setIcmpv6Code(code);
312 matchBuilder.setIcmpv6Match(icmpv6match.build());
318 * @param matchBuilder MatchBuilder Object without a match yet
319 * @param dstip String containing an IPv4 prefix
320 * @return matchBuilder Map Object with a match
322 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
324 EthernetMatchBuilder eth = new EthernetMatchBuilder();
325 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
326 ethTypeBuilder.setType(new EtherType(0x0800L));
327 eth.setEthernetType(ethTypeBuilder.build());
328 matchBuilder.setEthernetMatch(eth.build());
330 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
331 ipv4match.setIpv4Destination(dstip);
333 matchBuilder.setLayer3Match(ipv4match.build());
340 * @param matchBuilder MatchBuilder Object without a match yet
341 * @param dstip String containing an IPv4 prefix
342 * @return matchBuilder Map Object with a match
344 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
345 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
346 arpDstMatch.setArpTargetTransportAddress(dstip)
347 .setArpOp(FlowUtils.ARP_OP_REQUEST);
348 matchBuilder.setLayer3Match(arpDstMatch.build());
354 * Builds the arp match with src and destintion mac address.
355 * @param matchBuilder the match builder
356 * @param srcMac the src mac address , will not be added to match if null.
357 * @param dstMac the destination mac address, will not be added to match if null.
358 * @return the match builder with the matches
360 public static MatchBuilder addArpMacMatch(MatchBuilder matchBuilder, String srcMac, String dstMac) {
361 ArpMatchBuilder arpMatch = new ArpMatchBuilder();
362 if (null != srcMac) {
363 ArpSourceHardwareAddressBuilder arpSrc = new ArpSourceHardwareAddressBuilder();
364 arpSrc.setAddress(new MacAddress(srcMac));
365 arpMatch.setArpSourceHardwareAddress(arpSrc.build());
367 if (null != dstMac) {
368 ArpTargetHardwareAddressBuilder arpDst = new ArpTargetHardwareAddressBuilder();
369 arpDst.setAddress(new MacAddress(dstMac));
370 arpMatch.setArpTargetHardwareAddress(arpDst.build());
372 matchBuilder.setLayer3Match(arpMatch.build());
377 * @param matchBuilder MatchBuilder Object without a match yet
378 * @param srcip String containing an IPv4 prefix
379 * @return matchBuilder Map Object with a match
381 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
383 EthernetMatchBuilder eth = new EthernetMatchBuilder();
384 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
385 ethTypeBuilder.setType(new EtherType(0x0800L));
386 eth.setEthernetType(ethTypeBuilder.build());
387 matchBuilder.setEthernetMatch(eth.build());
389 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
390 ipv4match.setIpv4Source(srcip);
391 matchBuilder.setLayer3Match(ipv4match.build());
398 * Create Source TCP Port Match
400 * @param matchBuilder MatchBuilder Object without a match yet
401 * @param tcpport Integer representing a source TCP port
402 * @return matchBuilder Map MatchBuilder Object with a match
404 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
406 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
407 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
408 ethTypeBuilder.setType(new EtherType(0x0800L));
409 ethType.setEthernetType(ethTypeBuilder.build());
410 matchBuilder.setEthernetMatch(ethType.build());
412 IpMatchBuilder ipmatch = new IpMatchBuilder();
413 ipmatch.setIpProtocol((short) 6);
414 matchBuilder.setIpMatch(ipmatch.build());
416 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
417 tcpmatch.setTcpSourcePort(tcpport);
418 matchBuilder.setLayer4Match(tcpmatch.build());
425 * Create Destination TCP Port Match
427 * @param matchBuilder MatchBuilder Object without a match yet
428 * @param tcpDstPort Integer representing a destination TCP port
429 * @return matchBuilder Map MatchBuilder Object with a match
431 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
433 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
434 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
435 ethTypeBuilder.setType(new EtherType(0x0800L));
436 ethType.setEthernetType(ethTypeBuilder.build());
437 matchBuilder.setEthernetMatch(ethType.build());
439 IpMatchBuilder ipmatch = new IpMatchBuilder();
440 ipmatch.setIpProtocol((short) 6);
441 matchBuilder.setIpMatch(ipmatch.build());
443 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
444 tcpmatch.setTcpDestinationPort(tcpDstPort);
445 matchBuilder.setLayer4Match(tcpmatch.build());
451 * Test match for TCP_Flags
453 * @param matchBuilder MatchBuilder Object without a match yet
454 * @param tcpPort PortNumber representing a destination TCP port
455 * @param tcpFlags int representing a tcp_flag
456 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
458 * Defined TCP Flag values in OVS v2.1+
459 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
460 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
461 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
463 public static MatchBuilder createTcpFlagsMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlags) {
466 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
467 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
468 ethTypeBuilder.setType(new EtherType(0x0800L));
469 ethernetType.setEthernetType(ethTypeBuilder.build());
470 matchBuilder.setEthernetMatch(ethernetType.build());
472 // TCP Protocol Match
473 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
474 ipMatch.setIpProtocol((short) 6);
475 matchBuilder.setIpMatch(ipMatch.build());
478 PortNumber dstPort = new PortNumber(tcpPort);
479 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
480 tcpMatch.setTcpDestinationPort(dstPort);
481 matchBuilder.setLayer4Match(tcpMatch.build());
483 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
484 tcpFlagsMatch.setTcpFlags(tcpFlags);
485 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
490 * @return MatchBuilder containing the metadata match values
492 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
494 // metadata matchbuilder
495 MetadataBuilder metadata = new MetadataBuilder();
496 metadata.setMetadata(metaData);
497 // Optional metadata mask
498 if (metaDataMask != null) {
499 metadata.setMetadataMask(metaDataMask);
501 matchBuilder.setMetadata(metadata.build());
507 * Create TCP Port Match
509 * @param matchBuilder MatchBuilder Object without a match yet
510 * @param ipProtocol Integer representing the IP protocol
511 * @return matchBuilder Map MatchBuilder Object with a match
513 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
515 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
516 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
517 ethTypeBuilder.setType(new EtherType(0x0800L));
518 ethType.setEthernetType(ethTypeBuilder.build());
519 matchBuilder.setEthernetMatch(ethType.build());
521 IpMatchBuilder ipMmatch = new IpMatchBuilder();
522 if (ipProtocol == TCP_SHORT) {
523 ipMmatch.setIpProtocol(TCP_SHORT);
525 else if (ipProtocol == UDP_SHORT) {
526 ipMmatch.setIpProtocol(UDP_SHORT);
528 else if (ipProtocol == ICMP_SHORT) {
529 ipMmatch.setIpProtocol(ICMP_SHORT);
531 matchBuilder.setIpMatch(ipMmatch.build());
536 * Create IP protocol match with Eth Match
538 * @param matchBuilder MatchBuilder Object without a match yet
539 * @param ipProtocol Integer representing the IP protocol
540 * @param srcMac The source macAddress
541 * @param dstMac The destination mac address
542 * @return matchBuilder Map MatchBuilder Object with a match
544 public static MatchBuilder createIpProtocolAndEthMatch(MatchBuilder matchBuilder, short ipProtocol, String srcMac, String dstMac) {
546 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
547 ethTypeBuilder.setType(new EtherType(0x0800L));
548 EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
549 ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
550 if (null != srcMac) {
551 ethMatchBuilder.setEthernetSource(new EthernetSourceBuilder()
552 .setAddress(new MacAddress(srcMac)).build());
554 if (null != dstMac) {
555 ethMatchBuilder.setEthernetDestination(new EthernetDestinationBuilder()
556 .setAddress(new MacAddress(dstMac)).build());
558 matchBuilder.setEthernetMatch(ethMatchBuilder.build());
559 IpMatchBuilder ipMmatch = new IpMatchBuilder();
560 ipMmatch.setIpProtocol(ipProtocol);
561 matchBuilder.setIpMatch(ipMmatch.build());
566 * Create TCP Port Match
568 * @param matchBuilder MatchBuilder Object without a match yet
569 * @param ipProtocol Integer representing the IP protocol
570 * @return matchBuilder Map MatchBuilder Object with a match
572 public static MatchBuilder createIpv6ProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
574 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
575 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
576 ethTypeBuilder.setType(new EtherType(0x86DDL));
577 ethType.setEthernetType(ethTypeBuilder.build());
578 matchBuilder.setEthernetMatch(ethType.build());
580 IpMatchBuilder ipMmatch = new IpMatchBuilder();
581 if (ipProtocol == TCP_SHORT) {
582 ipMmatch.setIpProtocol(TCP_SHORT);
584 else if (ipProtocol == UDP_SHORT) {
585 ipMmatch.setIpProtocol(UDP_SHORT);
587 else if (ipProtocol == ICMP_SHORT) {
588 ipMmatch.setIpProtocol(ICMP_SHORT);
590 matchBuilder.setIpMatch(ipMmatch.build());
595 * Create tcp syn with proto match.
597 * @param matchBuilder the match builder
598 * @return matchBuilder match builder
600 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
603 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
604 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
605 ethTypeBuilder.setType(new EtherType(0x0800L));
606 ethernetType.setEthernetType(ethTypeBuilder.build());
607 matchBuilder.setEthernetMatch(ethernetType.build());
609 // TCP Protocol Match
610 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
611 ipMatch.setIpProtocol((short) 6);
612 matchBuilder.setIpMatch(ipMatch.build());
614 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
615 tcpFlagsMatch.setTcpFlags(TCP_SYN);
616 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
621 * Adds tcp syn match to the matchbuilder.
623 * @param matchBuilder the match builder
624 * @return matchBuilder match builder
626 public static MatchBuilder addTcpSynMatch(MatchBuilder matchBuilder) {
628 // TCP Protocol Match
629 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
630 ipMatch.setIpProtocol((short) 6);
631 matchBuilder.setIpMatch(ipMatch.build());
633 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
634 tcpFlagsMatch.setTcpFlags(TCP_SYN);
635 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
640 * Create tcp proto syn match.
642 * @param matchBuilder the match builder
643 * @return matchBuilder match builder
645 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
647 // TCP Protocol Match
648 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
649 ipMatch.setIpProtocol((short) 6);
650 matchBuilder.setIpMatch(ipMatch.build());
652 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
653 tcpFlagsMatch.setTcpFlags(TCP_SYN);
654 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
659 * Create dmac tcp port with flag match.
661 * @param matchBuilder the match builder
662 * @param attachedMac the attached mac
663 * @param tcpFlags the tcp flag
664 * @param tunnelID the tunnel iD
665 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
667 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
668 String attachedMac, Integer tcpFlags, String tunnelID) {
669 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlags, null, tunnelID);
673 * Create dmac ipSa match.
675 * @param matchBuilder the match builder
676 * @param attachedMac the attached mac
677 * @param ipPrefix the src ipPrefix
678 * @param tunnelID the tunnel iD
679 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
681 public static MatchBuilder createDmacIpSaMatch(
682 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
683 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
687 * Create dmac tcp port ipSa with flag match.
689 * @param matchBuilder the match builder
690 * @param attachedMac the attached mac
691 * @param tcpFlags the tcp flag
692 * @param ipPrefix the src ipPrefix
693 * @param tunnelID the tunnel iD
694 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
696 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
697 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlags, Ipv4Prefix ipPrefix, String tunnelID) {
699 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
700 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
701 ethTypeBuilder.setType(new EtherType(0x0800L));
702 ethernetMatch.setEthernetType(ethTypeBuilder.build());
704 if (attachedMac != null) {
705 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
706 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
707 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
708 matchBuilder.setEthernetMatch(ethernetMatch.build());
711 if (tcpFlags != null) {
712 // TCP Protocol Match
713 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
714 ipMatch.setIpProtocol(TCP_SHORT);
715 matchBuilder.setIpMatch(ipMatch.build());
717 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
718 tcpFlagsMatch.setTcpFlags(tcpFlags);
719 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
722 if (tunnelID != null) {
723 TunnelBuilder tunnelBuilder = new TunnelBuilder();
724 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
725 matchBuilder.setTunnel(tunnelBuilder.build());
728 if (ipPrefix != null) {
729 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
730 ipv4match.setIpv4Source(ipPrefix);
731 matchBuilder.setLayer3Match(ipv4match.build());
738 * Create dmac tcp syn match.
740 * @param matchBuilder the match builder
741 * @param attachedMac the attached mac
742 * @param tcpPort the tcp port
743 * @param tcpFlags the tcp flag
744 * @param tunnelID the tunnel iD
745 * @return the match builder
747 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
748 String attachedMac, PortNumber tcpPort, Integer tcpFlags, String tunnelID) {
750 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
751 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
752 ethTypeBuilder.setType(new EtherType(0x0800L));
753 ethernetMatch.setEthernetType(ethTypeBuilder.build());
755 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
756 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
757 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
758 matchBuilder.setEthernetMatch(ethernetMatch.build());
760 // TCP Protocol Match
761 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
762 ipMatch.setIpProtocol((short) 6);
763 matchBuilder.setIpMatch(ipMatch.build());
766 PortNumber dstPort = new PortNumber(tcpPort);
767 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
768 tcpMatch.setTcpDestinationPort(dstPort);
769 matchBuilder.setLayer4Match(tcpMatch.build());
771 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
772 tcpFlagsMatch.setTcpFlags(tcpFlags);
773 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
775 TunnelBuilder tunnelBuilder = new TunnelBuilder();
776 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
777 matchBuilder.setTunnel(tunnelBuilder.build());
783 * Create dmac tcp syn dst ip prefix tcp port.
785 * @param matchBuilder the match builder
786 * @param attachedMac the attached mac
787 * @param tcpPort the tcp port
788 * @param tcpFlags the tcp flag
789 * @param segmentationId the segmentation id
790 * @param dstIp the dst ip
791 * @return the match builder
793 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
794 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlags, String segmentationId,
797 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
798 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
799 ethTypeBuilder.setType(new EtherType(0x0800L));
800 ethernetMatch.setEthernetType(ethTypeBuilder.build());
802 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
803 ethDestinationBuilder.setAddress(attachedMac);
804 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
806 matchBuilder.setEthernetMatch(ethernetMatch.build());
808 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
809 ipv4match.setIpv4Destination(dstIp);
810 matchBuilder.setLayer3Match(ipv4match.build());
812 // TCP Protocol Match
813 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
814 ipMatch.setIpProtocol(TCP_SHORT);
815 matchBuilder.setIpMatch(ipMatch.build());
818 PortNumber dstPort = new PortNumber(tcpPort);
819 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
820 tcpMatch.setTcpDestinationPort(dstPort);
821 matchBuilder.setLayer4Match(tcpMatch.build());
823 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
824 tcpFlagsMatch.setTcpFlags(tcpFlags);
825 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
827 TunnelBuilder tunnelBuilder = new TunnelBuilder();
828 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
829 matchBuilder.setTunnel(tunnelBuilder.build());
835 * Create dmac ip tcp syn match.
837 * @param matchBuilder the match builder
838 * @param dMacAddr the d mac addr
839 * @param mask the mask
840 * @param ipPrefix the ip prefix
841 * @return MatchBuilder containing the metadata match values
843 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
844 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
846 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
847 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
848 ethDestBuilder.setAddress(dMacAddr);
850 ethDestBuilder.setMask(mask);
852 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
853 matchBuilder.setEthernetMatch(ethernetMatch.build());
855 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
856 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
857 ethTypeBuilder.setType(new EtherType(0x0800L));
858 ethernetType.setEthernetType(ethTypeBuilder.build());
859 matchBuilder.setEthernetMatch(ethernetType.build());
860 if (ipPrefix != null) {
861 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
862 ipv4match.setIpv4Destination(ipPrefix);
863 matchBuilder.setLayer3Match(ipv4match.build());
865 // TCP Protocol Match
866 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
867 ipMatch.setIpProtocol(TCP_SHORT);
868 matchBuilder.setIpMatch(ipMatch.build());
870 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
871 tcpFlagsMatch.setTcpFlags(TCP_SYN);
872 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
878 * Create smac tcp syn dst ip prefix tcp port.
880 * @param matchBuilder the match builder
881 * @param attachedMac the attached mac
882 * @param tcpPort the tcp port
883 * @param tcpFlags the tcp flag
884 * @param segmentationId the segmentation id
885 * @param dstIp the dst ip
886 * @return the match builder
888 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
889 PortNumber tcpPort, Integer tcpFlags, String segmentationId, Ipv4Prefix dstIp) {
891 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
892 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
893 ethTypeBuilder.setType(new EtherType(0x0800L));
894 ethernetMatch.setEthernetType(ethTypeBuilder.build());
896 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
897 ethSourceBuilder.setAddress(attachedMac);
898 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
900 matchBuilder.setEthernetMatch(ethernetMatch.build());
902 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
903 ipv4match.setIpv4Destination(dstIp);
904 matchBuilder.setLayer3Match(ipv4match.build());
906 // TCP Protocol Match
907 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
908 ipMatch.setIpProtocol(TCP_SHORT);
909 matchBuilder.setIpMatch(ipMatch.build());
912 PortNumber dstPort = new PortNumber(tcpPort);
913 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
914 tcpMatch.setTcpDestinationPort(dstPort);
915 matchBuilder.setLayer4Match(tcpMatch.build());
917 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
918 tcpFlagsMatch.setTcpFlags(tcpFlags);
919 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
921 TunnelBuilder tunnelBuilder = new TunnelBuilder();
922 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
923 matchBuilder.setTunnel(tunnelBuilder.build());
929 * Create smac tcp port with flag match.
931 * @param matchBuilder the match builder
932 * @param attachedMac the attached mac
933 * @param tcpFlags the tcp flag
934 * @param tunnelID the tunnel iD
935 * @return matchBuilder
937 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
938 Integer tcpFlags, String tunnelID) {
940 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
941 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
942 ethTypeBuilder.setType(new EtherType(0x0800L));
943 ethernetMatch.setEthernetType(ethTypeBuilder.build());
945 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
946 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
947 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
948 matchBuilder.setEthernetMatch(ethernetMatch.build());
950 // TCP Protocol Match
951 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
952 ipMatch.setIpProtocol(TCP_SHORT);
953 matchBuilder.setIpMatch(ipMatch.build());
955 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
956 tcpFlagsMatch.setTcpFlags(tcpFlags);
957 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
959 TunnelBuilder tunnelBuilder = new TunnelBuilder();
960 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
961 matchBuilder.setTunnel(tunnelBuilder.build());
967 * Create smac ip tcp syn match.
969 * @param matchBuilder the match builder
970 * @param dMacAddr the d mac addr
971 * @param mask the mask
972 * @param ipPrefix the ip prefix
973 * @return MatchBuilder containing the metadata match values
975 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
976 MacAddress mask, Ipv4Prefix ipPrefix) {
978 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
979 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
980 ethSrcBuilder.setAddress(dMacAddr);
982 ethSrcBuilder.setMask(mask);
984 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
985 matchBuilder.setEthernetMatch(ethernetMatch.build());
987 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
988 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
989 ethTypeBuilder.setType(new EtherType(0x0800L));
990 ethernetType.setEthernetType(ethTypeBuilder.build());
991 matchBuilder.setEthernetMatch(ethernetType.build());
992 if (ipPrefix != null) {
993 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
994 ipv4match.setIpv4Destination(ipPrefix);
995 matchBuilder.setLayer3Match(ipv4match.build());
997 // TCP Protocol Match
998 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
999 ipMatch.setIpProtocol(TCP_SHORT);
1000 matchBuilder.setIpMatch(ipMatch.build());
1002 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
1003 tcpFlagsMatch.setTcpFlags(TCP_SYN);
1004 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
1006 return matchBuilder;
1010 * Create smac tcp syn.
1012 * @param matchBuilder the match builder
1013 * @param attachedMac the attached mac
1014 * @param tcpPort the tcp port
1015 * @param tcpFlags the tcp flag
1016 * @param tunnelID the tunnel iD
1017 * @return the match builder
1019 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
1020 String attachedMac, PortNumber tcpPort, Integer tcpFlags, String tunnelID) {
1022 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1023 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1024 ethTypeBuilder.setType(new EtherType(0x0800L));
1025 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1027 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
1028 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
1029 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
1030 matchBuilder.setEthernetMatch(ethernetMatch.build());
1032 // TCP Protocol Match
1033 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
1034 ipMatch.setIpProtocol((short) 6);
1035 matchBuilder.setIpMatch(ipMatch.build());
1038 PortNumber dstPort = new PortNumber(tcpPort);
1039 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
1040 tcpMatch.setTcpDestinationPort(dstPort);
1041 matchBuilder.setLayer4Match(tcpMatch.build());
1044 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
1045 tcpFlagsMatch.setTcpFlags(tcpFlags);
1046 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
1048 TunnelBuilder tunnelBuilder = new TunnelBuilder();
1049 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
1050 matchBuilder.setTunnel(tunnelBuilder.build());
1052 return matchBuilder;
1056 * @return MatchBuilder containing the metadata match values
1058 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
1059 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
1061 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1062 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
1063 ethDestinationBuilder.setAddress(dMacAddr);
1065 ethDestinationBuilder.setMask(mask);
1067 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
1068 matchBuilder.setEthernetMatch(ethernetMatch.build());
1070 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
1071 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1072 ethTypeBuilder.setType(new EtherType(0x0800L));
1073 ethernetType.setEthernetType(ethTypeBuilder.build());
1074 matchBuilder.setEthernetMatch(ethernetType.build());
1075 if (ipPrefix != null) {
1076 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1077 ipv4match.setIpv4Source(ipPrefix);
1078 matchBuilder.setLayer3Match(ipv4match.build());
1080 // TCP Protocol Match
1081 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
1082 ipMatch.setIpProtocol(TCP_SHORT);
1083 matchBuilder.setIpMatch(ipMatch.build());
1085 TcpFlagsMatchBuilder tcpFlagsMatch = new TcpFlagsMatchBuilder();
1086 tcpFlagsMatch.setTcpFlags(TCP_SYN);
1087 matchBuilder.setTcpFlagsMatch(tcpFlagsMatch.build());
1089 return matchBuilder;
1093 * Create a DHCP match with port provided.
1095 * @param matchBuilder the match builder
1096 * @param srcPort the source port
1097 * @param dstPort the destination port
1098 * @return the DHCP match
1100 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
1101 int srcPort, int dstPort) {
1103 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1104 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1105 ethTypeBuilder.setType(new EtherType(0x0800L));
1106 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1107 matchBuilder.setEthernetMatch(ethernetMatch.build());
1109 IpMatchBuilder ipmatch = new IpMatchBuilder();
1110 ipmatch.setIpProtocol(UDP_SHORT);
1111 matchBuilder.setIpMatch(ipmatch.build());
1113 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1114 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1115 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1116 matchBuilder.setLayer4Match(udpmatch.build());
1118 return matchBuilder;
1123 * Create a DHCP match with port provided.
1125 * @param matchBuilder the match builder
1126 * @param srcPort the source port
1127 * @param dstPort the destination port
1128 * @return the DHCP match
1130 public static MatchBuilder createDhcpv6Match(MatchBuilder matchBuilder,
1131 int srcPort, int dstPort) {
1133 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1134 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1135 ethTypeBuilder.setType(new EtherType(0x86DDL));
1136 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1137 matchBuilder.setEthernetMatch(ethernetMatch.build());
1139 IpMatchBuilder ipmatch = new IpMatchBuilder();
1140 ipmatch.setIpProtocol(UDP_SHORT);
1141 matchBuilder.setIpMatch(ipmatch.build());
1143 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1144 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1145 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1146 matchBuilder.setLayer4Match(udpmatch.build());
1148 return matchBuilder;
1153 * Creates DHCP server packet match with DHCP mac address and port.
1155 * @param matchBuilder the matchbuilder
1156 * @param dhcpServerMac MAc address of the DHCP server of the subnet
1157 * @param srcPort the source port
1158 * @param dstPort the destination port
1159 * @return the DHCP server match
1161 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1164 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1165 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1166 ethTypeBuilder.setType(new EtherType(0x0800L));
1167 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1168 matchBuilder.setEthernetMatch(ethernetMatch.build());
1170 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1171 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1172 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1173 matchBuilder.setEthernetMatch(ethernetMatch.build());
1175 IpMatchBuilder ipmatch = new IpMatchBuilder();
1176 ipmatch.setIpProtocol(UDP_SHORT);
1177 matchBuilder.setIpMatch(ipmatch.build());
1179 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1180 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1181 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1182 matchBuilder.setLayer4Match(udpmatch.build());
1184 return matchBuilder;
1189 * Creates DHCPv6 server packet match with DHCP mac address and port.
1191 * @param matchBuilder the matchbuilder
1192 * @param dhcpServerMac MAc address of the DHCP server of the subnet
1193 * @param srcPort the source port
1194 * @param dstPort the destination port
1195 * @return the DHCP server match
1197 public static MatchBuilder createDhcpv6ServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1200 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1201 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1202 ethTypeBuilder.setType(new EtherType(0x86DDL));
1203 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1204 matchBuilder.setEthernetMatch(ethernetMatch.build());
1206 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1207 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1208 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1209 matchBuilder.setEthernetMatch(ethernetMatch.build());
1211 IpMatchBuilder ipmatch = new IpMatchBuilder();
1212 ipmatch.setIpProtocol(UDP_SHORT);
1213 matchBuilder.setIpMatch(ipmatch.build());
1215 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1216 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1217 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1218 matchBuilder.setLayer4Match(udpmatch.build());
1220 return matchBuilder;
1225 * Creates a Match with src ip address mac address set.
1226 * @param matchBuilder MatchBuilder Object
1227 * @param srcip String containing an IPv4 prefix
1228 * @param srcMac The source macAddress
1229 * @return matchBuilder Map Object with a match
1231 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
1233 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
1234 ipv4MatchBuilder.setIpv4Source(srcip);
1235 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1236 ethTypeBuilder.setType(new EtherType(0x0800L));
1237 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1238 eth.setEthernetType(ethTypeBuilder.build());
1239 eth.setEthernetSource(new EthernetSourceBuilder()
1243 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1244 matchBuilder.setEthernetMatch(eth.build());
1245 return matchBuilder;
1250 * Creates a Match with src ip address mac address set.
1251 * @param matchBuilder MatchBuilder Object
1252 * @param srcip String containing an IPv6 prefix
1253 * @param srcMac The source macAddress
1254 * @return matchBuilder Map Object with a match
1256 public static MatchBuilder createSrcL3Ipv6MatchWithMac(MatchBuilder matchBuilder, Ipv6Prefix srcip, MacAddress srcMac) {
1258 Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
1259 ipv6MatchBuilder.setIpv6Source(srcip);
1260 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1261 ethTypeBuilder.setType(new EtherType(0x86DDL));
1262 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1263 eth.setEthernetType(ethTypeBuilder.build());
1264 eth.setEthernetSource(new EthernetSourceBuilder()
1268 matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1269 matchBuilder.setEthernetMatch(eth.build());
1270 return matchBuilder;
1275 * Creates a ether net match with ether type set to 0x0800L.
1276 * @param matchBuilder MatchBuilder Object
1277 * @param srcMac The source macAddress
1278 * @param dstMac The destination mac address
1279 * @return matchBuilder Map Object with a match
1281 public static MatchBuilder createV4EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac, Long type)
1283 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1284 ethTypeBuilder.setType(new EtherType(type));
1285 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1286 eth.setEthernetType(ethTypeBuilder.build());
1287 if (null != srcMac) {
1288 eth.setEthernetSource(new EthernetSourceBuilder()
1289 .setAddress(new MacAddress(srcMac)).build());
1291 if (null != dstMac) {
1292 eth.setEthernetDestination(new EthernetDestinationBuilder()
1293 .setAddress(new MacAddress(dstMac)).build());
1295 matchBuilder.setEthernetMatch(eth.build());
1296 return matchBuilder;
1299 * Creates a ether net match with ether type set to 0x0800L.
1300 * @param matchBuilder MatchBuilder Object
1301 * @param srcMac The source macAddress
1302 * @param dstMac The destination mac address
1303 * @param type The Tcp type
1304 * @param dstPort The destination port
1305 * @return matchBuilder Map Object with a match
1307 public static MatchBuilder createV4EtherMatchWithLearnTCP(MatchBuilder matchBuilder,String srcMac, String dstMac, Long type, Integer dstPort)
1309 matchBuilder = createV4EtherMatchWithType(matchBuilder, srcMac, dstMac, type);
1310 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1311 tcpmatch.setTcpDestinationPort(new PortNumber(dstPort));
1312 matchBuilder.setLayer4Match(tcpmatch.build());
1313 return matchBuilder;
1316 * Creates a ether net match with ether type set to 0x0800L.
1317 * @param matchBuilder MatchBuilder Object
1318 * @param srcMac The source macAddress
1319 * @param dstMac The destination mac address
1320 * @param type The UDP type
1321 * @param dstPort The destination port
1322 * @return matchBuilder Map Object with a match
1324 public static MatchBuilder createV4EtherMatchWithLearnUDP(MatchBuilder matchBuilder,String srcMac, String dstMac, Long type, Integer dstPort)
1326 matchBuilder = createV4EtherMatchWithType(matchBuilder, srcMac, dstMac, type);
1327 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1328 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1329 matchBuilder.setLayer4Match(udpmatch.build());
1330 return matchBuilder;
1333 * Creates a ether net match with ether type set to 0x0800L.
1334 * @param matchBuilder MatchBuilder Object
1335 * @param srcMac The source macAddress
1336 * @param dstMac The destination mac address
1337 * @return matchBuilder Map Object with a match
1339 public static MatchBuilder createV4EtherMatchWithoutType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1341 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1342 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1343 if (null != srcMac) {
1344 eth.setEthernetSource(new EthernetSourceBuilder()
1345 .setAddress(new MacAddress(srcMac)).build());
1347 if (null != dstMac) {
1348 eth.setEthernetDestination(new EthernetDestinationBuilder()
1349 .setAddress(new MacAddress(dstMac)).build());
1351 matchBuilder.setEthernetMatch(eth.build());
1352 return matchBuilder;
1356 * Creates a ether net match with ether type set to 0x86DDL.
1357 * @param matchBuilder MatchBuilder Object
1358 * @param srcMac The source macAddress
1359 * @param dstMac The destination mac address
1360 * @return matchBuilder Map Object with a match
1362 public static MatchBuilder createV6EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1364 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1365 ethTypeBuilder.setType(new EtherType(0x86DDL));
1366 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1367 eth.setEthernetType(ethTypeBuilder.build());
1368 if (null != srcMac) {
1369 eth.setEthernetSource(new EthernetSourceBuilder()
1370 .setAddress(new MacAddress(srcMac)).build());
1372 if (null != dstMac) {
1373 eth.setEthernetDestination(new EthernetDestinationBuilder()
1374 .setAddress(new MacAddress(dstMac)).build());
1376 matchBuilder.setEthernetMatch(eth.build());
1377 return matchBuilder;
1381 * Adds remote Ip prefix to existing match.
1382 * @param matchBuilder The match builder
1383 * @param sourceIpPrefix The source IP prefix
1384 * @param destIpPrefix The destination IP prefix
1385 * @return matchBuilder Map Object with a match
1387 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1388 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1389 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1390 if (null != sourceIpPrefix) {
1391 ipv4match.setIpv4Source(sourceIpPrefix);
1393 if (null != destIpPrefix) {
1394 ipv4match.setIpv4Destination(destIpPrefix);
1396 matchBuilder.setLayer3Match(ipv4match.build());
1398 return matchBuilder;
1402 * Adds remote Ipv6 prefix to existing match.
1403 * @param matchBuilder The match builder
1404 * @param sourceIpPrefix The source IP prefix
1405 * @param destIpPrefix The destination IP prefix
1406 * @return matchBuilder Map Object with a match
1408 public static MatchBuilder addRemoteIpv6Prefix(MatchBuilder matchBuilder,
1409 Ipv6Prefix sourceIpPrefix,Ipv6Prefix destIpPrefix) {
1410 Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
1411 if (null != sourceIpPrefix) {
1412 ipv6match.setIpv6Source(sourceIpPrefix);
1414 if (null != destIpPrefix) {
1415 ipv6match.setIpv6Destination(destIpPrefix);
1417 matchBuilder.setLayer3Match(ipv6match.build());
1419 return matchBuilder;
1423 * Add a DSCP match to an existing match
1424 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1426 * @return {@link MatchBuilder}
1428 public static MatchBuilder addDscp(MatchBuilder matchBuilder, short dscpValue) {
1429 createEtherTypeMatch(matchBuilder, new EtherType(ETHERTYPE_IPV4));
1430 return matchBuilder.setIpMatch(
1431 new IpMatchBuilder()
1432 .setIpDscp(new Dscp(dscpValue))
1437 * Add a layer4 match to an existing match
1439 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1440 * @param protocol The layer4 protocol
1441 * @param srcPort The src port
1442 * @param destPort The destination port
1443 * @return matchBuilder Map Object with a match
1445 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1446 int protocol, int srcPort, int destPort) {
1447 IpMatchBuilder ipmatch = new IpMatchBuilder();
1448 if (TCP_SHORT == protocol) {
1449 ipmatch.setIpProtocol(TCP_SHORT);
1450 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1452 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1454 if (0 != destPort) {
1455 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1457 matchBuilder.setLayer4Match(tcpmatch.build());
1458 } else if (UDP_SHORT == protocol) {
1459 ipmatch.setIpProtocol(UDP_SHORT);
1460 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1462 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1464 if (0 != destPort) {
1465 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1467 matchBuilder.setLayer4Match(udpMatch.build());
1468 } else if (SCTP_SHORT == protocol) {
1469 ipmatch.setIpProtocol(SCTP_SHORT);
1470 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
1472 sctpMatchBuilder.setSctpSourcePort(new PortNumber(srcPort));
1474 if (0 != destPort) {
1475 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destPort));
1477 matchBuilder.setLayer4Match(sctpMatchBuilder.build());
1479 matchBuilder.setIpMatch(ipmatch.build());
1481 return matchBuilder;
1485 * Add a layer4 match to an existing match with mask
1487 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match.
1488 * @param protocol The layer4 protocol
1489 * @param srcPort The src port
1490 * @param destPort The destination port
1491 * @param mask the mask for the port
1492 * @return matchBuilder Map Object with a match
1494 public static MatchBuilder addLayer4MatchWithMask(MatchBuilder matchBuilder,
1495 int protocol, int srcPort, int destPort,int mask) {
1497 IpMatchBuilder ipmatch = new IpMatchBuilder();
1499 NxAugMatchNodesNodeTableFlow nxAugMatch = null;
1500 GeneralAugMatchNodesNodeTableFlow genAugMatch = null;
1501 if (protocol == TCP_SHORT) {
1502 ipmatch.setIpProtocol(TCP_SHORT);
1504 NxmOfTcpSrcBuilder tcpSrc = new NxmOfTcpSrcBuilder();
1505 tcpSrc.setPort(new PortNumber(srcPort));
1506 tcpSrc.setMask(mask);
1507 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpSrc(tcpSrc.build()).build();
1508 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1509 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpSrcKey.class)
1510 .setExtension(new ExtensionBuilder()
1511 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1512 .build()).build())).build();
1513 } else if (0 != destPort) {
1514 NxmOfTcpDstBuilder tcpDst = new NxmOfTcpDstBuilder();
1515 tcpDst.setPort(new PortNumber(destPort));
1516 tcpDst.setMask(mask);
1517 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1518 .setNxmOfTcpDst(tcpDst.build())
1520 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1521 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpDstKey.class)
1522 .setExtension(new ExtensionBuilder()
1523 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1524 .build()).build())).build();
1527 } else if (UDP_SHORT == protocol) {
1528 ipmatch.setIpProtocol(UDP_SHORT);
1529 new UdpMatchBuilder();
1531 NxmOfUdpSrcBuilder udpSrc = new NxmOfUdpSrcBuilder();
1532 udpSrc.setPort(new PortNumber(srcPort));
1533 udpSrc.setMask(mask);
1534 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpSrc(udpSrc.build()).build();
1535 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1536 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpSrcKey.class)
1537 .setExtension(new ExtensionBuilder()
1538 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1539 .build()).build())).build();
1540 } else if (0 != destPort) {
1541 NxmOfUdpDstBuilder udpDst = new NxmOfUdpDstBuilder();
1542 udpDst.setPort(new PortNumber(destPort));
1543 udpDst.setMask(mask);
1544 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1545 .setNxmOfUdpDst(udpDst.build())
1547 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1548 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpDstKey.class)
1549 .setExtension(new ExtensionBuilder()
1550 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1551 .build()).build())).build();
1554 matchBuilder.setIpMatch(ipmatch.build());
1555 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1556 return matchBuilder;
1559 public static MatchBuilder addCtState(MatchBuilder matchBuilder,int ct_state, int mask) {
1560 NxmNxCtStateBuilder ctStateBuilder = new NxmNxCtStateBuilder();
1561 ctStateBuilder.setCtState((long)ct_state);
1562 ctStateBuilder.setMask((long)mask);
1563 NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1564 .setNxmNxCtState(ctStateBuilder.build())
1566 GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1567 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtStateKey.class)
1568 .setExtension(new ExtensionBuilder()
1569 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1570 .build()).build())).build();
1571 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1572 return matchBuilder;
1575 public static MatchBuilder addCtZone(MatchBuilder matchBuilder,int ct_zone) {
1576 NxmNxCtZoneBuilder ctZoneBuilder = new NxmNxCtZoneBuilder();
1577 ctZoneBuilder.setCtZone(ct_zone);
1578 NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1579 .setNxmNxCtZone(ctZoneBuilder.build())
1581 GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1582 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtZoneKey.class)
1583 .setExtension(new ExtensionBuilder()
1584 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1585 .build()).build())).build();
1586 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1587 return matchBuilder;
1590 public static class RegMatch {
1591 final Class<? extends NxmNxReg> reg;
1593 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1598 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1599 return new RegMatch(reg, value);
1603 public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1604 List<ExtensionList> extensions = new ArrayList<>();
1605 for (RegMatch rm : matches) {
1606 Class<? extends ExtensionKey> key;
1607 if (NxmNxReg0.class.equals(rm.reg)) {
1608 key = NxmNxReg0Key.class;
1609 } else if (NxmNxReg1.class.equals(rm.reg)) {
1610 key = NxmNxReg1Key.class;
1611 } else if (NxmNxReg2.class.equals(rm.reg)) {
1612 key = NxmNxReg2Key.class;
1613 } else if (NxmNxReg3.class.equals(rm.reg)) {
1614 key = NxmNxReg3Key.class;
1615 } else if (NxmNxReg4.class.equals(rm.reg)) {
1616 key = NxmNxReg4Key.class;
1617 } else if (NxmNxReg5.class.equals(rm.reg)) {
1618 key = NxmNxReg5Key.class;
1619 } else if (NxmNxReg6.class.equals(rm.reg)) {
1620 key = NxmNxReg6Key.class;
1622 key = NxmNxReg7Key.class;
1624 NxAugMatchNodesNodeTableFlow am =
1625 new NxAugMatchNodesNodeTableFlowBuilder()
1626 .setNxmNxReg(new NxmNxRegBuilder()
1631 extensions.add(new ExtensionListBuilder()
1632 .setExtensionKey(key)
1633 .setExtension(new ExtensionBuilder()
1634 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1638 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1639 .setExtensionList(extensions)
1641 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1642 return matchBuilder;
1645 public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1646 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1647 .setNxmNxTunId(new NxmNxTunIdBuilder()
1648 .setValue(BigInteger.valueOf(tunId))
1651 GeneralAugMatchNodesNodeTableFlow m =
1652 new GeneralAugMatchNodesNodeTableFlowBuilder()
1653 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1654 .setExtensionKey(NxmNxTunIdKey.class)
1655 .setExtension(new ExtensionBuilder()
1656 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1660 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1661 return matchBuilder;
1664 public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1665 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1666 .setNxmNxNsp(new NxmNxNspBuilder()
1670 addExtension(matchBuilder, NxmNxNspKey.class, am);
1671 return matchBuilder;
1674 public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1675 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1676 .setNxmNxNsi(new NxmNxNsiBuilder()
1680 addExtension(matchBuilder, NxmNxNsiKey.class, am);
1681 return matchBuilder;
1684 private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1685 NxAugMatchNodesNodeTableFlow am) {
1686 GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1687 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1688 List<ExtensionList> extensions = null;
1689 if (existingAugmentations != null ) {
1690 extensions = existingAugmentations.getExtensionList();
1692 if (extensions == null) {
1693 extensions = new ArrayList<>();
1696 extensions.add(new ExtensionListBuilder()
1697 .setExtensionKey(extensionKey)
1698 .setExtension(new ExtensionBuilder()
1699 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1703 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1704 .setExtensionList(extensions)
1706 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1709 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1712 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1713 if (srcMac != null) {
1714 emb.setEthernetSource(new EthernetSourceBuilder()
1718 if (dstMac != null) {
1719 emb.setEthernetDestination(new EthernetDestinationBuilder()
1723 if (etherType != null) {
1724 emb.setEthernetType(new EthernetTypeBuilder()
1725 .setType(new EtherType(etherType))
1732 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1734 * @param ipv4AddressString the ip address, in string format
1735 * @return Ipv4Prefix with ipv4Address and /32 mask
1737 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1738 return new Ipv4Prefix(ipv4AddressString + "/32");
1742 * Create ipv6 prefix from ipv6 address, by appending /128 mask
1744 * @param ipv6AddressString the ip address, in string format
1745 * @return Ipv6Prefix with ipv6Address and /128 mask
1747 public static Ipv6Prefix iPv6PrefixFromIPv6Address(String ipv6AddressString) {
1748 return new Ipv6Prefix(ipv6AddressString + "/128");
1752 * Converts port range into a set of masked port ranges.
1754 * @param portMin the strating port of the range.
1755 * @param portMax the ending port of the range.
1756 * @return the map contianing the port no and their mask.
1759 public static Map<Integer,Integer> getLayer4MaskForRange(int portMin, int portMax) {
1760 int [] offset = {32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
1761 int[] mask = {0x8000,0xC000,0xE000,0xF000,0xF800,0xFC00,0xFE00,0xFF00,
1762 0xFF80,0xFFC0,0xFFE0,0xFFF0,0xFFF8,0xFFFC,0xFFFE,0xFFFF};
1763 int noOfPorts = portMax - portMin + 1;
1764 String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
1765 int medianOffset = 16 - binaryNoOfPorts.length();
1766 int medianLength = offset[medianOffset];
1768 for (int tempMedian = 0;tempMedian < portMax;) {
1769 tempMedian = medianLength + tempMedian;
1770 if (portMin < tempMedian) {
1771 median = tempMedian;
1775 Map<Integer,Integer> portMap = new HashMap<>();
1777 int currentMedain = median;
1778 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1779 tempMedian = currentMedain - offset[tempMedianOffset];
1780 if (portMin <= tempMedian) {
1781 for (;portMin <= tempMedian;) {
1782 portMap.put(tempMedian, mask[tempMedianOffset]);
1783 currentMedain = tempMedian;
1784 tempMedian = tempMedian - offset[tempMedianOffset];
1788 currentMedain = median;
1789 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1790 tempMedian = currentMedain + offset[tempMedianOffset];
1791 if (portMax >= tempMedian - 1) {
1792 for (;portMax >= tempMedian - 1;) {
1793 portMap.put(currentMedain, mask[tempMedianOffset]);
1794 currentMedain = tempMedian;
1795 tempMedian = tempMedian + offset[tempMedianOffset];
1803 * Return Long that represents OF port for strings where OF is explicitly provided
1805 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1806 * @return the OFport or null
1808 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1809 if (ofPortIdentifier != null) {
1810 String[] pair = ofPortIdentifier.split("\\|");
1811 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1812 return new Long(pair[1]);