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