Bug 1254 - Added MatchConvertorImpl unit tests
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorImplV10Test.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;\r
10 \r
11 import java.math.BigInteger;\r
12 \r
13 import org.junit.Assert;\r
14 import org.junit.Before;\r
15 import org.junit.Test;\r
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;\r
17 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;\r
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;\r
27 \r
28 /**\r
29  * @author michal.polkorab\r
30  *\r
31  */\r
32 public class MatchConvertorImplV10Test {\r
33 \r
34     /**\r
35      * Initializes OpenflowPortsUtil\r
36      */\r
37     @Before\r
38     public void startUp() {\r
39         OpenflowPortsUtil.init();\r
40     }\r
41 \r
42     /**\r
43      * Test {@link MatchConvertorImpl#fromOFMatchV10ToSALMatch(MatchV10, BigInteger, OpenflowVersion)}\r
44      */\r
45     @Test\r
46     public void test() {\r
47         MatchV10Builder builder = new MatchV10Builder();\r
48         builder.setWildcards(new FlowWildcardsV10(false, false, false, false,\r
49                 false, false, false, false, false, false));\r
50         builder.setNwSrcMask((short) 24);\r
51         builder.setNwDstMask((short) 16);\r
52         builder.setInPort(6653);\r
53         builder.setDlSrc(new MacAddress("01:01:01:01:01:01"));\r
54         builder.setDlDst(new MacAddress("02:02:02:02:02:02"));\r
55         builder.setDlVlan(128);\r
56         builder.setDlVlanPcp((short) 2);\r
57         builder.setDlType(15);\r
58         builder.setNwTos((short) 16);\r
59         builder.setNwProto((short) 6);\r
60         builder.setNwSrc(new Ipv4Address("1.1.1.2"));\r
61         builder.setNwDst(new Ipv4Address("32.16.8.1"));\r
62         builder.setTpSrc(2048);\r
63         builder.setTpDst(4096);\r
64         MatchV10 match = builder.build();\r
65 \r
66         Match salMatch = MatchConvertorImpl.fromOFMatchV10ToSALMatch(match, new BigInteger("42"), OpenflowVersion.OF10);\r
67 \r
68         Assert.assertEquals("Wrong in port", "openflow:42:6653", salMatch.getInPort().getValue());\r
69         Assert.assertEquals("Wrong dl src", new MacAddress("01:01:01:01:01:01"), salMatch.getEthernetMatch()\r
70                 .getEthernetSource().getAddress());\r
71         Assert.assertEquals("Wrong dl dst", new MacAddress("02:02:02:02:02:02"), salMatch.getEthernetMatch()\r
72                 .getEthernetDestination().getAddress());\r
73         Assert.assertEquals("Wrong dl type", 15, salMatch.getEthernetMatch().getEthernetType().getType().getValue().intValue());\r
74         Assert.assertEquals("Wrong dl vlan", 128, salMatch.getVlanMatch().getVlanId().getVlanId().getValue().intValue());\r
75         Assert.assertEquals("Wrong dl vlan pcp", 2, salMatch.getVlanMatch().getVlanPcp().getValue().intValue());\r
76         Ipv4Match ipv4Match = (Ipv4Match) salMatch.getLayer3Match();\r
77         Assert.assertEquals("Wrong nw src address", "1.1.1.2/24", ipv4Match.getIpv4Source().getValue());\r
78         Assert.assertEquals("Wrong nw dst address", "32.16.8.1/16", ipv4Match.getIpv4Destination().getValue());\r
79         Assert.assertEquals("Wrong ip protocol", 6, salMatch.getIpMatch().getIpProtocol().intValue());\r
80         Assert.assertEquals("Wrong ip proto", null, salMatch.getIpMatch().getIpProto());\r
81         Assert.assertEquals("Wrong ip ecn", null, salMatch.getIpMatch().getIpEcn());\r
82         Assert.assertEquals("Wrong ip dscp", 4, salMatch.getIpMatch().getIpDscp().getValue().intValue());\r
83         TcpMatch tcpMatch = (TcpMatch) salMatch.getLayer4Match();\r
84         Assert.assertEquals("Wrong tp dst", 4096, tcpMatch.getTcpDestinationPort().getValue().intValue());\r
85         Assert.assertEquals("Wrong tp src", 2048, tcpMatch.getTcpSourcePort().getValue().intValue());\r
86     }\r
87 \r
88     /**\r
89      * Test {@link MatchConvertorImpl#fromOFMatchV10ToSALMatch(MatchV10, BigInteger, OpenflowVersion)}\r
90      */\r
91     @Test\r
92     public void testWildcardedMatch() {\r
93         MatchV10Builder builder = new MatchV10Builder();\r
94         builder.setWildcards(new FlowWildcardsV10(true, true, true, true,\r
95                 true, true, true, true, true, true));\r
96         builder.setNwSrcMask((short) 24);\r
97         builder.setNwDstMask((short) 16);\r
98         builder.setInPort(6653);\r
99         builder.setDlSrc(new MacAddress("01:01:01:01:01:01"));\r
100         builder.setDlDst(new MacAddress("02:02:02:02:02:02"));\r
101         builder.setDlVlan(128);\r
102         builder.setDlVlanPcp((short) 2);\r
103         builder.setDlType(15);\r
104         builder.setNwTos((short) 14);\r
105         builder.setNwProto((short) 6);\r
106         builder.setNwSrc(new Ipv4Address("1.1.1.2"));\r
107         builder.setNwDst(new Ipv4Address("32.16.8.1"));\r
108         builder.setTpSrc(2048);\r
109         builder.setTpDst(4096);\r
110         MatchV10 match = builder.build();\r
111 \r
112         Match salMatch = MatchConvertorImpl.fromOFMatchV10ToSALMatch(match, new BigInteger("42"), OpenflowVersion.OF10);\r
113 \r
114         Assert.assertEquals("Wrong in port", null, salMatch.getInPort());\r
115         Assert.assertEquals("Wrong dl match", null, salMatch.getEthernetMatch());\r
116         Assert.assertEquals("Wrong dl vlan match", null, salMatch.getVlanMatch());\r
117         Assert.assertEquals("Wrong layer 3 match", null, salMatch.getLayer3Match());\r
118         Assert.assertEquals("Wrong layer 4 match", null, salMatch.getLayer4Match());\r
119         Assert.assertEquals("Wrong ip match", null, salMatch.getIpMatch());\r
120     }\r
121 \r
122     /**\r
123      * Test {@link MatchConvertorImpl#fromOFMatchV10ToSALMatch(MatchV10, BigInteger, OpenflowVersion)}\r
124      */\r
125     @Test\r
126     public void testWildcardedMatchWithNoValuesSet() {\r
127         MatchV10Builder builder = new MatchV10Builder();\r
128         builder.setWildcards(new FlowWildcardsV10(true, true, true, true,\r
129                 true, true, true, true, true, true));\r
130         MatchV10 match = builder.build();\r
131 \r
132         Match salMatch = MatchConvertorImpl.fromOFMatchV10ToSALMatch(match, new BigInteger("42"), OpenflowVersion.OF10);\r
133 \r
134         Assert.assertEquals("Wrong in port", null, salMatch.getInPort());\r
135         Assert.assertEquals("Wrong dl match", null, salMatch.getEthernetMatch());\r
136         Assert.assertEquals("Wrong dl vlan match", null, salMatch.getVlanMatch());\r
137         Assert.assertEquals("Wrong layer 3 match", null, salMatch.getLayer3Match());\r
138         Assert.assertEquals("Wrong layer 4 match", null, salMatch.getLayer4Match());\r
139         Assert.assertEquals("Wrong ip match", null, salMatch.getIpMatch());\r
140     }\r
141 \r
142     /**\r
143      * Test {@link MatchConvertorImpl#fromOFMatchV10ToSALMatch(MatchV10, BigInteger, OpenflowVersion)}\r
144      */\r
145     @Test\r
146     public void testMatchWithValuesUnset() {\r
147         MatchV10Builder builder = new MatchV10Builder();\r
148         builder.setWildcards(new FlowWildcardsV10(false, false, false, false,\r
149                 false, false, false, false, false, false));\r
150         builder.setNwProto((short) 17);\r
151         builder.setTpSrc(2048);\r
152         builder.setTpDst(4096);\r
153         MatchV10 match = builder.build();\r
154 \r
155         Match salMatch = MatchConvertorImpl.fromOFMatchV10ToSALMatch(match, new BigInteger("42"), OpenflowVersion.OF10);\r
156 \r
157         Assert.assertEquals("Wrong in port", null, salMatch.getInPort());\r
158         Assert.assertEquals("Wrong dl match", null, salMatch.getEthernetMatch());\r
159         Assert.assertEquals("Wrong dl vlan match", null, salMatch.getVlanMatch());\r
160         Assert.assertEquals("Wrong layer 3 match", null, salMatch.getLayer3Match());\r
161         UdpMatch udpMatch = (UdpMatch) salMatch.getLayer4Match();\r
162         Assert.assertEquals("Wrong udp dst", 4096, udpMatch.getUdpDestinationPort().getValue().intValue());\r
163         Assert.assertEquals("Wrong udp src", 2048, udpMatch.getUdpSourcePort().getValue().intValue());\r
164     }\r
165 \r
166     /**\r
167      * Test {@link MatchConvertorImpl#fromOFMatchV10ToSALMatch(MatchV10, BigInteger, OpenflowVersion)}\r
168      */\r
169     @Test(expected=NullPointerException.class)\r
170     public void testEmptyMatch() {\r
171         MatchV10Builder builder = new MatchV10Builder();\r
172         MatchV10 match = builder.build();\r
173 \r
174         MatchConvertorImpl.fromOFMatchV10ToSALMatch(match, new BigInteger("42"), OpenflowVersion.OF10);\r
175     }\r
176 }