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