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;
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.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
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.DestinationIpv4Case;
35 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;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.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.common.Uint32;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
50 public final class IPv4RIBSupportTest extends AbstractRIBSupportTest<Ipv4RoutesCase, Ipv4Routes, Ipv4Route,
52 private IPv4RIBSupport ribSupport;
53 private static final PathId PATH_ID = new PathId(Uint32.ONE);
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.getValue());
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()
59 .setDestinationIpv4(new DestinationIpv4Builder()
60 .setIpv4Prefixes(Lists.newArrayList(IPV4_PREFIXES)).build()).build();
62 private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
63 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case UNREACH_NLRI =
64 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
65 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
66 .setDestinationIpv4(new DestinationIpv4Builder()
67 .setIpv4Prefixes(Collections.singletonList(IPV4_PREFIXES)).build()).build();
69 private static final Ipv4Route ROUTE = new Ipv4RouteBuilder()
70 .setRouteKey(PREFIX.getValue())
71 .setAttributes(ATTRIBUTES)
73 .setPrefix(PREFIX).build();
74 private static final Ipv4Routes ROUTES = new Ipv4RoutesBuilder()
75 .setIpv4Route(Map.of(ROUTE.key(), ROUTE))
79 public void setUp() throws Exception {
81 this.ribSupport = IPv4RIBSupport.getInstance(this.adapter.currentSerializer());
82 setUpTestCustomizer(this.ribSupport);
86 public void testDeleteRoutes() {
87 this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
88 final InstanceIdentifier<Ipv4Route> instanceIdentifier = this.deletedRoutes.get(0);
89 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv4Route.class));
93 public void testPutRoutes() {
94 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
95 final Ipv4Route route = (Ipv4Route) this.insertedRoutes.get(0).getValue();
96 assertEquals(ROUTE, route);
101 public void testEmptyRoute() {
102 assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
106 public void testBuildMpUnreachNlriUpdate() {
107 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
108 assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
109 .getWithdrawnRoutes().getDestinationType());
110 assertNull(update.getAttributes().augmentation(Attributes1.class));
114 public void testBuildMpReachNlriUpdate() {
115 final Update update = this.ribSupport.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
116 assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
117 .getAdvertizedRoutes().getDestinationType());
118 assertNull(update.getAttributes().augmentation(Attributes2.class));
122 public void testCacheableNlriObjects() {
123 assertEquals(ImmutableSet.of(
124 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Ipv4PrefixAndPathId
125 .class), this.ribSupport.cacheableNlriObjects());
129 public void testCacheableAttributeObjects() {
130 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
134 public void testRouteIdAddPath() {
135 final Ipv4RouteKey expected = new Ipv4RouteKey(new PathId(Uint32.ONE), PREFIX.getValue());
136 assertEquals(expected, this.ribSupport.createRouteListKey(expected.getPathId(), PREFIX.getValue()));
140 public void testRoutePath() {
141 final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
142 assertEquals(getRoutePath().node(prefixNii), this.ribSupport.routePath(getTablePath(), prefixNii));
146 public void testRouteAttributesIdentifier() {
147 assertEquals(new NodeIdentifier(QName.create(Ipv4Routes.QNAME, Attributes.QNAME.getLocalName().intern())),
148 this.ribSupport.routeAttributesIdentifier());
152 public void testRoutesCaseClass() {
153 assertEquals(Ipv4RoutesCase.class, this.ribSupport.routesCaseClass());
157 public void testRoutesContainerClass() {
158 assertEquals(Ipv4Routes.class, this.ribSupport.routesContainerClass());
162 public void testRoutesListClass() {
163 assertEquals(Ipv4Route.class, this.ribSupport.routesListClass());
167 public void testChangedRoutes() {
168 final Routes emptyCase = new Ipv4RoutesCaseBuilder().build();
169 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
170 createRoutes(emptyCase)).getRootNode();
171 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
173 final Routes emptyRoutes = new Ipv4RoutesCaseBuilder().setIpv4Routes(new Ipv4RoutesBuilder().build()).build();
174 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
175 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
177 final Routes routes = new Ipv4RoutesCaseBuilder().setIpv4Routes(ROUTES).build();
178 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
179 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
180 Assert.assertFalse(result.isEmpty());