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