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