2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.linkstate;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.InvocationTargetException;
19 import java.math.BigInteger;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkNlriParser;
23 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
24 import org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser;
25 import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriParser;
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.AsNumber;
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.linkstate.rev150210.DomainIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfRouteType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.isis.lan.identifier.IsIsRouterIdentifierBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptors;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.node._case.NodeDescriptors;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.AdvertisingNodeDescriptors;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptors;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNodeBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
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.DestinationLinkstateCase;
52 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;
53 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.DestinationLinkstate;
54 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;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutes;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
69 public class LinkstateNlriParserTest {
71 private final byte[] nodeNlri = new byte[] { (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x30, (byte) 0x02, (byte) 0x00, (byte) 0x00,
72 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x23,
73 (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
74 (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x02, (byte) 0x00, (byte) 0x04,
75 (byte) 0x00, (byte) 0x29, (byte) 0x29, (byte) 0x29, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x07, (byte) 0x00, (byte) 0x00,
76 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x39, (byte) 0x05 };
78 private final byte[] linkNlri = new byte[] { (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x65, (byte) 0x02, (byte) 0x00, (byte) 0x00,
79 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x1a,
80 (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
81 (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x06,
82 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x18,
83 (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
84 (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x04,
85 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, 1, 2, 0, 8, 1, 2, 3, 4, 0x0a, 0x0b, 0x0c, 0x0d,
86 (byte) 0x01, (byte) 0x03, (byte) 0x00, (byte) 0x04, (byte) 0xc5, (byte) 0x14,
87 (byte) 0xa0, (byte) 0x2a, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0xc5, (byte) 0x14, (byte) 0xa0, (byte) 0x28,
90 private final byte[] prefixNlri = new byte[] { (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x39, (byte) 0x02, (byte) 0x00,
91 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00,
92 (byte) 0x1a, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02,
93 (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00,
94 (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42, (byte) 0x01, (byte) 0x07, (byte) 0x00,
95 (byte) 0x02, (byte) 0x00, (byte) 0x0F, (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x01, (byte) 0x03, (byte) 0x01, (byte) 0x09,
96 (byte) 0x00, (byte) 0x03, (byte) 0x10, (byte) 0xFF, (byte) 0xFF };
98 private CLinkstateDestination dest;
100 private void setUp(final byte[] data) throws BGPParsingException {
101 final LinkstateNlriParser parser = new LinkstateNlriParser(false);
102 final MpReachNlriBuilder builder = new MpReachNlriBuilder();
103 parser.parseNlri(Unpooled.copiedBuffer(data), builder);
105 final DestinationLinkstate ls = ((DestinationLinkstateCase) builder.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
106 assertEquals(1, ls.getCLinkstateDestination().size());
108 this.dest = ls.getCLinkstateDestination().get(0);
112 public void testNodeNlri() throws BGPParsingException {
113 setUp(this.nodeNlri);
115 assertNull(this.dest.getDistinguisher());
116 assertEquals(ProtocolId.IsisLevel2, this.dest.getProtocolId());
117 assertEquals(BigInteger.ONE, this.dest.getIdentifier().getValue());
118 final NodeCase nCase = ((NodeCase)this.dest.getObjectType());
120 final NodeDescriptors nodeD = nCase.getNodeDescriptors();
121 assertEquals(new AsNumber(72L), nodeD.getAsNumber());
122 assertEquals(new DomainIdentifier(0x28282828L), nodeD.getDomainId());
123 assertEquals(new IsisPseudonodeCaseBuilder().setIsisPseudonode(
124 new IsisPseudonodeBuilder().setPsn((short) 5).setIsIsRouterIdentifier(
125 new IsIsRouterIdentifierBuilder().setIsoSystemId(
126 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
127 (byte) 0x39 })).build()).build()).build(), nodeD.getCRouterIdentifier());
129 final ByteBuf buffer = Unpooled.buffer();
130 LinkstateNlriParser.serializeNlri(this.dest, buffer);
131 assertArrayEquals(this.nodeNlri, ByteArray.readAllBytes(buffer));
135 public void testLinkNlri() throws BGPParsingException {
136 setUp(this.linkNlri);
138 assertNull(this.dest.getDistinguisher());
139 assertEquals(ProtocolId.IsisLevel2, this.dest.getProtocolId());
140 assertEquals(BigInteger.ONE, this.dest.getIdentifier().getValue());
142 final LinkCase lCase = ((LinkCase)this.dest.getObjectType());
144 final LocalNodeDescriptors local = lCase.getLocalNodeDescriptors();
145 assertEquals(new AsNumber(72L), local.getAsNumber());
146 assertEquals(new DomainIdentifier(0x28282828L), local.getDomainId());
148 new IsisNodeCaseBuilder().setIsisNode(
149 new IsisNodeBuilder().setIsoSystemId(
150 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
151 (byte) 0x42 })).build()).build(), local.getCRouterIdentifier());
152 final RemoteNodeDescriptors remote = lCase.getRemoteNodeDescriptors();
153 assertEquals(new AsNumber(72L), remote.getAsNumber());
154 assertEquals(new DomainIdentifier(0x28282828L), remote.getDomainId());
155 assertEquals(new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x00000040L).build()).build(),
156 remote.getCRouterIdentifier());
157 final LinkDescriptors ld = lCase.getLinkDescriptors();
158 assertEquals("197.20.160.42", ld.getIpv4InterfaceAddress().getValue());
159 assertEquals("197.20.160.40", ld.getIpv4NeighborAddress().getValue());
161 final ByteBuf buffer = Unpooled.buffer();
162 LinkstateNlriParser.serializeNlri(this.dest, buffer);
163 assertArrayEquals(this.linkNlri, ByteArray.readAllBytes(buffer));
167 public void testPrefixNlri() throws BGPParsingException {
168 setUp(this.prefixNlri);
170 assertNull(this.dest.getDistinguisher());
171 assertEquals(ProtocolId.IsisLevel2, this.dest.getProtocolId());
172 assertEquals(BigInteger.ONE, this.dest.getIdentifier().getValue());
174 final PrefixCase pCase = ((PrefixCase)this.dest.getObjectType());
176 final AdvertisingNodeDescriptors local = pCase.getAdvertisingNodeDescriptors();
177 assertEquals(new AsNumber(72L), local.getAsNumber());
178 assertEquals(new DomainIdentifier(0x28282828L), local.getDomainId());
180 new IsisNodeCaseBuilder().setIsisNode(
181 new IsisNodeBuilder().setIsoSystemId(
182 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
183 (byte) 0x42 })).build()).build(), local.getCRouterIdentifier());
185 final PrefixDescriptors pd = pCase.getPrefixDescriptors();
186 assertEquals(OspfRouteType.External1, pd.getOspfRouteType());
187 assertEquals(new TopologyIdentifier(15), pd.getMultiTopologyId());
188 assertEquals(new Ipv4Prefix("255.255.0.0/16"), pd.getIpReachabilityInformation().getIpv4Prefix());
190 final ByteBuf buffer = Unpooled.buffer();
191 LinkstateNlriParser.serializeNlri(this.dest, buffer);
192 assertArrayEquals(this.prefixNlri, ByteArray.readAllBytes(buffer));
196 public void testSerializeAttribute() throws BGPParsingException {
197 final LinkstateNlriParser parser = new LinkstateNlriParser(true);
198 setUp(this.prefixNlri);
199 final List<CLinkstateDestination> dests = Lists.newArrayList( this.dest );
200 final DestinationLinkstateCase dlc = new DestinationLinkstateCaseBuilder().setDestinationLinkstate(new DestinationLinkstateBuilder().setCLinkstateDestination(dests).build()).build();
201 final AdvertizedRoutes aroutes = new AdvertizedRoutesBuilder().setDestinationType(dlc).build();
202 final PathAttributes1 reach = new PathAttributes1Builder().setMpReachNlri(new MpReachNlriBuilder().setAdvertizedRoutes(aroutes).build()).build();
204 PathAttributes pa = new PathAttributesBuilder().addAugmentation(PathAttributes1.class, reach).build();
206 ByteBuf result = Unpooled.buffer();
207 parser.serializeAttribute(pa, result);
208 assertArrayEquals(this.prefixNlri, ByteArray.getAllBytes(result));
210 setUp(this.nodeNlri);
211 final List<CLinkstateDestination> destsU = Lists.newArrayList( this.dest );
212 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase dlcU =
213 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(
214 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(destsU).build()).build();
215 final WithdrawnRoutes wroutes = new WithdrawnRoutesBuilder().setDestinationType(dlcU).build();
216 final PathAttributes2 unreach = new PathAttributes2Builder().setMpUnreachNlri(new MpUnreachNlriBuilder().setWithdrawnRoutes(wroutes).build()).build();
218 pa = new PathAttributesBuilder().addAugmentation(PathAttributes2.class, unreach).build();
220 result = Unpooled.buffer();
221 parser.serializeAttribute(pa, result);
222 assertArrayEquals(this.nodeNlri, ByteArray.getAllBytes(result));
225 @Test(expected=UnsupportedOperationException.class)
226 public void testLinkNlriPrivateConstructor() throws Throwable {
227 final Constructor<LinkNlriParser> c = LinkNlriParser.class.getDeclaredConstructor();
228 c.setAccessible(true);
231 } catch (final InvocationTargetException e) {
236 @Test(expected=UnsupportedOperationException.class)
237 public void testNodeNlriPrivateConstructor() throws Throwable {
238 final Constructor<NodeNlriParser> c = NodeNlriParser.class.getDeclaredConstructor();
239 c.setAccessible(true);
242 } catch (final InvocationTargetException e) {
247 @Test(expected=UnsupportedOperationException.class)
248 public void testPrefixNlriPrivateConstructor() throws Throwable {
249 final Constructor<PrefixNlriParser> c = PrefixNlriParser.class.getDeclaredConstructor();
250 c.setAccessible(true);
253 } catch (final InvocationTargetException e) {