/* * Copyright (c) 2018 AT&T Intellectual Property. 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.route.targetcontrain.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import org.junit.Test; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest; import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator; 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.message.rev200120.PathId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update; 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.Attributes; 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.route.target.constrain.rev180618.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainAs4ExtendedCommunityCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRoute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationRouteTargetConstrainAdvertizedCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationRouteTargetConstrainAdvertizedCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrainBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationRouteTargetConstrainWithdrawnCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationRouteTargetConstrainWithdrawnCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommon; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommonBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.Uint16; import org.opendaylight.yangtools.yang.common.Uint32; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; public class RouteTargetConstrainRIBSupportTest extends AbstractRIBSupportTest { private static final AsNumber ORIGIN_AS = new AsNumber(Uint32.valueOf(72)); private static final PathId PATH_ID = new PathId(Uint32.ZERO); private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder() .setAsNumber(new AsNumber(Uint32.valueOf(20))) .setLocalAdministrator(Uint16.valueOf(100)) .build(); private static final RouteTargetConstrainChoice RT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder() .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder() .setAs4SpecificCommon(AS_COMMON).build()) .build(); private static final RouteTargetConstrainRouteKey ROUTE_KEY = new RouteTargetConstrainRouteKey(PATH_ID, "AgIAAAAUAGQ="); private static final RouteTargetConstrainRoute ROUTE = new RouteTargetConstrainRouteBuilder() .setRouteKey(ROUTE_KEY.getRouteKey()) .setPathId(ROUTE_KEY.getPathId()) .setAttributes(ATTRIBUTES) .setRouteTargetConstrainChoice(RT) .setOriginAs(ORIGIN_AS) .build(); private static final RouteTargetConstrainRoutes RT_ROUTES = new RouteTargetConstrainRoutesBuilder() .setRouteTargetConstrainRoute(Collections.singletonList(ROUTE)).build(); private static final RouteTargetConstrainDestination RT_DESTINATION = new RouteTargetConstrainDestinationBuilder() .setRouteTargetConstrainChoice(RT) .setOriginAs(ORIGIN_AS) .setPathId(PATH_ID) .build(); private static final DestinationRouteTargetConstrainAdvertizedCase REACH_NLRI = new DestinationRouteTargetConstrainAdvertizedCaseBuilder() .setDestinationRouteTargetConstrain(new DestinationRouteTargetConstrainBuilder() .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build(); private static final DestinationRouteTargetConstrainWithdrawnCase UNREACH_NLRI = new DestinationRouteTargetConstrainWithdrawnCaseBuilder() .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp .route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination .type.destination.route.target.constrain.withdrawn._case.DestinationRouteTargetConstrainBuilder() .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build(); private RouteTargetConstrainRIBSupport ribSupport; @Override public void setUp() throws Exception { super.setUp(); NlriActivator.registerNlriParsers(new ArrayList<>()); this.ribSupport = RouteTargetConstrainRIBSupport.getInstance(this.mappingService); setUpTestCustomizer(this.ribSupport); } @Test public void testDeleteRoutes() { final ContainerNode withdraw = createNlriWithDrawnRoute(UNREACH_NLRI); this.ribSupport.deleteRoutes(this.tx, getTablePath(), withdraw); final InstanceIdentifier instanceIdentifier = this.deletedRoutes.get(0); assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(RouteTargetConstrainRoute.class)); } @Test public void testPutRoutes() { this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes()); final RouteTargetConstrainRoute route = (RouteTargetConstrainRoute) this.insertedRoutes.get(0).getValue(); assertEquals(ROUTE, route); } @Test public void testEmptyRoute() { assertEquals(createEmptyTable(), this.ribSupport.emptyTable()); } @Test public void testBuildMpUnreachNlriUpdate() { final Collection routes = createRoutes(RT_ROUTES); final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), routes, ATTRIBUTES); assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri() .getWithdrawnRoutes().getDestinationType()); assertNull(update.getAttributes().augmentation(Attributes1.class)); } @Test public void testBuildMpReachNlriUpdate() { final Collection routes = createRoutes(RT_ROUTES); final Update update = this.ribSupport.buildUpdate(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() { assertEquals(ImmutableSet.of(RouteTargetConstrainRoutesCase.class), this.ribSupport.cacheableNlriObjects()); } @Test public void testCacheableAttributeObjects() { assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects()); } @Test public void testRouteIdAddPath() { assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey())); } @Test public void testRoutePath() { final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(RT_ROUTES); final YangInstanceIdentifier expected = getRoutePath().node(prefixNii); final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath(), prefixNii); assertEquals(expected, actual); } @Test public void testRouteAttributesIdentifier() { assertEquals(new NodeIdentifier(Attributes.QNAME.withModule(BindingReflections .getQNameModule(RouteTargetConstrainRoutesCase.class))), this.ribSupport.routeAttributesIdentifier()); } @Test public void testRoutesCaseClass() { assertEquals(RouteTargetConstrainRoutesCase.class, this.ribSupport.routesCaseClass()); } @Test public void testRoutesContainerClass() { assertEquals(RouteTargetConstrainRoutes.class, this.ribSupport.routesContainerClass()); } @Test public void testRoutesListClass() { assertEquals(RouteTargetConstrainRoute.class, this.ribSupport.routesListClass()); } @Test public void testChangedRoutes() { final Routes emptyCase = new RouteTargetConstrainRoutesCaseBuilder().build(); DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode(); assertTrue(this.ribSupport.changedRoutes(tree).isEmpty()); final Routes emptyRoutes = new RouteTargetConstrainRoutesCaseBuilder() .setRouteTargetConstrainRoutes(new RouteTargetConstrainRoutesBuilder().build()).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode(); assertTrue(this.ribSupport.changedRoutes(tree).isEmpty()); final Routes routes = new RouteTargetConstrainRoutesCaseBuilder() .setRouteTargetConstrainRoutes(RT_ROUTES).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode(); final Collection result = this.ribSupport.changedRoutes(tree); assertFalse(result.isEmpty()); } }