Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / LinkstateRIBSupportTest.java
1 /*
2  * Copyright (c) 2015 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.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.math.BigInteger;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.linkstate.impl.BGPActivator;
23 import org.opendaylight.protocol.bgp.linkstate.impl.LinkstateRIBSupport;
24 import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry;
25 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
27 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.AreaIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.DomainIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Identifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.RouteDistinguisher;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.isis.lan.identifier.IsIsRouterIdentifierBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestinationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptorsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRoute;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.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.rev171207.PathId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.common.QName;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
66 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
69
70 public final class LinkstateRIBSupportTest extends AbstractRIBSupportTest {
71
72     private static final LinkstateRIBSupport RIB_SUPPORT = LinkstateRIBSupport.getInstance();
73     private static final LinkstateRoute ROUTE;
74     private static final LinkstateRoutes ROUTES;
75     private static final LinkstateRouteKey ROUTE_KEY;
76     private static final PathId PATH_ID = new PathId(0L);
77
78
79     private static final NodeCase OBJECT_TYPE2 = new NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder()
80         .setAreaId(new AreaIdentifier(2697513L)).setAsNumber(new AsNumber(72L)).setCRouterIdentifier(new IsisPseudonodeCaseBuilder()
81             .setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(new IsIsRouterIdentifierBuilder()
82                 .setIsoSystemId(new IsoSystemIdentifier(new byte[]{0, 0, 0, 0, 0, (byte) 0x39})).build()).setPsn((short) 5).build()).build())
83         .setDomainId(new DomainIdentifier(28282828L)).build()).build();
84
85     private static final RouteDistinguisher RD = new RouteDistinguisher(BigInteger.ONE);
86     private static final Identifier ID = new Identifier(BigInteger.ONE);
87     private static final CLinkstateDestination LINKSTATE_DESTINATION = new CLinkstateDestinationBuilder()
88         .setDistinguisher(RD)
89         .setIdentifier(ID)
90         .setObjectType(OBJECT_TYPE2)
91         .setProtocolId(ProtocolId.IsisLevel1).build();
92
93     private static final DestinationLinkstateCase REACH_NLRI = new DestinationLinkstateCaseBuilder().setDestinationLinkstate(new DestinationLinkstateBuilder()
94         .setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
95     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase UNREACH_NLRI =
96         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn
97             .routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(new org.opendaylight.yang.gen.v1.urn.opendaylight
98             .params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate
99             ._case.DestinationLinkstateBuilder().setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
100
101     static {
102         final BGPActivator act = new BGPActivator();
103         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
104         act.start(context);
105         assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388));
106         assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71));
107         final ByteBuf buffer = Unpooled.buffer();
108         SimpleNlriTypeRegistry.getInstance().serializeNlriType(LINKSTATE_DESTINATION, buffer);
109         final byte[] arrayKey = ByteArray.readAllBytes(buffer);
110         ROUTE_KEY = new LinkstateRouteKey(PATH_ID, Arrays.toString(arrayKey));
111         ROUTE = new LinkstateRouteBuilder().setKey(ROUTE_KEY).setDistinguisher(RD).setIdentifier(ID).setObjectType(OBJECT_TYPE2)
112             .setProtocolId(ProtocolId.IsisLevel1).setAttributes(new AttributesBuilder().build()).build();
113         ROUTES = new LinkstateRoutesBuilder().setLinkstateRoute(Collections.singletonList(ROUTE)).build();
114     }
115
116     @Override
117     public void setUp() throws Exception {
118         super.setUp();
119         setUpTestCustomizer(RIB_SUPPORT);
120     }
121
122     @Test
123     public void testDeleteRoutes() {
124         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
125         final InstanceIdentifier<LinkstateRoute> instanceIdentifier
126                 = (InstanceIdentifier<LinkstateRoute>) this.deletedRoutes.get(0);
127         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(LinkstateRoute.class));
128     }
129
130     @Test
131     public void testPutRoutes() {
132         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
133         final LinkstateRoute route = (LinkstateRoute) this.insertedRoutes.get(0).getValue();
134         assertEquals(ROUTE, route);
135     }
136
137     @Test
138     public void testEmptyRoute() {
139         final Routes empty = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(
140             new LinkstateRoutesBuilder().setLinkstateRoute(Collections.emptyList()).build()).build();
141         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
142         assertEquals(createRoutes(empty), emptyRoutes);
143     }
144
145     @Test
146     public void testBuildMpUnreachNlriUpdate() {
147         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
148         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
149             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
150         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
151     }
152
153     @Test
154     public void testBuildMpReachNlriUpdate() {
155         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
156         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class)
157                 .getMpReachNlri().getAdvertizedRoutes().getDestinationType());
158         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
159     }
160
161     @Test
162     public void testIsComplexRoute() {
163         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
164     }
165
166     @Test
167     public void testCacheableNlriObjects() {
168         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
169     }
170
171     @Test
172     public void testCacheableAttributeObjects() {
173         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
174     }
175
176     @Test
177     public void testRouteIdAddPath() {
178         Assert.assertEquals(ROUTE_KEY, RIB_SUPPORT.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
179                 ROUTE_KEY.getRouteKey()));
180     }
181
182     @Test
183     public void testRoutePath() {
184         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
185         Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
186     }
187
188     @Test
189     public void testRouteAttributesIdentifier() {
190         Assert.assertEquals(new NodeIdentifier(QName.create(LinkstateRoutes.QNAME,
191             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes.QNAME.getLocalName().intern())),
192             RIB_SUPPORT.routeAttributesIdentifier());
193     }
194
195     @Test
196     public void testRoutesCaseClass() {
197         Assert.assertEquals(LinkstateRoutesCase.class, RIB_SUPPORT.routesCaseClass());
198     }
199
200     @Test
201     public void testRoutesContainerClass() {
202         Assert.assertEquals(LinkstateRoutes.class, RIB_SUPPORT.routesContainerClass());
203     }
204
205     @Test
206     public void testRoutesListClass() {
207         Assert.assertEquals(LinkstateRoute.class, RIB_SUPPORT.routesListClass());
208     }
209
210     @Test
211     public void testChangedRoutes() {
212        final Routes emptyCase = new LinkstateRoutesCaseBuilder().build();
213         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
214                 createRoutes(emptyCase)).getRootNode();
215         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
216
217         final Routes emptyRoutes = new LinkstateRoutesCaseBuilder()
218                 .setLinkstateRoutes(new LinkstateRoutesBuilder().build()).build();
219         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
220         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
221
222         final Routes routes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(ROUTES).build();
223         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
224         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
225         Assert.assertFalse(result.isEmpty());
226     }
227 }