Do not create empty route list container
[bgpcep.git] / bgp / extensions / inet / src / test / java / org / opendaylight / protocol / bgp / inet / IPv6RIBSupportTest.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.inet;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Lists;
16 import java.util.Collection;
17 import java.util.Collections;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6Routes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6RoutesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6Case;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
46
47 public final class IPv6RIBSupportTest extends AbstractRIBSupportTest<Ipv6RoutesCase, Ipv6Routes, Ipv6Route,
48         Ipv6RouteKey> {
49     private IPv6RIBSupport ribSupport;
50     private static final PathId PATH_ID = new PathId(1L);
51     private static final Ipv6Prefix PREFIX = new Ipv6Prefix("2001:db8:1:2::/64");
52     private static final Ipv6RouteKey ROUTE_KEY = new Ipv6RouteKey(PATH_ID, PREFIX.getValue());
53     private static final Ipv6Prefixes IPV6_PREFIXES = new Ipv6PrefixesBuilder().setPathId(PATH_ID)
54             .setPrefix(PREFIX).build();
55     private static final DestinationIpv6Case REACH_NLRI = new DestinationIpv6CaseBuilder()
56             .setDestinationIpv6(new DestinationIpv6Builder()
57                     .setIpv6Prefixes(Lists.newArrayList(IPV6_PREFIXES)).build()).build();
58
59     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
60             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6Case UNREACH_NLRI =
61             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
62                     .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder()
63                     .setDestinationIpv6(new DestinationIpv6Builder()
64                             .setIpv6Prefixes(Collections.singletonList(IPV6_PREFIXES)).build()).build();
65
66     private static final Ipv6Route ROUTE = new Ipv6RouteBuilder()
67             .setRouteKey(PREFIX.getValue())
68             .setAttributes(ATTRIBUTES)
69             .setPathId(PATH_ID)
70             .setPrefix(PREFIX).build();
71     private static final Ipv6Routes ROUTES = new Ipv6RoutesBuilder()
72             .setIpv6Route(Collections.singletonList(ROUTE)).build();
73     private static final Ipv6Routes EMPTY_ROUTES = new Ipv6RoutesBuilder().build();
74
75     @Override
76     public void setUp() throws Exception {
77         super.setUp();
78         this.ribSupport = IPv6RIBSupport.getInstance(this.mappingService);
79         setUpTestCustomizer(this.ribSupport);
80     }
81
82     @Test
83     public void testDeleteRoutes() {
84         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
85         final InstanceIdentifier<Ipv6Route> instanceIdentifier =
86                 this.deletedRoutes.get(0);
87         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv6Route.class));
88     }
89
90     @Test
91     public void testPutRoutes() {
92         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
93         final Ipv6Route route = (Ipv6Route) this.insertedRoutes.get(0).getValue();
94         assertEquals(ROUTE, route);
95     }
96
97
98     @Test
99     public void testEmptyRoute() {
100         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
101     }
102
103     @Test
104     public void testBuildMpUnreachNlriUpdate() {
105         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
106         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
107                 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
108         assertNull(update.getAttributes().augmentation(Attributes1.class));
109     }
110
111     @Test
112     public void testBuildMpReachNlriUpdate() {
113         final Update update = this.ribSupport.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
114         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class)
115                 .getMpReachNlri().getAdvertizedRoutes().getDestinationType());
116         assertNull(update.getAttributes().augmentation(Attributes2.class));
117     }
118
119     @Test
120     public void testCacheableNlriObjects() {
121         assertEquals(ImmutableSet.of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet
122                 .rev180329.Ipv6Prefix.class), this.ribSupport.cacheableNlriObjects());
123     }
124
125     @Test
126     public void testCacheableAttributeObjects() {
127         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
128     }
129
130     @Test
131     public void testRouteIdAddPath() {
132         final Ipv6RouteKey expected = new Ipv6RouteKey(new PathId(1L), PREFIX.getValue());
133         assertEquals(expected, this.ribSupport.createRouteListKey(expected.getPathId().getValue(), PREFIX.getValue()));
134     }
135
136     @Test
137     public void testRoutePath() {
138         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
139         assertEquals(getRoutePath().node(prefixNii),
140                 this.ribSupport.routePath(getTablePath(), prefixNii));
141     }
142
143     @Test
144     public void testRouteAttributesIdentifier() {
145         assertEquals(new NodeIdentifier(QName.create(Ipv6Routes.QNAME,
146                 Attributes.QNAME.getLocalName().intern())), this.ribSupport.routeAttributesIdentifier());
147     }
148
149     @Test
150     public void testRoutesCaseClass() {
151         assertEquals(Ipv6RoutesCase.class, this.ribSupport.routesCaseClass());
152     }
153
154     @Test
155     public void testRoutesContainerClass() {
156         assertEquals(Ipv6Routes.class, this.ribSupport.routesContainerClass());
157     }
158
159     @Test
160     public void testRoutesListClass() {
161         assertEquals(Ipv6Route.class, this.ribSupport.routesListClass());
162     }
163
164     @Test
165     public void testChangedRoutes() {
166         final Routes emptyCase = new Ipv6RoutesCaseBuilder().build();
167         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
168                 createRoutes(emptyCase)).getRootNode();
169         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
170
171         final Routes emptyRoutes = new Ipv6RoutesCaseBuilder().setIpv6Routes(new Ipv6RoutesBuilder().build()).build();
172         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
173         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
174
175         final Routes routes = new Ipv6RoutesCaseBuilder().setIpv6Routes(ROUTES).build();
176         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
177         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
178         Assert.assertFalse(result.isEmpty());
179     }
180 }