Do not create empty route list container
[bgpcep.git] / bgp / extensions / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / mcast / L3vpnMcastIpv6RIBSupportTest.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 org.junit.Assert;
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.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv6Case;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv6CaseBuilder;
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.ipv6.L3vpnMcastRoutesIpv6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.ipv6.L3vpnMcastRoutesIpv6Builder;
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.DestinationL3vpnMcastIpv6AdvertizedCase;
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.DestinationL3vpnMcastIpv6AdvertizedCaseBuilder;
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.ipv6.advertized._case.DestinationIpv6L3vpnMcastBuilder;
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.DestinationL3vpnMcastIpv6WithdrawnCase;
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.DestinationL3vpnMcastIpv6WithdrawnCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.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.rev180329.RdIpv4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
52
53 public class L3vpnMcastIpv6RIBSupportTest extends AbstractRIBSupportTest<L3vpnMcastRoutesIpv6Case, L3vpnMcastRoutesIpv6,
54         L3vpnMcastRoute, L3vpnMcastRouteKey> {
55     private static final PathId PATH_ID = new PathId(0L);
56     private static final IpPrefix IPV6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:db8:1:1::/64"));
57     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdIpv4("1.2.3.4:258"));
58     private static final L3vpnMcastRouteKey ROUTE_KEY
59             = new L3vpnMcastRouteKey(PATH_ID, "gAABAQIDBAECQCABDbgAAQABAAAAAAAAAA==");
60     private static final L3vpnMcastRoute ROUTE = new L3vpnMcastRouteBuilder()
61             .setRouteKey(ROUTE_KEY.getRouteKey())
62             .setPathId(ROUTE_KEY.getPathId())
63             .setAttributes(ATTRIBUTES)
64             .setRouteDistinguisher(RD)
65             .setPrefix(IPV6_PREFIX)
66             .build();
67     private static final L3vpnMcastRoutesIpv6 MCAST_L3VPN_ROUTES
68             = new L3vpnMcastRoutesIpv6Builder().setL3vpnMcastRoute(Collections.singletonList(ROUTE)).build();
69
70     private static final L3vpnMcastDestination MCAST_L3VPN_DESTINATION = new L3vpnMcastDestinationBuilder()
71             .setRouteDistinguisher(RD)
72             .setPrefix(IPV6_PREFIX)
73             .setPathId(PATH_ID)
74             .build();
75     private static final DestinationL3vpnMcastIpv6AdvertizedCase REACH_NLRI
76             = new DestinationL3vpnMcastIpv6AdvertizedCaseBuilder()
77             .setDestinationIpv6L3vpnMcast(new DestinationIpv6L3vpnMcastBuilder()
78                     .setL3vpnMcastDestination(Collections.singletonList(MCAST_L3VPN_DESTINATION)).build()).build();
79     private static final DestinationL3vpnMcastIpv6WithdrawnCase UNREACH_NLRI
80             = new DestinationL3vpnMcastIpv6WithdrawnCaseBuilder()
81             .setDestinationIpv6L3vpnMcast(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
82                     .l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
83                     .destination.l3vpn.mcast.ipv6.withdrawn._case.DestinationIpv6L3vpnMcastBuilder()
84                     .setL3vpnMcastDestination(Collections.singletonList(MCAST_L3VPN_DESTINATION)).build()).build();
85
86     private L3VpnMcastIpv6RIBSupport ribSupport;
87
88     @Override
89     public void setUp() throws Exception {
90         super.setUp();
91         ribSupport = L3VpnMcastIpv6RIBSupport.getInstance(this.mappingService);
92         setUpTestCustomizer(ribSupport);
93     }
94
95     @Test
96     public void testDeleteRoutes() {
97         final ContainerNode withdraw = createNlriWithDrawnRoute(UNREACH_NLRI);
98         this.ribSupport.deleteRoutes(this.tx, getTablePath(), withdraw);
99         final InstanceIdentifier<L3vpnMcastRoute> instanceIdentifier = this.deletedRoutes.get(0);
100         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(L3vpnMcastRoute.class));
101     }
102
103     @Test
104     public void testPutRoutes() {
105         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
106         final L3vpnMcastRoute route = (L3vpnMcastRoute) this.insertedRoutes.get(0).getValue();
107         assertEquals(ROUTE, route);
108     }
109
110
111     @Test
112     public void testEmptyRoute() {
113         Assert.assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
114     }
115
116     @Test
117     public void testBuildMpUnreachNlriUpdate() {
118         final Collection<MapEntryNode> routes = createRoutes(MCAST_L3VPN_ROUTES);
119         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), routes, ATTRIBUTES);
120         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
121                 .getWithdrawnRoutes().getDestinationType());
122         assertNull(update.getAttributes().augmentation(Attributes1.class));
123     }
124
125     @Test
126     public void testBuildMpReachNlriUpdate() {
127         final Collection<MapEntryNode> routes = createRoutes(MCAST_L3VPN_ROUTES);
128         final Update update = this.ribSupport.buildUpdate(routes, Collections.emptyList(), ATTRIBUTES);
129         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
130                 .getAdvertizedRoutes().getDestinationType());
131         assertNull(update.getAttributes().augmentation(Attributes2.class));
132     }
133
134     @Test
135     public void testCacheableNlriObjects() {
136         Assert.assertEquals(ImmutableSet.of(L3vpnMcastRoutesIpv6Case.class), this.ribSupport.cacheableNlriObjects());
137     }
138
139     @Test
140     public void testCacheableAttributeObjects() {
141         Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
142     }
143
144     @Test
145     public void testRouteIdAddPath() {
146         Assert.assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
147                 ROUTE_KEY.getRouteKey()));
148     }
149
150     @Test
151     public void testRoutePath() {
152         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(MCAST_L3VPN_ROUTES);
153         final YangInstanceIdentifier expected = getRoutePath().node(prefixNii);
154         final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath(), prefixNii);
155         assertEquals(expected, actual);
156     }
157
158     @Test
159     public void testRouteAttributesIdentifier() {
160         Assert.assertEquals(new YangInstanceIdentifier.NodeIdentifier(
161                         Attributes.QNAME.withModule(BindingReflections.getQNameModule(L3vpnMcastRoutesIpv6Case.class))),
162                 this.ribSupport.routeAttributesIdentifier());
163     }
164
165     @Test
166     public void testRoutesCaseClass() {
167         Assert.assertEquals(L3vpnMcastRoutesIpv6Case.class, this.ribSupport.routesCaseClass());
168     }
169
170     @Test
171     public void testRoutesContainerClass() {
172         Assert.assertEquals(L3vpnMcastRoutesIpv6.class, this.ribSupport.routesContainerClass());
173     }
174
175     @Test
176     public void testRoutesListClass() {
177         Assert.assertEquals(L3vpnMcastRoute.class, this.ribSupport.routesListClass());
178     }
179
180     @Test
181     public void testChangedRoutes() {
182         final Routes emptyCase = new L3vpnMcastRoutesIpv6CaseBuilder().build();
183         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
184                 createRoutes(emptyCase)).getRootNode();
185         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
186
187         final Routes emptyRoutes
188                 = new L3vpnMcastRoutesIpv6CaseBuilder()
189                 .setL3vpnMcastRoutesIpv6(new L3vpnMcastRoutesIpv6Builder().build()).build();
190         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
191         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
192
193         final Routes routes = new L3vpnMcastRoutesIpv6CaseBuilder().setL3vpnMcastRoutesIpv6(MCAST_L3VPN_ROUTES).build();
194         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
195         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
196         assertFalse(result.isEmpty());
197     }
198 }