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