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