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.Dscp;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.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.ethernet.match.fields.EthernetDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv6MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfEthDst;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtStateKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtZoneKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpDstKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpSrcKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.state.grouping.NxmNxCtStateBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.zone.grouping.NxmNxCtZoneBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNspKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNsiKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.eth.dst.grouping.NxmOfEthDstBuilder;
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 import com.google.common.collect.ImmutableList;
94 import com.google.common.collect.Lists;
96 public class MatchUtils {
97 private static final Logger LOG = LoggerFactory.getLogger(MatchUtils.class);
98 public static final short ICMP_SHORT = 1;
99 public static final short TCP_SHORT = 6;
100 public static final short UDP_SHORT = 17;
101 public static final short SCTP_SHORT = 132;
102 public static final String TCP = "tcp";
103 public static final String UDP = "udp";
104 private static final int TCP_SYN = 0x0002;
105 public static final String ICMP = "icmp";
106 public static final String ICMPV6 = "icmpv6";
107 public static final short ALL_ICMP = -1;
108 public static final long ETHERTYPE_IPV4 = 0x0800;
109 public static final long ETHERTYPE_IPV6 = 0x86dd;
110 public static final int UNTRACKED_CT_STATE = 0x00;
111 public static final int UNTRACKED_CT_STATE_MASK = 0x20;
112 public static final int TRACKED_EST_CT_STATE = 0x22;
113 public static final int TRACKED_EST_CT_STATE_MASK = 0x22;
114 public static final int TRACKED_NEW_CT_STATE = 0x21;
115 public static final int TRACKED_NEW_CT_STATE_MASK = 0x21;
116 public static final int NEW_CT_STATE = 0x01;
117 public static final int NEW_CT_STATE_MASK = 0x01;
120 * Create Ingress Port Match dpidLong, inPort
122 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
123 * @param dpidLong Long the datapath ID of a switch/node
124 * @param inPort Long ingress port on a switch
125 * @return matchBuilder Map MatchBuilder Object with a match
127 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
129 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
130 LOG.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
131 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
132 matchBuilder.setInPort(ncid);
137 public static MatchBuilder createInPortReservedMatch(MatchBuilder matchBuilder, Long dpidLong, String inPort) {
139 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
140 LOG.debug("createInPortResrevedMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
142 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
143 matchBuilder.setInPort(ncid);
149 * Create EtherType Match
151 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
152 * @param etherType Long EtherType
153 * @return matchBuilder Map MatchBuilder Object with a match
155 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
157 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
158 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
159 ethTypeBuilder.setType(new EtherType(etherType));
160 ethernetMatch.setEthernetType(ethTypeBuilder.build());
161 matchBuilder.setEthernetMatch(ethernetMatch.build());
166 public static MatchBuilder createEthSrcDstMatch(MatchBuilder matchBuilder, MacAddress srcMac, MacAddress dstMac) {
167 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
168 if (srcMac != null) {
169 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
170 ethSourceBuilder.setAddress(new MacAddress(srcMac));
171 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
173 if (dstMac != null) {
174 EthernetDestinationBuilder ethDestinationBuild = new EthernetDestinationBuilder();
175 ethDestinationBuild.setAddress(new MacAddress(dstMac));
176 ethernetMatch.setEthernetDestination(ethDestinationBuild.build());
178 if (matchBuilder.getEthernetMatch() != null && matchBuilder.getEthernetMatch().getEthernetType() != null) {
179 ethernetMatch.setEthernetType(matchBuilder.getEthernetMatch().getEthernetType());
182 matchBuilder.setEthernetMatch(ethernetMatch.build());
188 * Create Ethernet Source Match
190 * @param matchBuilder MatchBuilder Object without a match yet
191 * @param sMacAddr String representing a source MAC
192 * @return matchBuilder Map MatchBuilder Object with a match
194 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
196 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
197 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
198 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
199 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
200 matchBuilder.setEthernetMatch(ethernetMatch.build());
206 * Create Ethernet Destination Match
208 * @param matchBuilder MatchBuilder Object without a match yet
209 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
210 * @return matchBuilder Map MatchBuilder Object with a match
212 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
213 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
214 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
215 vlanIdBuilder.setVlanId(new VlanId(vlanId));
216 vlanIdBuilder.setVlanIdPresent(present);
217 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
218 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
224 * Create Ethernet Destination Match
226 * @param matchBuilder MatchBuilder Object without a match yet
227 * @param dMacAddr String representing a destination MAC
228 * @return matchBuilder Map MatchBuilder Object with a match
230 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
232 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
233 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
234 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
236 ethDestinationBuilder.setMask(mask);
238 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
239 matchBuilder.setEthernetMatch(ethernetMatch.build());
245 * Tunnel ID Match Builder
247 * @param matchBuilder MatchBuilder Object without a match yet
248 * @param tunnelId BigInteger representing a tunnel ID
249 * @return matchBuilder Map MatchBuilder Object with a match
251 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
253 TunnelBuilder tunnelBuilder = new TunnelBuilder();
254 tunnelBuilder.setTunnelId(tunnelId);
255 matchBuilder.setTunnel(tunnelBuilder.build());
261 * Match ICMP code and type
263 * @param matchBuilder MatchBuilder Object
264 * @param type short representing an ICMP type
265 * @param code short representing an ICMP code
266 * @return matchBuilder Map MatchBuilder Object with a match
268 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
270 // Build the IPv4 Match requied per OVS Syntax
271 IpMatchBuilder ipmatch = new IpMatchBuilder();
272 ipmatch.setIpProtocol((short) 1);
273 matchBuilder.setIpMatch(ipmatch.build());
275 // Build the ICMPv4 Match
276 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
277 if (type != ALL_ICMP) {
278 icmpv4match.setIcmpv4Type(type);
280 if (code != ALL_ICMP) {
281 icmpv4match.setIcmpv4Code(code);
283 matchBuilder.setIcmpv4Match(icmpv4match.build());
289 * Match ICMPv6 code and type
291 * @param matchBuilder MatchBuilder Object
292 * @param type short representing an ICMP type
293 * @param code short representing an ICMP code
294 * @return matchBuilder Map MatchBuilder Object with a match
296 public static MatchBuilder createICMPv6Match(MatchBuilder matchBuilder, short type, short code) {
298 // Build the IPv6 Match required per OVS Syntax
299 IpMatchBuilder ipmatch = new IpMatchBuilder();
300 ipmatch.setIpProtocol((short) 58);
301 matchBuilder.setIpMatch(ipmatch.build());
303 // Build the ICMPv6 Match
304 Icmpv6MatchBuilder icmpv6match = new Icmpv6MatchBuilder();
305 if (type != ALL_ICMP || code != ALL_ICMP) {
306 icmpv6match.setIcmpv6Type(type);
307 icmpv6match.setIcmpv6Code(code);
309 matchBuilder.setIcmpv6Match(icmpv6match.build());
315 * @param matchBuilder MatchBuilder Object without a match yet
316 * @param dstip String containing an IPv4 prefix
317 * @return matchBuilder Map Object with a match
319 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
321 EthernetMatchBuilder eth = new EthernetMatchBuilder();
322 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
323 ethTypeBuilder.setType(new EtherType(0x0800L));
324 eth.setEthernetType(ethTypeBuilder.build());
325 matchBuilder.setEthernetMatch(eth.build());
327 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
328 ipv4match.setIpv4Destination(dstip);
330 matchBuilder.setLayer3Match(ipv4match.build());
337 * @param matchBuilder MatchBuilder Object without a match yet
338 * @param dstip String containing an IPv4 prefix
339 * @return matchBuilder Map Object with a match
341 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
342 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
343 arpDstMatch.setArpTargetTransportAddress(dstip)
344 .setArpOp(FlowUtils.ARP_OP_REQUEST);
345 matchBuilder.setLayer3Match(arpDstMatch.build());
351 * @param matchBuilder MatchBuilder Object without a match yet
352 * @param srcip String containing an IPv4 prefix
353 * @return matchBuilder Map Object with a match
355 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
357 EthernetMatchBuilder eth = new EthernetMatchBuilder();
358 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
359 ethTypeBuilder.setType(new EtherType(0x0800L));
360 eth.setEthernetType(ethTypeBuilder.build());
361 matchBuilder.setEthernetMatch(eth.build());
363 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
364 ipv4match.setIpv4Source(srcip);
365 matchBuilder.setLayer3Match(ipv4match.build());
372 * Create Source TCP Port Match
374 * @param matchBuilder MatchBuilder Object without a match yet
375 * @param tcpport Integer representing a source TCP port
376 * @return matchBuilder Map MatchBuilder Object with a match
378 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
380 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
381 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
382 ethTypeBuilder.setType(new EtherType(0x0800L));
383 ethType.setEthernetType(ethTypeBuilder.build());
384 matchBuilder.setEthernetMatch(ethType.build());
386 IpMatchBuilder ipmatch = new IpMatchBuilder();
387 ipmatch.setIpProtocol((short) 6);
388 matchBuilder.setIpMatch(ipmatch.build());
390 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
391 tcpmatch.setTcpSourcePort(tcpport);
392 matchBuilder.setLayer4Match(tcpmatch.build());
399 * Create Destination TCP Port Match
401 * @param matchBuilder MatchBuilder Object without a match yet
402 * @param tcpDstPort Integer representing a destination TCP port
403 * @return matchBuilder Map MatchBuilder Object with a match
405 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
407 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
408 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
409 ethTypeBuilder.setType(new EtherType(0x0800L));
410 ethType.setEthernetType(ethTypeBuilder.build());
411 matchBuilder.setEthernetMatch(ethType.build());
413 IpMatchBuilder ipmatch = new IpMatchBuilder();
414 ipmatch.setIpProtocol((short) 6);
415 matchBuilder.setIpMatch(ipmatch.build());
417 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
418 tcpmatch.setTcpDestinationPort(tcpDstPort);
419 matchBuilder.setLayer4Match(tcpmatch.build());
425 * Test match for TCP_Flags
427 * @param matchBuilder MatchBuilder Object without a match yet
428 * @param tcpPort PortNumber representing a destination TCP port
429 * @param tcpFlag int representing a tcp_flag
430 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
432 * Defined TCP Flag values in OVS v2.1+
433 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
434 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
435 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
437 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
440 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
441 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
442 ethTypeBuilder.setType(new EtherType(0x0800L));
443 ethernetType.setEthernetType(ethTypeBuilder.build());
444 matchBuilder.setEthernetMatch(ethernetType.build());
446 // TCP Protocol Match
447 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
448 ipMatch.setIpProtocol((short) 6);
449 matchBuilder.setIpMatch(ipMatch.build());
452 PortNumber dstPort = new PortNumber(tcpPort);
453 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
454 tcpMatch.setTcpDestinationPort(dstPort);
455 matchBuilder.setLayer4Match(tcpMatch.build());
457 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
458 tcpFlagMatch.setTcpFlag(tcpFlag);
459 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
464 * @return MatchBuilder containing the metadata match values
466 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
468 // metadata matchbuilder
469 MetadataBuilder metadata = new MetadataBuilder();
470 metadata.setMetadata(metaData);
471 // Optional metadata mask
472 if (metaDataMask != null) {
473 metadata.setMetadataMask(metaDataMask);
475 matchBuilder.setMetadata(metadata.build());
481 * Create TCP Port Match
483 * @param matchBuilder MatchBuilder Object without a match yet
484 * @param ipProtocol Integer representing the IP protocol
485 * @return matchBuilder Map MatchBuilder Object with a match
487 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
489 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
490 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
491 ethTypeBuilder.setType(new EtherType(0x0800L));
492 ethType.setEthernetType(ethTypeBuilder.build());
493 matchBuilder.setEthernetMatch(ethType.build());
495 IpMatchBuilder ipMmatch = new IpMatchBuilder();
496 if (ipProtocol == TCP_SHORT) {
497 ipMmatch.setIpProtocol(TCP_SHORT);
499 else if (ipProtocol == UDP_SHORT) {
500 ipMmatch.setIpProtocol(UDP_SHORT);
502 else if (ipProtocol == ICMP_SHORT) {
503 ipMmatch.setIpProtocol(ICMP_SHORT);
505 matchBuilder.setIpMatch(ipMmatch.build());
510 * Create TCP Port Match
512 * @param matchBuilder MatchBuilder Object without a match yet
513 * @param ipProtocol Integer representing the IP protocol
514 * @return matchBuilder Map MatchBuilder Object with a match
516 public static MatchBuilder createIpv6ProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
518 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
519 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
520 ethTypeBuilder.setType(new EtherType(0x86DDL));
521 ethType.setEthernetType(ethTypeBuilder.build());
522 matchBuilder.setEthernetMatch(ethType.build());
524 IpMatchBuilder ipMmatch = new IpMatchBuilder();
525 if (ipProtocol == TCP_SHORT) {
526 ipMmatch.setIpProtocol(TCP_SHORT);
528 else if (ipProtocol == UDP_SHORT) {
529 ipMmatch.setIpProtocol(UDP_SHORT);
531 else if (ipProtocol == ICMP_SHORT) {
532 ipMmatch.setIpProtocol(ICMP_SHORT);
534 matchBuilder.setIpMatch(ipMmatch.build());
539 * Create tcp syn with proto match.
541 * @param matchBuilder the match builder
542 * @return matchBuilder match builder
544 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
547 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
548 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
549 ethTypeBuilder.setType(new EtherType(0x0800L));
550 ethernetType.setEthernetType(ethTypeBuilder.build());
551 matchBuilder.setEthernetMatch(ethernetType.build());
553 // TCP Protocol Match
554 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
555 ipMatch.setIpProtocol((short) 6);
556 matchBuilder.setIpMatch(ipMatch.build());
558 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
559 tcpFlagMatch.setTcpFlag(TCP_SYN);
560 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
565 * Create tcp proto syn match.
567 * @param matchBuilder the match builder
568 * @return matchBuilder match builder
570 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
572 // TCP Protocol Match
573 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
574 ipMatch.setIpProtocol((short) 6);
575 matchBuilder.setIpMatch(ipMatch.build());
577 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
578 tcpFlagMatch.setTcpFlag(TCP_SYN);
579 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
584 * Create dmac tcp port with flag match.
586 * @param matchBuilder the match builder
587 * @param attachedMac the attached mac
588 * @param tcpFlag the tcp flag
589 * @param tunnelID the tunnel iD
590 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
592 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
593 String attachedMac, Integer tcpFlag, String tunnelID) {
594 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, tcpFlag, null, tunnelID);
598 * Create dmac ipSa match.
600 * @param matchBuilder the match builder
601 * @param attachedMac the attached mac
602 * @param ipPrefix the src ipPrefix
603 * @param tunnelID the tunnel iD
604 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
606 public static MatchBuilder createDmacIpSaMatch(
607 MatchBuilder matchBuilder, String attachedMac, Ipv4Prefix ipPrefix, String tunnelID) {
608 return createDmacTcpPortIpSaWithFlagMatch(matchBuilder, attachedMac, null, ipPrefix, tunnelID);
612 * Create dmac tcp port ipSa with flag match.
614 * @param matchBuilder the match builder
615 * @param attachedMac the attached mac
616 * @param tcpFlag the tcp flag
617 * @param ipPrefix the src ipPrefix
618 * @param tunnelID the tunnel iD
619 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN), Ip Source Address (IPsa)
621 public static MatchBuilder createDmacTcpPortIpSaWithFlagMatch(
622 MatchBuilder matchBuilder, String attachedMac, Integer tcpFlag, Ipv4Prefix ipPrefix, String tunnelID) {
624 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
625 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
626 ethTypeBuilder.setType(new EtherType(0x0800L));
627 ethernetMatch.setEthernetType(ethTypeBuilder.build());
629 if (attachedMac != null) {
630 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
631 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
632 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
633 matchBuilder.setEthernetMatch(ethernetMatch.build());
636 if (tcpFlag != null) {
637 // TCP Protocol Match
638 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
639 ipMatch.setIpProtocol(TCP_SHORT);
640 matchBuilder.setIpMatch(ipMatch.build());
642 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
643 tcpFlagMatch.setTcpFlag(tcpFlag);
644 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
647 if (tunnelID != null) {
648 TunnelBuilder tunnelBuilder = new TunnelBuilder();
649 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
650 matchBuilder.setTunnel(tunnelBuilder.build());
653 if (ipPrefix != null) {
654 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
655 ipv4match.setIpv4Source(ipPrefix);
656 matchBuilder.setLayer3Match(ipv4match.build());
663 * Create dmac tcp syn match.
665 * @param matchBuilder the match builder
666 * @param attachedMac the attached mac
667 * @param tcpPort the tcp port
668 * @param tcpFlag the tcp flag
669 * @param tunnelID the tunnel iD
670 * @return the match builder
672 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
673 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
675 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
676 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
677 ethTypeBuilder.setType(new EtherType(0x0800L));
678 ethernetMatch.setEthernetType(ethTypeBuilder.build());
680 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
681 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
682 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
683 matchBuilder.setEthernetMatch(ethernetMatch.build());
685 // TCP Protocol Match
686 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
687 ipMatch.setIpProtocol((short) 6);
688 matchBuilder.setIpMatch(ipMatch.build());
691 PortNumber dstPort = new PortNumber(tcpPort);
692 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
693 tcpMatch.setTcpDestinationPort(dstPort);
694 matchBuilder.setLayer4Match(tcpMatch.build());
696 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
697 tcpFlagMatch.setTcpFlag(tcpFlag);
698 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
700 TunnelBuilder tunnelBuilder = new TunnelBuilder();
701 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
702 matchBuilder.setTunnel(tunnelBuilder.build());
708 * Create dmac tcp syn dst ip prefix tcp port.
710 * @param matchBuilder the match builder
711 * @param attachedMac the attached mac
712 * @param tcpPort the tcp port
713 * @param tcpFlag the tcp flag
714 * @param segmentationId the segmentation id
715 * @param dstIp the dst ip
716 * @return the match builder
718 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
719 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
722 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
723 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
724 ethTypeBuilder.setType(new EtherType(0x0800L));
725 ethernetMatch.setEthernetType(ethTypeBuilder.build());
727 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
728 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
729 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
731 matchBuilder.setEthernetMatch(ethernetMatch.build());
733 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
734 ipv4match.setIpv4Destination(dstIp);
735 matchBuilder.setLayer3Match(ipv4match.build());
737 // TCP Protocol Match
738 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
739 ipMatch.setIpProtocol(TCP_SHORT);
740 matchBuilder.setIpMatch(ipMatch.build());
743 PortNumber dstPort = new PortNumber(tcpPort);
744 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
745 tcpMatch.setTcpDestinationPort(dstPort);
746 matchBuilder.setLayer4Match(tcpMatch.build());
748 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
749 tcpFlagMatch.setTcpFlag(tcpFlag);
750 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
752 TunnelBuilder tunnelBuilder = new TunnelBuilder();
753 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
754 matchBuilder.setTunnel(tunnelBuilder.build());
760 * Create dmac ip tcp syn match.
762 * @param matchBuilder the match builder
763 * @param dMacAddr the d mac addr
764 * @param mask the mask
765 * @param ipPrefix the ip prefix
766 * @return MatchBuilder containing the metadata match values
768 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
769 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
771 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
772 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
773 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
775 ethDestBuilder.setMask(mask);
777 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
778 matchBuilder.setEthernetMatch(ethernetMatch.build());
780 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
781 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
782 ethTypeBuilder.setType(new EtherType(0x0800L));
783 ethernetType.setEthernetType(ethTypeBuilder.build());
784 matchBuilder.setEthernetMatch(ethernetType.build());
785 if (ipPrefix != null) {
786 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
787 ipv4match.setIpv4Destination(ipPrefix);
788 matchBuilder.setLayer3Match(ipv4match.build());
790 // TCP Protocol Match
791 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
792 ipMatch.setIpProtocol(TCP_SHORT);
793 matchBuilder.setIpMatch(ipMatch.build());
795 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
796 tcpFlagMatch.setTcpFlag(TCP_SYN);
797 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
803 * Create smac tcp syn dst ip prefix tcp port.
805 * @param matchBuilder the match builder
806 * @param attachedMac the attached mac
807 * @param tcpPort the tcp port
808 * @param tcpFlag the tcp flag
809 * @param segmentationId the segmentation id
810 * @param dstIp the dst ip
811 * @return the match builder
813 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
814 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
816 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
817 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
818 ethTypeBuilder.setType(new EtherType(0x0800L));
819 ethernetMatch.setEthernetType(ethTypeBuilder.build());
821 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
822 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
823 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
825 matchBuilder.setEthernetMatch(ethernetMatch.build());
827 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
828 ipv4match.setIpv4Destination(dstIp);
829 matchBuilder.setLayer3Match(ipv4match.build());
831 // TCP Protocol Match
832 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
833 ipMatch.setIpProtocol(TCP_SHORT);
834 matchBuilder.setIpMatch(ipMatch.build());
837 PortNumber dstPort = new PortNumber(tcpPort);
838 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
839 tcpMatch.setTcpDestinationPort(dstPort);
840 matchBuilder.setLayer4Match(tcpMatch.build());
842 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
843 tcpFlagMatch.setTcpFlag(tcpFlag);
844 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
846 TunnelBuilder tunnelBuilder = new TunnelBuilder();
847 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
848 matchBuilder.setTunnel(tunnelBuilder.build());
854 * Create smac tcp port with flag match.
856 * @param matchBuilder the match builder
857 * @param attachedMac the attached mac
858 * @param tcpFlag the tcp flag
859 * @param tunnelID the tunnel iD
860 * @return matchBuilder
862 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
863 Integer tcpFlag, String tunnelID) {
865 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
866 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
867 ethTypeBuilder.setType(new EtherType(0x0800L));
868 ethernetMatch.setEthernetType(ethTypeBuilder.build());
870 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
871 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
872 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
873 matchBuilder.setEthernetMatch(ethernetMatch.build());
875 // TCP Protocol Match
876 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
877 ipMatch.setIpProtocol(TCP_SHORT);
878 matchBuilder.setIpMatch(ipMatch.build());
880 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
881 tcpFlagMatch.setTcpFlag(tcpFlag);
882 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
884 TunnelBuilder tunnelBuilder = new TunnelBuilder();
885 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
886 matchBuilder.setTunnel(tunnelBuilder.build());
892 * Create smac ip tcp syn match.
894 * @param matchBuilder the match builder
895 * @param dMacAddr the d mac addr
896 * @param mask the mask
897 * @param ipPrefix the ip prefix
898 * @return MatchBuilder containing the metadata match values
900 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
901 MacAddress mask, Ipv4Prefix ipPrefix) {
903 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
904 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
905 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
907 ethSrcBuilder.setMask(mask);
909 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
910 matchBuilder.setEthernetMatch(ethernetMatch.build());
912 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
913 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
914 ethTypeBuilder.setType(new EtherType(0x0800L));
915 ethernetType.setEthernetType(ethTypeBuilder.build());
916 matchBuilder.setEthernetMatch(ethernetType.build());
917 if (ipPrefix != null) {
918 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
919 ipv4match.setIpv4Destination(ipPrefix);
920 matchBuilder.setLayer3Match(ipv4match.build());
922 // TCP Protocol Match
923 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
924 ipMatch.setIpProtocol(TCP_SHORT);
925 matchBuilder.setIpMatch(ipMatch.build());
927 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
928 tcpFlagMatch.setTcpFlag(TCP_SYN);
929 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
935 * Create smac tcp syn.
937 * @param matchBuilder the match builder
938 * @param attachedMac the attached mac
939 * @param tcpPort the tcp port
940 * @param tcpFlag the tcp flag
941 * @param tunnelID the tunnel iD
942 * @return the match builder
944 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
945 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
947 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
948 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
949 ethTypeBuilder.setType(new EtherType(0x0800L));
950 ethernetMatch.setEthernetType(ethTypeBuilder.build());
952 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
953 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
954 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
955 matchBuilder.setEthernetMatch(ethernetMatch.build());
957 // TCP Protocol Match
958 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
959 ipMatch.setIpProtocol((short) 6);
960 matchBuilder.setIpMatch(ipMatch.build());
963 PortNumber dstPort = new PortNumber(tcpPort);
964 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
965 tcpMatch.setTcpDestinationPort(dstPort);
966 matchBuilder.setLayer4Match(tcpMatch.build());
969 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
970 tcpFlagMatch.setTcpFlag(tcpFlag);
971 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
973 TunnelBuilder tunnelBuilder = new TunnelBuilder();
974 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
975 matchBuilder.setTunnel(tunnelBuilder.build());
981 * @return MatchBuilder containing the metadata match values
983 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
984 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
986 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
987 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
988 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
990 ethDestinationBuilder.setMask(mask);
992 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
993 matchBuilder.setEthernetMatch(ethernetMatch.build());
995 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
996 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
997 ethTypeBuilder.setType(new EtherType(0x0800L));
998 ethernetType.setEthernetType(ethTypeBuilder.build());
999 matchBuilder.setEthernetMatch(ethernetType.build());
1000 if (ipPrefix != null) {
1001 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1002 ipv4match.setIpv4Source(ipPrefix);
1003 matchBuilder.setLayer3Match(ipv4match.build());
1005 // TCP Protocol Match
1006 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
1007 ipMatch.setIpProtocol(TCP_SHORT);
1008 matchBuilder.setIpMatch(ipMatch.build());
1010 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
1011 tcpFlagMatch.setTcpFlag(TCP_SYN);
1012 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
1014 return matchBuilder;
1018 * Create a DHCP match with port provided.
1020 * @param matchBuilder the match builder
1021 * @param srcPort the source port
1022 * @param dstPort the destination port
1023 * @return the DHCP match
1025 public static MatchBuilder createDhcpMatch(MatchBuilder matchBuilder,
1026 int srcPort, int dstPort) {
1028 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1029 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1030 ethTypeBuilder.setType(new EtherType(0x0800L));
1031 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1032 matchBuilder.setEthernetMatch(ethernetMatch.build());
1034 IpMatchBuilder ipmatch = new IpMatchBuilder();
1035 ipmatch.setIpProtocol(UDP_SHORT);
1036 matchBuilder.setIpMatch(ipmatch.build());
1038 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1039 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1040 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1041 matchBuilder.setLayer4Match(udpmatch.build());
1043 return matchBuilder;
1048 * Create a DHCP match with port provided.
1050 * @param matchBuilder the match builder
1051 * @param srcPort the source port
1052 * @param dstPort the destination port
1053 * @return the DHCP match
1055 public static MatchBuilder createDhcpv6Match(MatchBuilder matchBuilder,
1056 int srcPort, int dstPort) {
1058 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1059 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1060 ethTypeBuilder.setType(new EtherType(0x86DDL));
1061 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1062 matchBuilder.setEthernetMatch(ethernetMatch.build());
1064 IpMatchBuilder ipmatch = new IpMatchBuilder();
1065 ipmatch.setIpProtocol(UDP_SHORT);
1066 matchBuilder.setIpMatch(ipmatch.build());
1068 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1069 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1070 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1071 matchBuilder.setLayer4Match(udpmatch.build());
1073 return matchBuilder;
1078 * Creates DHCP server packet match with DHCP mac address and port.
1080 * @param matchBuilder the matchbuilder
1081 * @param dhcpServerMac MAc address of the DHCP server of the subnet
1082 * @param srcPort the source port
1083 * @param dstPort the destination port
1084 * @return the DHCP server match
1086 public static MatchBuilder createDhcpServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1089 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1090 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1091 ethTypeBuilder.setType(new EtherType(0x0800L));
1092 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1093 matchBuilder.setEthernetMatch(ethernetMatch.build());
1095 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1096 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1097 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1098 matchBuilder.setEthernetMatch(ethernetMatch.build());
1100 IpMatchBuilder ipmatch = new IpMatchBuilder();
1101 ipmatch.setIpProtocol(UDP_SHORT);
1102 matchBuilder.setIpMatch(ipmatch.build());
1104 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1105 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1106 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1107 matchBuilder.setLayer4Match(udpmatch.build());
1109 return matchBuilder;
1114 * Creates DHCPv6 server packet match with DHCP mac address and port.
1116 * @param matchBuilder the matchbuilder
1117 * @param dhcpServerMac MAc address of the DHCP server of the subnet
1118 * @param srcPort the source port
1119 * @param dstPort the destination port
1120 * @return the DHCP server match
1122 public static MatchBuilder createDhcpv6ServerMatch(MatchBuilder matchBuilder, String dhcpServerMac, int srcPort,
1125 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
1126 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1127 ethTypeBuilder.setType(new EtherType(0x86DDL));
1128 ethernetMatch.setEthernetType(ethTypeBuilder.build());
1129 matchBuilder.setEthernetMatch(ethernetMatch.build());
1131 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
1132 ethSourceBuilder.setAddress(new MacAddress(dhcpServerMac));
1133 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
1134 matchBuilder.setEthernetMatch(ethernetMatch.build());
1136 IpMatchBuilder ipmatch = new IpMatchBuilder();
1137 ipmatch.setIpProtocol(UDP_SHORT);
1138 matchBuilder.setIpMatch(ipmatch.build());
1140 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
1141 udpmatch.setUdpSourcePort(new PortNumber(srcPort));
1142 udpmatch.setUdpDestinationPort(new PortNumber(dstPort));
1143 matchBuilder.setLayer4Match(udpmatch.build());
1145 return matchBuilder;
1150 * Creates a Match with src ip address mac address set.
1151 * @param matchBuilder MatchBuilder Object
1152 * @param srcip String containing an IPv4 prefix
1153 * @param srcMac The source macAddress
1154 * @return matchBuilder Map Object with a match
1156 public static MatchBuilder createSrcL3Ipv4MatchWithMac(MatchBuilder matchBuilder, Ipv4Prefix srcip, MacAddress srcMac) {
1158 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
1159 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(srcip));
1160 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1161 ethTypeBuilder.setType(new EtherType(0x0800L));
1162 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1163 eth.setEthernetType(ethTypeBuilder.build());
1164 eth.setEthernetSource(new EthernetSourceBuilder()
1168 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
1169 matchBuilder.setEthernetMatch(eth.build());
1170 return matchBuilder;
1175 * Creates a Match with src ip address mac address set.
1176 * @param matchBuilder MatchBuilder Object
1177 * @param srcip String containing an IPv6 prefix
1178 * @param srcMac The source macAddress
1179 * @return matchBuilder Map Object with a match
1181 public static MatchBuilder createSrcL3Ipv6MatchWithMac(MatchBuilder matchBuilder, Ipv6Prefix srcip, MacAddress srcMac) {
1183 Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
1184 ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(srcip));
1185 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1186 ethTypeBuilder.setType(new EtherType(0x86DDL));
1187 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1188 eth.setEthernetType(ethTypeBuilder.build());
1189 eth.setEthernetSource(new EthernetSourceBuilder()
1193 matchBuilder.setLayer3Match(ipv6MatchBuilder.build());
1194 matchBuilder.setEthernetMatch(eth.build());
1195 return matchBuilder;
1200 * Creates a ether net match with ether type set to 0x0800L.
1201 * @param matchBuilder MatchBuilder Object
1202 * @param srcMac The source macAddress
1203 * @param dstMac The destination mac address
1204 * @return matchBuilder Map Object with a match
1206 public static MatchBuilder createV4EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1208 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1209 ethTypeBuilder.setType(new EtherType(0x0800L));
1210 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1211 eth.setEthernetType(ethTypeBuilder.build());
1212 if (null != srcMac) {
1213 eth.setEthernetSource(new EthernetSourceBuilder()
1214 .setAddress(new MacAddress(srcMac)).build());
1216 if (null != dstMac) {
1217 eth.setEthernetDestination(new EthernetDestinationBuilder()
1218 .setAddress(new MacAddress(dstMac)).build());
1220 matchBuilder.setEthernetMatch(eth.build());
1221 return matchBuilder;
1225 * Creates a ether net match with ether type set to 0x86DDL.
1226 * @param matchBuilder MatchBuilder Object
1227 * @param srcMac The source macAddress
1228 * @param dstMac The destination mac address
1229 * @return matchBuilder Map Object with a match
1231 public static MatchBuilder createV6EtherMatchWithType(MatchBuilder matchBuilder,String srcMac, String dstMac)
1233 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
1234 ethTypeBuilder.setType(new EtherType(0x86DDL));
1235 EthernetMatchBuilder eth = new EthernetMatchBuilder();
1236 eth.setEthernetType(ethTypeBuilder.build());
1237 if (null != srcMac) {
1238 eth.setEthernetSource(new EthernetSourceBuilder()
1239 .setAddress(new MacAddress(srcMac)).build());
1241 if (null != dstMac) {
1242 eth.setEthernetDestination(new EthernetDestinationBuilder()
1243 .setAddress(new MacAddress(dstMac)).build());
1245 matchBuilder.setEthernetMatch(eth.build());
1246 return matchBuilder;
1250 * Adds remote Ip prefix to existing match.
1251 * @param matchBuilder The match builder
1252 * @param sourceIpPrefix The source IP prefix
1253 * @param destIpPrefix The destination IP prefix
1254 * @return matchBuilder Map Object with a match
1256 public static MatchBuilder addRemoteIpPrefix(MatchBuilder matchBuilder,
1257 Ipv4Prefix sourceIpPrefix,Ipv4Prefix destIpPrefix) {
1258 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
1259 if (null != sourceIpPrefix) {
1260 ipv4match.setIpv4Source(sourceIpPrefix);
1262 if (null != destIpPrefix) {
1263 ipv4match.setIpv4Destination(destIpPrefix);
1265 matchBuilder.setLayer3Match(ipv4match.build());
1267 return matchBuilder;
1271 * Adds remote Ipv6 prefix to existing match.
1272 * @param matchBuilder The match builder
1273 * @param sourceIpPrefix The source IP prefix
1274 * @param destIpPrefix The destination IP prefix
1275 * @return matchBuilder Map Object with a match
1277 public static MatchBuilder addRemoteIpv6Prefix(MatchBuilder matchBuilder,
1278 Ipv6Prefix sourceIpPrefix,Ipv6Prefix destIpPrefix) {
1279 Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
1280 if (null != sourceIpPrefix) {
1281 ipv6match.setIpv6Source(sourceIpPrefix);
1283 if (null != destIpPrefix) {
1284 ipv6match.setIpv6Destination(destIpPrefix);
1286 matchBuilder.setLayer3Match(ipv6match.build());
1288 return matchBuilder;
1292 * Add a DSCP match to an existing match
1293 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1295 * @return {@link MatchBuilder}
1297 public static MatchBuilder addDscp(MatchBuilder matchBuilder, short dscpValue) {
1298 createEtherTypeMatch(matchBuilder, new EtherType(ETHERTYPE_IPV4));
1299 return matchBuilder.setIpMatch(
1300 new IpMatchBuilder()
1301 .setIpDscp(new Dscp(dscpValue))
1306 * Add a layer4 match to an existing match
1308 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match
1309 * @param protocol The layer4 protocol
1310 * @param srcPort The src port
1311 * @param destPort The destination port
1312 * @return matchBuilder Map Object with a match
1314 public static MatchBuilder addLayer4Match(MatchBuilder matchBuilder,
1315 int protocol, int srcPort, int destPort) {
1316 IpMatchBuilder ipmatch = new IpMatchBuilder();
1317 if (TCP_SHORT == protocol) {
1318 ipmatch.setIpProtocol(TCP_SHORT);
1319 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
1321 tcpmatch.setTcpSourcePort(new PortNumber(srcPort));
1323 if (0 != destPort) {
1324 tcpmatch.setTcpDestinationPort(new PortNumber(destPort));
1326 matchBuilder.setLayer4Match(tcpmatch.build());
1327 } else if (UDP_SHORT == protocol) {
1328 ipmatch.setIpProtocol(UDP_SHORT);
1329 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1331 udpMatch.setUdpSourcePort(new PortNumber(srcPort));
1333 if (0 != destPort) {
1334 udpMatch.setUdpDestinationPort(new PortNumber(destPort));
1336 matchBuilder.setLayer4Match(udpMatch.build());
1337 } else if (SCTP_SHORT == protocol) {
1338 ipmatch.setIpProtocol(SCTP_SHORT);
1339 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
1341 sctpMatchBuilder.setSctpSourcePort(new PortNumber(srcPort));
1343 if (0 != destPort) {
1344 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destPort));
1346 matchBuilder.setLayer4Match(sctpMatchBuilder.build());
1348 matchBuilder.setIpMatch(ipmatch.build());
1350 return matchBuilder;
1354 * Add a layer4 match to an existing match with mask
1356 * @param matchBuilder Map matchBuilder MatchBuilder Object with a match.
1357 * @param protocol The layer4 protocol
1358 * @param srcPort The src port
1359 * @param destPort The destination port
1360 * @param mask the mask for the port
1361 * @return matchBuilder Map Object with a match
1363 public static MatchBuilder addLayer4MatchWithMask(MatchBuilder matchBuilder,
1364 int protocol, int srcPort, int destPort,int mask) {
1366 IpMatchBuilder ipmatch = new IpMatchBuilder();
1368 NxAugMatchNodesNodeTableFlow nxAugMatch = null;
1369 GeneralAugMatchNodesNodeTableFlow genAugMatch = null;
1370 if (protocol == TCP_SHORT) {
1371 ipmatch.setIpProtocol(TCP_SHORT);
1373 NxmOfTcpSrcBuilder tcpSrc = new NxmOfTcpSrcBuilder();
1374 tcpSrc.setPort(new PortNumber(srcPort));
1375 tcpSrc.setMask(mask);
1376 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpSrc(tcpSrc.build()).build();
1377 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1378 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpSrcKey.class)
1379 .setExtension(new ExtensionBuilder()
1380 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1381 .build()).build())).build();
1382 } else if (0 != destPort) {
1383 NxmOfTcpDstBuilder tcpDst = new NxmOfTcpDstBuilder();
1384 tcpDst.setPort(new PortNumber(destPort));
1385 tcpDst.setMask(mask);
1386 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1387 .setNxmOfTcpDst(tcpDst.build())
1389 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1390 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfTcpDstKey.class)
1391 .setExtension(new ExtensionBuilder()
1392 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1393 .build()).build())).build();
1396 } else if (UDP_SHORT == protocol) {
1397 ipmatch.setIpProtocol(UDP_SHORT);
1398 UdpMatchBuilder udpMatch = new UdpMatchBuilder();
1400 NxmOfUdpSrcBuilder udpSrc = new NxmOfUdpSrcBuilder();
1401 udpSrc.setPort(new PortNumber(srcPort));
1402 udpSrc.setMask(mask);
1403 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpSrc(udpSrc.build()).build();
1404 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1405 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpSrcKey.class)
1406 .setExtension(new ExtensionBuilder()
1407 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1408 .build()).build())).build();
1409 } else if (0 != destPort) {
1410 NxmOfUdpDstBuilder udpDst = new NxmOfUdpDstBuilder();
1411 udpDst.setPort(new PortNumber(destPort));
1412 udpDst.setMask(mask);
1413 nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1414 .setNxmOfUdpDst(udpDst.build())
1416 genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1417 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmOfUdpDstKey.class)
1418 .setExtension(new ExtensionBuilder()
1419 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1420 .build()).build())).build();
1423 matchBuilder.setIpMatch(ipmatch.build());
1424 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1425 return matchBuilder;
1428 public static MatchBuilder addCtState(MatchBuilder matchBuilder,int ct_state, int mask) {
1429 NxmNxCtStateBuilder ctStateBuilder = new NxmNxCtStateBuilder();
1430 ctStateBuilder.setCtState((long)ct_state);
1431 ctStateBuilder.setMask((long)mask);
1432 NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1433 .setNxmNxCtState(ctStateBuilder.build())
1435 GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1436 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtStateKey.class)
1437 .setExtension(new ExtensionBuilder()
1438 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1439 .build()).build())).build();
1440 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1441 return matchBuilder;
1444 public static MatchBuilder addCtZone(MatchBuilder matchBuilder,int ct_zone) {
1445 NxmNxCtZoneBuilder ctZoneBuilder = new NxmNxCtZoneBuilder();
1446 ctZoneBuilder.setCtZone(ct_zone);
1447 NxAugMatchNodesNodeTableFlow nxAugMatch = new NxAugMatchNodesNodeTableFlowBuilder()
1448 .setNxmNxCtZone(ctZoneBuilder.build())
1450 GeneralAugMatchNodesNodeTableFlow genAugMatch = new GeneralAugMatchNodesNodeTableFlowBuilder()
1451 .setExtensionList(ImmutableList.of(new ExtensionListBuilder().setExtensionKey(NxmNxCtZoneKey.class)
1452 .setExtension(new ExtensionBuilder()
1453 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch)
1454 .build()).build())).build();
1455 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
1456 return matchBuilder;
1459 public static class RegMatch {
1460 final Class<? extends NxmNxReg> reg;
1462 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
1467 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
1468 return new RegMatch(reg, value);
1472 public static MatchBuilder addNxRegMatch(MatchBuilder matchBuilder, RegMatch... matches) {
1473 List<ExtensionList> extensions = new ArrayList<>();
1474 for (RegMatch rm : matches) {
1475 Class<? extends ExtensionKey> key;
1476 if (NxmNxReg0.class.equals(rm.reg)) {
1477 key = NxmNxReg0Key.class;
1478 } else if (NxmNxReg1.class.equals(rm.reg)) {
1479 key = NxmNxReg1Key.class;
1480 } else if (NxmNxReg2.class.equals(rm.reg)) {
1481 key = NxmNxReg2Key.class;
1482 } else if (NxmNxReg3.class.equals(rm.reg)) {
1483 key = NxmNxReg3Key.class;
1484 } else if (NxmNxReg4.class.equals(rm.reg)) {
1485 key = NxmNxReg4Key.class;
1486 } else if (NxmNxReg5.class.equals(rm.reg)) {
1487 key = NxmNxReg5Key.class;
1488 } else if (NxmNxReg6.class.equals(rm.reg)) {
1489 key = NxmNxReg6Key.class;
1491 key = NxmNxReg7Key.class;
1493 NxAugMatchNodesNodeTableFlow am =
1494 new NxAugMatchNodesNodeTableFlowBuilder()
1495 .setNxmNxReg(new NxmNxRegBuilder()
1500 extensions.add(new ExtensionListBuilder()
1501 .setExtensionKey(key)
1502 .setExtension(new ExtensionBuilder()
1503 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1507 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1508 .setExtensionList(extensions)
1510 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1511 return matchBuilder;
1514 public static MatchBuilder addNxTunIdMatch(MatchBuilder matchBuilder, int tunId) {
1515 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1516 .setNxmNxTunId(new NxmNxTunIdBuilder()
1517 .setValue(BigInteger.valueOf(tunId))
1520 GeneralAugMatchNodesNodeTableFlow m =
1521 new GeneralAugMatchNodesNodeTableFlowBuilder()
1522 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
1523 .setExtensionKey(NxmNxTunIdKey.class)
1524 .setExtension(new ExtensionBuilder()
1525 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1529 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1530 return matchBuilder;
1533 public static MatchBuilder addNxNspMatch(MatchBuilder matchBuilder, long nsp) {
1534 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1535 .setNxmNxNsp(new NxmNxNspBuilder()
1539 addExtension(matchBuilder, NxmNxNspKey.class, am);
1540 return matchBuilder;
1543 public static MatchBuilder addNxNsiMatch(MatchBuilder matchBuilder, short nsi) {
1544 NxAugMatchNodesNodeTableFlow am = new NxAugMatchNodesNodeTableFlowBuilder()
1545 .setNxmNxNsi(new NxmNxNsiBuilder()
1549 addExtension(matchBuilder, NxmNxNsiKey.class, am);
1550 return matchBuilder;
1553 private static void addExtension(MatchBuilder matchBuilder, Class<? extends ExtensionKey> extensionKey,
1554 NxAugMatchNodesNodeTableFlow am) {
1555 GeneralAugMatchNodesNodeTableFlow existingAugmentations =
1556 matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
1557 List<ExtensionList> extensions = null;
1558 if (existingAugmentations != null ) {
1559 extensions = existingAugmentations.getExtensionList();
1561 if (extensions == null) {
1562 extensions = Lists.newArrayList();
1565 extensions.add(new ExtensionListBuilder()
1566 .setExtensionKey(extensionKey)
1567 .setExtension(new ExtensionBuilder()
1568 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
1572 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
1573 .setExtensionList(extensions)
1575 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
1578 public static EthernetMatch ethernetMatch(MacAddress srcMac,
1581 EthernetMatchBuilder emb = new EthernetMatchBuilder();
1582 if (srcMac != null) {
1583 emb.setEthernetSource(new EthernetSourceBuilder()
1587 if (dstMac != null) {
1588 emb.setEthernetDestination(new EthernetDestinationBuilder()
1592 if (etherType != null) {
1593 emb.setEthernetType(new EthernetTypeBuilder()
1594 .setType(new EtherType(etherType))
1601 * Create ipv4 prefix from ipv4 address, by appending /32 mask
1603 * @param ipv4AddressString the ip address, in string format
1604 * @return Ipv4Prefix with ipv4Address and /32 mask
1606 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1607 return new Ipv4Prefix(ipv4AddressString + "/32");
1611 * Create ipv6 prefix from ipv6 address, by appending /128 mask
1613 * @param ipv6AddressString the ip address, in string format
1614 * @return Ipv6Prefix with ipv6Address and /128 mask
1616 public static Ipv6Prefix iPv6PrefixFromIPv6Address(String ipv6AddressString) {
1617 return new Ipv6Prefix(ipv6AddressString + "/128");
1621 * Converts port range into a set of masked port ranges.
1623 * @param portMin the strating port of the range.
1624 * @param portMax the ending port of the range.
1625 * @return the map contianing the port no and their mask.
1628 public static Map<Integer,Integer> getLayer4MaskForRange(int portMin, int portMax) {
1629 int [] offset = {32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
1630 int[] mask = {0x8000,0xC000,0xE000,0xF000,0xF800,0xFC00,0xFE00,0xFF00,
1631 0xFF80,0xFFC0,0xFFE0,0xFFF0,0xFFF8,0xFFFC,0xFFFE,0xFFFF};
1632 int noOfPorts = portMax - portMin + 1;
1633 String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
1634 int medianOffset = 16 - binaryNoOfPorts.length();
1635 int medianLength = offset[medianOffset];
1637 for (int tempMedian = 0;tempMedian < portMax;) {
1638 tempMedian = medianLength + tempMedian;
1639 if (portMin < tempMedian) {
1640 median = tempMedian;
1644 Map<Integer,Integer> portMap = new HashMap<Integer,Integer>();
1646 int currentMedain = median;
1647 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1648 tempMedian = currentMedain - offset[tempMedianOffset];
1649 if (portMin <= tempMedian) {
1650 for (;portMin <= tempMedian;) {
1651 portMap.put(tempMedian, mask[tempMedianOffset]);
1652 currentMedain = tempMedian;
1653 tempMedian = tempMedian - offset[tempMedianOffset];
1657 currentMedain = median;
1658 for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
1659 tempMedian = currentMedain + offset[tempMedianOffset];
1660 if (portMax >= tempMedian - 1) {
1661 for (;portMax >= tempMedian - 1;) {
1662 portMap.put(currentMedain, mask[tempMedianOffset]);
1663 currentMedain = tempMedian;
1664 tempMedian = tempMedian + offset[tempMedianOffset];
1672 * Return Long that represents OF port for strings where OF is explicitly provided
1674 * @param ofPortIdentifier the string with encoded OF port (example format "OFPort|999")
1675 * @return the OFport or null
1677 public static Long parseExplicitOFPort(String ofPortIdentifier) {
1678 if (ofPortIdentifier != null) {
1679 String[] pair = ofPortIdentifier.split("\\|");
1680 if ((pair.length > 1) && (pair[0].equalsIgnoreCase("OFPort"))) {
1681 return new Long(pair[1]);