Provide codec Serializer via RibSupport
[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 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.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestinationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRoute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteKey;
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.DestinationEvpnCase;
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.DestinationEvpnCaseBuilder;
40 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;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.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.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<EvpnRoute> {
56     private EvpnRibSupport ribSupport;
57     private static final EvpnRoute ROUTE;
58     private static final EvpnRouteKey ROUTE_KEY;
59     private static final EvpnDestination EVPN_DESTINATION = new EvpnDestinationBuilder()
60             .setRouteDistinguisher(RD)
61             .setEvpnChoice(ETHERNET_AD_ROUTE_CASE)
62             .setPathId(NON_PATH_ID)
63             .build();
64     private static final DestinationEvpnCase REACH_NLRI = new DestinationEvpnCaseBuilder()
65             .setDestinationEvpn(new DestinationEvpnBuilder()
66                     .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
67     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
68             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCase UNREACH_NLRI =
69             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes
70                     .mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
71                     .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
72                             .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
73                             .destination.evpn._case.DestinationEvpnBuilder()
74                             .setEvpnDestination(Collections.singletonList(EVPN_DESTINATION)).build()).build();
75
76     private static final EvpnRoutes EVPN_ROUTES;
77
78     static {
79         final BGPActivator act = new BGPActivator();
80         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
81         act.start(context);
82         final ByteBuf buffer = Unpooled.buffer();
83         EvpnNlriParser.serializeNlri(Collections.singletonList(new EvpnDestinationBuilder()
84                 .setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE_KEY).build()), buffer);
85         ROUTE_KEY = new EvpnRouteKey(new PathId(0L), ByteArray.encodeBase64(buffer));
86         ROUTE = new EvpnRouteBuilder().setRouteKey(ROUTE_KEY.getRouteKey())
87                 .setPathId(ROUTE_KEY.getPathId())
88                 .setAttributes(ATTRIBUTES).setRouteDistinguisher(RD).setEvpnChoice(ETHERNET_AD_ROUTE_CASE).build();
89         EVPN_ROUTES = new EvpnRoutesBuilder().setEvpnRoute(Collections.singletonList(ROUTE)).build();
90     }
91
92
93     @Override
94     public void setUp() throws Exception {
95         super.setUp();
96         this.ribSupport = EvpnRibSupport.getInstance(this.mappingService);
97         setUpTestCustomizer(this.ribSupport);
98     }
99
100     @Test
101     public void testDeleteRoutes() {
102         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
103         final InstanceIdentifier<EvpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
104         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(EvpnRoute.class));
105     }
106
107     @Test
108     public void testPutRoutes() {
109         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
110         final EvpnRoute route = (EvpnRoute) this.insertedRoutes.get(0).getValue();
111         assertEquals(ROUTE, route);
112     }
113
114
115     @Test
116     public void testEmptyRoute() {
117         final Routes empty = new EvpnRoutesCaseBuilder().setEvpnRoutes(
118                 new EvpnRoutesBuilder().setEvpnRoute(Collections.emptyList()).build()).build();
119         final ChoiceNode emptyRoutes = this.ribSupport.emptyRoutes();
120         assertEquals(createRoutes(empty), emptyRoutes);
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().getAugmentation(Attributes2.class).getMpUnreachNlri()
128                 .getWithdrawnRoutes().getDestinationType());
129         assertNull(update.getAttributes().getAugmentation(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().getAugmentation(Attributes1.class).getMpReachNlri()
137                 .getAdvertizedRoutes().getDestinationType());
138         assertNull(update.getAttributes().getAugmentation(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().getValue(),
154                 ROUTE_KEY.getRouteKey()));
155     }
156
157     @Test
158     public void testRoutePath() {
159         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(EVPN_ROUTES);
160         assertEquals(getRoutePath().node(prefixNii),
161                 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
162     }
163
164     @Test
165     public void testRouteAttributesIdentifier() {
166         assertEquals(new NodeIdentifier(QName.create(EvpnRoutes.QNAME,
167                 Attributes.QNAME.getLocalName().intern())), this.ribSupport.routeAttributesIdentifier());
168     }
169
170     @Test
171     public void testRoutesCaseClass() {
172         assertEquals(EvpnRoutesCase.class, this.ribSupport.routesCaseClass());
173     }
174
175     @Test
176     public void testRoutesContainerClass() {
177         assertEquals(EvpnRoutes.class, this.ribSupport.routesContainerClass());
178     }
179
180     @Test
181     public void testRoutesListClass() {
182         assertEquals(EvpnRoute.class, this.ribSupport.routesListClass());
183     }
184
185     @Test
186     public void testChangedRoutes() {
187         final Routes emptyCase = new EvpnRoutesCaseBuilder().build();
188         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
189                 createRoutes(emptyCase)).getRootNode();
190         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
191
192         final Routes emptyRoutes = new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
193         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
194         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
195
196         final Routes routes = new EvpnRoutesCaseBuilder().setEvpnRoutes(EVPN_ROUTES).build();
197         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
198         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
199         Assert.assertFalse(result.isEmpty());
200     }
201 }