/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.bgp.linkstate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import com.google.common.collect.ImmutableSet; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.util.Collection; import java.util.Collections; import org.junit.Assert; import org.junit.Test; import org.opendaylight.protocol.bgp.linkstate.impl.BGPActivator; import org.opendaylight.protocol.bgp.linkstate.impl.LinkstateRIBSupport; import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry; import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext; import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext; import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest; import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.AreaIdentifier; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.DomainIdentifier; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Identifier; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.isis.lan.identifier.IsIsRouterIdentifierBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestinationBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptorsBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRoute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder; 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; 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; 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; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RdTwoOctetAs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.Uint32; import org.opendaylight.yangtools.yang.common.Uint64; import org.opendaylight.yangtools.yang.common.Uint8; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; public final class LinkstateRIBSupportTest extends AbstractRIBSupportTest { private LinkstateRIBSupport ribSupport; private static final LinkstateRoute ROUTE; private static final LinkstateRoutes ROUTES; private static final LinkstateRouteKey ROUTE_KEY; private static final PathId PATH_ID = new PathId(Uint32.ZERO); private static final NodeCase OBJECT_TYPE2 = new NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder() .setAreaId(new AreaIdentifier(Uint32.valueOf(2697513))) .setAsNumber(new AsNumber(Uint32.valueOf(72))) .setCRouterIdentifier(new IsisPseudonodeCaseBuilder() .setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(new IsIsRouterIdentifierBuilder() .setIsoSystemId(new IsoSystemIdentifier(new byte[]{0, 0, 0, 0, 0, (byte) 0x39})).build()) .setPsn(Uint8.valueOf(5)).build()).build()) .setDomainId(new DomainIdentifier(Uint32.valueOf(28282828))).build()).build(); private static final RouteDistinguisher RD = new RouteDistinguisher(new RdTwoOctetAs("0:5:3")); private static final Identifier ID = new Identifier(Uint64.ONE); private static final CLinkstateDestination LINKSTATE_DESTINATION = new CLinkstateDestinationBuilder() .setRouteDistinguisher(RD) .setIdentifier(ID) .setObjectType(OBJECT_TYPE2) .setProtocolId(ProtocolId.IsisLevel1).build(); private static final DestinationLinkstateCase REACH_NLRI = new DestinationLinkstateCaseBuilder() .setDestinationLinkstate(new DestinationLinkstateBuilder() .setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build(); 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 = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes .mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder() .setDestinationLinkstate(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp .linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination .linkstate._case.DestinationLinkstateBuilder() .setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build(); static { final BGPActivator act = new BGPActivator(); final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext(); act.start(context); assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388)); assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71)); final ByteBuf buffer = Unpooled.buffer(); SimpleNlriTypeRegistry.getInstance().serializeNlriType(LINKSTATE_DESTINATION, buffer); ROUTE_KEY = new LinkstateRouteKey(PATH_ID, ByteArray.encodeBase64(buffer)); ROUTE = new LinkstateRouteBuilder().withKey(ROUTE_KEY).setRouteDistinguisher(RD) .setIdentifier(ID).setObjectType(OBJECT_TYPE2) .setProtocolId(ProtocolId.IsisLevel1).setAttributes(new AttributesBuilder().build()).build(); ROUTES = new LinkstateRoutesBuilder().setLinkstateRoute(Collections.singletonList(ROUTE)).build(); } @Override public void setUp() throws Exception { super.setUp(); this.ribSupport = LinkstateRIBSupport.getInstance(this.mappingService); setUpTestCustomizer(ribSupport); } @Test public void testDeleteRoutes() { this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI)); final InstanceIdentifier instanceIdentifier = this.deletedRoutes.get(0); assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(LinkstateRoute.class)); } @Test public void testPutRoutes() { this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes()); final LinkstateRoute route = (LinkstateRoute) this.insertedRoutes.get(0).getValue(); assertEquals(ROUTE, route); } @Test public void testEmptyRoute() { assertEquals(createEmptyTable(), this.ribSupport.emptyTable()); } @Test public void testBuildMpUnreachNlriUpdate() { final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES); assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class) .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType()); assertNull(update.getAttributes().augmentation(Attributes1.class)); } @Test public void testBuildMpReachNlriUpdate() { final Update update = this.ribSupport.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES); assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class) .getMpReachNlri().getAdvertizedRoutes().getDestinationType()); assertNull(update.getAttributes().augmentation(Attributes2.class)); } @Test public void testCacheableNlriObjects() { Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects()); } @Test public void testCacheableAttributeObjects() { Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects()); } @Test public void testRouteIdAddPath() { Assert.assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey())); } @Test public void testRoutePath() { final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES); Assert.assertEquals(getRoutePath().node(prefixNii), this.ribSupport.routePath(getTablePath(), prefixNii)); } @Test public void testRouteAttributesIdentifier() { Assert.assertEquals(new NodeIdentifier(QName.create(LinkstateRoutes.QNAME, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes .QNAME.getLocalName().intern())), this.ribSupport.routeAttributesIdentifier()); } @Test public void testRoutesCaseClass() { Assert.assertEquals(LinkstateRoutesCase.class, this.ribSupport.routesCaseClass()); } @Test public void testRoutesContainerClass() { Assert.assertEquals(LinkstateRoutes.class, this.ribSupport.routesContainerClass()); } @Test public void testRoutesListClass() { Assert.assertEquals(LinkstateRoute.class, this.ribSupport.routesListClass()); } @Test public void testChangedRoutes() { final Routes emptyCase = new LinkstateRoutesCaseBuilder().build(); DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode(); Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty()); final Routes emptyRoutes = new LinkstateRoutesCaseBuilder() .setLinkstateRoutes(new LinkstateRoutesBuilder().build()).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode(); Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty()); final Routes routes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(ROUTES).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode(); final Collection result = this.ribSupport.changedRoutes(tree); Assert.assertFalse(result.isEmpty()); } }