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