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