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