BUG-47 : PCEP migration to generated DTOs.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPXROSubobjectParserTest.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.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import java.io.IOException;
14 import java.util.List;
15
16 import org.junit.Test;
17 import org.opendaylight.protocol.concepts.IPv4Address;
18 import org.opendaylight.protocol.concepts.IPv4Prefix;
19 import org.opendaylight.protocol.concepts.IPv6Address;
20 import org.opendaylight.protocol.concepts.IPv6Prefix;
21 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
22 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
24 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv4PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv6PrefixSubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
27 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
28 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
29 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
30 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
31 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
32 import org.opendaylight.protocol.util.ByteArray;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
34
35 public class PCEPXROSubobjectParserTest {
36
37         @Test
38         public void testSerDeser() throws PCEPDeserializerException, IOException {
39                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfXROSubobjects.bin");
40                 final List<ExcludeRouteSubobject> objsToTest = PCEPXROSubobjectParser.parse(bytesFromFile);
41
42                 assertEquals(5, objsToTest.size());
43
44                 assertEquals(objsToTest.get(0), new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192,
45                                 (byte) 168, (byte) 0, (byte) 0 }), 16), true, XROSubobjectAttribute.NODE));
46                 assertEquals(objsToTest.get(1), new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(new byte[] { (byte) 0x12,
47                                 (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90,
48                                 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0, (byte) 0 }), 112), true, XROSubobjectAttribute.INTERFACE));
49                 // assertEquals(objsToTest.get(2), new XROUnnumberedInterfaceSubobject(new IPv4Address(new byte[] { (byte) 0,
50                 // (byte) 0, (byte) 0,
51                 // (byte) 0x20 }), new UnnumberedInterfaceIdentifier(0x1234L), false, XROSubobjectAttribute.SRLG));
52                 assertEquals(objsToTest.get(3), new XROAsNumberSubobject(new AsNumber((long) 0x1234), false));
53                 assertEquals(objsToTest.get(4), new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
54
55                 // assertArrayEquals(bytesFromFile, PCEPXROSubobjectParser.put(objsToTest));
56
57         }
58
59         @Test
60         public void testDifferentLengthExceptions() {
61                 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data for parsing subobjects
62
63                 try {
64                         XROAsNumberSubobjectParser.parse(bytes, true);
65                         fail("");
66                 } catch (final PCEPDeserializerException e) {
67                 }
68
69                 try {
70                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
71                         fail("");
72                 } catch (final PCEPDeserializerException e) {
73                 }
74
75                 try {
76                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
77                         fail("");
78                 } catch (final PCEPDeserializerException e) {
79                 }
80
81                 try {
82                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
83                         fail("");
84                 } catch (final PCEPDeserializerException e) {
85                 }
86         }
87
88         @Test
89         public void testNullExceptions() throws PCEPDeserializerException {
90                 final byte[] bytes = null; // not empty but not enought data for parsing subobjects
91
92                 try {
93                         XROAsNumberSubobjectParser.parse(bytes, true);
94                         fail("");
95                 } catch (final IllegalArgumentException e) {
96                 }
97
98                 try {
99                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
100                         fail("");
101                 } catch (final IllegalArgumentException e) {
102                 }
103
104                 try {
105                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
106                         fail("");
107                 } catch (final IllegalArgumentException e) {
108                 }
109
110                 try {
111                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
112                         fail("");
113                 } catch (final IllegalArgumentException e) {
114                 }
115         }
116
117         @Test
118         public void testUnknownInstanceExceptions() {
119
120                 final ExcludeRouteSubobject instance = new ExcludeRouteSubobject(true) {
121                 };
122
123                 try {
124                         XROAsNumberSubobjectParser.put(instance);
125                         fail("");
126                 } catch (final IllegalArgumentException e) {
127                 }
128
129                 try {
130                         XROUnnumberedInterfaceSubobjectParser.put(instance);
131                         fail("");
132                 } catch (final IllegalArgumentException e) {
133                 }
134
135                 try {
136                         XROIPv4PrefixSubobjectParser.put(instance);
137                         fail("");
138                 } catch (final IllegalArgumentException e) {
139                 }
140
141                 try {
142                         final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
143                                         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
144                         XROIPv4PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false, XROSubobjectAttribute.INTERFACE));
145                         fail("");
146                 } catch (final IllegalArgumentException e) {
147                 }
148
149                 try {
150                         XROIPv6PrefixSubobjectParser.put(instance);
151                         fail("");
152                 } catch (final IllegalArgumentException e) {
153                 }
154
155                 try {
156                         final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
157                         XROIPv6PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false, XROSubobjectAttribute.INTERFACE));
158                         fail("");
159                 } catch (final IllegalArgumentException e) {
160                 }
161
162         }
163
164         @Test
165         public void testEmptyExceptions() throws PCEPDeserializerException {
166                 final byte[] bytes = {}; // not empty but not enought data for parsing subobjects
167
168                 try {
169                         XROAsNumberSubobjectParser.parse(bytes, true);
170                         fail("");
171                 } catch (final IllegalArgumentException e) {
172                 }
173
174                 try {
175                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
176                         fail("");
177                 } catch (final IllegalArgumentException e) {
178                 }
179
180                 try {
181                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
182                         fail("");
183                 } catch (final IllegalArgumentException e) {
184                 }
185
186                 try {
187                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
188                         fail("");
189                 } catch (final IllegalArgumentException e) {
190                 }
191         }
192
193 }