Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / 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.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.RD;
15 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETHERNET_AD_ROUTE_CASE;
16 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETHERNET_AD_ROUTE_CASE_KEY;
17 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Set;
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.rev200120.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestination;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestinationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRoute;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.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.rev200120.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.rev200120.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.rev200120.PathId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.Uint32;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
54 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
55 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
56
57 public final class EvpnRibSupportTest extends AbstractRIBSupportTest<EvpnRoutesCase, EvpnRoutes, EvpnRoute> {
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)
64             .build();
65     private static final DestinationEvpnCase REACH_NLRI = new DestinationEvpnCaseBuilder()
66             .setDestinationEvpn(new DestinationEvpnBuilder()
67                     .setEvpnDestination(List.of(EVPN_DESTINATION)).build()).build();
68     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.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.rev200120.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
73                     .bgp.evpn.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
74                     .destination.evpn._case.DestinationEvpnBuilder()
75                     .setEvpnDestination(List.of(EVPN_DESTINATION))
76                     .build())
77                 .build();
78
79     private static final EvpnRoutes EVPN_ROUTES;
80
81     static {
82         final BGPActivator act = new BGPActivator();
83         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
84         act.start(context);
85         final ByteBuf buffer = Unpooled.buffer();
86         EvpnNlriParser.serializeNlri(List.of(new EvpnDestinationBuilder()
87                 .setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE_KEY).build()), buffer);
88         ROUTE_KEY = new EvpnRouteKey(new PathId(Uint32.ZERO), ByteArray.encodeBase64(buffer));
89         ROUTE = new EvpnRouteBuilder().setRouteKey(ROUTE_KEY.getRouteKey())
90                 .setPathId(ROUTE_KEY.getPathId())
91                 .setAttributes(ATTRIBUTES).setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE).build();
92         EVPN_ROUTES = new EvpnRoutesBuilder().setEvpnRoute(BindingMap.of(ROUTE)).build();
93     }
94
95     private EvpnRibSupport ribSupport;
96
97     @Override
98     public void setUp() throws Exception {
99         super.setUp();
100         ribSupport = new EvpnRibSupport(adapter.currentSerializer());
101         setUpTestCustomizer(ribSupport);
102     }
103
104     @Test
105     public void testDeleteRoutes() {
106         ribSupport.deleteRoutes(tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
107         final InstanceIdentifier<EvpnRoute> instanceIdentifier = deletedRoutes.get(0);
108         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(EvpnRoute.class));
109     }
110
111     @Test
112     public void testPutRoutes() {
113         ribSupport.putRoutes(tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
114         final EvpnRoute route = (EvpnRoute) insertedRoutes.get(0).getValue();
115         assertEquals(ROUTE, route);
116     }
117
118
119     @Test
120     public void testEmptyRoute() {
121         assertEquals(createEmptyTable(), ribSupport.emptyTable());
122     }
123
124     @Test
125     public void testBuildMpUnreachNlriUpdate() {
126         final Update update = ribSupport.buildUpdate(List.of(),
127                 createRoutes(EVPN_ROUTES), ATTRIBUTES);
128         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(AttributesUnreach.class).getMpUnreachNlri()
129                 .getWithdrawnRoutes().getDestinationType());
130         assertNull(update.getAttributes().augmentation(AttributesReach.class));
131     }
132
133     @Test
134     public void testBuildMpReachNlriUpdate() {
135         final Update update = ribSupport.buildUpdate(
136                 createRoutes(EVPN_ROUTES), List.of(), ATTRIBUTES);
137         assertEquals(REACH_NLRI, update.getAttributes().augmentation(AttributesReach.class).getMpReachNlri()
138                 .getAdvertizedRoutes().getDestinationType());
139         assertNull(update.getAttributes().augmentation(AttributesUnreach.class));
140     }
141
142     @Test
143     public void testCacheableNlriObjects() {
144         assertEquals(Set.of(), ribSupport.cacheableNlriObjects());
145     }
146
147     @Test
148     public void testCacheableAttributeObjects() {
149         assertEquals(Set.of(), ribSupport.cacheableAttributeObjects());
150     }
151
152     @Test
153     public void testRoutePath() {
154         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(EVPN_ROUTES);
155         assertEquals(getRoutePath().node(prefixNii),
156                 ribSupport.routePath(getTablePath(), prefixNii));
157     }
158
159     @Test
160     public void testRouteAttributesIdentifier() {
161         assertEquals(new NodeIdentifier(QName.create(EvpnRoutes.QNAME,
162                 Attributes.QNAME.getLocalName().intern())), ribSupport.routeAttributesIdentifier());
163     }
164
165     @Test
166     public void testRoutesCaseClass() {
167         assertEquals(EvpnRoutesCase.class, ribSupport.routesCaseClass());
168     }
169
170     @Test
171     public void testRoutesContainerClass() {
172         assertEquals(EvpnRoutes.class, ribSupport.routesContainerClass());
173     }
174
175     @Test
176     public void testRoutesListClass() {
177         assertEquals(EvpnRoute.class, ribSupport.routesListClass());
178     }
179
180     @Test
181     public void testChangedRoutes() {
182         final Routes emptyCase = new EvpnRoutesCaseBuilder().build();
183         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
184                 createRoutes(emptyCase)).getRootNode();
185         assertTrue(ribSupport.changedRoutes(tree).isEmpty());
186
187         final Routes emptyRoutes = new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
188         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
189         assertTrue(ribSupport.changedRoutes(tree).isEmpty());
190
191         final Routes routes = new EvpnRoutesCaseBuilder().setEvpnRoutes(EVPN_ROUTES).build();
192         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
193         final Collection<DataTreeCandidateNode> result = ribSupport.changedRoutes(tree);
194         assertFalse(result.isEmpty());
195     }
196 }