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
8 package org.opendaylight.protocol.bgp.flowspec;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.flowspec.FlowspecTypeRegistries.SAFI;
21 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
23 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv4.DestinationFlowspecIpv4;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv4.DestinationFlowspecIpv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRoute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
54 public class FlowspecIpv4RIBSupportTest extends AbstractRIBSupportTest<FlowspecRoutesCase, FlowspecRoutes,
57 private FlowspecIpv4RIBSupport ribSupport;
58 private FlowspecRoute route;
59 private FlowspecRoutes routes;
60 private FlowspecRouteKey routeKey;
61 private static final PathId PATH_ID = new PathId(Uint32.ONE);
63 private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
64 .setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
65 private static final List<Flowspec> FLOW_LIST
66 = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
67 private static final DestinationFlowspecIpv4 DEST_FLOW = new DestinationFlowspecIpv4Builder()
68 .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
69 private static final DestinationFlowspecCase REACH_NLRI = new DestinationFlowspecCaseBuilder()
70 .setDestinationFlowspecIpv4(DEST_FLOW).build();
71 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
72 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase UNREACH_NLRI =
73 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
74 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder()
75 .setDestinationFlowspecIpv4(DEST_FLOW).build();
78 public void setUp() throws Exception {
80 final BGPActivator act = new BGPActivator();
81 final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
83 this.ribSupport = new FlowspecIpv4RIBSupport(this.adapter.currentSerializer());
85 final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(SAFI.FLOWSPEC);
87 this.routeKey = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
88 this.route = new FlowspecRouteBuilder().withKey(this.routeKey).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
89 .setAttributes(new AttributesBuilder().build()).build();
90 this.routes = new FlowspecRoutesBuilder().setFlowspecRoute(Map.of(this.route.key(), this.route)).build();
91 setUpTestCustomizer(this.ribSupport);
95 public void testDeleteRoutes() {
96 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
97 final InstanceIdentifier<FlowspecRoute> instanceIdentifier = this.deletedRoutes.get(0);
98 assertEquals(this.routeKey, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
102 public void testPutRoutes() {
103 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
104 assertEquals(this.route, this.insertedRoutes.get(0).getValue());
108 public void testEmptyRoute() {
109 assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
113 public void testBuildMpUnreachNlriUpdate() {
114 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(routes), ATTRIBUTES);
115 assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(AttributesUnreach.class)
116 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
117 assertNull(update.getAttributes().augmentation(AttributesReach.class));
121 public void testBuildMpReachNlriUpdate() {
122 final Update update = this.ribSupport.buildUpdate(createRoutes(routes), Collections.emptyList(), ATTRIBUTES);
123 assertEquals(REACH_NLRI, update.getAttributes().augmentation(AttributesReach.class).getMpReachNlri()
124 .getAdvertizedRoutes().getDestinationType());
125 assertNull(update.getAttributes().augmentation(AttributesUnreach.class));
129 public void testCacheableNlriObjects() {
130 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
134 public void testCacheableAttributeObjects() {
135 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
139 public void testRoutePath() {
140 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(this.routes);
141 assertEquals(getRoutePath().node(prefixNii),
142 this.ribSupport.routePath(getTablePath(), prefixNii));
146 public void testRouteAttributesIdentifier() {
147 assertEquals(new NodeIdentifier(QName.create(FlowspecRoutes.QNAME,
148 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
149 .Attributes.QNAME.getLocalName().intern())),
150 this.ribSupport.routeAttributesIdentifier());
154 public void testRoutesCaseClass() {
155 assertEquals(FlowspecRoutesCase.class, this.ribSupport.routesCaseClass());
159 public void testRoutesContainerClass() {
160 assertEquals(FlowspecRoutes.class, this.ribSupport.routesContainerClass());
164 public void testRoutesListClass() {
165 assertEquals(FlowspecRoute.class, this.ribSupport.routesListClass());
169 public void testChangedRoutes() {
170 final Routes emptyCase = new FlowspecRoutesCaseBuilder().build();
171 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
172 createRoutes(emptyCase)).getRootNode();
173 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
175 final Routes emptyRoutes
176 = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder().build()).build();
177 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
178 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
180 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(new FlowspecRoutesCaseBuilder()
181 .setFlowspecRoutes(new FlowspecRoutesBuilder().setFlowspecRoute(Map.of(this.route.key(), this.route))
182 .build()).build())).getRootNode();
183 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
184 Assert.assertFalse(result.isEmpty());