8afdd08ddde6de35ad41ddcd17ce53ab0f615000
[bgpcep.git] / bgp / 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.Test;
23 import org.opendaylight.protocol.bgp.linkstate.attribute.LinkAttributesParser;
24 import org.opendaylight.protocol.bgp.linkstate.attribute.LinkstateAttributeParser;
25 import org.opendaylight.protocol.bgp.linkstate.attribute.NodeAttributesParser;
26 import org.opendaylight.protocol.bgp.linkstate.attribute.PrefixAttributesParser;
27 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.PathAttributes1;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
58
59 public class LinkstateAttributeParserTest {
60
61     private static final byte[] LINK_ATTR =  { 0x04, 0x04, 0, 0x04, 0x2a, 0x2a, 0x2a, 0x2a, 0x04, 0x06, 0, 0x04, 0x2b, 0x2b, 0x2b, 0x2b,
62         0x04, 0x40, 0, 0x04, 0, 0, 0, 0, 0x04, 0x41, 0, 0x04, 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, 0x04, 0x42, 0, 0x04,
63         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,
64         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,
65         (byte) 0xc0, 0x04, 0x47, 0, 0x03, 0, 0, 0x0a, 0x04, 0x48, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70, 0x04, 0x4a,
66         0, 0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
67
68     private static final byte[] NODE_ATTR = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xb0, 0x04, 0x02, 0,
69         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x04, 0, 0x04,
70         0x29, 0x29, 0x29, 0x29, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
71
72     private static final byte[] NODE_ATTR_S = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xb0, 0x04, 0x02, 0,
73         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x04, 0, 0x04,
74         0x29, 0x29, 0x29, 0x29};
75
76     private static final byte[] P4_ATTR = { 0x04, (byte) 0x80, 0, 0x01, (byte) 0x80, 0x04, (byte) 0x81, 0, 0x08, 0x12, 0x34, 0x56, 0x78,
77         0x10, 0x30, 0x50, 0x70, 0x04, (byte) 0x82, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70, 0x04, (byte) 0x84, 0, 0x04, 0x0a, 0x19, 0x02, 0x1b,
78         0x04, (byte) 0x83, 0, 0x04, 0, 0, 0, 0x0a, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
79
80     private final LinkstateAttributeParser parser = new LinkstateAttributeParser(false);
81
82     private static PathAttributesBuilder createBuilder(final ObjectType type) {
83         return new PathAttributesBuilder().addAugmentation(
84             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1.class,
85             new PathAttributes1Builder().setMpReachNlri(
86                 new MpReachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setAdvertizedRoutes(
87                     new AdvertizedRoutesBuilder().setDestinationType(
88                         new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
89                             new DestinationLinkstateBuilder().setCLinkstateDestination(
90                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setObjectType(type).build())).build()).build()).build()).build()).build());
91     }
92
93     private static PathAttributesBuilder createUnreachBuilder(final ObjectType type) {
94         return new PathAttributesBuilder().addAugmentation(
95             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2.class,
96             new PathAttributes2Builder().setMpUnreachNlri(
97                 new MpUnreachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setWithdrawnRoutes(
98                     new WithdrawnRoutesBuilder().setDestinationType(
99                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
100                             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
101                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setObjectType(type).build())).build()).build()).build()).build()).build());
102     }
103
104     @Test
105     public void testGetNlriType() throws BGPParsingException {
106         final ByteBuf b = Unpooled.buffer();
107         PathAttributesBuilder builder = new PathAttributesBuilder();
108         this.parser.parseAttribute(b, builder);
109         assertEquals(0, b.readableBytes());
110         builder = new PathAttributesBuilder();
111
112         final PathAttributes1Builder builder1 = new PathAttributes1Builder();
113         builder.addAugmentation(PathAttributes1.class, builder1.build());
114         this.parser.parseAttribute(b, builder);
115         assertEquals(0, b.readableBytes());
116         builder = new PathAttributesBuilder();
117
118         builder.addAugmentation(PathAttributes1.class, builder1.setMpReachNlri(
119             new MpReachNlriBuilder().setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv4CaseBuilder().build()).build()).build()).build());
120         this.parser.parseAttribute(b, builder);
121         assertEquals(0, b.readableBytes());
122         builder = new PathAttributesBuilder();
123
124         final PathAttributes2Builder builder2 = new PathAttributes2Builder();
125         builder.addAugmentation(PathAttributes2.class, builder2.build());
126         this.parser.parseAttribute(b, builder);
127         assertEquals(0, b.readableBytes());
128         builder = new PathAttributesBuilder();
129
130         builder.addAugmentation(PathAttributes2.class, builder2.setMpUnreachNlri(
131             new MpUnreachNlriBuilder().setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder().build()).build()).build()).build());
132         this.parser.parseAttribute(b, builder);
133         assertEquals(0, b.readableBytes());
134     }
135
136     @Test
137     public void testPositiveLinks() throws BGPParsingException {
138         final PathAttributesBuilder builder = createBuilder(new LinkCaseBuilder().build());
139         this.parser.parseAttribute(Unpooled.copiedBuffer(LINK_ATTR), builder);
140         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
141         final LinkAttributes ls = ((LinkAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getLinkAttributes();
142         assertNotNull(ls);
143
144         assertEquals("42.42.42.42", ls.getLocalIpv4RouterId().getValue());
145         assertEquals("43.43.43.43", ls.getRemoteIpv4RouterId().getValue());
146         assertEquals(Long.valueOf(0L), ls.getAdminGroup().getValue());
147         assertArrayEquals(new byte[] { (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80 }, ls.getMaxLinkBandwidth().getValue());
148         assertArrayEquals(new byte[] { (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00 }, ls.getMaxReservableBandwidth().getValue());
149         assertNotNull(ls.getUnreservedBandwidth());
150         assertEquals(8, ls.getUnreservedBandwidth().size());
151         assertEquals(LinkProtectionType.Dedicated1to1, ls.getLinkProtection());
152         assertTrue(ls.getMplsProtocol().isLdp());
153         assertTrue(ls.getMplsProtocol().isRsvpte());
154         assertEquals(new Long(10), ls.getMetric().getValue());
155         assertEquals(2, ls.getSharedRiskLinkGroups().size());
156         assertEquals(305419896, ls.getSharedRiskLinkGroups().get(0).getValue().intValue());
157         assertEquals("12K-2", ls.getLinkName());
158
159         //serialization
160         final ByteBuf buff = Unpooled.buffer();
161         this.parser.serializeAttribute(builder.build(), buff);
162         buff.skipBytes(3);
163         // there is unresolved TLV at the end, that needs to be cut off
164         assertArrayEquals(ByteArray.subByte(LINK_ATTR, 0, LINK_ATTR.length -5), ByteArray.getAllBytes(buff));
165     }
166
167     @Test
168     public void testPositiveNodes() throws BGPParsingException {
169         final PathAttributesBuilder builder = createBuilder(new NodeCaseBuilder().build());
170         this.parser.parseAttribute(Unpooled.copiedBuffer(NODE_ATTR), builder);
171
172         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
173         final NodeAttributes ls = ((NodeAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getNodeAttributes();
174         assertNotNull(ls);
175
176         assertEquals(2, ls.getTopologyIdentifier().size());
177         assertEquals(42, ls.getTopologyIdentifier().get(0).getValue().intValue());
178         assertTrue(ls.getNodeFlags().isOverload());
179         assertFalse(ls.getNodeFlags().isAttached());
180         assertTrue(ls.getNodeFlags().isExternal());
181         assertTrue(ls.getNodeFlags().isAbr());
182
183         assertEquals("12K-2", ls.getDynamicHostname());
184         assertEquals(2, ls.getIsisAreaId().size());
185         assertEquals("41.41.41.41", ls.getIpv4RouterId().getValue());
186
187         //serialization
188         final ByteBuf buff = Unpooled.buffer();
189         this.parser.serializeAttribute(builder.build(), buff);
190         buff.skipBytes(3);
191         // there is unresolved TLV at the end, that needs to be cut off
192         assertTrue(Arrays.equals(ByteArray.subByte(NODE_ATTR, 0, NODE_ATTR.length -5), ByteArray.getAllBytes(buff)) || Arrays.equals(NODE_ATTR_S, ByteArray.getAllBytes(buff)));
193     }
194
195     @Test
196     public void testPositiveV4Prefixes() throws BGPParsingException {
197         final PathAttributesBuilder builder = createUnreachBuilder(new PrefixCaseBuilder().setPrefixDescriptors(new PrefixDescriptorsBuilder().setIpReachabilityInformation(new IpPrefix(new Ipv4Prefix("127.0.0.1/32"))).build()).build());
198         this.parser.parseAttribute(Unpooled.copiedBuffer(P4_ATTR), builder);
199
200         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
201         final PrefixAttributes ls = ((PrefixAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getPrefixAttributes();
202         assertNotNull(ls);
203
204         assertTrue(ls.getIgpBits().getUpDown().isUpDown());
205         assertEquals(2, ls.getRouteTags().size());
206         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }, ls.getRouteTags().get(0).getValue());
207         assertEquals(1, ls.getExtendedTags().size());
208         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50,
209             (byte) 0x70 }, ls.getExtendedTags().get(0).getValue());
210         assertEquals(10, ls.getPrefixMetric().getValue().intValue());
211         assertEquals("10.25.2.27", ls.getOspfForwardingAddress().getIpv4Address().getValue());
212
213         //serialization
214         final ByteBuf buff = Unpooled.buffer();
215         this.parser.serializeAttribute(builder.build(), buff);
216         buff.skipBytes(3);
217         // there is unresolved TLV at the end, that needs to be cut off
218         assertArrayEquals(ByteArray.subByte(P4_ATTR, 0, P4_ATTR.length -5), ByteArray.getAllBytes(buff));
219     }
220
221     @Test(expected=UnsupportedOperationException.class)
222     public void testLinkAttributesPrivateConstructor() throws Throwable {
223         final Constructor<LinkAttributesParser> c = LinkAttributesParser.class.getDeclaredConstructor();
224         c.setAccessible(true);
225         try {
226             c.newInstance();
227         } catch (final InvocationTargetException e) {
228             throw e.getCause();
229         }
230     }
231
232     @Test(expected=UnsupportedOperationException.class)
233     public void testNodeAttributesPrivateConstructor() throws Throwable {
234         final Constructor<NodeAttributesParser> c = NodeAttributesParser.class.getDeclaredConstructor();
235         c.setAccessible(true);
236         try {
237             c.newInstance();
238         } catch (final InvocationTargetException e) {
239             throw e.getCause();
240         }
241     }
242
243     @Test(expected=UnsupportedOperationException.class)
244     public void testPrefixAttributesPrivateConstructor() throws Throwable {
245         final Constructor<PrefixAttributesParser> c = PrefixAttributesParser.class.getDeclaredConstructor();
246         c.setAccessible(true);
247         try {
248             c.newInstance();
249         } catch (final InvocationTargetException e) {
250             throw e.getCause();
251         }
252     }
253 }