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