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