2 * Copyright (c) 2018 AT&T Intellectual Property. 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.route.targetcontrain.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
23 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
24 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.route.target.constrain.choice.RouteTargetConstrainAs4ExtendedCommunityCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRoute;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationRouteTargetConstrainAdvertizedCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationRouteTargetConstrainAdvertizedCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrainBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationRouteTargetConstrainWithdrawnCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationRouteTargetConstrainWithdrawnCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommon;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommonBuilder;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.Uint16;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
61 public class RouteTargetConstrainRIBSupportTest extends AbstractRIBSupportTest<RouteTargetConstrainRoutesCase,
62 RouteTargetConstrainRoutes, RouteTargetConstrainRoute, RouteTargetConstrainRouteKey> {
63 private static final AsNumber ORIGIN_AS = new AsNumber(Uint32.valueOf(72));
64 private static final PathId PATH_ID = new PathId(Uint32.ZERO);
65 private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder()
66 .setAsNumber(new AsNumber(Uint32.valueOf(20)))
67 .setLocalAdministrator(Uint16.valueOf(100))
69 private static final RouteTargetConstrainChoice RT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder()
70 .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder()
71 .setAs4SpecificCommon(AS_COMMON).build())
73 private static final RouteTargetConstrainRouteKey ROUTE_KEY
74 = new RouteTargetConstrainRouteKey(PATH_ID, "AgIAAAAUAGQ=");
75 private static final RouteTargetConstrainRoute ROUTE = new RouteTargetConstrainRouteBuilder()
76 .setRouteKey(ROUTE_KEY.getRouteKey())
77 .setPathId(ROUTE_KEY.getPathId())
78 .setAttributes(ATTRIBUTES)
79 .setRouteTargetConstrainChoice(RT)
80 .setOriginAs(ORIGIN_AS)
82 private static final RouteTargetConstrainRoutes RT_ROUTES = new RouteTargetConstrainRoutesBuilder()
83 .setRouteTargetConstrainRoute(Map.of(ROUTE.key(), ROUTE))
86 private static final RouteTargetConstrainDestination RT_DESTINATION = new RouteTargetConstrainDestinationBuilder()
87 .setRouteTargetConstrainChoice(RT)
88 .setOriginAs(ORIGIN_AS)
91 private static final DestinationRouteTargetConstrainAdvertizedCase REACH_NLRI
92 = new DestinationRouteTargetConstrainAdvertizedCaseBuilder()
93 .setDestinationRouteTargetConstrain(new DestinationRouteTargetConstrainBuilder()
94 .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build();
95 private static final DestinationRouteTargetConstrainWithdrawnCase UNREACH_NLRI
96 = new DestinationRouteTargetConstrainWithdrawnCaseBuilder()
97 .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
98 .route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination
99 .type.destination.route.target.constrain.withdrawn._case.DestinationRouteTargetConstrainBuilder()
100 .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build();
102 private RouteTargetConstrainRIBSupport ribSupport;
105 public void setUp() throws Exception {
107 NlriActivator.registerNlriParsers(new ArrayList<>());
108 this.ribSupport = RouteTargetConstrainRIBSupport.getInstance(this.adapter.currentSerializer());
109 setUpTestCustomizer(this.ribSupport);
113 public void testDeleteRoutes() {
114 final ContainerNode withdraw = createNlriWithDrawnRoute(UNREACH_NLRI);
115 this.ribSupport.deleteRoutes(this.tx, getTablePath(), withdraw);
116 final InstanceIdentifier<RouteTargetConstrainRoute> instanceIdentifier = this.deletedRoutes.get(0);
117 assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(RouteTargetConstrainRoute.class));
121 public void testPutRoutes() {
122 this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
123 final RouteTargetConstrainRoute route = (RouteTargetConstrainRoute) this.insertedRoutes.get(0).getValue();
124 assertEquals(ROUTE, route);
129 public void testEmptyRoute() {
130 assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
134 public void testBuildMpUnreachNlriUpdate() {
135 final Collection<MapEntryNode> routes = createRoutes(RT_ROUTES);
136 final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), routes, ATTRIBUTES);
137 assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
138 .getWithdrawnRoutes().getDestinationType());
139 assertNull(update.getAttributes().augmentation(Attributes1.class));
143 public void testBuildMpReachNlriUpdate() {
144 final Collection<MapEntryNode> routes = createRoutes(RT_ROUTES);
145 final Update update = this.ribSupport.buildUpdate(routes, Collections.emptyList(), ATTRIBUTES);
146 assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
147 .getAdvertizedRoutes().getDestinationType());
148 assertNull(update.getAttributes().augmentation(Attributes2.class));
152 public void testCacheableNlriObjects() {
153 assertEquals(ImmutableSet.of(RouteTargetConstrainRoutesCase.class), this.ribSupport.cacheableNlriObjects());
157 public void testCacheableAttributeObjects() {
158 assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
162 public void testRouteIdAddPath() {
163 assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(), ROUTE_KEY.getRouteKey()));
167 public void testRoutePath() {
168 final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(RT_ROUTES);
169 final YangInstanceIdentifier expected = getRoutePath().node(prefixNii);
170 final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath(), prefixNii);
171 assertEquals(expected, actual);
175 public void testRouteAttributesIdentifier() {
176 assertEquals(new NodeIdentifier(Attributes.QNAME.bindTo(BindingReflections
177 .getQNameModule(RouteTargetConstrainRoutesCase.class))),
178 this.ribSupport.routeAttributesIdentifier());
182 public void testRoutesCaseClass() {
183 assertEquals(RouteTargetConstrainRoutesCase.class, this.ribSupport.routesCaseClass());
187 public void testRoutesContainerClass() {
188 assertEquals(RouteTargetConstrainRoutes.class, this.ribSupport.routesContainerClass());
192 public void testRoutesListClass() {
193 assertEquals(RouteTargetConstrainRoute.class, this.ribSupport.routesListClass());
197 public void testChangedRoutes() {
198 final Routes emptyCase = new RouteTargetConstrainRoutesCaseBuilder().build();
199 DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
200 createRoutes(emptyCase)).getRootNode();
201 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
203 final Routes emptyRoutes = new RouteTargetConstrainRoutesCaseBuilder()
204 .setRouteTargetConstrainRoutes(new RouteTargetConstrainRoutesBuilder().build()).build();
205 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
206 assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
208 final Routes routes = new RouteTargetConstrainRoutesCaseBuilder()
209 .setRouteTargetConstrainRoutes(RT_ROUTES).build();
210 tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
211 final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
212 assertFalse(result.isEmpty());