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