/* * Copyright (c) 2016 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.flowspec; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.util.Collection; import java.util.Collections; import java.util.List; import org.junit.Assert; import org.junit.Test; 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.Flowspec; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.FlowspecBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.DestinationFlowspec; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.DestinationFlowspecBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv4.route.FlowspecRoute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv4.route.FlowspecRouteBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv4.route.FlowspecRouteKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.FlowspecRoutes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.routes.FlowspecRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.QName; 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.ChoiceNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; public class FlowspecIpv4RIBSupportTest extends AbstractRIBSupportTest { private static final FlowspecIpv4RIBSupport RIB_SUPPORT; private static final FlowspecRoute ROUTE; private static final FlowspecRoutes ROUTES; private static final FlowspecRouteKey ROUTE_KEY; private static final PathId PATH_ID = new PathId(1L); private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder().setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build(); private static final List FLOW_LIST = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build()); private final static DestinationFlowspec DEST_FLOW = new DestinationFlowspecBuilder().setFlowspec(FLOW_LIST).setPathId(PATH_ID).build(); private static final DestinationFlowspecCase REACH_NLRI = new DestinationFlowspecCaseBuilder().setDestinationFlowspec(DEST_FLOW).build(); private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder().setDestinationFlowspec(DEST_FLOW).build(); static { final SimpleFlowspecExtensionProviderContext fs_context = new SimpleFlowspecExtensionProviderContext(); final FlowspecActivator activator = new FlowspecActivator(fs_context); final BGPActivator act = new BGPActivator(fs_context, activator); final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext(); act.start(context); RIB_SUPPORT = FlowspecIpv4RIBSupport.getInstance(fs_context); final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser( fs_context.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC)); ROUTE_KEY = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST)); ROUTE = new FlowspecRouteBuilder().setKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST) .setAttributes(new AttributesBuilder().build()).build(); ROUTES = new FlowspecRoutesBuilder().setFlowspecRoute(Collections.singletonList(ROUTE)).build(); } @Override public void setUp() throws Exception { super.setUp(); setUpTestCustomizer(RIB_SUPPORT); } @Test public void testDeleteRoutes() { RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI)); final InstanceIdentifier instanceIdentifier = (InstanceIdentifier) this.deletedRoutes.get(0); assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecRoute.class)); } @Test public void testPutRoutes() { RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes()); final FlowspecRoute route = (FlowspecRoute) this.insertedRoutes.get(0).getValue(); assertEquals(ROUTE, route); } @Test public void testEmptyRoute() throws Exception { final Routes empty = new FlowspecRoutesCaseBuilder().setFlowspecRoutes( new FlowspecRoutesBuilder().setFlowspecRoute(Collections.emptyList()).build()).build(); final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes(); assertEquals(createRoutes(empty), emptyRoutes); } @Test public void testBuildMpUnreachNlriUpdate() { final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES); assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class) .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType()); assertNull(update.getAttributes().getAugmentation(Attributes1.class)); } @Test public void testBuildMpReachNlriUpdate() { final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES); assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType()); assertNull(update.getAttributes().getAugmentation(Attributes2.class)); } @Test public void testIsComplexRoute() { Assert.assertTrue(RIB_SUPPORT.isComplexRoute()); } @Test public void testCacheableNlriObjects() { Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects()); } @Test public void testCacheableAttributeObjects() { Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects()); } @Test public void testRouteIdAddPath() { final NodeIdentifierWithPredicates expected = createRouteNIWP(ROUTES); final NodeIdentifierWithPredicates prefixNii = new NodeIdentifierWithPredicates(RIB_SUPPORT.routeQName(), ImmutableMap.of(RIB_SUPPORT.routeKeyQName(), ROUTE_KEY.getRouteKey())); Assert.assertEquals(expected, RIB_SUPPORT.getRouteIdAddPath(AbstractRIBSupportTest.PATH_ID, prefixNii)); } @Test public void testRoutePath() { final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES); Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii)); } @Test public void testExtractPathId() { Assert.assertEquals((Long) NON_PATH_ID, RIB_SUPPORT.extractPathId(null)); } @Test public void testRouteAttributesIdentifier() { Assert.assertEquals(new NodeIdentifier(QName.create(FlowspecRoutes.QNAME, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT.routeAttributesIdentifier()); } @Test public void testRoutesCaseClass() { Assert.assertEquals(FlowspecRoutesCase.class, RIB_SUPPORT.routesCaseClass()); } @Test public void testRoutesContainerClass() { Assert.assertEquals(FlowspecRoutes.class, RIB_SUPPORT.routesContainerClass()); } @Test public void testRoutesListClass() { Assert.assertEquals(FlowspecRoute.class, RIB_SUPPORT.routesListClass()); } @Test public void testChangedRoutes() { final Routes emptyCase = new FlowspecRoutesCaseBuilder().build(); DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode(); Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty()); final Routes emptyRoutes = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder().build()).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode(); Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty()); final Routes routes = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder() .setFlowspecRoute(Collections.singletonList(ROUTE)).build()).build(); tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode(); final Collection result = RIB_SUPPORT.changedRoutes(tree); Assert.assertFalse(result.isEmpty()); } }