MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / EvpnRibSupportTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.evpn.impl;
9
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
16 import com.google.common.collect.ImmutableSet;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.util.Collection;
20 import java.util.Collections;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser;
24 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
25 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
26 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestination;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestinationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRoute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteKey;
37 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;
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.DestinationEvpnCaseBuilder;
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.destination.evpn._case.DestinationEvpnBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
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.Attributes;
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.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
50 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
53
54 public final class EvpnRibSupportTest extends AbstractRIBSupportTest {
55     private static final EvpnRibSupport RIB_SUPPORT = EvpnRibSupport.getInstance();
56     private static final EvpnRoute ROUTE;
57     private static final EvpnRouteKey ROUTE_KEY;
58     private static final EvpnDestination EVPN_DESTINATION = new EvpnDestinationBuilder().setRouteDistinguisher(RD)
59             .setEvpnChoice(ETHERNET_AD_ROUTE_CASE).build();
60     private static final DestinationEvpnCase REACH_NLRI = new DestinationEvpnCaseBuilder()
61             .setDestinationEvpn(new DestinationEvpnBuilder()
62                     .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
63     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
64             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase UNREACH_NLRI =
65             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes
66                     .mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
67                     .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
68                             .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
69                             .destination.evpn._case.DestinationEvpnBuilder()
70                             .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
71
72     private static final EvpnRoutes EVPN_ROUTES;
73
74     static {
75         final BGPActivator act = new BGPActivator();
76         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
77         act.start(context);
78         final ByteBuf buffer = Unpooled.buffer();
79         EvpnNlriParser.serializeNlri(Collections.singletonList(new EvpnDestinationBuilder()
80                 .setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE_KEY).build()), buffer);
81         ROUTE_KEY = new EvpnRouteKey(new PathId(0L), ByteArray.encodeBase64(buffer));
82         ROUTE = new EvpnRouteBuilder().setRouteKey(ROUTE_KEY.getRouteKey())
83                 .setPathId(ROUTE_KEY.getPathId())
84                 .setAttributes(ATTRIBUTES).setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE).build();
85         EVPN_ROUTES = new EvpnRoutesBuilder().setEvpnRoute(Collections.singletonList(ROUTE)).build();
86     }
87
88
89     @Override
90     public void setUp() throws Exception {
91         super.setUp();
92         setUpTestCustomizer(RIB_SUPPORT);
93     }
94
95     @Test
96     public void testDeleteRoutes() {
97         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
98         @SuppressWarnings("unchecked") final InstanceIdentifier<EvpnRoute> instanceIdentifier =
99                 (InstanceIdentifier<EvpnRoute>) this.deletedRoutes.get(0);
100         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(EvpnRoute.class));
101     }
102
103     @Test
104     public void testPutRoutes() {
105         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
106         final EvpnRoute route = (EvpnRoute) this.insertedRoutes.get(0).getValue();
107         assertEquals(ROUTE, route);
108     }
109
110
111     @Test
112     public void testEmptyRoute() {
113         final Routes empty = new EvpnRoutesCaseBuilder().setEvpnRoutes(
114                 new EvpnRoutesBuilder().setEvpnRoute(Collections.emptyList()).build()).build();
115         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
116         assertEquals(createRoutes(empty), emptyRoutes);
117     }
118
119     @Test
120     public void testBuildMpUnreachNlriUpdate() {
121         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(EVPN_ROUTES), ATTRIBUTES);
122         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri()
123                 .getWithdrawnRoutes().getDestinationType());
124         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
125     }
126
127     @Test
128     public void testBuildMpReachNlriUpdate() {
129         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(EVPN_ROUTES), Collections.emptyList(), ATTRIBUTES);
130         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri()
131                 .getAdvertizedRoutes().getDestinationType());
132         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
133     }
134
135     @Test
136     public void testIsComplexRoute() {
137         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
138     }
139
140     @Test
141     public void testCacheableNlriObjects() {
142         assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
143     }
144
145     @Test
146     public void testCacheableAttributeObjects() {
147         assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
148     }
149
150     @Test
151     public void testRouteIdAddPath() {
152         assertEquals(ROUTE_KEY, RIB_SUPPORT.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
153                 ROUTE_KEY.getRouteKey()));
154     }
155
156     @Test
157     public void testRoutePath() {
158         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(EVPN_ROUTES);
159         assertEquals(getRoutePath().node(prefixNii),
160                 RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
161     }
162
163     @Test
164     public void testRouteAttributesIdentifier() {
165         assertEquals(new NodeIdentifier(QName.create(EvpnRoutes.QNAME,
166                 Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT.routeAttributesIdentifier());
167     }
168
169     @Test
170     public void testRoutesCaseClass() {
171         assertEquals(EvpnRoutesCase.class, RIB_SUPPORT.routesCaseClass());
172     }
173
174     @Test
175     public void testRoutesContainerClass() {
176         assertEquals(EvpnRoutes.class, RIB_SUPPORT.routesContainerClass());
177     }
178
179     @Test
180     public void testRoutesListClass() {
181         assertEquals(EvpnRoute.class, RIB_SUPPORT.routesListClass());
182     }
183
184     @Test
185     public void testChangedRoutes() {
186         final Routes emptyCase = new EvpnRoutesCaseBuilder().build();
187         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
188                 createRoutes(emptyCase)).getRootNode();
189         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
190
191         final Routes emptyRoutes = new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
192         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
193         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
194
195         final Routes routes = new EvpnRoutesCaseBuilder().setEvpnRoutes(EVPN_ROUTES).build();
196         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
197         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
198         Assert.assertFalse(result.isEmpty());
199     }
200 }