Update MRI projects for Aluminium
[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.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;
16
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.rev200120.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestinationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRoute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteKey;
38 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;
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.DestinationEvpnCaseBuilder;
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.destination.evpn._case.DestinationEvpnBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.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.common.Uint32;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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<EvpnRoutesCase, EvpnRoutes, EvpnRoute,
56         EvpnRouteKey> {
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)
64             .build();
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.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(Collections.singletonList(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(Collections.singletonList(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(Collections.singletonList(ROUTE)).build();
93     }
94
95
96     @Override
97     public void setUp() throws Exception {
98         super.setUp();
99         this.ribSupport = EvpnRibSupport.getInstance(this.adapter.currentSerializer());
100         setUpTestCustomizer(this.ribSupport);
101     }
102
103     @Test
104     public void testDeleteRoutes() {
105         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
106         final InstanceIdentifier<EvpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
107         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(EvpnRoute.class));
108     }
109
110     @Test
111     public void testPutRoutes() {
112         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
113         final EvpnRoute route = (EvpnRoute) this.insertedRoutes.get(0).getValue();
114         assertEquals(ROUTE, route);
115     }
116
117
118     @Test
119     public void testEmptyRoute() {
120         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
121     }
122
123     @Test
124     public void testBuildMpUnreachNlriUpdate() {
125         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(),
126                 createRoutes(EVPN_ROUTES), ATTRIBUTES);
127         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
128                 .getWithdrawnRoutes().getDestinationType());
129         assertNull(update.getAttributes().augmentation(Attributes1.class));
130     }
131
132     @Test
133     public void testBuildMpReachNlriUpdate() {
134         final Update update = this.ribSupport.buildUpdate(
135                 createRoutes(EVPN_ROUTES), Collections.emptyList(), ATTRIBUTES);
136         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
137                 .getAdvertizedRoutes().getDestinationType());
138         assertNull(update.getAttributes().augmentation(Attributes2.class));
139     }
140
141     @Test
142     public void testCacheableNlriObjects() {
143         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
144     }
145
146     @Test
147     public void testCacheableAttributeObjects() {
148         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
149     }
150
151     @Test
152     public void testRouteIdAddPath() {
153         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey()));
154     }
155
156     @Test
157     public void testRoutePath() {
158         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(EVPN_ROUTES);
159         assertEquals(getRoutePath().node(prefixNii),
160                 this.ribSupport.routePath(getTablePath(), prefixNii));
161     }
162
163     @Test
164     public void testRouteAttributesIdentifier() {
165         assertEquals(new NodeIdentifier(QName.create(EvpnRoutes.QNAME,
166                 Attributes.QNAME.getLocalName().intern())), this.ribSupport.routeAttributesIdentifier());
167     }
168
169     @Test
170     public void testRoutesCaseClass() {
171         assertEquals(EvpnRoutesCase.class, this.ribSupport.routesCaseClass());
172     }
173
174     @Test
175     public void testRoutesContainerClass() {
176         assertEquals(EvpnRoutes.class, this.ribSupport.routesContainerClass());
177     }
178
179     @Test
180     public void testRoutesListClass() {
181         assertEquals(EvpnRoute.class, this.ribSupport.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(this.ribSupport.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(this.ribSupport.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 = this.ribSupport.changedRoutes(tree);
198         Assert.assertFalse(result.isEmpty());
199     }
200 }