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.rib.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
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.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4RoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4RouteKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4Case;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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;
51 public final class IPv4RIBSupportTest extends AbstractRIBSupportTest {
52 private static final IPv4RIBSupport RIB_SUPPORT = IPv4RIBSupport.getInstance();
53 private static final PathId PATH_ID = new PathId(1L);
54 private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.2.3.4/32");
55 private static final Ipv4RouteKey ROUTE_KEY = new Ipv4RouteKey(PATH_ID, PREFIX);
56 private static final Ipv4Prefixes IPV4_PREFIXES = new Ipv4PrefixesBuilder().setPathId(PATH_ID)
57 .setPrefix(PREFIX).build();
58 private static final DestinationIpv4Case REACH_NLRI = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
59 .setIpv4Prefixes(Lists.newArrayList(IPV4_PREFIXES)).build()).build();
61 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update
62 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case UNREACH_NLRI =
63 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update
64 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
65 new DestinationIpv4Builder().setIpv4Prefixes(Lists.newArrayList(IPV4_PREFIXES)).build()).build();
67 private static final Ipv4Route ROUTE = new Ipv4RouteBuilder().setAttributes(ATTRIBUTES).setPathId(PATH_ID).setPrefix(PREFIX).build();
68 private static final Ipv4Routes ROUTES = new Ipv4RoutesBuilder().setIpv4Route(Collections.singletonList(ROUTE)).build();
69 private static final Ipv4Routes EMPTY_ROUTES = new Ipv4RoutesBuilder().setIpv4Route(Collections.emptyList()).build();
72 public void setUp() throws Exception {
74 setUpTestCustomizer(RIB_SUPPORT);
78 public void testDeleteRoutes() {
79 RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
80 final InstanceIdentifier<Ipv4Route> instanceIdentifier = (InstanceIdentifier<Ipv4Route>) this.deletedRoutes.get(0);
81 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv4Route.class));
85 public void testPutRoutes() {
86 RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
87 final Ipv4Route route = (Ipv4Route) this.insertedRoutes.get(0).getValue();
88 assertEquals(ROUTE, route);
93 public void testEmptyRoute() throws Exception {
94 final Routes empty = new Ipv4RoutesCaseBuilder().setIpv4Routes(EMPTY_ROUTES).build();
95 final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
96 assertEquals(createRoutes(empty), emptyRoutes);
100 public void testBuildMpUnreachNlriUpdate() {
101 final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
102 assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
103 assertNull(update.getAttributes().getAugmentation(Attributes1.class));
107 public void testBuildMpReachNlriUpdate() {
108 final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
109 assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType());
110 assertNull(update.getAttributes().getAugmentation(Attributes2.class));
114 public void testIsComplexRoute() {
115 Assert.assertFalse(RIB_SUPPORT.isComplexRoute());
119 public void testCacheableNlriObjects() {
120 Assert.assertEquals(ImmutableSet.of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.Ipv4Prefix.class), RIB_SUPPORT.cacheableNlriObjects());
124 public void testCacheableAttributeObjects() {
125 Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
129 public void testRouteIdAddPath() {
130 final NodeIdentifierWithPredicates expected = createRouteNIWP(ROUTES);
131 final NodeIdentifierWithPredicates prefixNii = new NodeIdentifierWithPredicates(RIB_SUPPORT.routeQName(), ImmutableMap.of(RIB_SUPPORT.routeKeyQName(), PREFIX.getValue()));
132 Assert.assertEquals(expected, RIB_SUPPORT.getRouteIdAddPath(AbstractRIBSupportTest.PATH_ID, prefixNii));
136 public void testRoutePath() {
137 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
138 Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
143 public void testExtractPathId() {
144 final NormalizedNode<?, ?> route = Iterables.getOnlyElement(createRoutes(ROUTES));
145 Assert.assertEquals(PATH_ID.getValue(), RIB_SUPPORT.extractPathId(route));
149 public void testRouteAttributesIdentifier() {
150 Assert.assertEquals(new NodeIdentifier(QName.create(Ipv4Routes.QNAME, Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT
151 .routeAttributesIdentifier());
155 public void testRoutesCaseClass() {
156 Assert.assertEquals(Ipv4RoutesCase.class, RIB_SUPPORT.routesCaseClass());
160 public void testRoutesContainerClass() {
161 Assert.assertEquals(Ipv4Routes.class, RIB_SUPPORT.routesContainerClass());
165 public void testRoutesListClass() {
166 Assert.assertEquals(Ipv4Route.class, RIB_SUPPORT.routesListClass());
170 public void testChangedRoutes() {
171 final Routes emptyCase = new Ipv4RoutesCaseBuilder().build();
172 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode();
173 Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
175 final Routes emptyRoutes = new Ipv4RoutesCaseBuilder().setIpv4Routes(new Ipv4RoutesBuilder().build()).build();
176 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
177 Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
179 final Routes routes = new Ipv4RoutesCaseBuilder().setIpv4Routes(ROUTES).build();
180 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
181 final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
182 Assert.assertFalse(result.isEmpty());