Merge "Speed up packetin throttling"
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorV10ImplTest.java
1 /*
2  * Copyright (c) 2014-2015 Cisco Systems, Inc. and others.  All rights reserved.
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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.math.BigInteger;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
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.match.layer._4.match.UdpMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
42
43 /**
44  * Created by Martin Bobak mbobak@cisco.com on 8/30/14.
45  */
46 public class MatchConvertorV10ImplTest {
47
48     private static final MatchConvertorV10Impl matchConvertorV10 = new MatchConvertorV10Impl();
49     private static final BigInteger dataPathId = BigInteger.TEN;
50     private static final long ETH_TYPE_802_3 = (long) 0x0000;
51     private static final MacAddress ZERO_MAC = MacAddress.getDefaultInstance("00:00:00:00:00:00");
52     private static final MacAddress FF_MAC = MacAddress.getDefaultInstance("ff:ff:ff:ff:ff:ff");
53     private static final String NODE_CONNECTOR_ID = "1234";
54     private static final short ZERO = 0;
55     private static final String DSCP = "0";
56     private static final short IP_PROTOCOL = 6;
57     private static final PortNumber DEFAULT_PORT = new PortNumber(9999);
58     private static final Ipv4Prefix ipv4Prefix = Ipv4Prefix.getDefaultInstance("10.0.0.1/24");
59     private static final VlanId DEFAULT_VLAN_ID = new VlanId(42);
60     private static final Ipv4Address DEFAULT_IPV4_ADDRESS = new Ipv4Address("10.0.0.1");
61     private static final short DEFAULT_MASK = 24;
62
63     @Before
64     public void setup() {
65         OpenflowPortsUtil.init();
66     }
67
68     @Test
69     /**
70      * Test method for {@link MatchConvertorV10Impl#convert(Match,BigInteger)}
71      */
72     public void testConvert() {
73         MatchV10 matchV10 = matchConvertorV10.convert(createL4UdpMatch().build(), dataPathId);
74
75         assertEquals(ZERO_MAC, matchV10.getDlDst());
76         assertEquals(FF_MAC, matchV10.getDlSrc());
77         assertEquals(0, matchV10.getDlType().intValue());
78         assertEquals(0xffff, matchV10.getDlVlan().intValue());
79         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
80         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
81         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID), matchV10.getInPort().intValue());
82         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
83         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
84         assertEquals(0, matchV10.getNwTos().shortValue());
85         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
86         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
87
88         matchV10 = matchConvertorV10.convert(createL4TcpMatch().build(), dataPathId);
89         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
90         assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
91
92         matchV10 = matchConvertorV10.convert(createVlanTcpMatch().build(), dataPathId);
93         assertEquals(DEFAULT_VLAN_ID.getValue().intValue(), matchV10.getDlVlan().intValue());
94
95     }
96
97     /**
98      * ICMPv4 match test for
99      * {@link MatchConvertorV10Impl#convert(Match,BigInteger)}.
100      */
101     @Test
102     public void testConvertIcmpv4() {
103         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
104         Match match = matchBuilder.build();
105         MatchV10 matchV10 = matchConvertorV10.convert(match, dataPathId);
106         Integer zero = 0;
107         boolean wcTpSrc = true;
108         boolean wcTpDst = true;
109         FlowWildcardsV10 wc = new FlowWildcardsV10(
110             false, false, false, true, true, false, false, false,
111             wcTpDst, wcTpSrc);
112         assertEquals(ZERO_MAC, matchV10.getDlDst());
113         assertEquals(FF_MAC, matchV10.getDlSrc());
114         assertEquals(0, matchV10.getDlType().intValue());
115         assertEquals(0xffff, matchV10.getDlVlan().intValue());
116         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
117                      matchV10.getInPort().intValue());
118         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
119                      matchV10.getNwDst().getValue());
120         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
121         assertEquals(0, matchV10.getNwTos().shortValue());
122         assertEquals(zero, matchV10.getTpSrc());
123         assertEquals(zero, matchV10.getTpDst());
124         assertEquals(wc, matchV10.getWildcards());
125
126         // Specify ICMP type only.
127         Integer icmpType = 55;
128         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder().
129             setIcmpv4Type(icmpType.shortValue());
130         wcTpSrc = false;
131         wc = new FlowWildcardsV10(
132             false, false, false, true, true, false, false, false,
133             wcTpDst, wcTpSrc);
134         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
135             build();
136         matchV10 = matchConvertorV10.convert(match, dataPathId);
137         assertEquals(ZERO_MAC, matchV10.getDlDst());
138         assertEquals(FF_MAC, matchV10.getDlSrc());
139         assertEquals(0, matchV10.getDlType().intValue());
140         assertEquals(0xffff, matchV10.getDlVlan().intValue());
141         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
142                      matchV10.getInPort().intValue());
143         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
144                      matchV10.getNwDst().getValue());
145         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
146         assertEquals(0, matchV10.getNwTos().shortValue());
147         assertEquals(icmpType, matchV10.getTpSrc());
148         assertEquals(zero, matchV10.getTpDst());
149         assertEquals(wc, matchV10.getWildcards());
150
151         // Specify ICMP code only.
152         Integer icmpCode = 31;
153         icmpv4MatchBuilder = new Icmpv4MatchBuilder().
154             setIcmpv4Type(null).setIcmpv4Code(icmpCode.shortValue());
155         wcTpSrc = true;
156         wcTpDst = false;
157         wc = new FlowWildcardsV10(
158             false, false, false, true, true, false, false, false,
159             wcTpDst, wcTpSrc);
160         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
161             build();
162         matchV10 = matchConvertorV10.convert(match, dataPathId);
163         assertEquals(ZERO_MAC, matchV10.getDlDst());
164         assertEquals(FF_MAC, matchV10.getDlSrc());
165         assertEquals(0, matchV10.getDlType().intValue());
166         assertEquals(0xffff, matchV10.getDlVlan().intValue());
167         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
168                      matchV10.getInPort().intValue());
169         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
170                      matchV10.getNwDst().getValue());
171         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
172         assertEquals(0, matchV10.getNwTos().shortValue());
173         assertEquals(zero, matchV10.getTpSrc());
174         assertEquals(icmpCode, matchV10.getTpDst());
175         assertEquals(wc, matchV10.getWildcards());
176
177         // Specify both ICMP type and code.
178         icmpType = 11;
179         icmpCode = 22;
180         icmpv4MatchBuilder = new Icmpv4MatchBuilder().
181             setIcmpv4Type(icmpType.shortValue()).
182             setIcmpv4Code(icmpCode.shortValue());
183         wcTpSrc = false;
184         wcTpDst = false;
185         wc = new FlowWildcardsV10(
186             false, false, false, true, true, false, false, false,
187             wcTpDst, wcTpSrc);
188         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
189             build();
190         matchV10 = matchConvertorV10.convert(match, dataPathId);
191         assertEquals(ZERO_MAC, matchV10.getDlDst());
192         assertEquals(FF_MAC, matchV10.getDlSrc());
193         assertEquals(0, matchV10.getDlType().intValue());
194         assertEquals(0xffff, matchV10.getDlVlan().intValue());
195         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
196                      matchV10.getInPort().intValue());
197         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
198                      matchV10.getNwDst().getValue());
199         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
200         assertEquals(0, matchV10.getNwTos().shortValue());
201         assertEquals(icmpType, matchV10.getTpSrc());
202         assertEquals(icmpCode, matchV10.getTpDst());
203         assertEquals(wc, matchV10.getWildcards());
204     }
205
206     private MatchBuilder createL4UdpMatch() {
207         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
208
209         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
210
211         udpMatchBuilder.setUdpDestinationPort(DEFAULT_PORT);
212         udpMatchBuilder.setUdpSourcePort(DEFAULT_PORT);
213         matchBuilder.setLayer4Match(udpMatchBuilder.build());
214
215         return matchBuilder;
216     }
217
218     private MatchBuilder createVlanTcpMatch() {
219         MatchBuilder matchBuilder = createL4TcpMatch();
220         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
221         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
222         vlanIdBuilder.setVlanId(DEFAULT_VLAN_ID);
223         vlanIdBuilder.setVlanIdPresent(true);
224         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
225         matchBuilder.setVlanMatch(vlanMatchBuilder.build());
226         return matchBuilder;
227     }
228
229     private MatchBuilder createL4TcpMatch() {
230         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
231
232         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
233         tcpMatchBuilder.setTcpDestinationPort(DEFAULT_PORT);
234         tcpMatchBuilder.setTcpSourcePort(DEFAULT_PORT);
235         matchBuilder.setLayer4Match(tcpMatchBuilder.build());
236
237         return matchBuilder;
238     }
239
240     private MatchBuilder createMatchBuilderWithDefaults() {
241         MatchBuilder matchBuilder = new MatchBuilder();
242         EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
243         EthernetTypeBuilder ethernetTypeBuilder = new EthernetTypeBuilder();
244
245         //IEEE802.3
246         EtherType etherType = new EtherType(ETH_TYPE_802_3);
247         ethernetTypeBuilder.setType(etherType);
248         ethernetMatchBuilder.setEthernetType(ethernetTypeBuilder.build());
249
250         EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
251         ethernetDestinationBuilder.setAddress(ZERO_MAC);
252         ethernetDestinationBuilder.setMask(ZERO_MAC);
253         ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
254
255         EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
256         ethernetSourceBuilder.setMask(FF_MAC);
257         ethernetSourceBuilder.setAddress(FF_MAC);
258         ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
259         matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
260
261         NodeConnectorId nodeConnectorId = NodeConnectorId.getDefaultInstance(NODE_CONNECTOR_ID);
262
263         matchBuilder.setInPhyPort(nodeConnectorId);
264         matchBuilder.setInPort(nodeConnectorId);
265
266         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
267         ipMatchBuilder.setIpDscp(Dscp.getDefaultInstance(DSCP));
268         ipMatchBuilder.setIpEcn(ZERO);
269         ipMatchBuilder.setIpProto(IpVersion.Ipv4);
270         ipMatchBuilder.setIpProtocol(IP_PROTOCOL);
271         matchBuilder.setIpMatch(ipMatchBuilder.build());
272
273         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
274         ipv4MatchBuilder.setIpv4Destination(ipv4Prefix);
275         ipv4MatchBuilder.setIpv4Source(ipv4Prefix);
276         matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
277         matchBuilder.setInPort(new NodeConnectorId(NODE_CONNECTOR_ID));
278         return matchBuilder;
279     }
280 }