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.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIdKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.id.grouping.NxmNxTunIdBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNspKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsp.grouping.NxmNxNspBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxmNxNsiKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.nxm.nx.nsi.grouping.NxmNxNsiBuilder;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 import com.google.common.collect.ImmutableList;
73 import com.google.common.collect.Lists;
75 public class MatchUtils {
76 private static final Logger logger = LoggerFactory.getLogger(MatchUtils.class);
77 public static final short ICMP_SHORT = 1;
78 public static final short TCP_SHORT = 6;
79 public static final short UDP_SHORT = 17;
80 public static final String TCP = "tcp";
81 public static final String UDP = "udp";
82 private static final int TCP_SYN = 0x0002;
85 * Create Ingress Port Match dpidLong, inPort
87 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
88 * @param dpidLong Long the datapath ID of a switch/node
89 * @param inPort Long ingress port on a switch
90 * @return matchBuilder Map MatchBuilder Object with a match
92 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
94 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
95 logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
96 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
97 matchBuilder.setInPort(ncid);
103 * Create EtherType Match
105 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
106 * @param etherType Long EtherType
107 * @return matchBuilder Map MatchBuilder Object with a match
109 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
111 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
112 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
113 ethTypeBuilder.setType(new EtherType(etherType));
114 ethernetMatch.setEthernetType(ethTypeBuilder.build());
115 matchBuilder.setEthernetMatch(ethernetMatch.build());
121 * Create Ethernet Source Match
123 * @param matchBuilder MatchBuilder Object without a match yet
124 * @param sMacAddr String representing a source MAC
125 * @return matchBuilder Map MatchBuilder Object with a match
127 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
129 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
130 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
131 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
132 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
133 matchBuilder.setEthernetMatch(ethernetMatch.build());
139 * Create Ethernet Destination Match
141 * @param matchBuilder MatchBuilder Object without a match yet
142 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
143 * @return matchBuilder Map MatchBuilder Object with a match
145 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId, boolean present) {
146 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
147 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
148 vlanIdBuilder.setVlanId(new VlanId(vlanId));
149 vlanIdBuilder.setVlanIdPresent(present);
150 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
151 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
157 * Create Ethernet Destination Match
159 * @param matchBuilder MatchBuilder Object without a match yet
160 * @param dMacAddr String representing a destination MAC
161 * @return matchBuilder Map MatchBuilder Object with a match
163 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
165 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
166 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
167 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
169 ethDestinationBuilder.setMask(mask);
171 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
172 matchBuilder.setEthernetMatch(ethernetMatch.build());
178 * Tunnel ID Match Builder
180 * @param matchBuilder MatchBuilder Object without a match yet
181 * @param tunnelId BigInteger representing a tunnel ID
182 * @return matchBuilder Map MatchBuilder Object with a match
184 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
186 TunnelBuilder tunnelBuilder = new TunnelBuilder();
187 tunnelBuilder.setTunnelId(tunnelId);
188 matchBuilder.setTunnel(tunnelBuilder.build());
194 * Match ICMP code and type
196 * @param matchBuilder MatchBuilder Object without a match yet
197 * @param type short representing an ICMP type
198 * @param code short representing an ICMP code
199 * @return matchBuilder Map MatchBuilder Object with a match
201 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
203 EthernetMatchBuilder eth = new EthernetMatchBuilder();
204 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
205 ethTypeBuilder.setType(new EtherType(0x0800L));
206 eth.setEthernetType(ethTypeBuilder.build());
207 matchBuilder.setEthernetMatch(eth.build());
209 // Build the IPv4 Match requied per OVS Syntax
210 IpMatchBuilder ipmatch = new IpMatchBuilder();
211 ipmatch.setIpProtocol((short) 1);
212 matchBuilder.setIpMatch(ipmatch.build());
214 // Build the ICMPv4 Match
215 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
216 icmpv4match.setIcmpv4Type(type);
217 icmpv4match.setIcmpv4Code(code);
218 matchBuilder.setIcmpv4Match(icmpv4match.build());
224 * @param matchBuilder MatchBuilder Object without a match yet
225 * @param dstip String containing an IPv4 prefix
226 * @return matchBuilder Map Object with a match
228 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
230 EthernetMatchBuilder eth = new EthernetMatchBuilder();
231 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
232 ethTypeBuilder.setType(new EtherType(0x0800L));
233 eth.setEthernetType(ethTypeBuilder.build());
234 matchBuilder.setEthernetMatch(eth.build());
236 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
237 ipv4match.setIpv4Destination(dstip);
239 matchBuilder.setLayer3Match(ipv4match.build());
246 * @param matchBuilder MatchBuilder Object without a match yet
247 * @param dstip String containing an IPv4 prefix
248 * @return matchBuilder Map Object with a match
250 public static MatchBuilder createArpDstIpv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
251 ArpMatchBuilder arpDstMatch = new ArpMatchBuilder();
252 arpDstMatch.setArpTargetTransportAddress(dstip);
253 matchBuilder.setLayer3Match(arpDstMatch.build());
259 * @param matchBuilder MatchBuilder Object without a match yet
260 * @param srcip String containing an IPv4 prefix
261 * @return matchBuilder Map Object with a match
263 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
265 EthernetMatchBuilder eth = new EthernetMatchBuilder();
266 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
267 ethTypeBuilder.setType(new EtherType(0x0800L));
268 eth.setEthernetType(ethTypeBuilder.build());
269 matchBuilder.setEthernetMatch(eth.build());
271 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
272 ipv4match.setIpv4Source(srcip);
273 matchBuilder.setLayer3Match(ipv4match.build());
280 * Create Source TCP Port Match
282 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
283 * @param tcpport Integer representing a source TCP port
284 * @return matchBuilder Map MatchBuilder Object with a match
286 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
288 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
289 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
290 ethTypeBuilder.setType(new EtherType(0x0800L));
291 ethType.setEthernetType(ethTypeBuilder.build());
292 matchBuilder.setEthernetMatch(ethType.build());
294 IpMatchBuilder ipmatch = new IpMatchBuilder();
295 ipmatch.setIpProtocol((short) 6);
296 matchBuilder.setIpMatch(ipmatch.build());
298 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
299 tcpmatch.setTcpSourcePort(tcpport);
300 matchBuilder.setLayer4Match(tcpmatch.build());
307 * Create Destination TCP Port Match
309 * @param matchBuilder MatchBuilder Object without a match yet
310 * @param tcpDstPort Integer representing a destination TCP port
311 * @return matchBuilder Map MatchBuilder Object with a match
313 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
315 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
316 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
317 ethTypeBuilder.setType(new EtherType(0x0800L));
318 ethType.setEthernetType(ethTypeBuilder.build());
319 matchBuilder.setEthernetMatch(ethType.build());
321 IpMatchBuilder ipmatch = new IpMatchBuilder();
322 ipmatch.setIpProtocol((short) 6);
323 matchBuilder.setIpMatch(ipmatch.build());
325 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
326 tcpmatch.setTcpDestinationPort(tcpDstPort);
327 matchBuilder.setLayer4Match(tcpmatch.build());
333 * Test match for TCP_Flags
335 * @param matchBuilder MatchBuilder Object without a match yet
336 * @param tcpPort PortNumber representing a destination TCP port
337 * @param tcpFlag int representing a tcp_flag
338 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
340 * Defined TCP Flag values in OVS v2.1+
341 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
342 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
343 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
345 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, PortNumber tcpPort, int tcpFlag) {
348 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
349 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
350 ethTypeBuilder.setType(new EtherType(0x0800L));
351 ethernetType.setEthernetType(ethTypeBuilder.build());
352 matchBuilder.setEthernetMatch(ethernetType.build());
354 // TCP Protocol Match
355 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
356 ipMatch.setIpProtocol((short) 6);
357 matchBuilder.setIpMatch(ipMatch.build());
360 PortNumber dstPort = new PortNumber(tcpPort);
361 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
362 tcpMatch.setTcpDestinationPort(dstPort);
363 matchBuilder.setLayer4Match(tcpMatch.build());
365 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
366 tcpFlagMatch.setTcpFlag(tcpFlag);
367 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
372 * @return MatchBuilder containing the metadata match values
374 public static MatchBuilder createMetadataMatch(MatchBuilder matchBuilder, BigInteger metaData, BigInteger metaDataMask) {
376 // metadata matchbuilder
377 MetadataBuilder metadata = new MetadataBuilder();
378 metadata.setMetadata(metaData);
379 // Optional metadata mask
380 if (metaDataMask != null) {
381 metadata.setMetadataMask(metaDataMask);
383 matchBuilder.setMetadata(metadata.build());
389 * Create TCP Port Match
391 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
392 * @param tcpport Integer representing a source TCP port
393 * @return matchBuilder Map MatchBuilder Object with a match
395 public static MatchBuilder createIpProtocolMatch(MatchBuilder matchBuilder, short ipProtocol) {
397 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
398 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
399 ethTypeBuilder.setType(new EtherType(0x0800L));
400 ethType.setEthernetType(ethTypeBuilder.build());
401 matchBuilder.setEthernetMatch(ethType.build());
403 IpMatchBuilder ipMmatch = new IpMatchBuilder();
404 if (ipProtocol == TCP_SHORT) {
405 ipMmatch.setIpProtocol(TCP_SHORT);
407 else if (ipProtocol == UDP_SHORT) {
408 ipMmatch.setIpProtocol(UDP_SHORT);
410 else if (ipProtocol == ICMP_SHORT) {
411 ipMmatch.setIpProtocol(ICMP_SHORT);
413 matchBuilder.setIpMatch(ipMmatch.build());
418 * Create tcp syn with proto match.
420 * @param matchBuilder the match builder
421 * @return matchBuilder match builder
423 public static MatchBuilder createTcpSynWithProtoMatch(MatchBuilder matchBuilder) {
426 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
427 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
428 ethTypeBuilder.setType(new EtherType(0x0800L));
429 ethernetType.setEthernetType(ethTypeBuilder.build());
430 matchBuilder.setEthernetMatch(ethernetType.build());
432 // TCP Protocol Match
433 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
434 ipMatch.setIpProtocol((short) 6);
435 matchBuilder.setIpMatch(ipMatch.build());
437 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
438 tcpFlagMatch.setTcpFlag(TCP_SYN);
439 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
444 * Create tcp proto syn match.
446 * @param matchBuilder the match builder
447 * @return matchBuilder match builder
449 public static MatchBuilder createTcpProtoSynMatch(MatchBuilder matchBuilder) {
451 // TCP Protocol Match
452 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
453 ipMatch.setIpProtocol((short) 6);
454 matchBuilder.setIpMatch(ipMatch.build());
456 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
457 tcpFlagMatch.setTcpFlag(TCP_SYN);
458 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
463 * Create dmac tcp port with flag match.
465 * @param matchBuilder the match builder
466 * @param attachedMac the attached mac
467 * @param tcpFlag the tcp flag
468 * @param tunnelID the tunnel iD
469 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
471 public static MatchBuilder createDmacTcpPortWithFlagMatch(MatchBuilder matchBuilder,
472 String attachedMac, Integer tcpFlag, String tunnelID) {
474 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
475 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
476 ethTypeBuilder.setType(new EtherType(0x0800L));
477 ethernetMatch.setEthernetType(ethTypeBuilder.build());
479 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
480 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
481 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
482 matchBuilder.setEthernetMatch(ethernetMatch.build());
484 // TCP Protocol Match
485 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
486 ipMatch.setIpProtocol(TCP_SHORT);
487 matchBuilder.setIpMatch(ipMatch.build());
489 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
490 tcpFlagMatch.setTcpFlag(tcpFlag);
491 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
493 TunnelBuilder tunnelBuilder = new TunnelBuilder();
494 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
495 matchBuilder.setTunnel(tunnelBuilder.build());
501 * Create dmac tcp syn match.
503 * @param matchBuilder the match builder
504 * @param attachedMac the attached mac
505 * @param tcpPort the tcp port
506 * @param tcpFlag the tcp flag
507 * @param tunnelID the tunnel iD
508 * @return the match builder
510 public static MatchBuilder createDmacTcpSynMatch(MatchBuilder matchBuilder,
511 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
513 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
514 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
515 ethTypeBuilder.setType(new EtherType(0x0800L));
516 ethernetMatch.setEthernetType(ethTypeBuilder.build());
518 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
519 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
520 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
521 matchBuilder.setEthernetMatch(ethernetMatch.build());
523 // TCP Protocol Match
524 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
525 ipMatch.setIpProtocol((short) 6);
526 matchBuilder.setIpMatch(ipMatch.build());
529 PortNumber dstPort = new PortNumber(tcpPort);
530 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
531 tcpMatch.setTcpDestinationPort(dstPort);
532 matchBuilder.setLayer4Match(tcpMatch.build());
534 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
535 tcpFlagMatch.setTcpFlag(tcpFlag);
536 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
538 TunnelBuilder tunnelBuilder = new TunnelBuilder();
539 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
540 matchBuilder.setTunnel(tunnelBuilder.build());
546 * Create dmac tcp syn dst ip prefix tcp port.
548 * @param matchBuilder the match builder
549 * @param attachedMac the attached mac
550 * @param tcpPort the tcp port
551 * @param tcpFlag the tcp flag
552 * @param segmentationId the segmentation id
553 * @param dstIp the dst ip
554 * @return the match builder
556 public static MatchBuilder createDmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder,
557 MacAddress attachedMac, PortNumber tcpPort, Integer tcpFlag, String segmentationId,
560 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
561 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
562 ethTypeBuilder.setType(new EtherType(0x0800L));
563 ethernetMatch.setEthernetType(ethTypeBuilder.build());
565 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
566 ethDestinationBuilder.setAddress(new MacAddress(attachedMac));
567 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
569 matchBuilder.setEthernetMatch(ethernetMatch.build());
571 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
572 ipv4match.setIpv4Destination(dstIp);
573 matchBuilder.setLayer3Match(ipv4match.build());
575 // TCP Protocol Match
576 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
577 ipMatch.setIpProtocol(TCP_SHORT);
578 matchBuilder.setIpMatch(ipMatch.build());
581 PortNumber dstPort = new PortNumber(tcpPort);
582 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
583 tcpMatch.setTcpDestinationPort(dstPort);
584 matchBuilder.setLayer4Match(tcpMatch.build());
586 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
587 tcpFlagMatch.setTcpFlag(tcpFlag);
588 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
590 TunnelBuilder tunnelBuilder = new TunnelBuilder();
591 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
592 matchBuilder.setTunnel(tunnelBuilder.build());
598 * Create dmac ip tcp syn match.
600 * @param matchBuilder the match builder
601 * @param dMacAddr the d mac addr
602 * @param mask the mask
603 * @param ipPrefix the ip prefix
604 * @return MatchBuilder containing the metadata match values
606 public static MatchBuilder createDmacIpTcpSynMatch(MatchBuilder matchBuilder,
607 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
609 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
610 EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
611 ethDestBuilder.setAddress(new MacAddress(dMacAddr));
613 ethDestBuilder.setMask(mask);
615 ethernetMatch.setEthernetDestination(ethDestBuilder.build());
616 matchBuilder.setEthernetMatch(ethernetMatch.build());
618 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
619 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
620 ethTypeBuilder.setType(new EtherType(0x0800L));
621 ethernetType.setEthernetType(ethTypeBuilder.build());
622 matchBuilder.setEthernetMatch(ethernetType.build());
623 if (ipPrefix != null) {
624 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
625 ipv4match.setIpv4Destination(ipPrefix);
626 matchBuilder.setLayer3Match(ipv4match.build());
628 // TCP Protocol Match
629 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
630 ipMatch.setIpProtocol(TCP_SHORT);
631 matchBuilder.setIpMatch(ipMatch.build());
633 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
634 tcpFlagMatch.setTcpFlag(TCP_SYN);
635 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
641 * Create smac tcp syn dst ip prefix tcp port.
643 * @param matchBuilder the match builder
644 * @param attachedMac the attached mac
645 * @param tcpPort the tcp port
646 * @param tcpFlag the tcp flag
647 * @param segmentationId the segmentation id
648 * @param dstIp the dst ip
649 * @return the match builder
651 public static MatchBuilder createSmacTcpSynDstIpPrefixTcpPort(MatchBuilder matchBuilder, MacAddress attachedMac,
652 PortNumber tcpPort, Integer tcpFlag, String segmentationId, Ipv4Prefix dstIp) {
654 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
655 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
656 ethTypeBuilder.setType(new EtherType(0x0800L));
657 ethernetMatch.setEthernetType(ethTypeBuilder.build());
659 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
660 ethSourceBuilder.setAddress(new MacAddress(attachedMac));
661 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
663 matchBuilder.setEthernetMatch(ethernetMatch.build());
665 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
666 ipv4match.setIpv4Destination(dstIp);
667 matchBuilder.setLayer3Match(ipv4match.build());
669 // TCP Protocol Match
670 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
671 ipMatch.setIpProtocol(TCP_SHORT);
672 matchBuilder.setIpMatch(ipMatch.build());
675 PortNumber dstPort = new PortNumber(tcpPort);
676 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
677 tcpMatch.setTcpDestinationPort(dstPort);
678 matchBuilder.setLayer4Match(tcpMatch.build());
680 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
681 tcpFlagMatch.setTcpFlag(tcpFlag);
682 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
684 TunnelBuilder tunnelBuilder = new TunnelBuilder();
685 tunnelBuilder.setTunnelId(new BigInteger(segmentationId));
686 matchBuilder.setTunnel(tunnelBuilder.build());
692 * Create smac tcp port with flag match.
694 * @param matchBuilder the match builder
695 * @param attachedMac the attached mac
696 * @param tcpFlag the tcp flag
697 * @param tunnelID the tunnel iD
698 * @return matchBuilder
700 public static MatchBuilder createSmacTcpPortWithFlagMatch(MatchBuilder matchBuilder, String attachedMac,
701 Integer tcpFlag, String tunnelID) {
703 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
704 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
705 ethTypeBuilder.setType(new EtherType(0x0800L));
706 ethernetMatch.setEthernetType(ethTypeBuilder.build());
708 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
709 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
710 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
711 matchBuilder.setEthernetMatch(ethernetMatch.build());
713 // TCP Protocol Match
714 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
715 ipMatch.setIpProtocol(TCP_SHORT);
716 matchBuilder.setIpMatch(ipMatch.build());
718 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
719 tcpFlagMatch.setTcpFlag(tcpFlag);
720 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
722 TunnelBuilder tunnelBuilder = new TunnelBuilder();
723 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
724 matchBuilder.setTunnel(tunnelBuilder.build());
730 * Create smac ip tcp syn match.
732 * @param matchBuilder the match builder
733 * @param dMacAddr the d mac addr
734 * @param mask the mask
735 * @param ipPrefix the ip prefix
736 * @return MatchBuilder containing the metadata match values
738 public static MatchBuilder createSmacIpTcpSynMatch(MatchBuilder matchBuilder, MacAddress dMacAddr,
739 MacAddress mask, Ipv4Prefix ipPrefix) {
741 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
742 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
743 ethSrcBuilder.setAddress(new MacAddress(dMacAddr));
745 ethSrcBuilder.setMask(mask);
747 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
748 matchBuilder.setEthernetMatch(ethernetMatch.build());
750 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
751 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
752 ethTypeBuilder.setType(new EtherType(0x0800L));
753 ethernetType.setEthernetType(ethTypeBuilder.build());
754 matchBuilder.setEthernetMatch(ethernetType.build());
755 if (ipPrefix != null) {
756 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
757 ipv4match.setIpv4Destination(ipPrefix);
758 matchBuilder.setLayer3Match(ipv4match.build());
760 // TCP Protocol Match
761 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
762 ipMatch.setIpProtocol(TCP_SHORT);
763 matchBuilder.setIpMatch(ipMatch.build());
765 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
766 tcpFlagMatch.setTcpFlag(TCP_SYN);
767 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
773 * Create smac tcp syn.
775 * @param matchBuilder the match builder
776 * @param attachedMac the attached mac
777 * @param tcpPort the tcp port
778 * @param tcpFlag the tcp flag
779 * @param tunnelID the tunnel iD
780 * @return the match builder
782 public static MatchBuilder createSmacTcpSyn(MatchBuilder matchBuilder,
783 String attachedMac, PortNumber tcpPort, Integer tcpFlag, String tunnelID) {
785 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
786 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
787 ethTypeBuilder.setType(new EtherType(0x0800L));
788 ethernetMatch.setEthernetType(ethTypeBuilder.build());
790 EthernetSourceBuilder ethSrcBuilder = new EthernetSourceBuilder();
791 ethSrcBuilder.setAddress(new MacAddress(attachedMac));
792 ethernetMatch.setEthernetSource(ethSrcBuilder.build());
793 matchBuilder.setEthernetMatch(ethernetMatch.build());
795 // TCP Protocol Match
796 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
797 ipMatch.setIpProtocol((short) 6);
798 matchBuilder.setIpMatch(ipMatch.build());
801 PortNumber dstPort = new PortNumber(tcpPort);
802 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
803 tcpMatch.setTcpDestinationPort(dstPort);
804 matchBuilder.setLayer4Match(tcpMatch.build());
807 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
808 tcpFlagMatch.setTcpFlag(tcpFlag);
809 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
811 TunnelBuilder tunnelBuilder = new TunnelBuilder();
812 tunnelBuilder.setTunnelId(new BigInteger(tunnelID));
813 matchBuilder.setTunnel(tunnelBuilder.build());
819 * @return MatchBuilder containing the metadata match values
821 public static MatchBuilder createMacSrcIpTcpSynMatch(MatchBuilder matchBuilder,
822 MacAddress dMacAddr, MacAddress mask, Ipv4Prefix ipPrefix) {
824 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
825 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
826 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
828 ethDestinationBuilder.setMask(mask);
830 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
831 matchBuilder.setEthernetMatch(ethernetMatch.build());
833 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
834 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
835 ethTypeBuilder.setType(new EtherType(0x0800L));
836 ethernetType.setEthernetType(ethTypeBuilder.build());
837 matchBuilder.setEthernetMatch(ethernetType.build());
838 if (ipPrefix != null) {
839 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
840 ipv4match.setIpv4Source(ipPrefix);
841 matchBuilder.setLayer3Match(ipv4match.build());
843 // TCP Protocol Match
844 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
845 ipMatch.setIpProtocol(TCP_SHORT);
846 matchBuilder.setIpMatch(ipMatch.build());
848 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
849 tcpFlagMatch.setTcpFlag(TCP_SYN);
850 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
855 public static class RegMatch {
856 final Class<? extends NxmNxReg> reg;
858 public RegMatch(Class<? extends NxmNxReg> reg, Long value) {
863 public static RegMatch of(Class<? extends NxmNxReg> reg, Long value) {
864 return new RegMatch(reg, value);
868 public static void addNxRegMatch(MatchBuilder match,
869 RegMatch... matches) {
870 ArrayList<ExtensionList> extensions = new ArrayList<>();
871 for (RegMatch rm : matches) {
872 Class<? extends ExtensionKey> key;
873 if (NxmNxReg0.class.equals(rm.reg)) {
874 key = NxmNxReg0Key.class;
875 } else if (NxmNxReg1.class.equals(rm.reg)) {
876 key = NxmNxReg1Key.class;
877 } else if (NxmNxReg2.class.equals(rm.reg)) {
878 key = NxmNxReg2Key.class;
879 } else if (NxmNxReg3.class.equals(rm.reg)) {
880 key = NxmNxReg3Key.class;
881 } else if (NxmNxReg4.class.equals(rm.reg)) {
882 key = NxmNxReg4Key.class;
883 } else if (NxmNxReg5.class.equals(rm.reg)) {
884 key = NxmNxReg5Key.class;
885 } else if (NxmNxReg6.class.equals(rm.reg)) {
886 key = NxmNxReg6Key.class;
888 key = NxmNxReg7Key.class;
890 NxAugMatchNodesNodeTableFlow am =
891 new NxAugMatchNodesNodeTableFlowBuilder()
892 .setNxmNxReg(new NxmNxRegBuilder()
897 extensions.add(new ExtensionListBuilder()
898 .setExtensionKey(key)
899 .setExtension(new ExtensionBuilder()
900 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
904 GeneralAugMatchNodesNodeTableFlow m =
905 new GeneralAugMatchNodesNodeTableFlowBuilder()
906 .setExtensionList(extensions)
908 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
911 public static void addNxTunIdMatch(MatchBuilder match,
913 NxAugMatchNodesNodeTableFlow am =
914 new NxAugMatchNodesNodeTableFlowBuilder()
915 .setNxmNxTunId(new NxmNxTunIdBuilder()
916 .setValue(BigInteger.valueOf(tunId))
919 GeneralAugMatchNodesNodeTableFlow m =
920 new GeneralAugMatchNodesNodeTableFlowBuilder()
921 .setExtensionList(ImmutableList.of(new ExtensionListBuilder()
922 .setExtensionKey(NxmNxTunIdKey.class)
923 .setExtension(new ExtensionBuilder()
924 .addAugmentation(NxAugMatchNodesNodeTableFlow.class, am)
928 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
931 public static void addNxNsp(MatchBuilder match, long nsp) {
932 org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
933 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
934 .setNxmNxNsp(new NxmNxNspBuilder()
938 addExtension(match, NxmNxNspKey.class, am);
941 public static void addNxNsi(MatchBuilder match, short nsi) {
942 org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am =
943 new org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder()
944 .setNxmNxNsi(new NxmNxNsiBuilder()
948 addExtension(match, NxmNxNsiKey.class, am);
951 private static void addExtension (MatchBuilder match, Class<? extends ExtensionKey> extensionKey, org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow am) {
952 GeneralAugMatchNodesNodeTableFlow existingAugmentations = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
953 List<ExtensionList> extensions = null;
954 if (existingAugmentations != null ) {
955 extensions = existingAugmentations.getExtensionList();
957 if (extensions == null) {
958 extensions = Lists.newArrayList();
961 extensions.add(new ExtensionListBuilder()
962 .setExtensionKey(extensionKey)
963 .setExtension(new ExtensionBuilder()
964 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.ovs.nx.sal.match.rev140714.NxAugMatchNodesNodeTableFlow.class, am)
968 GeneralAugMatchNodesNodeTableFlow m = new GeneralAugMatchNodesNodeTableFlowBuilder()
969 .setExtensionList(extensions)
971 match.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, m);
974 public static EthernetMatch ethernetMatch(MacAddress srcMac,
977 EthernetMatchBuilder emb = new EthernetMatchBuilder();
979 emb.setEthernetSource(new EthernetSourceBuilder()
983 emb.setEthernetDestination(new EthernetDestinationBuilder()
986 if (etherType != null)
987 emb.setEthernetType(new EthernetTypeBuilder()
988 .setType(new EtherType(etherType))
994 * Create ipv4 prefix from ipv4 address, by appending /32 mask
996 * @param ipv4AddressString the ip address, in string format
997 * @return Ipv4Prefix with ipv4Address and /32 mask
999 public static Ipv4Prefix iPv4PrefixFromIPv4Address(String ipv4AddressString) {
1000 return new Ipv4Prefix(ipv4AddressString + "/32");