Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / PacketTest.java
1
2 /*
3  * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.packet;
11
12 import java.net.InetAddress;
13 import java.util.Arrays;
14 import java.util.Map;
15
16 import junit.framework.Assert;
17
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.match.Match;
20 import org.opendaylight.controller.sal.match.MatchType;
21 import org.opendaylight.controller.sal.utils.EtherTypes;
22 import org.opendaylight.controller.sal.utils.IPProtocols;
23
24 public class PacketTest {
25
26     @Test
27     public void testDeserialize() throws NoSuchFieldException, Exception {
28         short startOffset, numBits;
29
30         Ethernet eth = new Ethernet();
31         byte[] data = { 10, 12, 14, 20, 55, 69, //DMAC
32                 -90, -20, -100, -82, -78, -97, //SMAC
33                 8, 6, //ethype
34                 0, 1, // hw type
35                 8, 0, // proto (ip)
36                 6, // hw addr len
37                 4, // proto addr len
38                 0, 1, // op codes
39                 -90, -20, -100, -82, -78, -97, //src hw addr
40                 9, 9, 9, 1, // src proto
41                 0, 0, 0, 0, 0, 0, // target hw addr
42                 9, 9, 9, -2 }; // target proto
43
44         startOffset = 0;
45         numBits = 42 * 8;
46         eth.deserialize(data, startOffset, numBits);
47
48         byte[] dMAC = eth.getDestinationMACAddress();
49         byte[] sMAC = eth.getSourceMACAddress();
50         short etherType = eth.getEtherType();
51
52         Assert.assertTrue(dMAC[0] == 10);
53         Assert.assertTrue(dMAC[1] == 12);
54         Assert.assertTrue(dMAC[2] == 14);
55         Assert.assertTrue(dMAC[3] == 20);
56         Assert.assertTrue(dMAC[4] == 55);
57         Assert.assertTrue(dMAC[5] == 69);
58
59         Assert.assertTrue(sMAC[0] == -90);
60         Assert.assertTrue(sMAC[1] == -20);
61         Assert.assertTrue(sMAC[2] == -100);
62         Assert.assertTrue(sMAC[3] == -82);
63         Assert.assertTrue(sMAC[4] == -78);
64         Assert.assertTrue(sMAC[5] == -97);
65
66         Assert.assertTrue(etherType == 0x806);
67
68         ARP arpPacket = (ARP) eth.getPayload();
69
70         Assert.assertTrue(arpPacket.getHardwareType() == (byte)0x1);
71         Assert.assertTrue(arpPacket.getProtocolType() == 2048);
72         Assert.assertTrue(arpPacket.getHardwareAddressLength() == (byte)0x6);
73         Assert.assertTrue(arpPacket.getProtocolAddressLength() == (byte)0x4);
74         Assert.assertTrue(arpPacket.getOpCode() == 1);
75
76         byte[] senderHwAddress = arpPacket.getSenderHardwareAddress();
77         byte[] senderProtocolAddress = arpPacket.getSenderProtocolAddress();
78
79         byte[] targetHwAddress = arpPacket.getTargetHardwareAddress();
80         byte[] targetProtocolAddress = arpPacket.getTargetProtocolAddress();
81
82
83         Assert.assertTrue(senderHwAddress[0] == (byte)0xA6);
84         Assert.assertTrue(senderHwAddress[1] == (byte)0xEC);
85         Assert.assertTrue(senderHwAddress[2] == (byte)0x9C);
86         Assert.assertTrue(senderHwAddress[3] == (byte)0xAE);
87         Assert.assertTrue(senderHwAddress[4] == (byte)0xB2);
88         Assert.assertTrue(senderHwAddress[5] == (byte)0x9F);
89
90         Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
91         Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
92         Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
93         Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
94
95         Assert.assertTrue(targetHwAddress[0] == (byte)0x0);
96         Assert.assertTrue(targetHwAddress[1] == (byte)0x0);
97         Assert.assertTrue(targetHwAddress[2] == (byte)0x0);
98         Assert.assertTrue(targetHwAddress[3] == (byte)0x0);
99         Assert.assertTrue(targetHwAddress[4] == (byte)0x0);
100         Assert.assertTrue(targetHwAddress[5] == (byte)0x0);
101
102         Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
103         Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
104         Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
105         Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
106
107         Assert.assertTrue(targetProtocolAddress[0] == (byte)0x9);
108         Assert.assertTrue(targetProtocolAddress[1] == (byte)0x9);
109         Assert.assertTrue(targetProtocolAddress[2] == (byte)0x9);
110         Assert.assertTrue(targetProtocolAddress[3] == (byte)0xFE);
111     }
112
113     @Test
114     public void testSerialize() throws NoSuchFieldException, Exception {
115         Ethernet eth = new Ethernet();
116         Map<String, byte[]> fCValues = eth.hdrFieldsMap;
117
118         byte[] dMAC = { 10, 12, 14, 20, 55, 69 };
119         byte[] sMAC = { 82, 97, 109, 117, 127, -50 };
120         short etherType = 2054;
121
122         byte[] dMACdata, sMACdata, etherTypedata;
123         byte[] data = new byte[20];
124
125         eth.setDestinationMACAddress(dMAC);
126         eth.setSourceMACAddress(sMAC);
127         eth.setEtherType(etherType);
128
129         dMACdata = fCValues.get("DestinationMACAddress");
130         sMACdata = fCValues.get("SourceMACAddress");
131         etherTypedata = fCValues.get("EtherType");
132
133         Assert.assertTrue(dMACdata[0] == 10);
134         Assert.assertTrue(dMACdata[1] == 12);
135         Assert.assertTrue(dMACdata[2] == 14);
136         Assert.assertTrue(dMACdata[3] == 20);
137         Assert.assertTrue(dMACdata[4] == 55);
138         Assert.assertTrue(dMACdata[5] == 69);
139
140         Assert.assertTrue(sMACdata[0] == 82);
141         Assert.assertTrue(sMACdata[1] == 97);
142         Assert.assertTrue(sMACdata[2] == 109);
143         Assert.assertTrue(sMACdata[3] == 117);
144         Assert.assertTrue(sMACdata[4] == 127);
145         Assert.assertTrue(sMACdata[5] == -50);
146
147         Assert.assertTrue(etherTypedata[0] == 8);
148         Assert.assertTrue(etherTypedata[1] == 6);
149         data = eth.serialize();
150
151         Assert.assertTrue(data[0] == 10);
152         Assert.assertTrue(data[1] == 12);
153         Assert.assertTrue(data[2] == 14);
154         Assert.assertTrue(data[3] == 20);
155         Assert.assertTrue(data[4] == 55);
156         Assert.assertTrue(data[5] == 69);
157
158         Assert.assertTrue(data[6] == 82);
159         Assert.assertTrue(data[7] == 97);
160         Assert.assertTrue(data[8] == 109);
161         Assert.assertTrue(data[9] == 117);
162         Assert.assertTrue(data[10] == 127);
163         Assert.assertTrue(data[11] == -50);
164
165         Assert.assertTrue(data[12] == 8);
166         Assert.assertTrue(data[13] == 6);
167
168     }
169
170     @Test
171     public void testGetMatch() throws Exception {
172         TCP tcp = new TCP();
173         short sport = (short) 11093;
174         short dport = (short) 23;
175         tcp.setSourcePort(sport);
176         tcp.setDestinationPort(dport);
177
178         IPv4 ip = new IPv4();
179         InetAddress sourceAddress = InetAddress.getByName("192.168.100.100");
180         InetAddress destintationAddress = InetAddress.getByName("192.168.100.101");
181         byte protocol = IPProtocols.TCP.byteValue();
182         byte tos = 5;
183         ip.setVersion((byte) 4);
184         ip.setIdentification((short) 5);
185         ip.setDiffServ(tos);
186         ip.setECN((byte) 0);
187         ip.setTotalLength((short) 84);
188         ip.setFlags((byte) 2);
189         ip.setFragmentOffset((short) 0);
190         ip.setTtl((byte) 64);
191         ip.setProtocol(protocol);
192         ip.setDestinationAddress(destintationAddress);
193         ip.setSourceAddress(sourceAddress);
194         ip.setPayload(tcp);
195
196         IEEE8021Q dot1q = new IEEE8021Q();
197         byte priority = 4;
198         short vlanId = 59;
199         short ethType = EtherTypes.IPv4.shortValue();
200         dot1q.setPcp(priority);
201         dot1q.setVid(vlanId);
202         dot1q.setEtherType(ethType);
203         dot1q.setPayload(ip);
204
205         Ethernet eth = new Ethernet();
206         byte smac[] = { (byte) 0xf0, (byte) 0xde, (byte) 0xf1, (byte) 0x71, (byte) 0x72, (byte) 0x8d };
207         byte dmac[] = { (byte) 0xde, (byte) 0x28, (byte) 0xdb, (byte) 0xb3, (byte) 0x7c, (byte) 0xf8 };
208         eth.setDestinationMACAddress(dmac);
209         eth.setSourceMACAddress(smac);
210         eth.setEtherType(EtherTypes.VLANTAGGED.shortValue());
211         eth.setPayload(dot1q);
212
213         Match match = eth.getMatch();
214
215         Assert.assertTrue(Arrays.equals(smac, (byte[]) match.getField(MatchType.DL_SRC).getValue()));
216         Assert.assertTrue(Arrays.equals(dmac, (byte[]) match.getField(MatchType.DL_DST).getValue()));
217         Assert.assertEquals(priority, (byte) match.getField(MatchType.DL_VLAN_PR).getValue());
218         Assert.assertEquals(vlanId, (short) match.getField(MatchType.DL_VLAN).getValue());
219         Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue());
220         Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue());
221         Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue());
222         Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue());
223         Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue());
224         Assert.assertEquals(sport, (short) match.getField(MatchType.TP_SRC).getValue());
225         Assert.assertEquals(dport, (short) match.getField(MatchType.TP_DST).getValue());
226     }
227 }