2 * Copyright (C) 2013 Red Hat, Inc.
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
10 package org.opendaylight.ovsdb.utils.mdsal.openflow;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.arp.tpa.grouping.NxmOfArpTpaBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNspKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNsiKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
74 import com.google.common.collect.ImmutableList;
75 import com.google.common.collect.Lists;
77 public class MatchUtils {
78 private static final Logger logger = LoggerFactory.getLogger(MatchUtils.class);
79 public static final short ICMP_SHORT = 1;
80 public static final short TCP_SHORT = 6;
81 public static final short UDP_SHORT = 17;
82 public static final String TCP = "tcp";
83 public static final String UDP = "udp";
84 private static final int TCP_SYN = 0x0002;
87 * Create Ingress Port Match dpidLong, inPort
89 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
90 * @param dpidLong Long the datapath ID of a switch/node
91 * @param inPort Long ingress port on a switch
92 * @return matchBuilder Map MatchBuilder Object with a match
94 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
96 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
97 logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
98 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
99 matchBuilder.setInPort(ncid);
105 * Create EtherType Match
107 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
108 * @param etherType Long EtherType
109 * @return matchBuilder Map MatchBuilder Object with a match
111 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
113 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
114 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
115 ethTypeBuilder.setType(new EtherType(etherType));
116 ethernetMatch.setEthernetType(ethTypeBuilder.build());
117 matchBuilder.setEthernetMatch(ethernetMatch.build());
123 * Create Ethernet Source Match
125 * @param matchBuilder MatchBuilder Object without a match yet
126 * @param sMacAddr String representing a source MAC
127 * @return matchBuilder Map MatchBuilder Object with a match
129 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
131 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
132 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
133 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
134 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
135 matchBuilder.setEthernetMatch(ethernetMatch.build());
141 * Create Ethernet Destination Match
143 * @param matchBuilder MatchBuilder Object without a match yet
144 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
145 * @return matchBuilder Map MatchBuilder Object with a match
147 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
148 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
149 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
150 vlanIdBuilder.setVlanId(new VlanId(vlanId));
151 vlanIdBuilder.setVlanIdPresent(present);
152 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
153 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
159 * Create Ethernet Destination Match
161 * @param matchBuilder MatchBuilder Object without a match yet
162 * @param dMacAddr String representing a destination MAC
163 * @return matchBuilder Map MatchBuilder Object with a match
165 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
167 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
168 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
169 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
171 ethDestinationBuilder.setMask(mask);
173 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
174 matchBuilder.setEthernetMatch(ethernetMatch.build());
180 * Tunnel ID Match Builder
182 * @param matchBuilder MatchBuilder Object without a match yet
183 * @param tunnelId BigInteger representing a tunnel ID
184 * @return matchBuilder Map MatchBuilder Object with a match
186 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
188 TunnelBuilder tunnelBuilder = new TunnelBuilder();
189 tunnelBuilder.setTunnelId(tunnelId);
190 matchBuilder.setTunnel(tunnelBuilder.build());
196 * Match ICMP code and type
198 * @param matchBuilder MatchBuilder Object without a match yet
199 * @param type short representing an ICMP type
200 * @param code short representing an ICMP code
201 * @return matchBuilder Map MatchBuilder Object with a match
203 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
205 EthernetMatchBuilder eth = new EthernetMatchBuilder();
206 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
207 ethTypeBuilder.setType(new EtherType(0x0800L));
208 eth.setEthernetType(ethTypeBuilder.build());
209 matchBuilder.setEthernetMatch(eth.build());
211 // Build the IPv4 Match requied per OVS Syntax
212 IpMatchBuilder ipmatch = new IpMatchBuilder();
213 ipmatch.setIpProtocol((short) 1);
214 matchBuilder.setIpMatch(ipmatch.build());
216 // Build the ICMPv4 Match
217 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
218 icmpv4match.setIcmpv4Type(type);
219 icmpv4match.setIcmpv4Code(code);
220 matchBuilder.setIcmpv4Match(icmpv4match.build());
226 * @param matchBuilder MatchBuilder Object without a match yet
227 * @param dstip String containing an IPv4 prefix
228 * @return matchBuilder Map Object with a match
230 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
232 EthernetMatchBuilder eth = new EthernetMatchBuilder();
233 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
234 ethTypeBuilder.setType(new EtherType(0x0800L));
235 eth.setEthernetType(ethTypeBuilder.build());
236 matchBuilder.setEthernetMatch(eth.build());
238 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
239 ipv4match.setIpv4Destination(dstip);
241 matchBuilder.setLayer3Match(ipv4match.build());
248 * @param matchBuilder MatchBuilder Object without a match yet
249 * @param dstip String containing an IPv4 prefix
250 * @return matchBuilder Map Object with a match
252 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
253 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
254 arpDstMatch.setArpTargetTransportAddress(dstip);
255 matchBuilder.setLayer3Match(arpDstMatch.build());
261 * @param matchBuilder MatchBuilder Object without a match yet
262 * @param srcip String containing an IPv4 prefix
263 * @return matchBuilder Map Object with a match
265 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
267 EthernetMatchBuilder eth = new EthernetMatchBuilder();
268 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
269 ethTypeBuilder.setType(new EtherType(0x0800L));
270 eth.setEthernetType(ethTypeBuilder.build());
271 matchBuilder.setEthernetMatch(eth.build());
273 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
274 ipv4match.setIpv4Source(srcip);
275 matchBuilder.setLayer3Match(ipv4match.build());
282 * Create Source TCP Port Match
284 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
285 * @param tcpport Integer representing a source TCP port
286 * @return matchBuilder Map MatchBuilder Object with a match
288 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
290 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
291 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
292 ethTypeBuilder.setType(new EtherType(0x0800L));
293 ethType.setEthernetType(ethTypeBuilder.build());
294 matchBuilder.setEthernetMatch(ethType.build());
296 IpMatchBuilder ipmatch = new IpMatchBuilder();
297 ipmatch.setIpProtocol((short) 6);
298 matchBuilder.setIpMatch(ipmatch.build());
300 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
301 tcpmatch.setTcpSourcePort(tcpport);
302 matchBuilder.setLayer4Match(tcpmatch.build());
309 * Create Destination TCP Port Match
311 * @param matchBuilder MatchBuilder Object without a match yet
312 * @param tcpDstPort Integer representing a destination TCP port
313 * @return matchBuilder Map MatchBuilder Object with a match
315 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
317 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
318 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
319 ethTypeBuilder.setType(new EtherType(0x0800L));
320 ethType.setEthernetType(ethTypeBuilder.build());
321 matchBuilder.setEthernetMatch(ethType.build());
323 IpMatchBuilder ipmatch = new IpMatchBuilder();
324 ipmatch.setIpProtocol((short) 6);
325 matchBuilder.setIpMatch(ipmatch.build());
327 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
328 tcpmatch.setTcpDestinationPort(tcpDstPort);
329 matchBuilder.setLayer4Match(tcpmatch.build());
335 * Test match for TCP_Flags
337 * @param matchBuilder MatchBuilder Object without a match yet
338 * @param tcpPort PortNumber representing a destination TCP port
339 * @param tcpFlag int representing a tcp_flag
340 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
342 * Defined TCP Flag values in OVS v2.1+
343 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
344 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
345 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
347 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
350 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
351 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
352 ethTypeBuilder.setType(new EtherType(0x0800L));
353 ethernetType.setEthernetType(ethTypeBuilder.build());
354 matchBuilder.setEthernetMatch(ethernetType.build());
356 // TCP Protocol Match
357 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
358 ipMatch.setIpProtocol((short) 6);
359 matchBuilder.setIpMatch(ipMatch.build());
362 PortNumber dstPort = new PortNumber(tcpPort);
363 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
364 tcpMatch.setTcpDestinationPort(dstPort);
365 matchBuilder.setLayer4Match(tcpMatch.build());
367 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
368 tcpFlagMatch.setTcpFlag(tcpFlag);
369 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
374 * @return MatchBuilder containing the metadata match values
376 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
378 // metadata matchbuilder
379 MetadataBuilder metadata = new MetadataBuilder();
380 metadata.setMetadata(metaData);
381 // Optional metadata mask
382 if (metaDataMask != null) {
383 metadata.setMetadataMask(metaDataMask);
385 matchBuilder.setMetadata(metadata.build());
391 * Create TCP Port Match
393 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
394 * @param tcpport Integer representing a source TCP port
395 * @return matchBuilder Map MatchBuilder Object with a match
397 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
399 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
400 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
401 ethTypeBuilder.setType(new EtherType(0x0800L));
402 ethType.setEthernetType(ethTypeBuilder.build());
403 matchBuilder.setEthernetMatch(ethType.build());
405 IpMatchBuilder ipMmatch = new IpMatchBuilder();
406 if (ipProtocol == TCP_SHORT) {
407 ipMmatch.setIpProtocol(TCP_SHORT);
409 else if (ipProtocol == UDP_SHORT) {
410 ipMmatch.setIpProtocol(UDP_SHORT);
412 else if (ipProtocol == ICMP_SHORT) {
413 ipMmatch.setIpProtocol(ICMP_SHORT);
415 matchBuilder.setIpMatch(ipMmatch.build());
420 * Create tcp syn with proto match.
422 * @param matchBuilder the match builder
423 * @return matchBuilder match builder
425 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
428 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
429 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
430 ethTypeBuilder.setType(new EtherType(0x0800L));
431 ethernetType.setEthernetType(ethTypeBuilder.build());
432 matchBuilder.setEthernetMatch(ethernetType.build());
434 // TCP Protocol Match
435 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
436 ipMatch.setIpProtocol((short) 6);
437 matchBuilder.setIpMatch(ipMatch.build());
439 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
440 tcpFlagMatch.setTcpFlag(TCP_SYN);
441 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
446 * Create tcp proto syn match.
448 * @param matchBuilder the match builder
449 * @return matchBuilder match builder
451 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
453 // TCP Protocol Match
454 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
455 ipMatch.setIpProtocol((short) 6);
456 matchBuilder.setIpMatch(ipMatch.build());
458 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
459 tcpFlagMatch.setTcpFlag(TCP_SYN);
460 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
465 * Create dmac tcp port with flag match.
467 * @param matchBuilder the match builder
468 * @param attachedMac the attached mac
469 * @param tcpFlag the tcp flag
470 * @param tunnelID the tunnel iD
471 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
473 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
474 String attachedMac, Integer tcpFlag, String tunnelID) {
476 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
477 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
478 ethTypeBuilder.setType(new EtherType(0x0800L));
479 ethernetMatch.setEthernetType(ethTypeBuilder.build());
481 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
482 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
483 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
484 matchBuilder.setEthernetMatch(ethernetMatch.build());
486 // TCP Protocol Match
487 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
488 ipMatch.setIpProtocol(TCP_SHORT);
489 matchBuilder.setIpMatch(ipMatch.build());
491 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
492 tcpFlagMatch.setTcpFlag(tcpFlag);
493 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
495 TunnelBuilder tunnelBuilder = new TunnelBuilder();
496 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
497 matchBuilder.setTunnel(tunnelBuilder.build());
503 * Create dmac tcp syn match.
505 * @param matchBuilder the match builder
506 * @param attachedMac the attached mac
507 * @param tcpPort the tcp port
508 * @param tcpFlag the tcp flag
509 * @param tunnelID the tunnel iD
510 * @return the match builder
512 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
513 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
515 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
516 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
517 ethTypeBuilder.setType(new EtherType(0x0800L));
518 ethernetMatch.setEthernetType(ethTypeBuilder.build());
520 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
521 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
522 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
523 matchBuilder.setEthernetMatch(ethernetMatch.build());
525 // TCP Protocol Match
526 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
527 ipMatch.setIpProtocol((short) 6);
528 matchBuilder.setIpMatch(ipMatch.build());
531 PortNumber dstPort = new PortNumber(tcpPort);
532 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
533 tcpMatch.setTcpDestinationPort(dstPort);
534 matchBuilder.setLayer4Match(tcpMatch.build());
536 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
537 tcpFlagMatch.setTcpFlag(tcpFlag);
538 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
540 TunnelBuilder tunnelBuilder = new TunnelBuilder();
541 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
542 matchBuilder.setTunnel(tunnelBuilder.build());
548 * Create dmac tcp syn dst ip prefix tcp port.
550 * @param matchBuilder the match builder
551 * @param attachedMac the attached mac
552 * @param tcpPort the tcp port
553 * @param tcpFlag the tcp flag
554 * @param segmentationId the segmentation id
555 * @param dstIp the dst ip
556 * @return the match builder
558 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
559 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
562 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
563 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
564 ethTypeBuilder.setType(new EtherType(0x0800L));
565 ethernetMatch.setEthernetType(ethTypeBuilder.build());
567 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
568 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
569 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
571 matchBuilder.setEthernetMatch(ethernetMatch.build());
573 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
574 ipv4match.setIpv4Destination(dstIp);
575 matchBuilder.setLayer3Match(ipv4match.build());
577 // TCP Protocol Match
578 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
579 ipMatch.setIpProtocol(TCP_SHORT);
580 matchBuilder.setIpMatch(ipMatch.build());
583 PortNumber dstPort = new PortNumber(tcpPort);
584 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
585 tcpMatch.setTcpDestinationPort(dstPort);
586 matchBuilder.setLayer4Match(tcpMatch.build());
588 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
589 tcpFlagMatch.setTcpFlag(tcpFlag);
590 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
592 TunnelBuilder tunnelBuilder = new TunnelBuilder();
593 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
594 matchBuilder.setTunnel(tunnelBuilder.build());
600 * Create dmac ip tcp syn match.
602 * @param matchBuilder the match builder
603 * @param dMacAddr the d mac addr
604 * @param mask the mask
605 * @param ipPrefix the ip prefix
606 * @return MatchBuilder containing the metadata match values
608 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
609 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
611 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
612 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
613 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
615 ethDestBuilder.setMask(mask);
617 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
618 matchBuilder.setEthernetMatch(ethernetMatch.build());
620 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
621 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
622 ethTypeBuilder.setType(new EtherType(0x0800L));
623 ethernetType.setEthernetType(ethTypeBuilder.build());
624 matchBuilder.setEthernetMatch(ethernetType.build());
625 if (ipPrefix != null) {
626 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
627 ipv4match.setIpv4Destination(ipPrefix);
628 matchBuilder.setLayer3Match(ipv4match.build());
630 // TCP Protocol Match
631 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
632 ipMatch.setIpProtocol(TCP_SHORT);
633 matchBuilder.setIpMatch(ipMatch.build());
635 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
636 tcpFlagMatch.setTcpFlag(TCP_SYN);
637 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
643 * Create smac tcp syn dst ip prefix tcp port.
645 * @param matchBuilder the match builder
646 * @param attachedMac the attached mac
647 * @param tcpPort the tcp port
648 * @param tcpFlag the tcp flag
649 * @param segmentationId the segmentation id
650 * @param dstIp the dst ip
651 * @return the match builder
653 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
654 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
656 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
657 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
658 ethTypeBuilder.setType(new EtherType(0x0800L));
659 ethernetMatch.setEthernetType(ethTypeBuilder.build());
661 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
662 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
663 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
665 matchBuilder.setEthernetMatch(ethernetMatch.build());
667 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
668 ipv4match.setIpv4Destination(dstIp);
669 matchBuilder.setLayer3Match(ipv4match.build());
671 // TCP Protocol Match
672 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
673 ipMatch.setIpProtocol(TCP_SHORT);
674 matchBuilder.setIpMatch(ipMatch.build());
677 PortNumber dstPort = new PortNumber(tcpPort);
678 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
679 tcpMatch.setTcpDestinationPort(dstPort);
680 matchBuilder.setLayer4Match(tcpMatch.build());
682 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
683 tcpFlagMatch.setTcpFlag(tcpFlag);
684 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
686 TunnelBuilder tunnelBuilder = new TunnelBuilder();
687 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
688 matchBuilder.setTunnel(tunnelBuilder.build());
694 * Create smac tcp port with flag match.
696 * @param matchBuilder the match builder
697 * @param attachedMac the attached mac
698 * @param tcpFlag the tcp flag
699 * @param tunnelID the tunnel iD
700 * @return matchBuilder
702 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
703 Integer tcpFlag, String tunnelID) {
705 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
706 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
707 ethTypeBuilder.setType(new EtherType(0x0800L));
708 ethernetMatch.setEthernetType(ethTypeBuilder.build());
710 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
711 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
712 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
713 matchBuilder.setEthernetMatch(ethernetMatch.build());
715 // TCP Protocol Match
716 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
717 ipMatch.setIpProtocol(TCP_SHORT);
718 matchBuilder.setIpMatch(ipMatch.build());
720 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
721 tcpFlagMatch.setTcpFlag(tcpFlag);
722 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
724 TunnelBuilder tunnelBuilder = new TunnelBuilder();
725 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
726 matchBuilder.setTunnel(tunnelBuilder.build());
732 * Create smac ip tcp syn match.
734 * @param matchBuilder the match builder
735 * @param dMacAddr the d mac addr
736 * @param mask the mask
737 * @param ipPrefix the ip prefix
738 * @return MatchBuilder containing the metadata match values
740 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
741 MacAddress mask, Ipv4Prefix ipPrefix) {
743 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
744 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
745 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
747 ethSrcBuilder.setMask(mask);
749 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
750 matchBuilder.setEthernetMatch(ethernetMatch.build());
752 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
753 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
754 ethTypeBuilder.setType(new EtherType(0x0800L));
755 ethernetType.setEthernetType(ethTypeBuilder.build());
756 matchBuilder.setEthernetMatch(ethernetType.build());
757 if (ipPrefix != null) {
758 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
759 ipv4match.setIpv4Destination(ipPrefix);
760 matchBuilder.setLayer3Match(ipv4match.build());
762 // TCP Protocol Match
763 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
764 ipMatch.setIpProtocol(TCP_SHORT);
765 matchBuilder.setIpMatch(ipMatch.build());
767 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
768 tcpFlagMatch.setTcpFlag(TCP_SYN);
769 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
775 * Create smac tcp syn.
777 * @param matchBuilder the match builder
778 * @param attachedMac the attached mac
779 * @param tcpPort the tcp port
780 * @param tcpFlag the tcp flag
781 * @param tunnelID the tunnel iD
782 * @return the match builder
784 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
785 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
787 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
788 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
789 ethTypeBuilder.setType(new EtherType(0x0800L));
790 ethernetMatch.setEthernetType(ethTypeBuilder.build());
792 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
793 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
794 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
795 matchBuilder.setEthernetMatch(ethernetMatch.build());
797 // TCP Protocol Match
798 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
799 ipMatch.setIpProtocol((short) 6);
800 matchBuilder.setIpMatch(ipMatch.build());
803 PortNumber dstPort = new PortNumber(tcpPort);
804 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
805 tcpMatch.setTcpDestinationPort(dstPort);
806 matchBuilder.setLayer4Match(tcpMatch.build());
809 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
810 tcpFlagMatch.setTcpFlag(tcpFlag);
811 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
813 TunnelBuilder tunnelBuilder = new TunnelBuilder();
814 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
815 matchBuilder.setTunnel(tunnelBuilder.build());
821 * @return MatchBuilder containing the metadata match values
823 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
824 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
826 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
827 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
828 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
830 ethDestinationBuilder.setMask(mask);
832 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
833 matchBuilder.setEthernetMatch(ethernetMatch.build());
835 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
836 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
837 ethTypeBuilder.setType(new EtherType(0x0800L));
838 ethernetType.setEthernetType(ethTypeBuilder.build());
839 matchBuilder.setEthernetMatch(ethernetType.build());
840 if (ipPrefix != null) {
841 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
842 ipv4match.setIpv4Source(ipPrefix);
843 matchBuilder.setLayer3Match(ipv4match.build());
845 // TCP Protocol Match
846 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
847 ipMatch.setIpProtocol(TCP_SHORT);
848 matchBuilder.setIpMatch(ipMatch.build());
850 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
851 tcpFlagMatch.setTcpFlag(TCP_SYN);
852 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
857 public static class RegMatch {
858 final Class<? extends NxmNxReg> reg;
860 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
865 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
866 return new RegMatch(reg, value);
870 public static void addNxRegMatch(MatchBuilder match,
871 RegMatch... matches) {
872 ArrayList<ExtensionList> extensions = new ArrayList<>();
873 for (RegMatch rm : matches) {
874 Class<? extends ExtensionKey> key;
875 if (NxmNxReg0.class.equals(rm.reg)) {
876 key = NxmNxReg0Key.class;
877 } else if (NxmNxReg1.class.equals(rm.reg)) {
878 key = NxmNxReg1Key.class;
879 } else if (NxmNxReg2.class.equals(rm.reg)) {
880 key = NxmNxReg2Key.class;
881 } else if (NxmNxReg3.class.equals(rm.reg)) {
882 key = NxmNxReg3Key.class;
883 } else if (NxmNxReg4.class.equals(rm.reg)) {
884 key = NxmNxReg4Key.class;
885 } else if (NxmNxReg5.class.equals(rm.reg)) {
886 key = NxmNxReg5Key.class;
887 } else if (NxmNxReg6.class.equals(rm.reg)) {
888 key = NxmNxReg6Key.class;
890 key = NxmNxReg7Key.class;
892 NxAugMatchNodesNodeTableFlow am =
893 new NxAugMatchNodesNodeTableFlowBuilder()
894 .setNxmNxReg(new NxmNxRegBuilder()
899 extensions.add(new ExtensionListBuilder()
900 .setExtensionKey(key)
901 .setExtension(new ExtensionBuilder()
902 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
906 GeneralAugMatchNodesNodeTableFlow m =
907 new GeneralAugMatchNodesNodeTableFlowBuilder()
908 .setExtensionList(extensions)
910 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
913 public static void addNxTunIdMatch(MatchBuilder match,
915 NxAugMatchNodesNodeTableFlow am =
916 new NxAugMatchNodesNodeTableFlowBuilder()
917 .setNxmNxTunId(new NxmNxTunIdBuilder()
918 .setValue(BigInteger.valueOf(tunId))
921 GeneralAugMatchNodesNodeTableFlow m =
922 new GeneralAugMatchNodesNodeTableFlowBuilder()
923 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
924 .setExtensionKey(NxmNxTunIdKey.class)
925 .setExtension(new ExtensionBuilder()
926 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
930 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
933 public static void addNxNsp(MatchBuilder match, long nsp) {
934 org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
935 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
936 .setNxmNxNsp(new NxmNxNspBuilder()
940 addExtension(match, NxmNxNspKey.class, am);
943 public static void addNxNsi(MatchBuilder match, short nsi) {
944 org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
945 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
946 .setNxmNxNsi(new NxmNxNsiBuilder()
950 addExtension(match, NxmNxNsiKey.class, am);
953 private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
954 GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
955 List<ExtensionList> extensions = null;
956 if (existingAugmentations != null ) {
957 extensions = existingAugmentations.getExtensionList();
959 if (extensions == null) {
960 extensions = Lists.newArrayList();
963 extensions.add(new ExtensionListBuilder()
964 .setExtensionKey(extensionKey)
965 .setExtension(new ExtensionBuilder()
966 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
970 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
971 .setExtensionList(extensions)
973 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
976 public static EthernetMatch ethernetMatch(MacAddress srcMac,
979 EthernetMatchBuilder emb = new EthernetMatchBuilder();
981 emb.setEthernetSource(new EthernetSourceBuilder()
985 emb.setEthernetDestination(new EthernetDestinationBuilder()
988 if (etherType != null)
989 emb.setEthernetType(new EthernetTypeBuilder()
990 .setType(new EtherType(etherType))