Create common parent for extensions families
[bgpcep.git] / bgp / extensions / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / LinkstateAttributeParserTest.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.bgp.linkstate;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.Lists;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Arrays;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.protocol.bgp.linkstate.impl.attribute.LinkAttributesParser;
25 import org.opendaylight.protocol.bgp.linkstate.impl.attribute.LinkstateAttributeParser;
26 import org.opendaylight.protocol.bgp.linkstate.impl.attribute.NodeAttributesParser;
27 import org.opendaylight.protocol.bgp.linkstate.impl.attribute.PrefixAttributesParser;
28 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
29 import org.opendaylight.protocol.rsvp.parser.impl.RSVPActivator;
30 import org.opendaylight.protocol.rsvp.parser.spi.RSVPExtensionProviderContext;
31 import org.opendaylight.protocol.rsvp.parser.spi.pojo.SimpleRSVPExtensionProviderContext;
32 import org.opendaylight.protocol.util.ByteArray;
33 import org.opendaylight.protocol.util.Ipv4Util;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Attributes1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkProtectionType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.ObjectType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestinationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.PrefixCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.TeLspCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.TeLspAttributesCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.te.lsp.attributes._case.TeLspAttributes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.prefix.state.IgpBits;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.adj.flags.flags.IsisAdjFlagsCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.adj.flags.flags.IsisAdjFlagsCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AssociationType;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.association.object.AssociationObject;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObject;
77
78 public class LinkstateAttributeParserTest {
79
80     private static final byte[] TE_LSP_ATTR = {0x00, (byte) 0x63, 0x00, (byte) 0x30, // TE LSP Attribute Type, lenght, value
81         0x00, (byte) 0x20, (byte) 0x0c, 0x02,  // Lenght, Class, Ctype
82         0x00, 0x00, 0x00, 0x07,
83         0x01, 0x00, 0x00, 0x06,
84         (byte) 0x7f, 0x00, 0x00, 0x05,
85         0x00, 0x00, 0x00, 0x01, //Token Bucket Rate
86         0x00, 0x00, 0x00, 0x02, //Token Bucket Size
87         0x00, 0x00, 0x00, 0x03, //Peak Data Rate
88         0x00, 0x00, 0x00, 0x04, //Minimum Policed Unit
89         0x00, 0x00, 0x00, 0x05, //Maximum Packet Size
90         0x00, (byte) 0x08, (byte) 0xc7, 0x01,  // Lenght, Class, Ctype
91         0x00, 0x01, 0x00, 0x02,
92         0x01, 0x02, 0x03, 0x04,};
93
94     private static final byte[] LINK_ATTR = {0x04, 0x04, 0, 0x04, 0x2a, 0x2a, 0x2a, 0x2a, 0x04, 0x06, 0, 0x04, 0x2b, 0x2b, 0x2b, 0x2b,
95         0x04, 0x40, 0, 0x04, 0, 0, 0, 0, 0x04, 0x41, 0, 0x04, 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, 0x04, 0x42, 0, 0x04,
96         0x46, 0x43, 0x50, 0, 0x04, 0x43, 0, 0x20, 0x46, 0x43, 0x50, 0, 0x46, 0x43, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x44, 0, 0x08, 0, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x45, 0, 0x02, 0, 0x08, 0x04, 0x46, 0, 0x01,
98         (byte) 0xc0, 0x04, 0x47, 0, 0x03, 0, 0, 0x0a, 0x04, 0x48, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70, 0x04, 0x4a,
99         0, 0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32,
100         0x04, 0x4b, 0, 0x07, (byte)-80, 10, 0, 0, (byte)0x0f, (byte)0xff, (byte)0xff, // sr-adj
101         0x04, 0x4b, 0, 0x07, (byte)-80, 10, 0, 0, (byte)0x0f, (byte)0xff, (byte)0xef, // sr-adj
102         0x04, 0x4c, 0, 0x0d, (byte)-80, 10, 0, 0, 1, 2, 3, 4, 5, 6, (byte)0x0f, (byte)0xff, (byte)0xff, // sr-lan-adj
103         0x04, 0x4c, 0, 0x0d, (byte)-80, 10, 0, 0, 1, 2, 3, 4, 5, 6, (byte)0x0f, (byte)0xff, (byte)0xef, // sr-lan-adj
104         0x04, 0x4d, 0, 0x08, 0, 0x05, 0, 0, 0x0a, 0x0b, 0x0c, 0x0d, // peer-node-sid
105         0x04, 0x4e, 0, 0x08, 0, 0x05, 0, 0, 0x0a, 0x0b, 0x0c, 0x0f, // peer-adj-sid
106         0x04, 0x4f, 0, 0x08, 0, 0x05, 0, 0, 0x0a, 0x0b, 0x0c, 0x0e, // peer-set-sid
107         0x04, (byte) 0x88, 0, 0x01, 0x0a };
108
109     private static final byte[] NODE_ATTR = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xbc, 0x04, 0x02, 0,
110         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x04, 0, 0x04,
111         0x29, 0x29, 0x29, 0x29, 0x04, (byte) 0x88, 0, 0x01, 0x0a,
112         4, 0x0a, 0, 0x0c, (byte)0xe0, 0, 1, 2, 3, 4, (byte)0x89, 0, 3, 1, 2, 0, // sr-caps
113         4, 0x0b, 0, 2, 0, 1 // sr-algorythms
114         };
115
116     private static final byte[] NODE_ATTR_S = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xbc, 0x04, 0x02, 0,
117         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x04, 0, 0x04,
118         0x29, 0x29, 0x29, 0x29,
119         4, 0x0a, 0, 0x0c, (byte)0xe0, 0, 1, 2, 3, 4, (byte)0x89, 0, 3, 1, 2, 0, // sr-caps
120         4, 0x0b, 0, 2, 0, 1 // sr-algorythms
121         };
122
123     private static final byte[] P4_ATTR = { 0x04, (byte) 0x80, 0, 0x01, (byte) 0xF0, 0x04, (byte) 0x81, 0, 0x08, 0x12, 0x34, 0x56, 0x78,
124         0x10, 0x30, 0x50, 0x70, 0x04, (byte) 0x82, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70,
125         0x04, (byte) 0x83, 0, 0x04, 0, 0, 0, 0x0a, 0x04, (byte) 0x84, 0, 0x04, 0x0a, 0x19, 0x02, 0x1b,
126         4, (byte)0x86, 0,8, (byte)0xf0, 0, 0,0, 1,2,3,4, // prefix-sid tlv
127         4, (byte)0x87, 0,0x0c, 0, 0, 0, 5, 4, (byte)0x89, 0, 4, 1,2,3,4, // range tlv
128         4, (byte)0x88, 0, 4, 1, (byte)0xf0, 0, 0 // binding sid tlv
129         };
130
131     private RSVPExtensionProviderContext context;
132     private RSVPActivator rsvpActivator;
133     private LinkstateAttributeParser parser;
134
135     @Before
136     public final void setUp() {
137         this.context = new SimpleRSVPExtensionProviderContext();
138         this.rsvpActivator = new RSVPActivator();
139         this.rsvpActivator.start(this.context);
140         this.parser = new LinkstateAttributeParser(false,this.context.getRsvpRegistry());
141     }
142     private static AttributesBuilder createBuilder(final ObjectType type) {
143         return new AttributesBuilder().addAugmentation(
144             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1.class,
145             new Attributes1Builder().setMpReachNlri(
146                 new MpReachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setAdvertizedRoutes(
147                     new AdvertizedRoutesBuilder().setDestinationType(
148                         new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
149                             new DestinationLinkstateBuilder().setCLinkstateDestination(
150                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setObjectType(type).setProtocolId(ProtocolId.IsisLevel1).build())).build()).build()).build()).build()).build());
151     }
152
153     private static AttributesBuilder createUnreachBuilder(final ObjectType type) {
154         return new AttributesBuilder().addAugmentation(
155             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2.class,
156             new Attributes2Builder().setMpUnreachNlri(
157                 new MpUnreachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setWithdrawnRoutes(
158                     new WithdrawnRoutesBuilder().setDestinationType(
159                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
160                             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
161                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setObjectType(type).setProtocolId(ProtocolId.IsisLevel1).build())).build()).build()).build()).build()).build());
162     }
163
164     @Test
165     public void testGetNlriType() throws BGPParsingException {
166         final ByteBuf b = Unpooled.buffer();
167         AttributesBuilder builder = new AttributesBuilder();
168         this.parser.parseAttribute(b, builder);
169         assertEquals(0, b.readableBytes());
170         builder = new AttributesBuilder();
171
172         final Attributes1Builder builder1 = new Attributes1Builder();
173         builder.addAugmentation(Attributes1.class, builder1.build());
174         this.parser.parseAttribute(b, builder);
175         assertEquals(0, b.readableBytes());
176         builder = new AttributesBuilder();
177
178         builder.addAugmentation(Attributes1.class, builder1.setMpReachNlri(
179             new MpReachNlriBuilder().setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv4CaseBuilder().build()).build()).build()).build());
180         this.parser.parseAttribute(b, builder);
181         assertEquals(0, b.readableBytes());
182         builder = new AttributesBuilder();
183
184         final Attributes2Builder builder2 = new Attributes2Builder();
185         builder.addAugmentation(Attributes2.class, builder2.build());
186         this.parser.parseAttribute(b, builder);
187         assertEquals(0, b.readableBytes());
188         builder = new AttributesBuilder();
189
190         builder.addAugmentation(Attributes2.class, builder2.setMpUnreachNlri(
191             new MpUnreachNlriBuilder().setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder().build()).build()).build()).build());
192         this.parser.parseAttribute(b, builder);
193         assertEquals(0, b.readableBytes());
194     }
195
196     @Test
197     public void testPositiveLinks() throws BGPParsingException {
198         final AttributesBuilder builder = createBuilder(new LinkCaseBuilder().build());
199         this.parser.parseAttribute(Unpooled.copiedBuffer(LINK_ATTR), builder);
200         final Attributes1 attrs = builder.augmentation(Attributes1.class);
201         final LinkAttributes ls = ((LinkAttributesCase) attrs.getLinkStateAttribute()).getLinkAttributes();
202         assertNotNull(ls);
203
204         assertEquals("42.42.42.42", ls.getLocalIpv4RouterId().getValue());
205         assertEquals("43.43.43.43", ls.getRemoteIpv4RouterId().getValue());
206         assertEquals(Long.valueOf(0L), ls.getAdminGroup().getValue());
207         assertArrayEquals(new byte[] { (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80 }, ls.getMaxLinkBandwidth().getValue());
208         assertArrayEquals(new byte[] { (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00 }, ls.getMaxReservableBandwidth().getValue());
209         assertNotNull(ls.getUnreservedBandwidth());
210         assertEquals(8, ls.getUnreservedBandwidth().size());
211         assertEquals(LinkProtectionType.Dedicated1to1, ls.getLinkProtection());
212         assertTrue(ls.getMplsProtocol().isLdp());
213         assertTrue(ls.getMplsProtocol().isRsvpte());
214         assertEquals(new Long(10), ls.getMetric().getValue());
215         assertEquals(2, ls.getSharedRiskLinkGroups().size());
216         assertEquals(305419896, ls.getSharedRiskLinkGroups().get(0).getValue().intValue());
217         assertEquals("12K-2", ls.getLinkName());
218         final IsisAdjFlagsCase flags = new IsisAdjFlagsCaseBuilder().setAddressFamily(Boolean.TRUE).setBackup(Boolean.FALSE).setSet(Boolean.FALSE).build();
219         assertEquals(flags, ls.getSrAdjIds().get(0).getFlags());
220         assertEquals(flags, ls.getSrAdjIds().get(1).getFlags());
221         assertEquals(new Long(1048575L), ((LocalLabelCase)ls.getSrAdjIds().get(0).getSidLabelIndex()).getLocalLabel().getValue());
222         assertEquals(new Long(1048559L), ((LocalLabelCase)ls.getSrAdjIds().get(1).getSidLabelIndex()).getLocalLabel().getValue());
223         assertEquals(new Long(168496141L), ((SidCase) ls.getPeerNodeSid().getSidLabelIndex()).getSid());
224         assertEquals(new Short("5"), ls.getPeerNodeSid().getWeight().getValue());
225         assertEquals(new Long(168496142L), ((SidCase) ls.getPeerSetSids().get(0).getSidLabelIndex()).getSid());
226         assertEquals(new Short("5"), ls.getPeerSetSids().get(0).getWeight().getValue());
227         assertEquals(new Long(168496143L), ((SidCase) ls.getPeerAdjSid().getSidLabelIndex()).getSid());
228         assertEquals(new Short("5"), ls.getPeerAdjSid().getWeight().getValue());
229
230         //serialization
231         final ByteBuf buff = Unpooled.buffer();
232         this.parser.serializeAttribute(builder.build(), buff);
233         buff.skipBytes(3);
234         // there is unresolved TLV at the end, that needs to be cut off
235
236         assertArrayEquals(ByteArray.subByte(LINK_ATTR, 0, LINK_ATTR.length -5), ByteArray.getAllBytes(buff));
237     }
238
239     @Test
240     public void testPositiveNodes() throws BGPParsingException {
241         final AttributesBuilder builder = createBuilder(new NodeCaseBuilder().build());
242         this.parser.parseAttribute(Unpooled.copiedBuffer(NODE_ATTR), builder);
243
244         final Attributes1 attrs = builder.augmentation(Attributes1.class);
245         final NodeAttributes ls = ((NodeAttributesCase) attrs.getLinkStateAttribute()).getNodeAttributes();
246         assertNotNull(ls);
247
248         assertEquals(2, ls.getTopologyIdentifier().size());
249         assertEquals(42, ls.getTopologyIdentifier().get(0).getValue().intValue());
250         assertTrue(ls.getNodeFlags().isOverload());
251         assertFalse(ls.getNodeFlags().isAttached());
252         assertTrue(ls.getNodeFlags().isExternal());
253         assertTrue(ls.getNodeFlags().isAbr());
254         assertTrue(ls.getNodeFlags().isRouter());
255         assertTrue(ls.getNodeFlags().isV6());
256
257         assertEquals("12K-2", ls.getDynamicHostname());
258         assertEquals(2, ls.getIsisAreaId().size());
259         assertEquals("41.41.41.41", ls.getIpv4RouterId().getValue());
260
261         //serialization
262         final ByteBuf buff = Unpooled.buffer();
263         this.parser.serializeAttribute(builder.build(), buff);
264         buff.skipBytes(3);
265         assertArrayEquals(NODE_ATTR_S, ByteArray.getAllBytes(buff));
266     }
267
268     @Test
269     public void testPositiveV4Prefixes() throws BGPParsingException {
270         final AttributesBuilder builder = createUnreachBuilder(new PrefixCaseBuilder().setPrefixDescriptors(
271             new PrefixDescriptorsBuilder().setIpReachabilityInformation(new IpPrefix(new Ipv4Prefix("127.0.0.1/32"))).build()).build());
272         this.parser.parseAttribute(Unpooled.copiedBuffer(P4_ATTR), builder);
273
274         final Attributes1 attrs = builder.augmentation(Attributes1.class);
275         final PrefixAttributes ls = ((PrefixAttributesCase) attrs.getLinkStateAttribute()).getPrefixAttributes();
276         assertNotNull(ls);
277
278         assertNotNull(ls.getSrRange());
279         assertFalse(ls.getSrRange().isInterArea());
280         assertEquals(1, ls.getSrRange().getSubTlvs().size());
281         assertNotNull(ls.getSrBindingSidLabels());
282         final IgpBits ispBits = ls.getIgpBits();
283         assertTrue(ispBits.getUpDown().isUpDown());
284         assertTrue(ispBits.isIsIsUpDown());
285         assertTrue(ispBits.isOspfNoUnicast());
286         assertTrue(ispBits.isOspfLocalAddress());
287         assertTrue(ispBits.isOspfPropagateNssa());
288         assertEquals(2, ls.getRouteTags().size());
289         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }, ls.getRouteTags().get(0).getValue());
290         assertEquals(1, ls.getExtendedTags().size());
291         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50,
292             (byte) 0x70 }, ls.getExtendedTags().get(0).getValue());
293         assertEquals(10, ls.getPrefixMetric().getValue().intValue());
294         assertEquals("10.25.2.27", ls.getOspfForwardingAddress().getIpv4Address().getValue());
295
296         //serialization
297         final ByteBuf buff = Unpooled.buffer();
298         this.parser.serializeAttribute(builder.build(), buff);
299         buff.skipBytes(3);
300         // there is unresolved TLV at the end, that needs to be cut off
301         assertArrayEquals(P4_ATTR, ByteArray.getAllBytes(buff));
302     }
303
304     @Test
305     public void testPositiveTELspAttribute() throws BGPParsingException {
306         final AttributesBuilder builder = createBuilder(new TeLspCaseBuilder().build());
307         this.parser.parseAttribute(Unpooled.copiedBuffer(TE_LSP_ATTR), builder);
308
309         final Attributes1 attrs = builder.augmentation(Attributes1.class);
310         final TeLspAttributes teLspAttributes = ((TeLspAttributesCase) attrs.getLinkStateAttribute()).getTeLspAttributes();
311         assertNotNull(teLspAttributes);
312         final TspecObject tSpec = teLspAttributes.getTspecObject();
313         assertNotNull(tSpec);
314         assertEquals(new Float32(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}), tSpec.getTokenBucketRate());
315         assertEquals(new Float32(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02}), teLspAttributes.getTspecObject().getTokenBucketSize());
316         assertEquals(new Float32(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03}), tSpec.getPeakDataRate());
317         assertEquals(new Long("4"), tSpec.getMinimumPolicedUnit());
318         assertEquals(new Long("5"), tSpec.getMaximumPacketSize());
319
320         final AssociationObject associationObject = teLspAttributes.getAssociationObject();
321         assertEquals(AssociationType.Recovery, associationObject.getAssociationType());
322         final IpAddress ipv4 = new IpAddress(Ipv4Util.addressForByteBuf(Unpooled.copiedBuffer(new byte[]{0x01, 0x02, 0x03, 0x04})));
323         assertEquals(ipv4, associationObject.getIpAddress());
324         final short associationId = 2;
325         assertEquals(associationId, associationObject.getAssociationId().shortValue());
326
327         //serialization
328         final ByteBuf buff = Unpooled.buffer();
329         this.parser.serializeAttribute(builder.build(), buff);
330         assertArrayEquals(TE_LSP_ATTR, ByteArray.getAllBytes(buff));
331         assertTrue(Arrays.equals(TE_LSP_ATTR, ByteArray.getAllBytes(buff)));
332     }
333
334     @Test(expected=UnsupportedOperationException.class)
335     public void testLinkAttributesPrivateConstructor() throws Throwable {
336         final Constructor<LinkAttributesParser> c = LinkAttributesParser.class.getDeclaredConstructor();
337         c.setAccessible(true);
338         try {
339             c.newInstance();
340         } catch (final InvocationTargetException e) {
341             throw e.getCause();
342         }
343     }
344
345     @Test(expected=UnsupportedOperationException.class)
346     public void testNodeAttributesPrivateConstructor() throws Throwable {
347         final Constructor<NodeAttributesParser> c = NodeAttributesParser.class.getDeclaredConstructor();
348         c.setAccessible(true);
349         try {
350             c.newInstance();
351         } catch (final InvocationTargetException e) {
352             throw e.getCause();
353         }
354     }
355
356     @Test(expected=UnsupportedOperationException.class)
357     public void testPrefixAttributesPrivateConstructor() throws Throwable {
358         final Constructor<PrefixAttributesParser> c = PrefixAttributesParser.class.getDeclaredConstructor();
359         c.setAccessible(true);
360         try {
361             c.newInstance();
362         } catch (final InvocationTargetException e) {
363             throw e.getCause();
364         }
365     }
366 }