Merge "Initial code drop of Netty.io integration"
[bgpcep.git] / pcep / ietf-stateful07 / src / test / java / org / opendaylight / protocol / pcep / ietf / PCEPTlvParserTest.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.ietf;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import org.junit.Test;
14 import org.opendaylight.protocol.concepts.Ipv4Util;
15 import org.opendaylight.protocol.concepts.Ipv6Util;
16 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LSPIdentifierIpv4TlvParser;
17 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LSPIdentifierIpv6TlvParser;
18 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LspSymbolicNameTlvParser;
19 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LspUpdateErrorTlvParser;
20 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07RSVPErrorSpecTlvParser;
21 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCodeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv4CaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv6CaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv4._case.Ipv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpErrorBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserErrorBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv4ExtendedTunnelId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
48
49 public class PCEPTlvParserTest {
50
51         private static final byte[] statefulBytes = { 0x00, 0x10, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01 };
52         private static final byte[] symbolicNameBytes = { 0x00, 0x11, 0x00, 0x1C, 0x4d, 0x65, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6f,
53                 0x66, 0x20, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x69, 0x63, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x65, 0x65, 0x65 };
54         private static final byte[] lspUpdateErrorBytes = { 0x00, 0x14, 0x00, 0x04, 0x25, 0x68, (byte) 0x95, 0x03 };
55         private static final byte[] lspIdentifiers4Bytes = { 0x00, 0x12, 0x00, 0x10, 0x12, 0x34, 0x56, 0x78, (byte) 0xFF, (byte) 0xFF, 0x12, 0x34, 0x12, 0x34,
56                 0x56, 0x78, 0x12, 0x34, 0x56, 0x78 };
57         private static final byte[] lspIdentifiers6Bytes = { 0x00, 0x13, 0x00, 0x34, 0x12, 0x34, 0x56, 0x78, (byte) 0x9A, (byte) 0xBC,
58                 (byte) 0xDE, (byte) 0xF0, 0x12, 0x34, 0x56, 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
59                 (byte) 0xF0, 0x12, 0x34, (byte) 0xFF, (byte) 0xFF, 0x12, 0x34, 0x56, 0x78,
60                 0x12, 0x34, 0x56, 0x78, 0x01, 0x23, 0x45, 0x67, 0x01,
61                 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, (byte) 0x9A, (byte) 0xBC,
62                 (byte) 0xDE, (byte) 0xF0, 0x12, 0x34, 0x56, 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
63                 (byte) 0xF0 };
64         private static final byte[] rsvpErrorBytes = { 0x00, 0x15, 0x00, 0x0a, 0x06, 0x01, 0x12, 0x34, 0x56, 0x78, 0x02, (byte) 0x92, 0x16, 0x02, 0x00, 0x00 };
65         private static final byte[] rsvpError6Bytes = { 0x00, 0x15, 0x00, 0x16, 0x06, 0x02, 0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0,
66                 0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, 0x02, (byte) 0xd5, (byte) 0xc5, (byte) 0xd9, 0x00, 0x00 };
67         private static final byte[] userErrorBytes = { 0x00, 0x15, 0x00, 0x13, (byte) 0xc2, 0x01, 0x00, 0x00, 0x30, 0x39, 0x05, 0x09, 0x00, 0x26,
68                 0x75, 0x73, 0x65, 0x72, 0x20, 0x64, 0x65, 0x73, 0x63, 0x00 };
69
70         @Test
71         public void testStatefulTlv() throws PCEPDeserializerException {
72                 final Stateful07StatefulCapabilityTlvParser parser = new Stateful07StatefulCapabilityTlvParser();
73                 final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
74                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(statefulBytes, 4)));
75                 assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
76         }
77
78         @Test
79         public void testSymbolicNameTlv() throws PCEPDeserializerException {
80                 final Stateful07LspSymbolicNameTlvParser parser = new Stateful07LspSymbolicNameTlvParser();
81                 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
82                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med test of symbolic nameeee".getBytes())).build();
83                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(symbolicNameBytes, 4)));
84                 assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
85         }
86
87         @Test
88         public void testLspErrorCodeTlv() throws PCEPDeserializerException {
89                 final Stateful07LspUpdateErrorTlvParser parser = new Stateful07LspUpdateErrorTlvParser();
90                 final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
91                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(lspUpdateErrorBytes, 4)));
92                 assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
93         }
94
95         @Test
96         public void testLspIdentifiers4Tlv() throws PCEPDeserializerException {
97                 final Stateful07LSPIdentifierIpv4TlvParser parser = new Stateful07LSPIdentifierIpv4TlvParser();
98                 final Ipv4Builder afi = new Ipv4Builder();
99                 afi.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
100                 afi.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
101                                 (byte) 0x78 })));
102                 afi.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
103                 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(afi.build()).build()).setLspId(
104                                 new LspId(65535L)).setTunnelId(new TunnelId(4660)).build();
105                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(lspIdentifiers4Bytes, 4)));
106                 assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
107         }
108
109         @Test
110         public void testLspIdentifiers6Tlv() throws PCEPDeserializerException {
111                 final Stateful07LSPIdentifierIpv6TlvParser parser = new Stateful07LSPIdentifierIpv6TlvParser();
112                 final Ipv6Builder afi = new Ipv6Builder();
113                 afi.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
114                                 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
115                                 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
116                 afi.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
117                                 (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
118                                 (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 })));
119                 afi.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
120                                 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
121                                 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
122                 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv6CaseBuilder().setIpv6(afi.build()).build()).setLspId(
123                                 new LspId(4660L)).setTunnelId(new TunnelId(65535)).build();
124                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(lspIdentifiers6Bytes, 4)));
125                 assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
126         }
127
128         @Test
129         public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
130                 final Stateful07RSVPErrorSpecTlvParser parser = new Stateful07RSVPErrorSpecTlvParser();
131                 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
132                 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
133                 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
134                 builder.setCode((short) 146);
135                 builder.setValue(5634);
136                 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
137                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(rsvpErrorBytes, 4)));
138                 assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
139         }
140
141         @Test
142         public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
143                 final Stateful07RSVPErrorSpecTlvParser parser = new Stateful07RSVPErrorSpecTlvParser();
144                 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
145                 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
146                                 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
147                                 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
148                 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
149                 builder.setCode((short) 213);
150                 builder.setValue(50649);
151                 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
152                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(rsvpError6Bytes, 4)));
153                 assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
154         }
155
156         @Test
157         public void testUserErrorSpecTlv() throws PCEPDeserializerException {
158                 final Stateful07RSVPErrorSpecTlvParser parser = new Stateful07RSVPErrorSpecTlvParser();
159                 final UserErrorBuilder builder = new UserErrorBuilder();
160                 builder.setEnterprise(new EnterpriseNumber(12345L));
161                 builder.setSubOrg((short) 5);
162                 builder.setValue(38);
163                 builder.setDescription("user desc");
164                 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserCaseBuilder().setUserError(builder.build()).build()).build();
165                 assertEquals(tlv, parser.parseTlv(ByteArray.cutBytes(userErrorBytes, 4)));
166                 assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
167         }
168 }