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