57f6a5ab34c6456d0fcd69a1f7d0174827a3d040
[bgpcep.git] / bgp / extensions / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / mcast / L3vpnMcastIpv4RIBSupportTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.l3vpn.mcast;
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
15 import com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Map;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
21 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv4Case;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv4CaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.L3vpnMcastRoute;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.L3vpnMcastRouteBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.L3vpnMcastRouteKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.ipv4.L3vpnMcastRoutesIpv4;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.ipv4.L3vpnMcastRoutesIpv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.l3vpn.mcast.ipv4.advertized._case.DestinationIpv4L3vpnMcastBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RdIpv4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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 L3vpnMcastIpv4RIBSupportTest extends AbstractRIBSupportTest<L3vpnMcastRoutesIpv4Case ,L3vpnMcastRoutesIpv4,
57         L3vpnMcastRoute, L3vpnMcastRouteKey> {
58     private static final PathId PATH_ID = new PathId(Uint32.ZERO);
59     private static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
60     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdIpv4("1.2.3.4:258"));
61     private static final L3vpnMcastRouteKey ROUTE_KEY
62             = new L3vpnMcastRouteKey(PATH_ID, "IAABAQIDBAECGCIBFgAAAAA=");
63     private static final L3vpnMcastRoute ROUTE = new L3vpnMcastRouteBuilder()
64             .setRouteKey(ROUTE_KEY.getRouteKey())
65             .setPathId(ROUTE_KEY.getPathId())
66             .setAttributes(ATTRIBUTES)
67             .setRouteDistinguisher(RD)
68             .setPrefix(IPV4_PREFIX)
69             .build();
70     private static final L3vpnMcastRoutesIpv4 MCAST_L3VPN_ROUTES = new L3vpnMcastRoutesIpv4Builder()
71             .setL3vpnMcastRoute(Map.of(ROUTE.key(), ROUTE))
72             .build();
73
74     private static final L3vpnMcastDestination MCAST_L3VPN_DESTINATION = new L3vpnMcastDestinationBuilder()
75             .setRouteDistinguisher(RD)
76             .setPrefix(IPV4_PREFIX)
77             .setPathId(PATH_ID)
78             .build();
79     private static final DestinationL3vpnMcastIpv4AdvertizedCase REACH_NLRI
80             = new DestinationL3vpnMcastIpv4AdvertizedCaseBuilder()
81             .setDestinationIpv4L3vpnMcast(new DestinationIpv4L3vpnMcastBuilder()
82                     .setL3vpnMcastDestination(Collections.singletonList(MCAST_L3VPN_DESTINATION)).build()).build();
83     private static final DestinationL3vpnMcastIpv4WithdrawnCase UNREACH_NLRI
84             = new DestinationL3vpnMcastIpv4WithdrawnCaseBuilder()
85             .setDestinationIpv4L3vpnMcast(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
86                     .l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
87                     .destination.l3vpn.mcast.ipv4.withdrawn._case.DestinationIpv4L3vpnMcastBuilder()
88                     .setL3vpnMcastDestination(Collections.singletonList(MCAST_L3VPN_DESTINATION)).build()).build();
89
90     private L3VpnMcastIpv4RIBSupport ribSupport;
91
92     @Override
93     public void setUp() throws Exception {
94         super.setUp();
95         ribSupport = L3VpnMcastIpv4RIBSupport.getInstance(this.adapter.currentSerializer());
96         setUpTestCustomizer(ribSupport);
97     }
98
99     @Test
100     public void testDeleteRoutes() {
101         final ContainerNode withdraw = createNlriWithDrawnRoute(UNREACH_NLRI);
102         this.ribSupport.deleteRoutes(this.tx, getTablePath(), withdraw);
103         final InstanceIdentifier<L3vpnMcastRoute> instanceIdentifier = this.deletedRoutes.get(0);
104         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(L3vpnMcastRoute.class));
105     }
106
107     @Test
108     public void testPutRoutes() {
109         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
110         final L3vpnMcastRoute route = (L3vpnMcastRoute) this.insertedRoutes.get(0).getValue();
111         assertEquals(ROUTE, route);
112     }
113
114     @Test
115     public void testEmptyRoute() {
116         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
117     }
118
119     @Test
120     public void testBuildMpUnreachNlriUpdate() {
121         final Collection<MapEntryNode> routes = createRoutes(MCAST_L3VPN_ROUTES);
122         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), routes, ATTRIBUTES);
123         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
124                 .getWithdrawnRoutes().getDestinationType());
125         assertNull(update.getAttributes().augmentation(Attributes1.class));
126     }
127
128     @Test
129     public void testBuildMpReachNlriUpdate() {
130         final Collection<MapEntryNode> routes = createRoutes(MCAST_L3VPN_ROUTES);
131         final Update update = this.ribSupport.buildUpdate(routes, Collections.emptyList(), ATTRIBUTES);
132         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
133                 .getAdvertizedRoutes().getDestinationType());
134         assertNull(update.getAttributes().augmentation(Attributes2.class));
135     }
136
137     @Test
138     public void testCacheableNlriObjects() {
139         assertEquals(ImmutableSet.of(L3vpnMcastRoutesIpv4Case.class), this.ribSupport.cacheableNlriObjects());
140     }
141
142     @Test
143     public void testCacheableAttributeObjects() {
144         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
145     }
146
147     @Test
148     public void testRouteIdAddPath() {
149         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey()));
150     }
151
152     @Test
153     public void testRoutePath() {
154         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(MCAST_L3VPN_ROUTES);
155         final YangInstanceIdentifier expected = getRoutePath().node(prefixNii);
156         final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath(), prefixNii);
157         assertEquals(expected, actual);
158     }
159
160     @Test
161     public void testRouteAttributesIdentifier() {
162         assertEquals(new NodeIdentifier(Attributes.QNAME.bindTo(
163             BindingReflections.getQNameModule(L3vpnMcastRoutesIpv4Case.class))),
164             this.ribSupport.routeAttributesIdentifier());
165     }
166
167     @Test
168     public void testRoutesCaseClass() {
169         assertEquals(L3vpnMcastRoutesIpv4Case.class, this.ribSupport.routesCaseClass());
170     }
171
172     @Test
173     public void testRoutesContainerClass() {
174         assertEquals(L3vpnMcastRoutesIpv4.class, this.ribSupport.routesContainerClass());
175     }
176
177     @Test
178     public void testRoutesListClass() {
179         assertEquals(L3vpnMcastRoute.class, this.ribSupport.routesListClass());
180     }
181
182     @Test
183     public void testChangedRoutes() {
184         final Routes emptyCase = new L3vpnMcastRoutesIpv4CaseBuilder().build();
185         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
186                 createRoutes(emptyCase)).getRootNode();
187         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
188
189         final Routes emptyRoutes
190                 = new L3vpnMcastRoutesIpv4CaseBuilder()
191                 .setL3vpnMcastRoutesIpv4(new L3vpnMcastRoutesIpv4Builder().build()).build();
192         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
193         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
194
195         final Routes routes = new L3vpnMcastRoutesIpv4CaseBuilder().setL3vpnMcastRoutesIpv4(MCAST_L3VPN_ROUTES).build();
196         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
197         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
198         assertFalse(result.isEmpty());
199     }
200 }