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