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