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.openstack.netvirt.providers.mdsalopenflow13;
12 import org.opendaylight.ovsdb.openstack.netvirt.providers.OF13Provider;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.math.BigInteger;
37 public class OF13MdSalMatch extends OF13Provider {
38 private static final Logger logger = LoggerFactory.getLogger(OF13MdSalMatch.class);
41 * Create Ingress Port Match dpidLong, inPort
43 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
44 * @param dpidLong Long the datapath ID of a switch/node
45 * @param inPort Long ingress port on a switch
46 * @return matchBuilder Map MatchBuilder Object with a match
48 public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
50 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + inPort);
51 logger.debug("createInPortMatch() Node Connector ID is - Type=openflow: DPID={} inPort={} ", dpidLong, inPort);
52 matchBuilder.setInPort(NodeConnectorId.getDefaultInstance(ncid.getValue()));
53 matchBuilder.setInPort(ncid);
59 * Create EtherType Match
61 * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
62 * @param etherType Long EtherType
63 * @return matchBuilder Map MatchBuilder Object with a match
65 public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
67 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
68 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
69 ethTypeBuilder.setType(new EtherType(etherType));
70 ethernetMatch.setEthernetType(ethTypeBuilder.build());
71 matchBuilder.setEthernetMatch(ethernetMatch.build());
77 * Create Ethernet Source Match
79 * @param matchBuilder MatchBuilder Object without a match yet
80 * @param sMacAddr String representing a source MAC
81 * @return matchBuilder Map MatchBuilder Object with a match
83 public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
85 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
86 EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
87 ethSourceBuilder.setAddress(new MacAddress(sMacAddr));
88 ethernetMatch.setEthernetSource(ethSourceBuilder.build());
89 matchBuilder.setEthernetMatch(ethernetMatch.build());
95 * Create Ethernet Destination Match
97 * @param matchBuilder MatchBuilder Object without a match yet
98 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
99 * @return matchBuilder Map MatchBuilder Object with a match
101 public static MatchBuilder createVlanIdMatch(MatchBuilder matchBuilder, VlanId vlanId) {
102 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
103 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
104 vlanIdBuilder.setVlanId(new VlanId(vlanId));
105 vlanIdBuilder.setVlanIdPresent(true);
106 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
107 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
113 * Create Ethernet Destination Match
115 * @param matchBuilder MatchBuilder Object without a match yet
116 * @param dMacAddr String representing a destination MAC
117 * @return matchBuilder Map MatchBuilder Object with a match
119 public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
121 EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
122 EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
123 ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
125 ethDestinationBuilder.setMask(mask);
127 ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
128 matchBuilder.setEthernetMatch(ethernetMatch.build());
134 * Tunnel ID Match Builder
136 * @param matchBuilder MatchBuilder Object without a match yet
137 * @param tunnelId BigInteger representing a tunnel ID
138 * @return matchBuilder Map MatchBuilder Object with a match
140 public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
142 TunnelBuilder tunnelBuilder = new TunnelBuilder();
143 tunnelBuilder.setTunnelId(tunnelId);
144 matchBuilder.setTunnel(tunnelBuilder.build());
150 * Match ICMP code and type
152 * @param matchBuilder MatchBuilder Object without a match yet
153 * @param type short representing an ICMP type
154 * @param code short representing an ICMP code
155 * @return matchBuilder Map MatchBuilder Object with a match
157 public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
159 EthernetMatchBuilder eth = new EthernetMatchBuilder();
160 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
161 ethTypeBuilder.setType(new EtherType(0x0800L));
162 eth.setEthernetType(ethTypeBuilder.build());
163 matchBuilder.setEthernetMatch(eth.build());
165 // Build the IPv4 Match requied per OVS Syntax
166 IpMatchBuilder ipmatch = new IpMatchBuilder();
167 ipmatch.setIpProtocol((short) 1);
168 matchBuilder.setIpMatch(ipmatch.build());
170 // Build the ICMPv4 Match
171 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
172 icmpv4match.setIcmpv4Type(type);
173 icmpv4match.setIcmpv4Code(code);
174 matchBuilder.setIcmpv4Match(icmpv4match.build());
180 * @param matchBuilder MatchBuilder Object without a match yet
181 * @param dstip String containing an IPv4 prefix
182 * @return matchBuilder Map Object with a match
184 public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
186 EthernetMatchBuilder eth = new EthernetMatchBuilder();
187 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
188 ethTypeBuilder.setType(new EtherType(0x0800L));
189 eth.setEthernetType(ethTypeBuilder.build());
190 matchBuilder.setEthernetMatch(eth.build());
192 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
193 ipv4match.setIpv4Destination(dstip);
195 matchBuilder.setLayer3Match(ipv4match.build());
202 * @param matchBuilder MatchBuilder Object without a match yet
203 * @param srcip String containing an IPv4 prefix
204 * @return matchBuilder Map Object with a match
206 public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
208 EthernetMatchBuilder eth = new EthernetMatchBuilder();
209 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
210 ethTypeBuilder.setType(new EtherType(0x0800L));
211 eth.setEthernetType(ethTypeBuilder.build());
212 matchBuilder.setEthernetMatch(eth.build());
214 Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
215 ipv4match.setIpv4Source(srcip);
216 matchBuilder.setLayer3Match(ipv4match.build());
223 * Create Source TCP Port Match
225 * @param matchBuilder @param matchbuilder MatchBuilder Object without a match yet
226 * @param tcpport Integer representing a source TCP port
227 * @return matchBuilder Map MatchBuilder Object with a match
229 public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
231 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
232 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
233 ethTypeBuilder.setType(new EtherType(0x0800L));
234 ethType.setEthernetType(ethTypeBuilder.build());
235 matchBuilder.setEthernetMatch(ethType.build());
237 IpMatchBuilder ipmatch = new IpMatchBuilder();
238 ipmatch.setIpProtocol((short) 6);
239 matchBuilder.setIpMatch(ipmatch.build());
241 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
242 tcpmatch.setTcpSourcePort(tcpport);
243 matchBuilder.setLayer4Match(tcpmatch.build());
250 * Create Destination TCP Port Match
252 * @param matchBuilder MatchBuilder Object without a match yet
253 * @param tcpDstPort Integer representing a destination TCP port
254 * @return matchBuilder Map MatchBuilder Object with a match
256 public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
258 EthernetMatchBuilder ethType = new EthernetMatchBuilder();
259 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
260 ethTypeBuilder.setType(new EtherType(0x0800L));
261 ethType.setEthernetType(ethTypeBuilder.build());
262 matchBuilder.setEthernetMatch(ethType.build());
264 IpMatchBuilder ipmatch = new IpMatchBuilder();
265 ipmatch.setIpProtocol((short) 6);
266 matchBuilder.setIpMatch(ipmatch.build());
268 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
269 tcpmatch.setTcpDestinationPort(tcpDstPort);
270 matchBuilder.setLayer4Match(tcpmatch.build());
276 * Test match for TCP_Flags
278 * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
280 * Defined TCP Flag values in OVS v2.1+
281 * TCP_FIN 0x001 / TCP_SYN 0x002 / TCP_RST 0x004
282 * TCP_PSH 0x008 / TCP_ACK 0x010 / TCP_URG 0x020
283 * TCP_ECE 0x040 / TCP_CWR 0x080 / TCP_NS 0x100
285 public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, int tcpFlag) {
288 EthernetMatchBuilder ethernetType = new EthernetMatchBuilder();
289 EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
290 ethTypeBuilder.setType(new EtherType(0x0800L));
291 ethernetType.setEthernetType(ethTypeBuilder.build());
292 matchBuilder.setEthernetMatch(ethernetType.build());
294 // TCP Protocol Match
295 IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
296 ipMatch.setIpProtocol((short) 6);
297 matchBuilder.setIpMatch(ipMatch.build());
300 PortNumber dstPort = new PortNumber(80);
301 TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
302 tcpMatch.setTcpDestinationPort(dstPort);
303 matchBuilder.setLayer4Match(tcpMatch.build());
305 TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
306 tcpFlagMatch.setTcpFlag(0x002);
307 matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());