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.evpn.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.RD;
13 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETHERNET_AD_ROUTE_CASE;
14 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETHERNET_AD_ROUTE_CASE_KEY;
15 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
17 import com.google.common.collect.ImmutableSet;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.Collection;
21 import java.util.Collections;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser;
25 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
27 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestinationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRoute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.QName;
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.ChoiceNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
55 public final class EvpnRibSupportTest extends AbstractRIBSupportTest<EvpnRoutesCase, EvpnRoutes, EvpnRoute,
57 private EvpnRibSupport ribSupport;
58 private static final EvpnRoute ROUTE;
59 private static final EvpnRouteKey ROUTE_KEY;
60 private static final EvpnDestination EVPN_DESTINATION = new EvpnDestinationBuilder()
61 .setRouteDistinguisher(RD)
62 .setEvpnChoice(ETHERNET_AD_ROUTE_CASE)
63 .setPathId(NON_PATH_ID)
65 private static final DestinationEvpnCase REACH_NLRI = new DestinationEvpnCaseBuilder()
66 .setDestinationEvpn(new DestinationEvpnBuilder()
67 .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
68 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
69 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase UNREACH_NLRI =
70 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes
71 .mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
72 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
73 .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
74 .destination.evpn._case.DestinationEvpnBuilder()
75 .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
77 private static final EvpnRoutes EVPN_ROUTES;
80 final BGPActivator act = new BGPActivator();
81 final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
83 final ByteBuf buffer = Unpooled.buffer();
84 EvpnNlriParser.serializeNlri(Collections.singletonList(new EvpnDestinationBuilder()
85 .setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE_KEY).build()), buffer);
86 ROUTE_KEY = new EvpnRouteKey(new PathId(0L), ByteArray.encodeBase64(buffer));
87 ROUTE = new EvpnRouteBuilder().setRouteKey(ROUTE_KEY.getRouteKey())
88 .setPathId(ROUTE_KEY.getPathId())
89 .setAttributes(ATTRIBUTES).setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE).build();
90 EVPN_ROUTES = new EvpnRoutesBuilder().setEvpnRoute(Collections.singletonList(ROUTE)).build();
95 public void setUp() throws Exception {
97 this.ribSupport = EvpnRibSupport.getInstance(this.mappingService);
98 setUpTestCustomizer(this.ribSupport);
102 public void testDeleteRoutes() {
103 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
104 final InstanceIdentifier<EvpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
105 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(EvpnRoute.class));
109 public void testPutRoutes() {
110 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
111 final EvpnRoute route = (EvpnRoute) this.insertedRoutes.get(0).getValue();
112 assertEquals(ROUTE, route);
117 public void testEmptyRoute() {
118 final Routes empty = new EvpnRoutesCaseBuilder().setEvpnRoutes(
119 new EvpnRoutesBuilder().setEvpnRoute(Collections.emptyList()).build()).build();
120 final ChoiceNode emptyRoutes = this.ribSupport.emptyRoutes();
121 assertEquals(createRoutes(empty), emptyRoutes);
125 public void testBuildMpUnreachNlriUpdate() {
126 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(),
127 createRoutes(EVPN_ROUTES), ATTRIBUTES);
128 assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
129 .getWithdrawnRoutes().getDestinationType());
130 assertNull(update.getAttributes().augmentation(Attributes1.class));
134 public void testBuildMpReachNlriUpdate() {
135 final Update update = this.ribSupport.buildUpdate(
136 createRoutes(EVPN_ROUTES), Collections.emptyList(), ATTRIBUTES);
137 assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
138 .getAdvertizedRoutes().getDestinationType());
139 assertNull(update.getAttributes().augmentation(Attributes2.class));
143 public void testCacheableNlriObjects() {
144 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
148 public void testCacheableAttributeObjects() {
149 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
153 public void testRouteIdAddPath() {
154 assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
155 ROUTE_KEY.getRouteKey()));
159 public void testRoutePath() {
160 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(EVPN_ROUTES);
161 assertEquals(getRoutePath().node(prefixNii),
162 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
166 public void testRouteAttributesIdentifier() {
167 assertEquals(new NodeIdentifier(QName.create(EvpnRoutes.QNAME,
168 Attributes.QNAME.getLocalName().intern())), this.ribSupport.routeAttributesIdentifier());
172 public void testRoutesCaseClass() {
173 assertEquals(EvpnRoutesCase.class, this.ribSupport.routesCaseClass());
177 public void testRoutesContainerClass() {
178 assertEquals(EvpnRoutes.class, this.ribSupport.routesContainerClass());
182 public void testRoutesListClass() {
183 assertEquals(EvpnRoute.class, this.ribSupport.routesListClass());
187 public void testChangedRoutes() {
188 final Routes emptyCase = new EvpnRoutesCaseBuilder().build();
189 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
190 createRoutes(emptyCase)).getRootNode();
191 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
193 final Routes emptyRoutes = new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
194 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
195 Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
197 final Routes routes = new EvpnRoutesCaseBuilder().setEvpnRoutes(EVPN_ROUTES).build();
198 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
199 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
200 Assert.assertFalse(result.isEmpty());