13ef5cf2c09880386a83d9b7ed4aec63003a53f9
[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
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
14
15 import com.google.common.collect.ImmutableSet;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.math.BigInteger;
19 import java.util.Collection;
20 import java.util.Collections;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.protocol.bgp.linkstate.impl.BGPActivator;
24 import org.opendaylight.protocol.bgp.linkstate.impl.LinkstateRIBSupport;
25 import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry;
26 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
27 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
28 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.AreaIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.DomainIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.Identifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateSubsequentAddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.ProtocolId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.RouteDistinguisher;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.isis.lan.identifier.IsIsRouterIdentifierBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.destination.CLinkstateDestination;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.destination.CLinkstateDestinationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.object.type.NodeCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.object.type.NodeCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.object.type.node._case.NodeDescriptorsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.routes.LinkstateRoutes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.routes.LinkstateRoutesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.routes.linkstate.routes.LinkstateRoute;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.linkstate.routes.linkstate.routes.LinkstateRouteKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
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.rev171207.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
77     private static final NodeCase OBJECT_TYPE2 = new NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder()
78         .setAreaId(new AreaIdentifier(2697513L)).setAsNumber(new AsNumber(72L)).setCRouterIdentifier(new IsisPseudonodeCaseBuilder()
79             .setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(new IsIsRouterIdentifierBuilder()
80                 .setIsoSystemId(new IsoSystemIdentifier(new byte[]{0, 0, 0, 0, 0, (byte) 0x39})).build()).setPsn((short) 5).build()).build())
81         .setDomainId(new DomainIdentifier(28282828L)).build()).build();
82
83     private static final RouteDistinguisher RD = new RouteDistinguisher(BigInteger.ONE);
84     private static final Identifier ID = new Identifier(BigInteger.ONE);
85     private static final CLinkstateDestination LINKSTATE_DESTINATION = new CLinkstateDestinationBuilder()
86         .setDistinguisher(RD)
87         .setIdentifier(ID)
88         .setObjectType(OBJECT_TYPE2)
89         .setProtocolId(ProtocolId.IsisLevel1).build();
90
91     private static final DestinationLinkstateCase REACH_NLRI = new DestinationLinkstateCaseBuilder().setDestinationLinkstate(new DestinationLinkstateBuilder()
92         .setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
93     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase UNREACH_NLRI =
94         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.unreach.nlri.withdrawn
95             .routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(new org.opendaylight.yang.gen.v1.urn.opendaylight
96             .params.xml.ns.yang.bgp.linkstate.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate
97             ._case.DestinationLinkstateBuilder().setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
98
99     static {
100         final BGPActivator act = new BGPActivator();
101         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
102         act.start(context);
103         assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388));
104         assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71));
105         final ByteBuf buffer = Unpooled.buffer();
106         SimpleNlriTypeRegistry.getInstance().serializeNlriType(LINKSTATE_DESTINATION, buffer);
107         final byte[] arrayKey = ByteArray.readAllBytes(buffer);
108         ROUTE_KEY = new LinkstateRouteKey(arrayKey);
109         ROUTE = new LinkstateRouteBuilder().setKey(ROUTE_KEY).setDistinguisher(RD).setIdentifier(ID).setObjectType(OBJECT_TYPE2)
110             .setProtocolId(ProtocolId.IsisLevel1).setAttributes(new AttributesBuilder().build()).build();
111         ROUTES = new LinkstateRoutesBuilder().setLinkstateRoute(Collections.singletonList(ROUTE)).build();
112     }
113
114     @Override
115     public void setUp() throws Exception {
116         super.setUp();
117         setUpTestCustomizer(RIB_SUPPORT);
118     }
119
120     @Test
121     public void testDeleteRoutes() {
122         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
123         final InstanceIdentifier<LinkstateRoute> instanceIdentifier = (InstanceIdentifier<LinkstateRoute>) this.deletedRoutes.get(0);
124         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(LinkstateRoute.class));
125     }
126
127     @Test
128     public void testPutRoutes() {
129         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
130         final LinkstateRoute route = (LinkstateRoute) this.insertedRoutes.get(0).getValue();
131         assertEquals(ROUTE, route);
132     }
133
134     @Test
135     public void testEmptyRoute() throws Exception {
136         final Routes empty = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(
137             new LinkstateRoutesBuilder().setLinkstateRoute(Collections.emptyList()).build()).build();
138         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
139         assertEquals(createRoutes(empty), emptyRoutes);
140     }
141
142     @Test
143     public void testBuildMpUnreachNlriUpdate() {
144         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
145         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
146             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
147         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
148     }
149
150     @Test
151     public void testBuildMpReachNlriUpdate() {
152         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
153         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType());
154         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
155     }
156
157     @Test
158     public void testIsComplexRoute() {
159         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
160     }
161
162     @Test
163     public void testCacheableNlriObjects() {
164         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
165     }
166
167     @Test
168     public void testCacheableAttributeObjects() {
169         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
170     }
171
172     @Test
173     public void testRouteIdAddPath() {
174         Assert.assertNull(RIB_SUPPORT.getRouteIdAddPath(AbstractRIBSupportTest.PATH_ID, null));
175     }
176
177     @Test
178     public void testRoutePath() {
179         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
180         Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
181     }
182
183     @Test
184     public void testExtractPathId() {
185         Assert.assertEquals((Long) NON_PATH_ID, RIB_SUPPORT.extractPathId(null));
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.rev171207.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(), createRoutes(emptyCase)).getRootNode();
214         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
215
216         final Routes emptyRoutes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(new LinkstateRoutesBuilder().build()).build();
217         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
218         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
219
220         final Routes routes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(ROUTES).build();
221         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
222         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
223         Assert.assertFalse(result.isEmpty());
224     }
225 }