BUG-6020: Implement Ribsupport for extensions
[bgpcep.git] / bgp / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / ipv4 / VpnIpv4RIBSupportTest.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
9 package org.opendaylight.protocol.bgp.l3vpn.ipv4;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.opendaylight.protocol.bgp.l3vpn.ipv4.VpnIpv4NlriParserTest.DISTINGUISHER;
14 import static org.opendaylight.protocol.bgp.l3vpn.ipv4.VpnIpv4NlriParserTest.IPV4_VPN;
15 import static org.opendaylight.protocol.bgp.l3vpn.ipv4.VpnIpv4NlriParserTest.IPv4_PREFIX;
16 import static org.opendaylight.protocol.bgp.l3vpn.ipv4.VpnIpv4NlriParserTest.LABEL_STACK;
17 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
18
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Lists;
21 import io.netty.buffer.ByteBuf;
22 import io.netty.buffer.Unpooled;
23 import java.util.Collection;
24 import java.util.Collections;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.opendaylight.protocol.bgp.l3vpn.AbstractVpnNlriParser;
28 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
30 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.bgp.rib.rib.loc.rib.tables.routes.VpnIpv4RoutesCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.bgp.rib.rib.loc.rib.tables.routes.VpnIpv4RoutesCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.l3vpn.ipv4.routes.VpnIpv4Routes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.l3vpn.ipv4.routes.VpnIpv4RoutesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4Case;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.route.VpnRoute;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.route.VpnRouteBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.route.VpnRouteKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.QName;
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.ChoiceNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
55
56 public class VpnIpv4RIBSupportTest extends AbstractRIBSupportTest {
57     private static final VpnIpv4RIBSupport RIB_SUPPORT = new VpnIpv4RIBSupport();
58
59     private static final DestinationVpnIpv4Case REACH_NLRI = new DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
60         new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN)).build()).build();
61
62     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
63         DestinationVpnIpv4Case UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
64         DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN)).build()).build();
65
66     private static final VpnRoute ROUTE;
67     private static final VpnIpv4Routes ROUTES;
68     private static final VpnRouteKey ROUTE_KEY;
69
70     static {
71         final BgpIpv4Activator act = new BgpIpv4Activator();
72         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
73         act.start(context);
74         final ByteBuf buffer = Unpooled.buffer();
75         AbstractVpnNlriParser.serializeNlri(Collections.singletonList(IPV4_VPN), buffer);
76         final byte[] arrayKey = ByteArray.readAllBytes(buffer);
77         ROUTE_KEY = new VpnRouteKey(arrayKey);
78         ROUTE = new VpnRouteBuilder().setPathId(new PathId(NON_PATH_ID)).setAttributes(ATTRIBUTES).setPrefix(IPv4_PREFIX)
79             .setLabelStack(LABEL_STACK).setRouteDistinguisher(DISTINGUISHER).setKey(ROUTE_KEY).build();
80         ROUTES = new VpnIpv4RoutesBuilder().setVpnRoute(Collections.singletonList(ROUTE)).build();
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<VpnRoute> instanceIdentifier = (InstanceIdentifier<VpnRoute>) this.deletedRoutes.get(0);
93         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(VpnRoute.class));
94     }
95
96     @Test
97     public void testPutRoutes() {
98         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
99         final VpnRoute route = (VpnRoute) 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 VpnIpv4RoutesCaseBuilder().setVpnIpv4Routes(new VpnIpv4RoutesBuilder().setVpnRoute(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(ROUTES), ATTRIBUTES);
114         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
115             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
116         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
117     }
118
119     @Test
120     public void testBuildMpReachNlriUpdate() {
121         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
122         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType());
123         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
124     }
125
126     @Test
127     public void testIsComplexRoute() {
128         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
129     }
130
131     @Test
132     public void testCacheableNlriObjects() {
133         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
134     }
135
136     @Test
137     public void testCacheableAttributeObjects() {
138         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
139     }
140
141     @Test
142     public void testRouteIdAddPath() {
143         Assert.assertNull(RIB_SUPPORT.getRouteIdAddPath(AbstractRIBSupportTest.PATH_ID, null));
144     }
145
146     @Test
147     public void testRoutePath() {
148         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
149         Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
150     }
151
152     @Test
153     public void testExtractPathId() {
154         Assert.assertEquals((Long) NON_PATH_ID, RIB_SUPPORT.extractPathId(null));
155     }
156
157     @Test
158     public void testRouteAttributesIdentifier() {
159         Assert.assertEquals(new NodeIdentifier(QName.create(VpnIpv4Routes.QNAME, Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT
160             .routeAttributesIdentifier());
161     }
162
163     @Test
164     public void testRoutesCaseClass() {
165         Assert.assertEquals(VpnIpv4RoutesCase.class, RIB_SUPPORT.routesCaseClass());
166     }
167
168     @Test
169     public void testRoutesContainerClass() {
170         Assert.assertEquals(VpnIpv4Routes.class, RIB_SUPPORT.routesContainerClass());
171     }
172
173     @Test
174     public void testRoutesListClass() {
175         Assert.assertEquals(VpnRoute.class, RIB_SUPPORT.routesListClass());
176     }
177
178     @Test
179     public void testChangedRoutes() {
180         final Routes emptyCase = new VpnIpv4RoutesCaseBuilder().build();
181         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode();
182         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
183
184         final Routes emptyRoutes = new VpnIpv4RoutesCaseBuilder().setVpnIpv4Routes(new VpnIpv4RoutesBuilder().build()).build();
185         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
186         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
187
188         final Routes routes = new VpnIpv4RoutesCaseBuilder().setVpnIpv4Routes(ROUTES).build();
189         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
190         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
191         Assert.assertFalse(result.isEmpty());
192     }
193 }