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.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
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.DestinationIpv4Case;
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.DestinationIpv4CaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.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.common.Uint32;
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.tree.DataTreeCandidateNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
49 public final class IPv4RIBSupportTest extends AbstractRIBSupportTest<Ipv4RoutesCase, Ipv4Routes, Ipv4Route,
51 private IPv4RIBSupport ribSupport;
52 private static final PathId PATH_ID = new PathId(Uint32.ONE);
53 private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.2.3.4/32");
54 private static final Ipv4RouteKey ROUTE_KEY = new Ipv4RouteKey(PATH_ID, PREFIX.getValue());
55 private static final Ipv4Prefixes IPV4_PREFIXES = new Ipv4PrefixesBuilder().setPathId(PATH_ID)
56 .setPrefix(PREFIX).build();
57 private static final DestinationIpv4Case REACH_NLRI = new DestinationIpv4CaseBuilder()
58 .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.rev180329.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.rev180329.update
64 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
65 .setDestinationIpv4(new DestinationIpv4Builder()
66 .setIpv4Prefixes(Collections.singletonList(IPV4_PREFIXES)).build()).build();
68 private static final Ipv4Route ROUTE = new Ipv4RouteBuilder()
69 .setRouteKey(PREFIX.getValue())
70 .setAttributes(ATTRIBUTES)
72 .setPrefix(PREFIX).build();
73 private static final Ipv4Routes ROUTES = new Ipv4RoutesBuilder()
74 .setIpv4Route(Collections.singletonList(ROUTE)).build();
77 public void setUp() throws Exception {
79 this.ribSupport = IPv4RIBSupport.getInstance(this.mappingService);
80 setUpTestCustomizer(this.ribSupport);
84 public void testDeleteRoutes() {
85 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
86 final InstanceIdentifier<Ipv4Route> instanceIdentifier = this.deletedRoutes.get(0);
87 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv4Route.class));
91 public void testPutRoutes() {
92 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
93 final Ipv4Route route = (Ipv4Route) this.insertedRoutes.get(0).getValue();
94 assertEquals(ROUTE, route);
99 public void testEmptyRoute() {
100 assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
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).getMpUnreachNlri()
107 .getWithdrawnRoutes().getDestinationType());
108 assertNull(update.getAttributes().augmentation(Attributes1.class));
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).getMpReachNlri()
115 .getAdvertizedRoutes().getDestinationType());
116 assertNull(update.getAttributes().augmentation(Attributes2.class));
120 public void testCacheableNlriObjects() {
121 assertEquals(ImmutableSet.of(
122 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Ipv4PrefixAndPathId
123 .class), this.ribSupport.cacheableNlriObjects());
127 public void testCacheableAttributeObjects() {
128 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
132 public void testRouteIdAddPath() {
133 final Ipv4RouteKey expected = new Ipv4RouteKey(new PathId(Uint32.ONE), PREFIX.getValue());
134 assertEquals(expected, this.ribSupport.createRouteListKey(expected.getPathId(), PREFIX.getValue()));
138 public void testRoutePath() {
139 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
140 assertEquals(getRoutePath().node(prefixNii), this.ribSupport.routePath(getTablePath(), prefixNii));
144 public void testRouteAttributesIdentifier() {
145 assertEquals(new NodeIdentifier(QName.create(Ipv4Routes.QNAME, Attributes.QNAME.getLocalName().intern())),
146 this.ribSupport.routeAttributesIdentifier());
150 public void testRoutesCaseClass() {
151 assertEquals(Ipv4RoutesCase.class, this.ribSupport.routesCaseClass());
155 public void testRoutesContainerClass() {
156 assertEquals(Ipv4Routes.class, this.ribSupport.routesContainerClass());
160 public void testRoutesListClass() {
161 assertEquals(Ipv4Route.class, this.ribSupport.routesListClass());
165 public void testChangedRoutes() {
166 final Routes emptyCase = new Ipv4RoutesCaseBuilder().build();
167 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
168 createRoutes(emptyCase)).getRootNode();
169 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
171 final Routes emptyRoutes = new Ipv4RoutesCaseBuilder().setIpv4Routes(new Ipv4RoutesBuilder().build()).build();
172 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
173 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
175 final Routes routes = new Ipv4RoutesCaseBuilder().setIpv4Routes(ROUTES).build();
176 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
177 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
178 Assert.assertFalse(result.isEmpty());