Route Constrain policies
[bgpcep.git] / bgp / extensions / route-target / src / test / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / RouteTargetConstrainRIBSupportTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.route.targetcontrain.impl;
10
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;
15
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
22 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
30 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;
31 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;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
35 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;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
38 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;
39 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;
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.RouteTargetConstrainRouteKey;
41 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;
42 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;
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.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrainBuilder;
44 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;
45 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;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
57
58 public class RouteTargetConstrainRIBSupportTest extends AbstractRIBSupportTest<RouteTargetConstrainRoutesCase,
59         RouteTargetConstrainRoutes, RouteTargetConstrainRoute, RouteTargetConstrainRouteKey> {
60     private static final AsNumber ORIGIN_AS = new AsNumber(72L);
61     private static final PathId PATH_ID = new PathId(0L);
62     private static final As4SpecificCommon AS_COMMON = new As4SpecificCommonBuilder()
63             .setAsNumber(new AsNumber(20L))
64             .setLocalAdministrator(100)
65             .build();
66     private static final RouteTargetConstrainChoice RT = new RouteTargetConstrainAs4ExtendedCommunityCaseBuilder()
67             .setAs4RouteTargetExtendedCommunity(new As4RouteTargetExtendedCommunityBuilder()
68                     .setAs4SpecificCommon(AS_COMMON).build())
69             .build();
70     private static final RouteTargetConstrainRouteKey ROUTE_KEY
71             = new RouteTargetConstrainRouteKey(PATH_ID, "AgIAAAAUAGQ=");
72     private static final RouteTargetConstrainRoute ROUTE = new RouteTargetConstrainRouteBuilder()
73             .setRouteKey(ROUTE_KEY.getRouteKey())
74             .setPathId(ROUTE_KEY.getPathId())
75             .setAttributes(ATTRIBUTES)
76             .setRouteTargetConstrainChoice(RT)
77             .setOriginAs(ORIGIN_AS)
78             .build();
79     private static final RouteTargetConstrainRoutes RT_ROUTES
80             = new RouteTargetConstrainRoutesBuilder()
81             .setRouteTargetConstrainRoute(Collections.singletonList(ROUTE)).build();
82
83     private static final RouteTargetConstrainDestination RT_DESTINATION = new RouteTargetConstrainDestinationBuilder()
84             .setRouteTargetConstrainChoice(RT)
85             .setOriginAs(ORIGIN_AS)
86             .setPathId(PATH_ID)
87             .build();
88     private static final DestinationRouteTargetConstrainAdvertizedCase REACH_NLRI
89             = new DestinationRouteTargetConstrainAdvertizedCaseBuilder()
90             .setDestinationRouteTargetConstrain(new DestinationRouteTargetConstrainBuilder()
91                     .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build();
92     private static final DestinationRouteTargetConstrainWithdrawnCase UNREACH_NLRI
93             = new DestinationRouteTargetConstrainWithdrawnCaseBuilder()
94             .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
95                     .route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination
96                     .type.destination.route.target.constrain.withdrawn._case.DestinationRouteTargetConstrainBuilder()
97                     .setRouteTargetConstrainDestination(Collections.singletonList(RT_DESTINATION)).build()).build();
98
99     private RouteTargetConstrainRIBSupport ribSupport;
100
101     @Override
102     public void setUp() throws Exception {
103         super.setUp();
104         NlriActivator.registerNlriParsers(new ArrayList<>());
105         this.ribSupport = RouteTargetConstrainRIBSupport.getInstance(this.mappingService);
106         setUpTestCustomizer(this.ribSupport);
107     }
108
109     @Test
110     public void testDeleteRoutes() {
111         final ContainerNode withdraw = createNlriWithDrawnRoute(UNREACH_NLRI);
112         this.ribSupport.deleteRoutes(this.tx, getTablePath(), withdraw);
113         final InstanceIdentifier<RouteTargetConstrainRoute> instanceIdentifier = this.deletedRoutes.get(0);
114         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(RouteTargetConstrainRoute.class));
115     }
116
117     @Test
118     public void testPutRoutes() {
119         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
120         final RouteTargetConstrainRoute route = (RouteTargetConstrainRoute) this.insertedRoutes.get(0).getValue();
121         assertEquals(ROUTE, route);
122     }
123
124
125     @Test
126     public void testEmptyRoute() {
127         final Routes empty = new RouteTargetConstrainRoutesCaseBuilder()
128                 .setRouteTargetConstrainRoutes(new RouteTargetConstrainRoutesBuilder()
129                         .setRouteTargetConstrainRoute(Collections.emptyList()).build()).build();
130         assertEquals(createEmptyTable(empty), this.ribSupport.emptyTable());
131     }
132
133     @Test
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));
140     }
141
142     @Test
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));
149     }
150
151     @Test
152     public void testCacheableNlriObjects() {
153         assertEquals(ImmutableSet.of(RouteTargetConstrainRoutesCase.class), this.ribSupport.cacheableNlriObjects());
154     }
155
156     @Test
157     public void testCacheableAttributeObjects() {
158         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
159     }
160
161     @Test
162     public void testRouteIdAddPath() {
163         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
164                 ROUTE_KEY.getRouteKey()));
165     }
166
167     @Test
168     public void testRoutePath() {
169         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(RT_ROUTES);
170         final YangInstanceIdentifier expected = getRoutePath().node(prefixNii);
171         final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii);
172         assertEquals(expected, actual);
173     }
174
175     @Test
176     public void testRouteAttributesIdentifier() {
177         assertEquals(new NodeIdentifier(Attributes.QNAME.withModule(BindingReflections
178                         .getQNameModule(RouteTargetConstrainRoutesCase.class))),
179                 this.ribSupport.routeAttributesIdentifier());
180     }
181
182     @Test
183     public void testRoutesCaseClass() {
184         assertEquals(RouteTargetConstrainRoutesCase.class, this.ribSupport.routesCaseClass());
185     }
186
187     @Test
188     public void testRoutesContainerClass() {
189         assertEquals(RouteTargetConstrainRoutes.class, this.ribSupport.routesContainerClass());
190     }
191
192     @Test
193     public void testRoutesListClass() {
194         assertEquals(RouteTargetConstrainRoute.class, this.ribSupport.routesListClass());
195     }
196
197     @Test
198     public void testChangedRoutes() {
199         final Routes emptyCase = new RouteTargetConstrainRoutesCaseBuilder().build();
200         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
201                 createRoutes(emptyCase)).getRootNode();
202         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
203
204         final Routes emptyRoutes = new RouteTargetConstrainRoutesCaseBuilder()
205                 .setRouteTargetConstrainRoutes(new RouteTargetConstrainRoutesBuilder().build()).build();
206         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
207         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
208
209         final Routes routes = new RouteTargetConstrainRoutesCaseBuilder()
210                 .setRouteTargetConstrainRoutes(RT_ROUTES).build();
211         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
212         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
213         assertFalse(result.isEmpty());
214     }
215 }