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