Initial clone of OF plugin 1.0 into the openflowplugin repository with small amendment
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / controller / protocol_plugin / openflow / vendorextension / v6extension / V6ExtensionTest.java
1 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
2
3 import static org.junit.Assert.fail;
4
5 import java.net.InetAddress;
6 import java.net.UnknownHostException;
7 import java.nio.ByteBuffer;
8 import java.util.Arrays;
9
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.openflowplugin.openflow.vendorextension.v6extension.V6Match;
13 import org.openflow.protocol.OFMatch;
14
15 public class V6ExtensionTest {
16
17     @Test
18     public void testFromString() throws UnknownHostException {
19         // This tests creating V6Match using fromString and OFMatch by comparing
20         // the results to each other
21         V6Match match = new V6Match();
22         V6Match match2 = new V6Match();
23
24         OFMatch ofm = new OFMatch();
25         V6Match match4 = new V6Match(ofm);
26
27         match.fromString("");
28         Assert.assertTrue(match.equals(match2));
29         match.fromString("any");
30         Assert.assertTrue(match.equals(match2));
31         Assert.assertTrue(match.equals(match4));
32         try {
33             match.fromString("invalidArgument");
34
35             fail("Did not throw IllegalArgumentException");
36         } catch (IllegalArgumentException e) {
37             // passed test for throwing exception.
38         }
39         try {
40             match.fromString("invalidParameter=abcdefg");
41             fail("Did not throw IllegalArgumentException");
42         } catch (IllegalArgumentException e) {
43             // passed test for throwing exception.
44         }
45
46         match.fromString("input_port=1");
47         match.fromString("dl_dst=20:A0:11:10:00:99");
48         match.fromString("dl_src=00:10:08:22:12:75");
49
50         match.fromString("ip_src=10.1.1.1");
51         match.fromString("ip_dst=1.2.3.4");
52         match.fromString("eth_type=0x800");
53         match.fromString("dl_vlan=10");
54         match.fromString("dl_vpcp=1");
55         match.fromString("nw_proto=6");
56         match.fromString("nw_tos=100");
57         match.fromString("tp_dst=8080");
58         match.fromString("tp_src=60");
59
60         Assert.assertTrue(match.getInputPort() == 1);
61         // Assert.assertTrue(match.getIPv6MatchLen()==6);
62
63         ofm.setInputPort((short) 1);
64         // V6Match is meant for IPv6, but if using OFMatch, it will be set to
65         // IPv4 values, as OF1.0 doesn't support IPv6.
66         InetAddress addr = InetAddress.getByName("10.1.1.1");
67         int ipsrc = ByteBuffer.wrap(addr.getAddress()).getInt();
68         ofm.setNetworkSource(ipsrc);
69
70         addr = InetAddress.getByName("1.2.3.4");
71         int ipdst = ByteBuffer.wrap(addr.getAddress()).getInt();
72         ofm.setNetworkDestination(ipdst);
73
74         byte[] macSrc = { 0x00, 0x10, 0x08, 0x22, 0x12, 0x75 };
75         ofm.setDataLayerSource(macSrc);
76         byte[] macDst = { 0x20, (byte) 0xA0, 0x11, 0x10, 0x00, (byte) 0x99 };
77         ofm.setDataLayerDestination(macDst);
78         ofm.setDataLayerType((short) 0x800);
79         ofm.setDataLayerVirtualLan((short) 10);
80         ofm.setDataLayerVirtualLanPriorityCodePoint((byte) 1);
81         ofm.setNetworkProtocol((byte) 6);
82         ofm.setNetworkTypeOfService((byte) 100);
83         ofm.setTransportSource((short) 60);
84         ofm.setTransportDestination((short) 8080);
85
86         V6Match match3 = new V6Match(ofm);
87
88         Assert.assertTrue(match.getInputPort() == match3.getInputPort());
89         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
90                 match3.getDataLayerSource()));
91         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
92                 match3.getDataLayerDestination()));
93         Assert.assertNull(match.getNetworkSrc());
94         Assert.assertNull(match3.getNetworkSrc());
95         Assert.assertNull(match.getNetworkDest());
96         Assert.assertNull(match3.getNetworkDest());
97         Assert.assertTrue(match.getDataLayerVirtualLan() == match3
98                 .getDataLayerVirtualLan());
99         Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match3
100                 .getDataLayerVirtualLanPriorityCodePoint());
101         Assert.assertTrue(match.getNetworkProtocol() == match3
102                 .getNetworkProtocol());
103         Assert.assertTrue(match.getNetworkTypeOfService() == match3
104                 .getNetworkTypeOfService());
105         Assert.assertTrue(match.getTransportSource() == match3
106                 .getTransportSource());
107         Assert.assertTrue(match.getTransportDestination() == match3
108                 .getTransportDestination());
109         Assert.assertTrue(match.getWildcards() == match3.getWildcards());
110
111     }
112
113     @Test
114     public void testReadWriteBuffer() {
115         V6Match match = new V6Match();
116         match.fromString("input_port=1");
117         match.fromString("dl_dst=20:A0:11:10:00:99");
118         match.fromString("dl_src=00:10:08:22:12:75");
119         // writeTo(ByteBuffer) will only write IPv6
120         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
121         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
122         match.fromString("dl_vlan=10");
123         match.fromString("dl_vpcp=1");
124         match.fromString("nw_proto=6");
125         match.fromString("nw_tos=100");
126         match.fromString("tp_dst=8080");
127         match.fromString("tp_src=60");
128         match.fromString("dl_type=0x800");
129
130         ByteBuffer data = ByteBuffer.allocateDirect(10000);
131         match.writeTo(data);
132         data.flip();
133         V6Match match2 = new V6Match();
134         match2.readFrom(data);
135         Assert.assertTrue(match.getInputPort() == match2.getInputPort());
136         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
137                 match2.getDataLayerSource()));
138         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
139                 match2.getDataLayerDestination()));
140
141         Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
142         Assert.assertTrue(match.getNetworkDest()
143                 .equals(match2.getNetworkDest()));
144
145         Assert.assertTrue(match.getDataLayerVirtualLan() == match2
146                 .getDataLayerVirtualLan());
147         Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match2
148                 .getDataLayerVirtualLanPriorityCodePoint());
149         Assert.assertTrue(match.getNetworkProtocol() == match2
150                 .getNetworkProtocol());
151         Assert.assertTrue(match.getNetworkTypeOfService() == match2
152                 .getNetworkTypeOfService());
153         Assert.assertTrue(match.getTransportSource() == match2
154                 .getTransportSource());
155         Assert.assertTrue(match.getTransportDestination() == match2
156                 .getTransportDestination());
157
158     }
159
160     @Test
161     public void testClone() {
162         V6Match match = new V6Match();
163         match.fromString("input_port=1");
164         match.fromString("dl_dst=20:A0:11:10:00:99");
165         match.fromString("dl_src=00:10:08:22:12:75");
166         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
167         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
168         match.fromString("dl_vlan=10");
169         match.fromString("dl_vpcp=1");
170         match.fromString("nw_proto=6");
171         match.fromString("nw_tos=100");
172         match.fromString("tp_dst=8080");
173         match.fromString("tp_src=60");
174         match.fromString("dl_type=0x800");
175
176         V6Match match2 = match.clone();
177         Assert.assertTrue(match.getInputPort() == match2.getInputPort());
178         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
179                 match2.getDataLayerSource()));
180         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
181                 match2.getDataLayerDestination()));
182         Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
183         Assert.assertTrue(match.getNetworkDest()
184                 .equals(match2.getNetworkDest()));
185         Assert.assertTrue(match.getDataLayerVirtualLan() == match2
186                 .getDataLayerVirtualLan());
187         Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match2
188                 .getDataLayerVirtualLanPriorityCodePoint());
189         Assert.assertTrue(match.getNetworkProtocol() == match2
190                 .getNetworkProtocol());
191         Assert.assertTrue(match.getNetworkTypeOfService() == match2
192                 .getNetworkTypeOfService());
193         Assert.assertTrue(match.getTransportSource() == match2
194                 .getTransportSource());
195         Assert.assertTrue(match.getTransportDestination() == match2
196                 .getTransportDestination());
197         Assert.assertTrue(match.getWildcards() == match2.getWildcards());
198     }
199
200     @Test
201     public void testPadding() {
202         // testing that matchlen+pad keeps the 8byte alignment
203         V6Match match = new V6Match();
204
205         match.fromString("input_port=1");
206         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
207         match.fromString("dl_dst=20:A0:11:10:00:99");
208         match.fromString("dl_src=00:10:08:22:12:75");
209         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
210         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333");
211         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
212         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222");
213         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
214         match.fromString("dl_vlan=10");
215         match.fromString("dl_vpcp=1");
216         match.fromString("nw_proto=6");
217         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
218         match.fromString("nw_tos=100");
219         match.fromString("tp_dst=8080");
220         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
221         match.fromString("tp_src=60");
222         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
223     }
224 }