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