2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.flowspec;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.Flowspec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.FlowspecBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.ipv4.DestinationFlowspec;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.ipv4.DestinationFlowspecBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv4.route.FlowspecRoute;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv4.route.FlowspecRouteBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.ipv4.route.FlowspecRouteKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.routes.FlowspecRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.routes.FlowspecRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
49 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
53 public class FlowspecIpv4RIBSupportTest extends AbstractRIBSupportTest<FlowspecRoute> {
55 private FlowspecIpv4RIBSupport ribSupport;
56 private FlowspecRoute route;
57 private FlowspecRoutes routes;
58 private FlowspecRouteKey routeKey;
59 private static final PathId PATH_ID = new PathId(1L);
61 private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
62 .setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
63 private static final List<Flowspec> FLOW_LIST
64 = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
65 private static final DestinationFlowspec DEST_FLOW = new DestinationFlowspecBuilder()
66 .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
67 private static final DestinationFlowspecCase REACH_NLRI
68 = new DestinationFlowspecCaseBuilder().setDestinationFlowspec(DEST_FLOW).build();
69 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
70 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase UNREACH_NLRI
71 = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
72 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder()
73 .setDestinationFlowspec(DEST_FLOW).build();
76 public void setUp() throws Exception {
78 final SimpleFlowspecExtensionProviderContext fsContext = new SimpleFlowspecExtensionProviderContext();
79 final FlowspecActivator activator = new FlowspecActivator(fsContext);
80 final BGPActivator act = new BGPActivator(activator);
81 final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
83 this.ribSupport = FlowspecIpv4RIBSupport.getInstance(fsContext, this.mappingService);
85 final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(
86 fsContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4,
87 SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
89 this.routeKey = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
90 this.route = new FlowspecRouteBuilder().setKey(this.routeKey).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
91 .setAttributes(new AttributesBuilder().build()).build();
92 this.routes = new FlowspecRoutesBuilder().setFlowspecRoute(Collections.singletonList(this.route)).build();
93 setUpTestCustomizer(this.ribSupport);
97 public void testDeleteRoutes() {
98 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
99 final InstanceIdentifier<FlowspecRoute> instanceIdentifier = this.deletedRoutes.get(0);
100 assertEquals(this.routeKey, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
104 public void testPutRoutes() {
105 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
106 final FlowspecRoute route = (FlowspecRoute) this.insertedRoutes.get(0).getValue();
107 assertEquals(this.route, route);
111 public void testEmptyRoute() {
112 final Routes empty = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(
113 new FlowspecRoutesBuilder().setFlowspecRoute(Collections.emptyList()).build()).build();
114 final ChoiceNode emptyRoutes = this.ribSupport.emptyRoutes();
115 assertEquals(createRoutes(empty), emptyRoutes);
119 public void testBuildMpUnreachNlriUpdate() {
120 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(routes), ATTRIBUTES);
121 assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
122 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
123 assertNull(update.getAttributes().getAugmentation(Attributes1.class));
127 public void testBuildMpReachNlriUpdate() {
128 final Update update = this.ribSupport.buildUpdate(createRoutes(routes), Collections.emptyList(), ATTRIBUTES);
129 assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri()
130 .getAdvertizedRoutes().getDestinationType());
131 assertNull(update.getAttributes().getAugmentation(Attributes2.class));
135 public void testCacheableNlriObjects() {
136 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
140 public void testCacheableAttributeObjects() {
141 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
145 public void testRouteIdAddPath() {
146 assertEquals(this.routeKey, this.ribSupport.createRouteListKey(1L, this.routeKey.getRouteKey()));
150 public void testRoutePath() {
151 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(this.routes);
152 assertEquals(getRoutePath().node(prefixNii),
153 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
157 public void testRouteAttributesIdentifier() {
158 assertEquals(new NodeIdentifier(QName.create(FlowspecRoutes.QNAME,
159 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
160 .Attributes.QNAME.getLocalName().intern())),
161 this.ribSupport.routeAttributesIdentifier());
165 public void testRoutesCaseClass() {
166 assertEquals(FlowspecRoutesCase.class, this.ribSupport.routesCaseClass());
170 public void testRoutesContainerClass() {
171 assertEquals(FlowspecRoutes.class, this.ribSupport.routesContainerClass());
175 public void testRoutesListClass() {
176 assertEquals(FlowspecRoute.class, this.ribSupport.routesListClass());
180 public void testChangedRoutes() {
181 final Routes emptyCase = new FlowspecRoutesCaseBuilder().build();
182 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
183 createRoutes(emptyCase)).getRootNode();
184 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
186 final Routes emptyRoutes
187 = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder().build()).build();
188 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
189 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
191 final Routes routes = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder()
192 .setFlowspecRoute(Collections.singletonList(this.route)).build()).build();
193 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
194 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
195 Assert.assertFalse(result.isEmpty());