refactoring ovsdb of13 neutron
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / mdsalopenflow13 / OF13MdSalMatch.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
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
7  *
8  */
9
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.mdsalopenflow13;
11
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;
34
35 import java.math.BigInteger;
36
37 public class OF13MdSalMatch extends OF13Provider {
38     private static final Logger logger = LoggerFactory.getLogger(OF13MdSalMatch.class);
39
40     /**
41      * Create Ingress Port Match dpidLong, inPort
42      *
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
47      */
48     public static MatchBuilder createInPortMatch(MatchBuilder matchBuilder, Long dpidLong, Long inPort) {
49
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);
54
55         return matchBuilder;
56     }
57
58     /**
59      * Create EtherType Match
60      *
61      * @param matchBuilder Map matchBuilder MatchBuilder Object without a match
62      * @param etherType    Long EtherType
63      * @return matchBuilder Map MatchBuilder Object with a match
64      */
65     public static MatchBuilder createEtherTypeMatch(MatchBuilder matchBuilder, EtherType etherType) {
66
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());
72
73         return matchBuilder;
74     }
75
76     /**
77      * Create Ethernet Source Match
78      *
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
82      */
83     public static MatchBuilder createEthSrcMatch(MatchBuilder matchBuilder, MacAddress sMacAddr) {
84
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());
90
91         return matchBuilder;
92     }
93
94     /**
95      * Create Ethernet Destination Match
96      *
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
100      */
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());
108
109         return matchBuilder;
110     }
111
112     /**
113      * Create Ethernet Destination Match
114      *
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
118      */
119     public static MatchBuilder createDestEthMatch(MatchBuilder matchBuilder, MacAddress dMacAddr, MacAddress mask) {
120
121         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
122         EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
123         ethDestinationBuilder.setAddress(new MacAddress(dMacAddr));
124         if (mask != null) {
125             ethDestinationBuilder.setMask(mask);
126         }
127         ethernetMatch.setEthernetDestination(ethDestinationBuilder.build());
128         matchBuilder.setEthernetMatch(ethernetMatch.build());
129
130         return matchBuilder;
131     }
132
133     /**
134      * Tunnel ID Match Builder
135      *
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
139      */
140     public static MatchBuilder createTunnelIDMatch(MatchBuilder matchBuilder, BigInteger tunnelId) {
141
142         TunnelBuilder tunnelBuilder = new TunnelBuilder();
143         tunnelBuilder.setTunnelId(tunnelId);
144         matchBuilder.setTunnel(tunnelBuilder.build());
145
146         return matchBuilder;
147     }
148
149     /**
150      * Match ICMP code and type
151      *
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
156      */
157     public static MatchBuilder createICMPv4Match(MatchBuilder matchBuilder, short type, short code) {
158
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());
164
165         // Build the IPv4 Match requied per OVS Syntax
166         IpMatchBuilder ipmatch = new IpMatchBuilder();
167         ipmatch.setIpProtocol((short) 1);
168         matchBuilder.setIpMatch(ipmatch.build());
169
170         // Build the ICMPv4 Match
171         Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
172         icmpv4match.setIcmpv4Type(type);
173         icmpv4match.setIcmpv4Code(code);
174         matchBuilder.setIcmpv4Match(icmpv4match.build());
175
176         return matchBuilder;
177     }
178
179     /**
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
183      */
184     public static MatchBuilder createDstL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix dstip) {
185
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());
191
192         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
193         ipv4match.setIpv4Destination(dstip);
194
195         matchBuilder.setLayer3Match(ipv4match.build());
196
197         return matchBuilder;
198
199     }
200
201     /**
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
205      */
206     public static MatchBuilder createSrcL3IPv4Match(MatchBuilder matchBuilder, Ipv4Prefix srcip) {
207
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());
213
214         Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
215         ipv4match.setIpv4Source(srcip);
216         matchBuilder.setLayer3Match(ipv4match.build());
217
218         return matchBuilder;
219
220     }
221
222     /**
223      * Create Source TCP Port Match
224      *
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
228      */
229     public static MatchBuilder createSetSrcTcpMatch(MatchBuilder matchBuilder, PortNumber tcpport) {
230
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());
236
237         IpMatchBuilder ipmatch = new IpMatchBuilder();
238         ipmatch.setIpProtocol((short) 6);
239         matchBuilder.setIpMatch(ipmatch.build());
240
241         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
242         tcpmatch.setTcpSourcePort(tcpport);
243         matchBuilder.setLayer4Match(tcpmatch.build());
244
245         return matchBuilder;
246
247     }
248
249     /**
250      * Create Destination TCP Port Match
251      *
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
255      */
256     public static MatchBuilder createSetDstTcpMatch(MatchBuilder matchBuilder, PortNumber tcpDstPort) {
257
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());
263
264         IpMatchBuilder ipmatch = new IpMatchBuilder();
265         ipmatch.setIpProtocol((short) 6);
266         matchBuilder.setIpMatch(ipmatch.build());
267
268         TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
269         tcpmatch.setTcpDestinationPort(tcpDstPort);
270         matchBuilder.setLayer4Match(tcpmatch.build());
271
272         return matchBuilder;
273     }
274
275     /**
276      * Test match for TCP_Flags
277      *
278      * @return match containing TCP_Flag (), IP Protocol (TCP), TCP_Flag (SYN)
279      * <p/>
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
284      */
285     public static MatchBuilder createTcpFlagMatch(MatchBuilder matchBuilder, int tcpFlag) {
286
287         // Ethertype match
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());
293
294         // TCP Protocol Match
295         IpMatchBuilder ipMatch = new IpMatchBuilder(); // ipv4 version
296         ipMatch.setIpProtocol((short) 6);
297         matchBuilder.setIpMatch(ipMatch.build());
298
299         // TCP Port Match
300         PortNumber dstPort = new PortNumber(80);
301         TcpMatchBuilder tcpMatch = new TcpMatchBuilder();
302         tcpMatch.setTcpDestinationPort(dstPort);
303         matchBuilder.setLayer4Match(tcpMatch.build());
304
305         TcpFlagMatchBuilder tcpFlagMatch = new TcpFlagMatchBuilder();
306         tcpFlagMatch.setTcpFlag(0x002);
307         matchBuilder.setTcpFlagMatch(tcpFlagMatch.build());
308         return matchBuilder;
309     }
310 }