BUG-45 : migrated AsNumber to generated source code.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPObjectParserTest.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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.NoSuchElementException;
19
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.protocol.concepts.Bandwidth;
23 import org.opendaylight.protocol.concepts.IGPMetric;
24 import org.opendaylight.protocol.concepts.IPv4Address;
25 import org.opendaylight.protocol.concepts.IPv4Prefix;
26 import org.opendaylight.protocol.concepts.IPv6Address;
27 import org.opendaylight.protocol.concepts.IPv6Prefix;
28 import org.opendaylight.protocol.concepts.TEMetric;
29 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
30 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
31 import org.opendaylight.protocol.pcep.PCEPErrors;
32 import org.opendaylight.protocol.pcep.PCEPOFCodes;
33 import org.opendaylight.protocol.pcep.PCEPObject;
34 import org.opendaylight.protocol.pcep.PCEPTlv;
35 import org.opendaylight.protocol.pcep.impl.object.PCEPClassTypeObjectParser;
36 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser;
37 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser.PCEPErrorIdentifier;
38 import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
39 import org.opendaylight.protocol.pcep.object.PCEPBranchNodeListObject;
40 import org.opendaylight.protocol.pcep.object.PCEPClassTypeObject;
41 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
42 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
43 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
44 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
45 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
46 import org.opendaylight.protocol.pcep.object.PCEPGlobalConstraintsObject;
47 import org.opendaylight.protocol.pcep.object.PCEPIncludeRouteObject;
48 import org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject;
49 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
50 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
51 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
52 import org.opendaylight.protocol.pcep.object.PCEPNoPathObject;
53 import org.opendaylight.protocol.pcep.object.PCEPNonBranchNodeListObject;
54 import org.opendaylight.protocol.pcep.object.PCEPNotificationObject;
55 import org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject;
56 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
57 import org.opendaylight.protocol.pcep.object.PCEPP2MPEndPointsObject;
58 import org.opendaylight.protocol.pcep.object.PCEPReportedRouteObject;
59 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
60 import org.opendaylight.protocol.pcep.object.PCEPRequestedPathBandwidthObject;
61 import org.opendaylight.protocol.pcep.object.PCEPSecondaryExplicitRouteObject;
62 import org.opendaylight.protocol.pcep.object.PCEPSecondaryRecordRouteObject;
63 import org.opendaylight.protocol.pcep.object.PCEPSvecObject;
64 import org.opendaylight.protocol.pcep.object.PCEPUnreachedDestinationObject;
65 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
66 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
67 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
68 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
69 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
70 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
71 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
72 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
73 import org.opendaylight.protocol.pcep.tlv.LSPStateDBVersionTlv;
74 import org.opendaylight.protocol.pcep.tlv.NoPathVectorTlv;
75 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
76 import org.opendaylight.protocol.pcep.tlv.OrderTlv;
77 import org.opendaylight.protocol.pcep.tlv.OverloadedDurationTlv;
78 import org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv;
79 import org.opendaylight.protocol.util.ByteArray;
80 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
81
82 /**
83  * Used resources<br/>
84  * <br/>
85  * PCEPOpenObject3.bin<br/>
86  * objClass: 1<br/>
87  * objType: 1<br/>
88  * objLength: 8<br/>
89  * version: 1<br/>
90  * Flags:<br/>
91  * - processing: true<br/>
92  * - ignored: true<br/>
93  * <br/>
94  * keepAlive: 30<br/>
95  * deadTimer: 120<br/>
96  * sessionId: 1<br/>
97  * tlvs:NO<br/>
98  * <br/>
99  * PCEPBandwidthObject1LowerBounds.bin<br/>
100  * objClass: 5 <br/>
101  * objType: 1<br/>
102  * objLength: 16<br/>
103  * version: 1<br/>
104  * Flags:<br/>
105  * - processing: true<br/>
106  * - ignored: true<br/>
107  * <br/>
108  * Bandwidth: 0<br/>
109  * <br/>
110  * PCEPBandwidthObject2UpperBounds.bin<br/>
111  * objClass: 5 <br/>
112  * objType: 1<br/>
113  * objLength: 16<br/>
114  * version: 1<br/>
115  * Flags:<br/>
116  * - processing: true<br/>
117  * - ignored: true<br/>
118  * <br/>
119  * Bandwidth: 0xFFFFFFFF<br/>
120  * <br/>
121  * PCEPEndPointsObject1IPv4.bin<br/>
122  * objClass: 4 <br/>
123  * objType: 1<br/>
124  * objLength: 12<br/>
125  * version: 1<br/>
126  * Flags:<br/>
127  * - processing: true<br/>
128  * - ignored: true<br/>
129  * <br/>
130  * src IP: 0xA2F5110E <br/>
131  * dest IP: 0xFFFFFFFF <br/>
132  * <br/>
133  * PCEPEndPointsObject2IPv6.bin<br/>
134  * objClass: 4 <br/>
135  * objType: 2<br/>
136  * objLength: 36<br/>
137  * version: 1<br/>
138  * Flags:<br/>
139  * - processing: true<br/>
140  * - ignored: true<br/>
141  * <br/>
142  * src IP: 0xFFFFFFFFF FFFFFFFFF FFFFFFFFF FFFFFFFFF<br/>
143  * dest IP: 0x00025DD2 FFECA1B6 581E9F50 00000000 <br/>
144  * <br/>
145  * PCEPErrorObject1.bin<br/>
146  * objClass: 13 (RP)<br/>
147  * objType: 1<br/>
148  * objLength: 8<br/>
149  * version: 1<br/>
150  * Flags:<br/>
151  * - processing: false<br/>
152  * - ignored: false<br/>
153  * <br/>
154  * Error-type: 1<br/>
155  * Error-value: 1<br/>
156  * Tlvs: NO<br/>
157  * <br/>
158  * PCEPErrorObject2Invalid.bin<br/>
159  * objClass: 13 (RP)<br/>
160  * objType: 1<br/>
161  * objLength: 8<br/>
162  * version: 1<br/>
163  * Flags:<br/>
164  * - processing: false<br/>
165  * - ignored: false<br/>
166  * <br/>
167  * Error-type: 3<br/>
168  * Error-value: 0<br/>
169  * Tlvs: NO<br/>
170  * <br/>
171  * PCEPErrorObject3.bin<br/>
172  * objClass: 13 (RP)<br/>
173  * objType: 1<br/>
174  * objLength: 8<br/>
175  * version: 1<br/>
176  * Flags:<br/>
177  * - processing: false<br/>
178  * - ignored: false<br/>
179  * <br/>
180  * Error-type: 2<br/>
181  * Error-value: 0<br/>
182  * Tlvs: NO<br/>
183  * <br/>
184  * PCEPLspaObject1LowerBounds.bin<br/>
185  * objClass: 9<br/>
186  * objType: 1<br/>
187  * objLength: 20<br/>
188  * version: 1<br/>
189  * Flags:<br/>
190  * - processing: true<br/>
191  * - ignored: true<br/>
192  * <br/>
193  * Exclude-any: 0x00000000L<br/>
194  * Include-any: 0x00000000L<br/>
195  * Include-all: 0x00000000L<br/>
196  * Setup Prio: 0x00<br/>
197  * Holding Prio: 0x00<br/>
198  * Flags: - L : false<br/>
199  * <br/>
200  * PCEPLspaObject2UpperBounds.bin<br/>
201  * objClass: 9<br/>
202  * objType: 1<br/>
203  * objLength: 20<br/>
204  * version: 1<br/>
205  * Flags:<br/>
206  * - processing: true<br/>
207  * - ignored: true<br/>
208  * <br/>
209  * Exclude-any: 0xFFFFFFFFL<br/>
210  * Include-any: 0xFFFFFFFFL<br/>
211  * Include-all: 0xFFFFFFFFL<br/>
212  * Setup Prio: 0xFF<br/>
213  * Holding Prio: 0xFF<br/>
214  * Flags: - L : true<br/>
215  * <br/>
216  * PCEPLspaObject3RandVals.bin<br/>
217  * objClass: 9<br/>
218  * objType: 1<br/>
219  * objLength: 20<br/>
220  * version: 1<br/>
221  * Flags:<br/>
222  * - processing: true<br/>
223  * - ignored: true<br/>
224  * <br/>
225  * Exclude-any: 0x20A1FEE3L<br/>
226  * Include-any: 0x1A025CC7L<br/>
227  * Include-all: 0x2BB66532L<br/>
228  * Setup Prio: 0x03<br/>
229  * Holding Prio: 0x02<br/>
230  * Flags: - L : true<br/>
231  * <br/>
232  * NoPathObject1WithTLV.bin<br/>
233  * objClass: 3 (RP)<br/>
234  * objType: 1<br/>
235  * objLength: 16<br/>
236  * version: 1<br/>
237  * Flags:<br/>
238  * - processing: false<br/>
239  * - ignored: false<br/>
240  * <br/>
241  * Nature of Issue: 2<br/>
242  * No-Path flags:<br/>
243  * - C: true<br/>
244  * <br/>
245  * tlvs:<br/>
246  * -- NO-PATH-VECTOR<br/>
247  * - flags (0x4000):<br/>
248  * - PCE currently unavailable: false<br/>
249  * - unknown destination: true<br/>
250  * - unknown source: false<br/>
251  * 
252  * <br/>
253  * NoPathObject2WithoutTLV.bin<br/>
254  * objClass: 3 (RP)<br/>
255  * objType: 1<br/>
256  * objLength: 8<br/>
257  * version: 1<br/>
258  * Flags:<br/>
259  * - processing: false<br/>
260  * - ignored: true<br/>
261  * <br/>
262  * Nature of Issue: 16<br/>
263  * No-Path flags:<br/>
264  * - C: false<br/>
265  * <br/>
266  * tlvs:NO<br/>
267  * <br/>
268  * PCEPNotificationObject1WithTlv.bin <br/>
269  * objClass: 12<br/>
270  * objType: 1<br/>
271  * objLength: 16<br/>
272  * version: 1<br/>
273  * Flags:<br/>
274  * - processing: false<br/>
275  * - ignored: false<br/>
276  * <br/>
277  * NT: 1<br/>
278  * NV: 1<br/>
279  * Tlvs:<br/>
280  * - OverloaderDuration(0xFF0000A2L)<br/>
281  * <br/>
282  * PCEPNotificationObject2WithoutTlv.bin <br/>
283  * objClass: 12<br/>
284  * objType: 1<br/>
285  * objLength: 8<br/>
286  * version: 1<br/>
287  * Flags:<br/>
288  * - processing: false<br/>
289  * - ignored: false<br/>
290  * <br/>
291  * NT: 0xFF<br/>
292  * NV: 0xFF<br/>
293  * Tlvs: NO<br/>
294  * <br/>
295  * PCEPOpenObject1.bin<br/>
296  * objClass: 1<br/>
297  * objType: 1<br/>
298  * objLength: 28<br/>
299  * version: 1<br/>
300  * Flags:<br/>
301  * - processing: false<br/>
302  * - ignored: false<br/>
303  * <br/>
304  * keepAlive: 30<br/>
305  * deadTimer: 120<br/>
306  * sessionId: 1<br/>
307  * tlvs:<br/>
308  * - PCEPStatefulCapability<br/>
309  * - LSPStateDBVersionTlv<br/>
310  * - NodeIdentifierTlv<br/>
311  * <br/>
312  * PCEPOpenObject2UpperBoundsNoTlv.bin<br/>
313  * objClass: 1<br/>
314  * objType: 1<br/>
315  * objLength: 34<br/>
316  * version: 1<br/>
317  * Flags:<br/>
318  * - processing: false<br/>
319  * - ignored: false<br/>
320  * <br/>
321  * keepAlive: 0xFF<br/>
322  * deadTimer: 0xFF<br/>
323  * sessionId: 0xFF<br/>
324  * tlvs: NO<br/>
325  * <br/>
326  * PCEPRPObject1.bin<br/>
327  * objClass: 2 (RP)<br/>
328  * objType: 1<br/>
329  * objLength: 12<br/>
330  * version: 1<br/>
331  * Flags:<br/>
332  * - processing: false<br/>
333  * - ignored: false<br/>
334  * <br/>
335  * RP flags:<br/>
336  * - loose/strict: true<br/>
337  * - Bi-directional: false<br/>
338  * - Reoptimization: false<br/>
339  * - Priority: 5<br/>
340  * Request ID: 0xDEADBEEF<br/>
341  * tlvs: NO<br/>
342  * <br/>
343  * PCEPSvecObject1_10ReqIDs.bin <br/>
344  * objClass: 11<br/>
345  * objType: 1<br/>
346  * objLength: 48<br/>
347  * version: 1<br/>
348  * Flags:<br/>
349  * - processing: true<br/>
350  * - ignored: false<br/>
351  * <br/>
352  * Flags:<br/>
353  * - Link diverse: true<br/>
354  * - Node diverse: false<br/>
355  * - SRLG diverse: true<br/>
356  * Reques-ID-numbers:<br/>
357  * #1 - 0xFFFFFFFFL<br/>
358  * #2 - 0x00000000L<br/>
359  * #3 - 0x01234567L<br/>
360  * #4 - 0x89ABCDEFL<br/>
361  * #5 - 0xFEDCBA98L<br/>
362  * #6 - 0x76543210L<br/>
363  * #7 - 0x15825266L<br/>
364  * #8 - 0x48120BBEL<br/>
365  * #9 - 0x25FB7E52L<br/>
366  * #10 - 0xB2F2546BL<br/>
367  * <br/>
368  * PCEPSvecObject2.bin <br/>
369  * objClass: 11<br/>
370  * objType: 1<br/>
371  * objLength: 08<br/>
372  * version: 1<br/>
373  * Flags:<br/>
374  * - processing: false<br/>
375  * - ignored: false<br/>
376  * <br/>
377  * Flags:<br/>
378  * - Link diverse: false<br/>
379  * - Node diverse: false<br/>
380  * - SRLG diverse: false<br/>
381  * Reques-ID-numbers:<br/>
382  * #1 - 0x000000FFL<br/>
383  * PCEPExcludeRouteObject.1.bin <br/>
384  * objClass: 17 <br/>
385  * objType: 1 <br/>
386  * objLength: 20 <br/>
387  * version: 1 <br/>
388  * Flags: <br/>
389  * - fail: true <br/>
390  * Subobjects: <br/>
391  * - XROIPv4PreffixSubobject(192.168.0.0/16, exclude, node) <br/>
392  * - XROASnumber(0x1234) <br/>
393  */
394
395 public class PCEPObjectParserTest {
396
397         IPv4Address ipv4addr = new IPv4Address(new byte[] { (byte) 192, (byte) 168, 1, 8 });
398
399         IPv6Address ipv6addr = new IPv6Address(new byte[] { (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168,
400                         2, 1, (byte) 192, (byte) 168, 2, 1 });
401
402         @SuppressWarnings("unchecked")
403         private static <T extends PCEPObject> void serDeserTest(final String srcFile, final T specObject) throws IOException,
404                         PCEPDeserializerException, PCEPDocumentedException {
405                 final byte[] bytesFromFile = ByteArray.fileToBytes(srcFile);
406                 final T deserSpecObj = (T) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
407                 final byte[] serSpecObj = PCEPObjectFactory.put(Arrays.asList((PCEPObject) specObject));
408
409                 assertEquals(specObject, deserSpecObj);
410                 assertArrayEquals(bytesFromFile, serSpecObj);
411         }
412
413         /**
414          * Standard serialization test<br/>
415          * Used resources:<br/>
416          * - PCEPOpenObject1.bin<br/>
417          * 
418          * @throws PCEPDeserializerException
419          * @throws IOException
420          * @throws PCEPDocumentedException
421          */
422         @Test
423         @Ignore
424         // FIXME: temporary
425         public void testObjectDeserialization() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
426                 PCEPObjectFactory.parseObjects(ByteArray.fileToBytes("src/test/resources/PCEPOpenObject1.bin"));
427         }
428
429         @Test
430         public void testUnknownClass() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
431
432                 final PCEPObject obj = PCEPObjectFactory.parseObjects(ByteArray.fileToBytes("src/test/resources/PCEPObject1UnknownClass.bin")).get(
433                                 0);
434
435                 assertTrue(obj instanceof UnknownObject);
436                 assertEquals(((UnknownObject) obj).getError(), PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
437         }
438
439         @Test
440         public void testUnknownType() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
441                 final PCEPObject obj = PCEPObjectFactory.parseObjects(ByteArray.fileToBytes("src/test/resources/PCEPObject2UnknownType.bin")).get(0);
442
443                 assertTrue(obj instanceof UnknownObject);
444                 assertEquals(((UnknownObject) obj).getError(), PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
445         }
446
447         @Test
448         public void testCloseObjSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
449                 serDeserTest("src/test/resources/PCEPCloseObject1.bin", new PCEPCloseObject(Reason.TOO_MANY_UNKNOWN_MSG));
450         }
451
452         @Test
453         public void testLoadBalancingObjSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
454                 serDeserTest("src/test/resources/PCEPLoadBalancingObject1.bin",
455                                 new PCEPLoadBalancingObject(0xF1, new Bandwidth(Float.intBitsToFloat(0xFFFFFFFF)), true));
456         }
457
458         @Test
459         public void testLspObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
460                 serDeserTest("src/test/resources/PCEPLspObject1NoTlvsUpperBounds.bin", new PCEPLspObject(0xFFFFF, true, false, true, false, null));
461         }
462
463         @Test
464         public void testERObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
465                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PCEPExplicitRouteObject1PackOfSubobjects.bin");
466
467                 final PCEPExplicitRouteObject specObj = (PCEPExplicitRouteObject) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
468
469                 assertEquals(8, specObj.getSubobjects().size());
470
471                 final byte[] bytesActual = PCEPObjectFactory.put(Arrays.asList((PCEPObject) specObj));
472                 assertArrayEquals(bytesFromFile, bytesActual);
473         }
474
475         @Test
476         public void testIRObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
477                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PCEPIncludeRouteObject1PackOfSubobjects.bin");
478
479                 final PCEPIncludeRouteObject specObj = (PCEPIncludeRouteObject) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
480
481                 assertEquals(8, specObj.getSubobjects().size());
482
483                 final byte[] bytesActual = PCEPObjectFactory.put(Arrays.asList((PCEPObject) specObj));
484                 assertArrayEquals(bytesFromFile, bytesActual);
485         }
486
487         @Test
488         public void tesRRObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
489                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PCEPReportedRouteObject1PackOfSubobjects.bin");
490
491                 final PCEPReportedRouteObject specObj = (PCEPReportedRouteObject) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
492
493                 assertEquals(6, specObj.getSubobjects().size());
494
495                 final byte[] bytesActual = PCEPObjectFactory.put(Arrays.asList((PCEPObject) specObj));
496                 assertArrayEquals(bytesFromFile, bytesActual);
497         }
498
499         /**
500          * Test for upper/lower bounds (Serialization/Deserialization)<br/>
501          * Used resources:<br/>
502          * - PCEPBandwidthObject2UpperBounds.bin<br/>
503          * - PCEPBandwidthObject1LowerBounds.bin<br/>
504          * 
505          * @throws IOException
506          * @throws PCEPDeserializerException
507          * @throws PCEPDocumentedException
508          */
509         @Test
510         public void testBandwidthObjectBounds() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
511                 final byte[] bytesFloat = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
512                 serDeserTest("src/test/resources/PCEPBandwidthObject2UpperBounds.bin",
513                                 new PCEPRequestedPathBandwidthObject(new Bandwidth(ByteArray.bytesToFloat(bytesFloat)), true, true));
514                 serDeserTest("src/test/resources/PCEPBandwidthObject1LowerBounds.bin",
515                                 new PCEPRequestedPathBandwidthObject(new Bandwidth(0), true, true));
516         }
517
518         /**
519          * Test for upper/lower bounds of IPv4 EndPoints (Serialization/Deserialization)<br/>
520          * Used resources:<br/>
521          * - PCEPEndPointsObject1IPv4.bin<br/>
522          * 
523          * @throws IOException
524          * @throws PCEPDeserializerException
525          * @throws PCEPDocumentedException
526          */
527         @Test
528         public void testEndPointsObjectSerDeserIPv4() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
529                 final byte[] srcIPBytes = { (byte) 0xA2, (byte) 0xF5, (byte) 0x11, (byte) 0x0E };
530                 final byte[] destIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
531                 serDeserTest("src/test/resources/PCEPEndPointsObject1IPv4.bin",
532                                 new PCEPEndPointsObject<IPv4Address>(new IPv4Address(srcIPBytes), new IPv4Address(destIPBytes)));
533         }
534
535         /**
536          * Test for upper/lower bounds of IPv6 EndPoints (Serialization/Deserialization)<br/>
537          * Used resources:<br/>
538          * - PCEPEndPointsObject2IPv6.bin<br/>
539          * 
540          * @throws IOException
541          * @throws PCEPDeserializerException
542          * @throws PCEPDocumentedException
543          */
544         @Test
545         public void testEndPointsObjectSerDeserIPv6() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
546                 final byte[] destIPBytes = { (byte) 0x00, (byte) 0x02, (byte) 0x5D, (byte) 0xD2, (byte) 0xFF, (byte) 0xEC, (byte) 0xA1,
547                                 (byte) 0xB6, (byte) 0x58, (byte) 0x1E, (byte) 0x9F, (byte) 0x50, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, };
548                 final byte[] srcIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
549                                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
550
551                 serDeserTest("src/test/resources/PCEPEndPointsObject2IPv6.bin",
552                                 new PCEPEndPointsObject<IPv6Address>(new IPv6Address(srcIPBytes), new IPv6Address(destIPBytes)));
553         }
554
555         /**
556          * Test of Serialization/Deserialization of PCEPErrorObjectParser.<br/>
557          * <br/>
558          * Used resources:<br/>
559          * - PCEPErrorObject1.bin<br/>
560          * - PCEPErrorObject3.bin<br/>
561          * 
562          * @throws PCEPDeserializerException
563          * @throws IOException
564          * @throws PCEPDocumentedException
565          */
566         @Test
567         public void testErrorObjectSerDeserWithTlv() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
568                 serDeserTest("src/test/resources/PCEPErrorObject1.bin", new PCEPErrorObject(PCEPErrors.NON_OR_INVALID_OPEN_MSG));
569                 serDeserTest("src/test/resources/PCEPErrorObject3.bin", new PCEPErrorObject(PCEPErrors.CAPABILITY_NOT_SUPPORTED));
570         }
571
572         /**
573          * Test of validity of PCEPErrorObjectParser. Expect throwed NoSuchElementException.<br/>
574          * <br/>
575          * Used resources:<br/>
576          * - PCEPErrorObject2Invalid.bin<br/>
577          * 
578          * @throws NoSuchElementException
579          * @throws IOException
580          * @throws PCEPDocumentedException
581          */
582         @Test(expected = PCEPDeserializerException.class)
583         public void testUnknownError() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
584                 PCEPObjectFactory.parseObjects(ByteArray.fileToBytes("src/test/resources/PCEPErrorObject2Invalid.bin")).get(0);
585         }
586
587         /**
588          * Test for upper/lower bounds of PCEPLspaObject (Serialization/Deserialization)<br/>
589          * Used resources:<br/>
590          * - PCEPLspaObject1LowerBounds.bin<br/>
591          * - PCEPLspaObject2UpperBounds.bin<br/>
592          * - PCEPLspaObject3RandVals.bin<br/>
593          * 
594          * @throws IOException
595          * @throws PCEPDeserializerException
596          * @throws PCEPDocumentedException
597          */
598         @Test
599         public void testLspaObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
600                 serDeserTest("src/test/resources/PCEPLspaObject2UpperBounds.bin",
601                                 new PCEPLspaObject(0xFFFFFFFFL, 0xFFFFFFFFL, 0xFFFFFFFFL, (short) 0xFF, (short) 0xFF, false, true, true, true));
602                 serDeserTest("src/test/resources/PCEPLspaObject1LowerBounds.bin",
603                                 new PCEPLspaObject(0x00000000L, 0x00000000L, 0x00000000L, (short) 0x00, (short) 0x00, false, false, true, true));
604                 serDeserTest("src/test/resources/PCEPLspaObject3RandVals.bin",
605                                 new PCEPLspaObject(0x20A1FEE3L, 0x1A025CC7L, 0x2BB66532L, (short) 0x03, (short) 0x02, false, true, true, true));
606         }
607
608         @Test
609         public void testMetricObjectSerDeserBounds() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
610                 final byte[] bytesFromFileUB = ByteArray.fileToBytes("src/test/resources/PCEPMetricObject2UpperBounds.bin");
611                 final byte[] bytesFromFileLB = ByteArray.fileToBytes("src/test/resources/PCEPMetricObject1LowerBounds.bin");
612
613                 final PCEPMetricObject metricObjectLB = (PCEPMetricObject) PCEPObjectFactory.parseObjects(bytesFromFileLB).get(0);
614                 final PCEPMetricObject metricObjectUB = (PCEPMetricObject) PCEPObjectFactory.parseObjects(bytesFromFileUB).get(0);
615
616                 assertEquals(new PCEPMetricObject(false, false, new IGPMetric(0), true, true), metricObjectLB);
617                 assertEquals(new PCEPMetricObject(false, true, new TEMetric(4026531840L), true, true), metricObjectUB);
618
619                 final byte[] bytesActualLB = PCEPObjectFactory.put(Arrays.asList((PCEPObject) metricObjectLB));
620                 final byte[] bytesActualUB = PCEPObjectFactory.put(Arrays.asList((PCEPObject) metricObjectUB));
621                 assertArrayEquals(bytesFromFileLB, bytesActualLB);
622                 assertArrayEquals(bytesFromFileUB, bytesActualUB);
623         }
624
625         /**
626          * Standard deserialization test + specific test without tlv<br/>
627          * Used resources:<br/>
628          * - NoPathObject1WithTLV.bin<br/>
629          * - NoPathObject2WithoutTLV.bin<br/>
630          * 
631          * @throws PCEPDeserializerException
632          * @throws IOException
633          * @throws PCEPDocumentedException
634          */
635         @Test
636         public void testNoPathObjectDeserialization() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
637                 final List<PCEPTlv> tlvs = new ArrayList<PCEPTlv>(1);
638                 tlvs.add(new NoPathVectorTlv(false, false, true, false, false, false));
639                 serDeserTest("src/test/resources/NoPathObject1WithTLV.bin", new PCEPNoPathObject((short) 2, true, tlvs, false));
640                 serDeserTest("src/test/resources/NoPathObject2WithoutTLV.bin", new PCEPNoPathObject((short) 0x10, false, true));
641
642         }
643
644         /**
645          * Standard serialization test + without tlv<br/>
646          * Used resources:<br/>
647          * - NoPathObject1WithTLV.bin<br/>
648          * - NoPathObject2WithoutTLV.bin<br/>
649          * 
650          * @throws PCEPDeserializerException
651          * @throws IOException
652          * @throws PCEPDocumentedException
653          */
654         @Test
655         public void testNoPathObjectSerialization() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
656                 byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/NoPathObject2WithoutTLV.bin");
657                 PCEPNoPathObject noPathObject = (PCEPNoPathObject) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
658                 byte[] bytesActual = PCEPObjectFactory.put(Arrays.asList((PCEPObject) noPathObject));
659                 assertArrayEquals(bytesFromFile, bytesActual);
660
661                 bytesFromFile = ByteArray.fileToBytes("src/test/resources/NoPathObject1WithTLV.bin");
662                 noPathObject = (PCEPNoPathObject) PCEPObjectFactory.parseObjects(bytesFromFile).get(0);
663                 bytesActual = PCEPObjectFactory.put(Arrays.asList((PCEPObject) noPathObject));
664                 assertArrayEquals(bytesFromFile, bytesActual);
665         }
666
667         /**
668          * Specific test with/without tlvs (Ser/Deser)<br/>
669          * Used resources:<br/>
670          * - PCEPNotificationObject1WithTlv.bin - PCEPNotificationObject2WithoutTlv.bin
671          * 
672          * @throws PCEPDeserializerException
673          * @throws IOException
674          * @throws PCEPDocumentedException
675          */
676         @Test
677         public void testNotifyObjectSerDeserWithTlv() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
678                 final List<PCEPTlv> tlvs = new ArrayList<PCEPTlv>(1);
679                 tlvs.add(new OverloadedDurationTlv(0xFF0000A2));
680                 serDeserTest("src/test/resources/PCEPNotificationObject1WithTlv.bin", new PCEPNotificationObject((short) 1, (short) 1, tlvs));
681                 serDeserTest("src/test/resources/PCEPNotificationObject2WithoutTlv.bin", new PCEPNotificationObject((short) 0xFF, (short) 0xFF));
682         }
683
684         /**
685          * Standard ser deser test<br/>
686          * used resources:<br/>
687          * - PCEPOpenObject1.bin
688          * 
689          * @throws PCEPDeserializerException
690          * @throws IOException
691          * @throws PCEPDocumentedException
692          */
693         @Test
694         @Ignore
695         // FIXME: temporary
696         public void testOpenObjectSerDeser() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
697                 final List<PCEPTlv> tlvs = new ArrayList<PCEPTlv>();
698                 tlvs.add(new PCEStatefulCapabilityTlv(false, true, true));
699                 tlvs.add(new LSPStateDBVersionTlv(0x80));
700                 final byte[] valueBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 };
701                 tlvs.add(new NodeIdentifierTlv(valueBytes));
702                 final PCEPOpenObject specObject = new PCEPOpenObject(30, 120, 1, tlvs);
703
704                 serDeserTest("src/test/resources/PCEPOpenObject1.bin", specObject);
705         }
706
707         /**
708          * Specific test for upper bounds and without tlvs<br/>
709          * Used resources:<br/>
710          * - PCEPOpenObject2UpperBoundsNoTlv.bin
711          * 
712          * @throws PCEPDeserializerException
713          * @throws IOException
714          * @throws PCEPDocumentedException
715          */
716         @Test
717         public void testOpenObjectBoundsWithoutTlvs() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
718                 final List<PCEPTlv> tlvs = new ArrayList<PCEPTlv>();
719                 serDeserTest("src/test/resources/PCEPOpenObject2UpperBoundsNoTlv.bin", new PCEPOpenObject(0xFF, 0xFF, 0xFF, tlvs));
720                 serDeserTest("src/test/resources/PCEPOpenObject2UpperBoundsNoTlv.bin", new PCEPOpenObject(0xFF, 0xFF, 0xFF, null));
721         }
722
723         /**
724          * Standard deserialization test<br/>
725          * Used resources:<br/>
726          * - PCEPRPObject1.bin
727          * 
728          * @throws PCEPDeserializerException
729          * @throws IOException
730          * @throws PCEPDocumentedException
731          */
732         @Test
733         public void testRPObjectSerDeser() throws PCEPDeserializerException, IOException, PCEPDocumentedException {
734                 serDeserTest("src/test/resources/PCEPRPObject1.bin",
735                                 new PCEPRequestParameterObject(true, false, true, true, false, false, false, false, (short) 5, 0xdeadbeefL, false, false));
736                 serDeserTest(
737                                 "src/test/resources/PCEPRPObject2.bin",
738                                 new PCEPRequestParameterObject(true, false, false, false, true, false, true, false, true, (short) 5, 0xdeadbeefL, new ArrayList<PCEPTlv>() {
739                                         private static final long serialVersionUID = 1L;
740
741                                         {
742                                                 this.add(new OrderTlv(0xFFFFFFFFL, 0x00000001L));
743                                         }
744                                 }, false, false));
745         }
746
747         /**
748          * Test for upper/lower bounds of PCEPSvecObject (Serialization/Deserialization)<br/>
749          * Used resources:<br/>
750          * - PCEPSvecObject1_10ReqIDs.bin<br/>
751          * 
752          * @throws IOException
753          * @throws PCEPDeserializerException
754          * @throws PCEPDocumentedException
755          */
756         @Test
757         public void testSvecObjectSerDeser() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
758                 final List<Long> requestIDs = new ArrayList<Long>(10);
759                 requestIDs.add(0xFFFFFFFFL);
760                 requestIDs.add(0x00000000L);
761                 requestIDs.add(0x01234567L);
762                 requestIDs.add(0x89ABCDEFL);
763                 requestIDs.add(0xFEDCBA98L);
764                 requestIDs.add(0x76543210L);
765                 requestIDs.add(0x15825266L);
766                 requestIDs.add(0x48120BBEL);
767                 requestIDs.add(0x25FB7E52L);
768                 requestIDs.add(0xB2F2546BL);
769
770                 serDeserTest("src/test/resources/PCEPSvecObject1_10ReqIDs.bin",
771                                 new PCEPSvecObject(true, false, true, false, true, requestIDs, true));
772         }
773
774         /**
775          * Test for lowest bounds of PCEPSvecObject (Serialization/Deserialization)<br/>
776          * Used resources:<br/>
777          * - PCEPSvecObject2.bin<br/>
778          * 
779          * @throws IOException
780          * @throws PCEPDeserializerException
781          * @throws PCEPDocumentedException
782          */
783         @Test
784         public void testSvecObjectSerDeserNoReqIDs() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
785                 final List<Long> requestIDs = new ArrayList<Long>();
786                 requestIDs.add(0xFFL);
787                 serDeserTest("src/test/resources/PCEPSvecObject2.bin", new PCEPSvecObject(false, false, false, false, false, requestIDs, false));
788         }
789
790         @Test
791         public void testClassTypeObject() throws PCEPDeserializerException, PCEPDocumentedException {
792                 final PCEPClassTypeObject ct = new PCEPClassTypeObject((short) 4);
793                 final PCEPClassTypeObjectParser parser = new PCEPClassTypeObjectParser();
794                 final byte[] bytes = parser.put(ct);
795                 assertEquals(ct, parser.parse(bytes, true, false));
796         }
797
798         /**
799          * Test PCEPExcludeRouteObjectObject (Serialization/Deserialization)<br/>
800          * Used resources:<br/>
801          * - PCEPExcludeRouteObject.1.bin<br/>
802          * 
803          * @throws IOException
804          * @throws PCEPDeserializerException
805          * @throws PCEPDocumentedException
806          */
807         @Test
808         public void testExcludeRouteObject() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
809                 final List<ExcludeRouteSubobject> xroSubobjects = new ArrayList<ExcludeRouteSubobject>();
810                 xroSubobjects.add(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192, (byte) 168,
811                                 (byte) 100, (byte) 100 }), 16), true, XROSubobjectAttribute.NODE));
812                 xroSubobjects.add(new XROAsNumberSubobject(new AsNumber(0x1234L), false));
813
814         }
815
816         @Test
817         public void tesObjectiveFunctionObject() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
818                 serDeserTest("src/test/resources/PCEPObjectiveFunctionObject.1.bin", new PCEPObjectiveFunctionObject(PCEPOFCodes.MBC, true, false));
819         }
820
821         @Test
822         public void tesGlobalConstraintsObject() throws IOException, PCEPDeserializerException, PCEPDocumentedException {
823                 serDeserTest("src/test/resources/PCEPGlobalConstraintsObject.1.bin",
824                                 new PCEPGlobalConstraintsObject((short) 1, (short) 0, (short) 100, (short) 0xFF, true, false));
825         }
826
827         // FIXME: add at least one test with true value
828         @Test
829         public void openObjectWithTlv() throws PCEPDeserializerException, PCEPDocumentedException {
830                 this.testOpenObjectWithSpecTlv(new PCEStatefulCapabilityTlv(false, false, false));
831                 this.testOpenObjectWithSpecTlv(new PCEStatefulCapabilityTlv(false, false, true));
832                 this.testOpenObjectWithSpecTlv(new PCEStatefulCapabilityTlv(false, true, false));
833                 this.testOpenObjectWithSpecTlv(new PCEStatefulCapabilityTlv(false, true, true));
834         }
835
836         private void testOpenObjectWithSpecTlv(final PCEPTlv tlv) throws PCEPDeserializerException, PCEPDocumentedException {
837                 final List<PCEPObject> objs = new ArrayList<PCEPObject>();
838                 final List<PCEPTlv> tlvs = new ArrayList<PCEPTlv>();
839                 tlvs.add(tlv);
840                 final PCEPOpenObject oo = new PCEPOpenObject(30, 120, 0, tlvs);
841                 objs.add(oo);
842                 final byte[] bytes = PCEPObjectFactory.put(objs);
843                 final PCEPObject obj = PCEPObjectFactory.parseObjects(bytes).get(0);
844                 assertEquals(oo, obj);
845         }
846
847         @Test
848         public void testErrorsMapping() {
849                 final PCEPErrorObjectParser.PCEPErrorsMaping mapper = PCEPErrorObjectParser.PCEPErrorsMaping.getInstance();
850
851                 for (final PCEPErrors error : PCEPErrors.values()) {
852                         final PCEPErrorIdentifier errorId = mapper.getFromErrorsEnum(error);
853                         assertEquals(error, mapper.getFromErrorIdentifier(errorId));
854                 }
855         }
856
857         @Test
858         public void testOFCodesMapping() {
859                 final PCEPOFCodesMapping mapper = PCEPOFCodesMapping.getInstance();
860
861                 for (final PCEPOFCodes ofCode : PCEPOFCodes.values()) {
862                         final int ofCodeId = mapper.getFromOFCodesEnum(ofCode);
863                         assertEquals(ofCode, mapper.getFromCodeIdentifier(ofCodeId));
864                 }
865         }
866
867         @SuppressWarnings("unchecked")
868         private static <T extends PCEPObject> void serDeserTestWithoutBin(final T object) throws PCEPDeserializerException,
869                         PCEPDocumentedException {
870                 final byte[] serBytes = PCEPObjectFactory.put(Arrays.asList((PCEPObject) object));
871                 final T deserObj = (T) PCEPObjectFactory.parseObjects(serBytes).get(0);
872
873                 assertEquals(object, deserObj);
874         }
875
876         /*
877          * tests without the need of binary files
878          */
879         @Test
880         public void testBranchNodeObjects() throws PCEPDocumentedException, PCEPDeserializerException {
881                 final List<ExplicitRouteSubobject> eroSubobjects = new ArrayList<ExplicitRouteSubobject>();
882                 eroSubobjects.add(new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192, (byte) 168, 1, 8 }), 16), false));
883                 eroSubobjects.add(new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(new byte[] { (byte) 192, (byte) 168, 2, 1,
884                                 (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168, 2, 1 }), 64), false));
885
886                 serDeserTestWithoutBin(new PCEPBranchNodeListObject(eroSubobjects, true, false));
887                 serDeserTestWithoutBin(new PCEPNonBranchNodeListObject(eroSubobjects, true, false));
888
889         }
890
891         @Test
892         public void testSERObjects() throws PCEPDocumentedException, PCEPDeserializerException {
893                 final List<ExplicitRouteSubobject> eroSubobjects = new ArrayList<ExplicitRouteSubobject>();
894                 eroSubobjects.add(new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192, (byte) 168, 1, 8 }), 16), false));
895                 eroSubobjects.add(new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(new byte[] { (byte) 192, (byte) 168, 2, 1,
896                                 (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168, 2, 1, (byte) 192, (byte) 168, 2, 1 }), 64), false));
897
898                 serDeserTestWithoutBin(new PCEPSecondaryExplicitRouteObject(eroSubobjects, true, false));
899         }
900
901         @Test
902         public void testSRRObject() throws PCEPDocumentedException, PCEPDeserializerException {
903                 final List<ReportedRouteSubobject> rroSubobjects = new ArrayList<ReportedRouteSubobject>();
904                 rroSubobjects.add(new RROIPAddressSubobject<IPv4Prefix>(new IPv4Prefix(this.ipv4addr, 16), true, false));
905                 rroSubobjects.add(new RROIPAddressSubobject<IPv6Prefix>(new IPv6Prefix(this.ipv6addr, 64), false, true));
906
907                 serDeserTestWithoutBin(new PCEPSecondaryRecordRouteObject(rroSubobjects, true, false));
908         }
909
910         @Test
911         public void testP2MPEndpointsObjects() throws PCEPDeserializerException, PCEPDocumentedException {
912                 serDeserTestWithoutBin(new PCEPP2MPEndPointsObject<IPv4Address>(2, this.ipv4addr, Arrays.asList(this.ipv4addr, this.ipv4addr,
913                                 this.ipv4addr), true, false));
914                 serDeserTestWithoutBin(new PCEPP2MPEndPointsObject<IPv4Address>(1, this.ipv4addr, Arrays.asList(this.ipv4addr), true, false));
915                 serDeserTestWithoutBin(new PCEPP2MPEndPointsObject<IPv6Address>(2, this.ipv6addr, Arrays.asList(this.ipv6addr, this.ipv6addr,
916                                 this.ipv6addr), true, false));
917                 serDeserTestWithoutBin(new PCEPP2MPEndPointsObject<IPv6Address>(1, this.ipv6addr, Arrays.asList(this.ipv6addr), true, false));
918         }
919
920         @Test
921         public void testUnreachedDestinationObjects() throws PCEPDeserializerException, PCEPDocumentedException {
922                 serDeserTestWithoutBin(new PCEPUnreachedDestinationObject<IPv4Address>(Arrays.asList(this.ipv4addr, this.ipv4addr, this.ipv4addr), true, false));
923                 serDeserTestWithoutBin(new PCEPUnreachedDestinationObject<IPv4Address>(Arrays.asList(this.ipv4addr), true, false));
924                 serDeserTestWithoutBin(new PCEPUnreachedDestinationObject<IPv6Address>(Arrays.asList(this.ipv6addr, this.ipv6addr, this.ipv6addr), true, false));
925                 serDeserTestWithoutBin(new PCEPUnreachedDestinationObject<IPv6Address>(Arrays.asList(this.ipv6addr), true, false));
926         }
927 }