BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPSubobjectParserTest.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 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.fail;
11
12 import java.io.IOException;
13
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROIpPrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
20
21 /**
22  * Tests for subobjects
23  */
24 public class PCEPSubobjectParserTest {
25         final byte[] ipv6bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
26                         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
27         final byte[] ipv6bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
28                         (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
29
30         final byte[] ipv4bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
31         final byte[] ipv4bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 };
32
33         final byte[] asnumber = { (byte) 0xA0, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64 };
34
35         @Test
36         public void testSerDeser() throws PCEPDeserializerException, IOException {
37                 // final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfSubobjects.bin");
38                 // final List<ExplicitRouteSubobject> objsToTest = PCEPEROSubobjectParser.parse(bytesFromFile);
39                 //
40                 // assertEquals(8, objsToTest.size());
41
42                 // final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
43                 // final CSubobject s = parser.parseSubobject(ByteArray.cutBytes(this.asnumber, 2));
44                 //
45                 // assertEquals(s, new AsNumberBuilder().setAsNumber(new AsNumber((long) 0x64)).build());
46                 // assertEquals(objsToTest.get(1), new AsNumberBuilder().setAsNumber(new AsNumber(0x0010L)).build());
47
48                 // assertEquals(objsToTest.get(2), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new
49                 // IPv4Address(this.ipv4bytes1), 0x20), true));
50                 //
51                 // assertEquals(objsToTest.get(3), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new
52                 // IPv4Address(this.ipv4bytes2), 0x15), false));
53                 // assertEquals(objsToTest.get(4), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new
54                 // IPv6Address(this.ipv6bytes1), 0x80), true));
55                 //
56                 // assertEquals(objsToTest.get(5), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new
57                 // IPv6Address(this.ipv6bytes2), 0x16), false));
58                 // final byte[] addr1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
59                 // assertEquals(objsToTest.get(6),
60                 // new EROUnnumberedInterfaceSubobject(new IPv4Address(addr1), new UnnumberedInterfaceIdentifier(0xFFFFFFFFL),
61                 // true));
62                 //
63                 // final byte[] addr2 = { (byte) 0x01, (byte) 0x24, (byte) 0x56, (byte) 0x78 };
64                 // assertEquals(objsToTest.get(7),
65                 // new EROUnnumberedInterfaceSubobject(new IPv4Address(addr2), new UnnumberedInterfaceIdentifier(0x9ABCDEF0L),
66                 // false));
67                 //
68                 // assertArrayEquals(bytesFromFile, PCEPEROSubobjectParser.put(objsToTest));
69
70         }
71
72         @Test
73         @Ignore
74         public void testEROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
75                 // final List<ExplicitRouteSubobject> objsToTest = new ArrayList<ExplicitRouteSubobject>();
76                 // objsToTest.add(new EROType1LabelSubobject(0xFFFF51F2L, true, false));
77                 // objsToTest.add(new EROType1LabelSubobject(0x12345648L, false, true));
78                 // objsToTest.add(new EROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte)
79                 // 0xFF }, true, true));
80                 // objsToTest.add(new EROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false, false));
81                 // objsToTest.add(new EROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF,
82                 // (byte) 0xF1 }, true));
83                 // objsToTest.add(new EROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte)
84                 // 0xFF, (byte) 0xF1,
85                 // (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
86                 // (byte) 0x00,
87                 // (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }, true));
88                 // objsToTest.add(new EROExplicitExclusionRouteSubobject(Arrays.asList((ExcludeRouteSubobject) new
89                 // XROAsNumberSubobject(new AsNumber((long) 2588), true))));
90
91                 // assertEquals(objsToTest, PCEPEROSubobjectParser.parse(PCEPEROSubobjectParser.put(objsToTest)));
92         }
93
94         @Test
95         public void testRROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
96                 // final List<ReportedRouteSubobject> objsToTest = new ArrayList<ReportedRouteSubobject>();
97                 // objsToTest.add(new RROIPAddressSubobject(new IpPrefix(Ipv6Util.prefixForBytes(this.ipv6bytes2, 0x16)), true,
98                 // false));
99                 // objsToTest.add(new RROIPAddressSubobject(new IpPrefix(Ipv4Util.prefixForBytes(this.ipv4bytes1, 0x16)), true,
100                 // false));
101                 // objsToTest.add(new RROType1LabelSubobject(0xFFFF51F2L, true));
102                 // objsToTest.add(new RROType1LabelSubobject(0x12345648L, false));
103                 // objsToTest.add(new RROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte)
104                 // 0xFF }, true));
105                 // objsToTest.add(new RROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false));
106                 // objsToTest.add(new RROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF,
107                 // (byte) 0xF1 }));
108                 // objsToTest.add(new RROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte)
109                 // 0xFF, (byte) 0xF1,
110                 // (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
111                 // (byte) 0x00,
112                 // (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }));
113                 // objsToTest.add(new RROAttributesSubobject(new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
114                 // (byte) 0x00,
115                 // (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
116                 // (byte) 0x55,
117                 // (byte) 0xFF, (byte) 0xF1 }));
118
119                 // assertEquals(objsToTest, PCEPRROSubobjectParser.parse(PCEPRROSubobjectParser.put(objsToTest)));
120         }
121
122         @Test
123         public void testXROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
124                 // final List<ExcludeRouteSubobject> objsToTest = new ArrayList<ExcludeRouteSubobject>();
125                 // objsToTest.add(new XROIPPrefixSubobject(new IpPrefix(Ipv6Util.prefixForBytes(this.ipv6bytes2, 0x16)), true,
126                 // XROSubobjectAttribute.INTERFACE));
127                 // objsToTest.add(new XROIPPrefixSubobject(new IpPrefix(Ipv4Util.prefixForBytes(this.ipv4bytes1, 0x16)), false,
128                 // XROSubobjectAttribute.INTERFACE));
129                 // objsToTest.add(new XROAsNumberSubobject(new AsNumber((long) 0x1234), true));
130                 // objsToTest.add(new XROUnnumberedInterfaceSubobject(new IPv4Address(this.ipv4bytes1), new
131                 // UnnumberedInterfaceIdentifier(0xFFFFFFFFL), true, XROSubobjectAttribute.SRLG));
132                 // objsToTest.add(new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
133
134                 // assertEquals(objsToTest, PCEPXROSubobjectParser.parse(PCEPXROSubobjectParser.put(objsToTest)));
135         }
136
137         //
138         // @Test
139         // public void testDifferentLengthExceptions() {
140         // final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
141         // // for parsing subobjects
142         //
143         // try {
144         // EROAsNumberSubobjectParser.parse(bytes, true);
145         // fail("");
146         // } catch (final PCEPDeserializerException e) {
147         // }
148         //
149         // try {
150         // EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
151         // fail("");
152         // } catch (final PCEPDeserializerException e) {
153         // }
154         //
155         // try {
156         // EROIpPrefixSubobjectParser.parse(bytes, true);
157         // fail("");
158         // } catch (final PCEPDeserializerException e) {
159         // }
160         // }
161
162         @Test
163         public void testNullExceptions() throws PCEPDeserializerException {
164                 final byte[] bytes = null; // not empty but not enought data for parsing
165                 // subobjects
166
167                 try {
168                         new EROAsNumberSubobjectParser().parseSubobject(bytes, false);
169                         fail("");
170                 } catch (final IllegalArgumentException e) {
171                 }
172
173                 try {
174                         new EROUnnumberedInterfaceSubobjectParser().parseSubobject(bytes, false);
175                         fail("");
176                 } catch (final IllegalArgumentException e) {
177                 }
178
179                 try {
180                         new EROIpPrefixSubobjectParser().parseSubobject(bytes, false);
181                         fail("");
182                 } catch (final IllegalArgumentException e) {
183                 }
184         }
185
186         @Test
187         public void testUnknownInstanceExceptions() {
188
189                 // final ExplicitRouteSubobject instance = new ExplicitRouteSubobject() {
190                 // };
191
192                 // try {
193                 // new EROAsNumberSubobjectParser().serializeSubobject(instance);
194                 // fail("");
195                 // } catch (final IllegalArgumentException e) {
196                 // }
197                 //
198                 // try {
199                 // EROUnnumberedInterfaceSubobjectParser.put(instance);
200                 // fail("");
201                 // } catch (final IllegalArgumentException e) {
202                 // }
203
204                 // try {
205                 // EROIpPrefixSubobjectParser.put(instance);
206                 // fail("");
207                 // } catch (final IllegalArgumentException e) {
208                 // }
209
210                 // try {
211                 // final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
212                 // (byte) 0x00,
213                 // (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
214                 // (byte) 0x00 };
215                 // EROIpPrefixSubobjectParser.put(new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr),
216                 // 1), false));
217                 // fail("");
218                 // } catch (final IllegalArgumentException e) {
219                 // }
220                 //
221                 // try {
222                 // EROIPv6PrefixSubobjectParser.put(instance);
223                 // fail("");
224                 // } catch (final IllegalArgumentException e) {
225                 // }
226                 //
227                 // try {
228                 // final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
229                 // EROIPv6PrefixSubobjectParser.put(new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new
230                 // IPv4Address(ipv4addr), 1), false));
231                 // fail("");
232                 // } catch (final IllegalArgumentException e) {
233                 // }
234
235         }
236 }