Merge "integration tests - fixed unresolved bundle"
[bgpcep.git] / bgp / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / LinkstateNlriParserTest.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.assertNull;
13 import io.netty.buffer.Unpooled;
14
15 import java.math.BigInteger;
16
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.DomainIdentifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfRouteType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ProtocolId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.isis.lan.identifier.IsIsRouterIdentifierBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptors;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptors;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.PrefixDescriptors;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptors;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNodeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstate;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
42
43 public class LinkstateNlriParserTest {
44
45     private final byte[] nodeNlri = new byte[] { (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x30, (byte) 0x02, (byte) 0x00, (byte) 0x00,
46         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x23,
47         (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
48         (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x02, (byte) 0x00, (byte) 0x04,
49         (byte) 0x00, (byte) 0x29, (byte) 0x29, (byte) 0x29, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x07, (byte) 0x00, (byte) 0x00,
50         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x39, (byte) 0x05 };
51
52     private final byte[] linkNlri = new byte[] { (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x53, (byte) 0x02, (byte) 0x00, (byte) 0x00,
53         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x1a,
54         (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
55         (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x06,
56         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x18,
57         (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02, (byte) 0x01,
58         (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00, (byte) 0x04,
59         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x01, (byte) 0x03, (byte) 0x00, (byte) 0x04, (byte) 0xc5, (byte) 0x14,
60         (byte) 0xa0, (byte) 0x2a, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0xc5, (byte) 0x14, (byte) 0xa0, (byte) 0x28 };
61
62     private final byte[] prefixNlri = new byte[] { (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x39, (byte) 0x02, (byte) 0x00,
63         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00,
64         (byte) 0x1a, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02,
65         (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02, (byte) 0x03, (byte) 0x00,
66         (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42, (byte) 0x01, (byte) 0x07, (byte) 0x00,
67         (byte) 0x02, (byte) 0x00, (byte) 0x0F, (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x01, (byte) 0x03, (byte) 0x01, (byte) 0x09,
68         (byte) 0x00, (byte) 0x03, (byte) 0x10, (byte) 0xFF, (byte) 0xFF };
69
70     @Test
71     public void testNodeNlri() throws BGPParsingException {
72         final LinkstateNlriParser parser = new LinkstateNlriParser(false);
73         final MpReachNlriBuilder builder = new MpReachNlriBuilder();
74         parser.parseNlri(Unpooled.copiedBuffer(this.nodeNlri), builder);
75
76         final DestinationLinkstate ls = ((DestinationLinkstateCase) builder.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
77
78         assertEquals(1, ls.getCLinkstateDestination().size());
79
80         final CLinkstateDestination dest = ls.getCLinkstateDestination().get(0);
81
82         assertEquals(NlriType.Node, dest.getNlriType());
83         assertNull(dest.getDistinguisher());
84         assertEquals(ProtocolId.IsisLevel2, dest.getProtocolId());
85         assertEquals(BigInteger.ONE, dest.getIdentifier().getValue());
86
87         final LocalNodeDescriptors nodeD = dest.getLocalNodeDescriptors();
88         assertEquals(new AsNumber(72L), nodeD.getAsNumber());
89         assertEquals(new DomainIdentifier(0x28282828L), nodeD.getDomainId());
90         assertEquals(new IsisPseudonodeCaseBuilder().setIsisPseudonode(
91                 new IsisPseudonodeBuilder().setPsn((short) 5).setIsIsRouterIdentifier(
92                         new IsIsRouterIdentifierBuilder().setIsoSystemId(
93                                 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
94                                     (byte) 0x39 })).build()).build()).build(), nodeD.getCRouterIdentifier());
95
96         assertNull(dest.getRemoteNodeDescriptors());
97
98         assertArrayEquals(this.nodeNlri, LinkstateNlriParser.serializeNlri(dest));
99     }
100
101     @Test
102     public void testLinkNlri() throws BGPParsingException {
103         final LinkstateNlriParser parser = new LinkstateNlriParser(false);
104         final MpReachNlriBuilder builder = new MpReachNlriBuilder();
105         parser.parseNlri(Unpooled.copiedBuffer(this.linkNlri), builder);
106
107         final DestinationLinkstate ls = ((DestinationLinkstateCase) builder.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
108
109         assertEquals(1, ls.getCLinkstateDestination().size());
110
111         final CLinkstateDestination dest = ls.getCLinkstateDestination().get(0);
112
113         assertEquals(NlriType.Link, dest.getNlriType());
114         assertNull(dest.getDistinguisher());
115         assertEquals(ProtocolId.IsisLevel2, dest.getProtocolId());
116         assertEquals(BigInteger.ONE, dest.getIdentifier().getValue());
117
118         final LocalNodeDescriptors local = dest.getLocalNodeDescriptors();
119         assertEquals(new AsNumber(72L), local.getAsNumber());
120         assertEquals(new DomainIdentifier(0x28282828L), local.getDomainId());
121         assertEquals(
122                 new IsisNodeCaseBuilder().setIsisNode(
123                         new IsisNodeBuilder().setIsoSystemId(
124                                 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
125                                     (byte) 0x42 })).build()).build(), local.getCRouterIdentifier());
126         final RemoteNodeDescriptors remote = dest.getRemoteNodeDescriptors();
127         assertEquals(new AsNumber(72L), remote.getAsNumber());
128         assertEquals(new DomainIdentifier(0x28282828L), remote.getDomainId());
129         assertEquals(new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x00000040L).build()).build(),
130                 remote.getCRouterIdentifier());
131         final LinkDescriptors ld = dest.getLinkDescriptors();
132         assertEquals("197.20.160.42", ld.getIpv4InterfaceAddress().getValue());
133         assertEquals("197.20.160.40", ld.getIpv4NeighborAddress().getValue());
134
135         assertArrayEquals(this.linkNlri, LinkstateNlriParser.serializeNlri(dest));
136     }
137
138     @Test
139     public void testPrefixNlri() throws BGPParsingException {
140         final LinkstateNlriParser parser = new LinkstateNlriParser(false);
141         final MpReachNlriBuilder builder = new MpReachNlriBuilder();
142         parser.parseNlri(Unpooled.copiedBuffer(this.prefixNlri), builder);
143
144         final DestinationLinkstate ls = ((DestinationLinkstateCase) builder.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
145
146         assertEquals(1, ls.getCLinkstateDestination().size());
147
148         final CLinkstateDestination dest = ls.getCLinkstateDestination().get(0);
149
150         assertEquals(NlriType.Ipv4Prefix, dest.getNlriType());
151         assertNull(dest.getDistinguisher());
152         assertEquals(ProtocolId.IsisLevel2, dest.getProtocolId());
153         assertEquals(BigInteger.ONE, dest.getIdentifier().getValue());
154
155         final LocalNodeDescriptors local = dest.getLocalNodeDescriptors();
156         assertEquals(new AsNumber(72L), local.getAsNumber());
157         assertEquals(new DomainIdentifier(0x28282828L), local.getDomainId());
158         assertEquals(
159                 new IsisNodeCaseBuilder().setIsisNode(
160                         new IsisNodeBuilder().setIsoSystemId(
161                                 new IsoSystemIdentifier(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
162                                     (byte) 0x42 })).build()).build(), local.getCRouterIdentifier());
163         assertNull(dest.getRemoteNodeDescriptors());
164
165         final PrefixDescriptors pd = dest.getPrefixDescriptors();
166         assertEquals(OspfRouteType.External1, pd.getOspfRouteType());
167         assertEquals(new TopologyIdentifier(15), pd.getMultiTopologyId());
168         assertEquals(new Ipv4Prefix("255.255.0.0/16"), pd.getIpReachabilityInformation().getIpv4Prefix());
169
170         assertArrayEquals(this.prefixNlri, LinkstateNlriParser.serializeNlri(dest));
171     }
172 }