2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.inet;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Lists;
17 import java.util.Collection;
18 import java.util.Collections;
19 import org.junit.Assert;
20 import org.junit.Test;
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.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6Routes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6RoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteKey;
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.DestinationIpv6Case;
34 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;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
45 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
49 public final class IPv6RIBSupportTest extends AbstractRIBSupportTest {
50 private static final IPv6RIBSupport RIB_SUPPORT = IPv6RIBSupport.getInstance();
51 private static final PathId PATH_ID = new PathId(1L);
52 private static final Ipv6Prefix PREFIX = new Ipv6Prefix("2001:db8:1:2::/64");
53 private static final Ipv6RouteKey ROUTE_KEY = new Ipv6RouteKey(PATH_ID, PREFIX.getValue());
54 private static final Ipv6Prefixes IPV6_PREFIXES = new Ipv6PrefixesBuilder().setPathId(PATH_ID)
55 .setPrefix(PREFIX).build();
56 private static final DestinationIpv6Case REACH_NLRI = new DestinationIpv6CaseBuilder()
57 .setDestinationIpv6(new DestinationIpv6Builder()
58 .setIpv6Prefixes(Lists.newArrayList(IPV6_PREFIXES)).build()).build();
60 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
61 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6Case UNREACH_NLRI =
62 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
63 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder()
64 .setDestinationIpv6(new DestinationIpv6Builder()
65 .setIpv6Prefixes(Collections.singletonList(IPV6_PREFIXES)).build()).build();
67 private static final Ipv6Route ROUTE = new Ipv6RouteBuilder()
68 .setRouteKey(PREFIX.getValue())
69 .setAttributes(ATTRIBUTES)
71 .setPrefix(PREFIX).build();
72 private static final Ipv6Routes ROUTES = new Ipv6RoutesBuilder()
73 .setIpv6Route(Collections.singletonList(ROUTE)).build();
74 private static final Ipv6Routes EMPTY_ROUTES = new Ipv6RoutesBuilder()
75 .setIpv6Route(Collections.emptyList()).build();
78 public void setUp() throws Exception {
80 setUpTestCustomizer(RIB_SUPPORT);
84 public void testDeleteRoutes() {
85 RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
86 @SuppressWarnings("unchecked") final InstanceIdentifier<Ipv6Route> instanceIdentifier =
87 (InstanceIdentifier<Ipv6Route>) this.deletedRoutes.get(0);
88 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv6Route.class));
92 public void testPutRoutes() {
93 RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
94 final Ipv6Route route = (Ipv6Route) this.insertedRoutes.get(0).getValue();
95 assertEquals(ROUTE, route);
100 public void testEmptyRoute() {
101 final Routes empty = new Ipv6RoutesCaseBuilder().setIpv6Routes(EMPTY_ROUTES).build();
102 final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
103 assertEquals(createRoutes(empty), emptyRoutes);
107 public void testBuildMpUnreachNlriUpdate() {
108 final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
109 assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
110 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
111 assertNull(update.getAttributes().getAugmentation(Attributes1.class));
115 public void testBuildMpReachNlriUpdate() {
116 final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
117 assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class)
118 .getMpReachNlri().getAdvertizedRoutes().getDestinationType());
119 assertNull(update.getAttributes().getAugmentation(Attributes2.class));
123 public void testIsComplexRoute() {
124 assertTrue(RIB_SUPPORT.isComplexRoute());
128 public void testCacheableNlriObjects() {
129 assertEquals(ImmutableSet.of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet
130 .rev180329.Ipv6Prefix.class), RIB_SUPPORT.cacheableNlriObjects());
134 public void testCacheableAttributeObjects() {
135 assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
139 public void testRouteIdAddPath() {
140 final Ipv6RouteKey expected = new Ipv6RouteKey(new PathId(1L), PREFIX.getValue());
141 assertEquals(expected, RIB_SUPPORT.createRouteListKey(expected.getPathId().getValue(), PREFIX.getValue()));
145 public void testRoutePath() {
146 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
147 assertEquals(getRoutePath().node(prefixNii),
148 RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
152 public void testRouteAttributesIdentifier() {
153 assertEquals(new NodeIdentifier(QName.create(Ipv6Routes.QNAME,
154 Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT.routeAttributesIdentifier());
158 public void testRoutesCaseClass() {
159 assertEquals(Ipv6RoutesCase.class, RIB_SUPPORT.routesCaseClass());
163 public void testRoutesContainerClass() {
164 assertEquals(Ipv6Routes.class, RIB_SUPPORT.routesContainerClass());
168 public void testRoutesListClass() {
169 assertEquals(Ipv6Route.class, RIB_SUPPORT.routesListClass());
173 public void testChangedRoutes() {
174 final Routes emptyCase = new Ipv6RoutesCaseBuilder().build();
175 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
176 createRoutes(emptyCase)).getRootNode();
177 Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
179 final Routes emptyRoutes = new Ipv6RoutesCaseBuilder().setIpv6Routes(new Ipv6RoutesBuilder().build()).build();
180 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
181 Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
183 final Routes routes = new Ipv6RoutesCaseBuilder().setIpv6Routes(ROUTES).build();
184 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
185 final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
186 Assert.assertFalse(result.isEmpty());