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.l3vpn.ipv4.FlowspecL3vpnIpv4RIBSupport;
21 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRoute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4Routes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4RoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4Case;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4CaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RdTwoOctetAs;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.common.Uint32;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
55 public class FlowspecL3vpnIpv4RIBSupportTest extends AbstractRIBSupportTest<FlowspecL3vpnIpv4RoutesCase,
56 FlowspecL3vpnIpv4Routes, FlowspecL3vpnRoute, FlowspecL3vpnRouteKey> {
58 private static final FlowspecL3vpnRoute ROUTE;
59 private static final FlowspecL3vpnRouteKey ROUTE_KEY;
60 private static final PathId PATH_ID = new PathId(Uint32.ONE);
61 private static final RouteDistinguisher RD = new RouteDistinguisher(new RdTwoOctetAs("0:5:3"));
62 private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
63 .setDestinationPrefix(new Ipv4Prefix("172.17.1.0/24")).build();
64 private static final List<Flowspec> FLOW_LIST
65 = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
66 private static final DestinationFlowspecL3vpnIpv4 DEST_FLOW
67 = new DestinationFlowspecL3vpnIpv4Builder().setRouteDistinguisher(RD)
68 .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
69 private static final DestinationFlowspecL3vpnIpv4Case REACH_NLRI
70 = new DestinationFlowspecL3vpnIpv4CaseBuilder().setDestinationFlowspecL3vpnIpv4(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.DestinationFlowspecL3vpnIpv4Case 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.DestinationFlowspecL3vpnIpv4CaseBuilder()
75 .setDestinationFlowspecL3vpnIpv4(DEST_FLOW).build();
76 private FlowspecL3vpnIpv4RIBSupport ribSupport;
79 ROUTE_KEY = new FlowspecL3vpnRouteKey(PATH_ID,
80 "[l3vpn with route-distinguisher 0:5:3] all packets to 172.17.1.0/24");
81 ROUTE = new FlowspecL3vpnRouteBuilder().withKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
82 .setAttributes(new AttributesBuilder().build()).setRouteDistinguisher(RD).build();
86 public void setUp() throws Exception {
88 this.ribSupport = new FlowspecL3vpnIpv4RIBSupport(this.adapter.currentSerializer());
89 setUpTestCustomizer(this.ribSupport);
93 public void testDeleteRoutes() {
94 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
95 final InstanceIdentifier<FlowspecL3vpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
96 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecL3vpnRoute.class));
100 public void testPutRoutes() {
101 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
102 final FlowspecL3vpnRoute route = (FlowspecL3vpnRoute) this.insertedRoutes.get(0).getValue();
103 assertEquals(ROUTE, route);
107 public void testEmptyRoute() {
108 assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
112 public void testBuildMpUnreachNlriUpdate() {
113 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(
114 new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build()),
116 assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
117 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
118 assertNull(update.getAttributes().augmentation(Attributes1.class));
122 public void testBuildMpReachNlriUpdate() {
123 final Update update = this.ribSupport.buildUpdate(createRoutes(
124 new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build()),
125 Collections.emptyList(), ATTRIBUTES);
126 final AdvertizedRoutes advertised = update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
127 .getAdvertizedRoutes();
128 assertEquals(REACH_NLRI, advertised.getDestinationType());
129 assertNull(update.getAttributes().augmentation(Attributes2.class));
133 public void testCacheableNlriObjects() {
134 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
138 public void testCacheableAttributeObjects() {
139 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
143 public void testRouteIdAddPath() {
144 assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey()));
148 public void testRoutePath() {
149 final NodeIdentifierWithPredicates prefixNii
150 = createRouteNIWP(new FlowspecL3vpnIpv4RoutesBuilder()
151 .setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build());
152 assertEquals(getRoutePath().node(prefixNii),
153 this.ribSupport.routePath(getTablePath(), prefixNii));
157 public void testRouteAttributesIdentifier() {
158 assertEquals(new NodeIdentifier(QName.create(FlowspecL3vpnIpv4Routes.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(FlowspecL3vpnIpv4RoutesCase.class, this.ribSupport.routesCaseClass());
170 public void testRoutesContainerClass() {
171 assertEquals(FlowspecL3vpnIpv4Routes.class, this.ribSupport.routesContainerClass());
175 public void testRoutesListClass() {
176 assertEquals(FlowspecL3vpnRoute.class, this.ribSupport.routesListClass());
180 public void testChangedRoutes() {
181 final Routes emptyCase = new FlowspecL3vpnIpv4RoutesCaseBuilder().build();
182 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
183 createRoutes(emptyCase)).getRootNode();
184 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
186 final Routes emptyRoutes = new FlowspecL3vpnIpv4RoutesCaseBuilder()
187 .setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder().build()).build();
188 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
189 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
191 final Routes routes = new FlowspecL3vpnIpv4RoutesCaseBuilder()
192 .setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder()
193 .setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build()).build();
194 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
195 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
196 Assert.assertFalse(result.isEmpty());