Merge "Bug 1245: Dropped Binding prefix from Binding Data APIs."
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / IEEE8021QTest.java
1 /*
2  * Copyright (c) 2013-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
9 package org.opendaylight.controller.sal.packet;
10
11 import junit.framework.Assert;
12
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.match.Match;
15 import org.opendaylight.controller.sal.match.MatchType;
16 import org.opendaylight.controller.sal.utils.EtherTypes;
17 import org.opendaylight.controller.sal.utils.NetUtils;
18
19 public class IEEE8021QTest {
20
21     @Test
22     public void testGetPcp() throws Exception {
23         IEEE8021Q vlan = new IEEE8021Q();
24         byte pcp[] = { 5 };
25         vlan.hdrFieldsMap.put("PriorityCodePoint", pcp);
26         byte spcp = vlan.getPcp();
27         Assert.assertTrue(spcp == 5);
28     }
29
30     @Test
31     public void testGetCfi() throws Exception {
32         IEEE8021Q vlan = new IEEE8021Q();
33         byte cfi[] = { 0 };
34         vlan.hdrFieldsMap.put("CanonicalFormatIndicator", cfi);
35         byte scfi = vlan.getCfi();
36         Assert.assertTrue(scfi == 0);
37     }
38
39     @Test
40     public void testGetVid() throws Exception {
41         IEEE8021Q vlan = new IEEE8021Q();
42         byte vid[] = { (byte) 0xF, (byte) 0xFE }; // 4094
43         vlan.hdrFieldsMap.put("VlanIdentifier", vid);
44         short svid = vlan.getVid();
45         Assert.assertTrue(svid == 4094);
46     }
47
48     @Test
49     public void testGetEthertype() throws Exception {
50         IEEE8021Q vlan = new IEEE8021Q();
51         byte ethType[] = { 8, 6 };
52         vlan.hdrFieldsMap.put("EtherType", ethType);
53         short etherType = vlan.getEtherType();
54         Assert.assertTrue(etherType == 2054);
55     }
56
57     @Test
58     public void testSetPcp() throws Exception {
59         IEEE8021Q vlan = new IEEE8021Q();
60         byte pcp = 5;
61         vlan.setPcp(pcp);
62         byte[] bpcp = vlan.hdrFieldsMap.get("PriorityCodePoint");
63         Assert.assertTrue(bpcp[0] == 5);
64     }
65
66     @Test
67     public void testSetCfi() throws Exception {
68         IEEE8021Q vlan = new IEEE8021Q();
69         byte cfi = 0;
70         vlan.setCfi(cfi);
71         byte[] bcfi = vlan.hdrFieldsMap.get("CanonicalFormatIndicator");
72         Assert.assertTrue(bcfi[0] == 0);
73     }
74
75     @Test
76     public void testSetVid() throws Exception {
77         IEEE8021Q vlan = new IEEE8021Q();
78         short vid = 4094; // 0xFFE
79         vlan.setVid(vid);
80         byte[] bvid = vlan.hdrFieldsMap.get("VlanIdentifier");
81         Assert.assertTrue(bvid[0] == (byte) 0xF);
82         Assert.assertTrue(bvid[1] == (byte) 0xFE);
83     }
84
85     @Test
86     public void testSetEthertype() throws Exception {
87         Ethernet eth = new Ethernet();
88         short ethType = 2054; // 0x806
89         eth.setEtherType(ethType);
90         byte[] etherType = eth.hdrFieldsMap.get("EtherType");
91         Assert.assertTrue(etherType[0] == 8);
92         Assert.assertTrue(etherType[1] == 6);
93     }
94
95     @Test
96     public void testDeserialize() throws Exception {
97         short startOffset, numBits;
98         Ethernet eth = new Ethernet();
99         byte[] data = {
100                 (byte) 0xA, (byte) 0xC, (byte) 0xE, (byte) 0x14, (byte) 0x37, (byte) 0x45, // Destination MAC
101                 (byte) 0xA6, (byte) 0xEC, (byte) 0x9C, (byte) 0xAE, (byte) 0xB2, (byte) 0x9F, // Source MAC
102                 (byte) 0x81, (byte) 0x00, // EtherType
103                 (byte) 0xAF, (byte) 0xFE, // PCP, CFI, VLAN ID
104                 8, 6, // EtherType
105                 0, 1, // Hardware Type
106                 8, 0, // Protocol Type
107                 6, // Hardware Address Length
108                 4, // Protocol Address Length
109                 0, 1, // opCode
110                 (byte) 0xA6, (byte) 0xEC, (byte) 0x9C, (byte) 0xAE, (byte) 0xB2, (byte) 0x9F, // Sender Hardware Address
111                 (byte) 0x9, (byte) 0x9, (byte) 0x9, (byte) 0x1, // Sender Protocol Address
112                 (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, // Target Hardware Address
113                 (byte) 0x9, (byte) 0x9, (byte) 0x9, (byte) 0xFE }; // Target Protocol Address
114
115         startOffset = 0;
116         numBits = (short) (data.length * 8);
117         eth.deserialize(data, startOffset, numBits);
118
119         short etherType = eth.getEtherType();
120         Assert.assertTrue(NetUtils.getUnsignedShort(etherType) == 0x8100);
121
122         IEEE8021Q vlanPacket = (IEEE8021Q) eth.getPayload();
123         Assert.assertTrue(vlanPacket.getCfi() == 0);
124         Assert.assertTrue(vlanPacket.getPcp() == 5);
125         Assert.assertTrue(vlanPacket.getVid() == 4094);
126         Assert.assertTrue(vlanPacket.getEtherType() == 2054); // 0x806
127
128         Packet arpPkt = (vlanPacket).getPayload();
129         Assert.assertTrue(arpPkt instanceof ARP);
130     }
131
132     @Test
133     public void testSerialize() throws Exception {
134         Ethernet eth = new Ethernet();
135
136         byte[] dMac = { (byte) 0xA, (byte) 0xC, (byte) 0xE, (byte) 0x14, (byte) 0x37, (byte) 0x45 };
137         byte[] sMac = { (byte) 0xA6, (byte) 0xEC, (byte) 0x9C, (byte) 0xAE, (byte) 0xB2, (byte) 0x9F };
138         eth.setDestinationMACAddress(dMac);
139         eth.setSourceMACAddress(sMac);
140         eth.setEtherType((short) 33024);
141
142         IEEE8021Q vlan = new IEEE8021Q();
143         vlan.setCfi((byte) 0x0);
144         vlan.setPcp((byte) 0x5);
145         vlan.setVid((short) 4094);
146         vlan.setEtherType((short) 2054);
147
148         vlan.setParent(eth);
149         eth.setPayload(vlan);
150
151         ARP arp = new ARP();
152         arp.setHardwareType((short) 1);
153         arp.setProtocolType((short) 2048);
154         arp.setHardwareAddressLength((byte) 0x6);
155         arp.setProtocolAddressLength((byte) 0x4);
156         arp.setOpCode((byte) 0x1);
157
158         byte[] senderHardwareAddress = { (byte) 0xA6, (byte) 0xEC, (byte) 0x9C, (byte) 0xAE, (byte) 0xB2, (byte) 0x9F };
159         byte[] senderProtocolAddress = { (byte) 0x9, (byte) 0x9, (byte) 0x9, (byte) 0x1 };
160         byte[] targetProtocolAddress = { (byte) 0x9, (byte) 0x9, (byte) 0x9, (byte) 0xFE };
161         byte[] targetHardwareAddress = { (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0 };
162         arp.setSenderHardwareAddress(senderHardwareAddress);
163         arp.setSenderProtocolAddress(senderProtocolAddress);
164         arp.setTargetHardwareAddress(targetHardwareAddress);
165         arp.setTargetProtocolAddress(targetProtocolAddress);
166
167         arp.setParent(vlan);
168         vlan.setPayload(arp);
169
170         byte[] data = eth.serialize();
171
172         Assert.assertTrue(data[0] == (byte) 0x0A); // Destination MAC
173         Assert.assertTrue(data[1] == (byte) 0x0C);
174         Assert.assertTrue(data[2] == (byte) 0x0E);
175         Assert.assertTrue(data[3] == (byte) 0x14);
176         Assert.assertTrue(data[4] == (byte) 0x37);
177         Assert.assertTrue(data[5] == (byte) 0x45);
178         Assert.assertTrue(data[6] == (byte) 0xA6); // Source MAC
179         Assert.assertTrue(data[7] == (byte) 0xEC);
180         Assert.assertTrue(data[8] == (byte) 0x9C);
181         Assert.assertTrue(data[9] == (byte) 0xAE);
182         Assert.assertTrue(data[10] == (byte) 0xB2);
183         Assert.assertTrue(data[11] == (byte) 0x9F);
184         Assert.assertTrue(data[12] == (byte) 0x81); // EtherType
185         Assert.assertTrue(data[13] == (byte) 0x00);
186         Assert.assertTrue(data[14] == (byte) 0xAF); // PCP, CFI, VLAN ID
187         Assert.assertTrue(data[15] == (byte) 0xFE);
188         Assert.assertTrue(data[16] == (byte) 0x08); // EtherType
189         Assert.assertTrue(data[17] == (byte) 0x06);
190         Assert.assertTrue(data[18] == (byte) 0x00); // Hardware Type
191         Assert.assertTrue(data[19] == (byte) 0x01);
192         Assert.assertTrue(data[20] == (byte) 0x08); // Protocol Type
193         Assert.assertTrue(data[21] == (byte) 0x0);
194         Assert.assertTrue(data[22] == (byte) 0x6); // Hardware Address Length
195         Assert.assertTrue(data[23] == (byte) 0x4); // Protocol Address Length
196         Assert.assertTrue(data[24] == (byte) 0x0); // opCode
197         Assert.assertTrue(data[25] == (byte) 0x1); // opCode
198         Assert.assertTrue(data[26] == (byte) 0xA6); // Source MAC
199         Assert.assertTrue(data[27] == (byte) 0xEC);
200         Assert.assertTrue(data[28] == (byte) 0x9C);
201         Assert.assertTrue(data[29] == (byte) 0xAE);
202         Assert.assertTrue(data[30] == (byte) 0xB2);
203         Assert.assertTrue(data[31] == (byte) 0x9F);
204         Assert.assertTrue(data[32] == (byte) 0x09); // Sender Protocol Address
205         Assert.assertTrue(data[33] == (byte) 0x09);
206         Assert.assertTrue(data[34] == (byte) 0x09);
207         Assert.assertTrue(data[35] == (byte) 0x01); // Target Hardware Address
208         Assert.assertTrue(data[36] == (byte) 0x00);
209         Assert.assertTrue(data[37] == (byte) 0x00);
210         Assert.assertTrue(data[38] == (byte) 0x00);
211         Assert.assertTrue(data[39] == (byte) 0x00);
212         Assert.assertTrue(data[40] == (byte) 0x00);
213         Assert.assertTrue(data[41] == (byte) 0x00);
214         Assert.assertTrue(data[42] == (byte) 0x09); // Target Protocol Address
215         Assert.assertTrue(data[43] == (byte) 0x09);
216         Assert.assertTrue(data[44] == (byte) 0x09);
217         Assert.assertTrue(data[45] == (byte) 0xFE);
218     }
219
220     @Test
221     public void testGetMatchFullPacket() throws Exception {
222         IEEE8021Q dot1q = new IEEE8021Q();
223         byte priority = 4;
224         short vlanId = 59;
225         short ethType = EtherTypes.IPv4.shortValue();
226         dot1q.setPcp(priority);
227         dot1q.setVid(vlanId);
228         dot1q.setEtherType(ethType);
229
230         Match match = dot1q.getMatch();
231
232         Assert.assertEquals(priority, (byte) match.getField(MatchType.DL_VLAN_PR).getValue());
233         Assert.assertEquals(vlanId, (short) match.getField(MatchType.DL_VLAN).getValue());
234         Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue());
235     }
236 }