Do not create empty route list container
[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.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
23 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.NlriActivator;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
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.RouteTargetConstrainRoutesCase;
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.RouteTargetConstrainRoutesCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
36 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;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
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.RouteTargetConstrainRoute;
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.RouteTargetConstrainRouteBuilder;
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.RouteTargetConstrainRouteKey;
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.DestinationRouteTargetConstrainAdvertizedCase;
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.DestinationRouteTargetConstrainAdvertizedCaseBuilder;
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.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrainBuilder;
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.DestinationRouteTargetConstrainWithdrawnCase;
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.DestinationRouteTargetConstrainWithdrawnCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.route.target.extended.community.grouping.As4RouteTargetExtendedCommunityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
128     }
129
130     @Test
131     public void testBuildMpUnreachNlriUpdate() {
132         final Collection<MapEntryNode> routes = createRoutes(RT_ROUTES);
133         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), routes, ATTRIBUTES);
134         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
135                 .getWithdrawnRoutes().getDestinationType());
136         assertNull(update.getAttributes().augmentation(Attributes1.class));
137     }
138
139     @Test
140     public void testBuildMpReachNlriUpdate() {
141         final Collection<MapEntryNode> routes = createRoutes(RT_ROUTES);
142         final Update update = this.ribSupport.buildUpdate(routes, Collections.emptyList(), ATTRIBUTES);
143         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
144                 .getAdvertizedRoutes().getDestinationType());
145         assertNull(update.getAttributes().augmentation(Attributes2.class));
146     }
147
148     @Test
149     public void testCacheableNlriObjects() {
150         assertEquals(ImmutableSet.of(RouteTargetConstrainRoutesCase.class), this.ribSupport.cacheableNlriObjects());
151     }
152
153     @Test
154     public void testCacheableAttributeObjects() {
155         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
156     }
157
158     @Test
159     public void testRouteIdAddPath() {
160         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId().getValue(),
161                 ROUTE_KEY.getRouteKey()));
162     }
163
164     @Test
165     public void testRoutePath() {
166         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(RT_ROUTES);
167         final YangInstanceIdentifier expected = getRoutePath().node(prefixNii);
168         final YangInstanceIdentifier actual = this.ribSupport.routePath(getTablePath(), prefixNii);
169         assertEquals(expected, actual);
170     }
171
172     @Test
173     public void testRouteAttributesIdentifier() {
174         assertEquals(new NodeIdentifier(Attributes.QNAME.withModule(BindingReflections
175                         .getQNameModule(RouteTargetConstrainRoutesCase.class))),
176                 this.ribSupport.routeAttributesIdentifier());
177     }
178
179     @Test
180     public void testRoutesCaseClass() {
181         assertEquals(RouteTargetConstrainRoutesCase.class, this.ribSupport.routesCaseClass());
182     }
183
184     @Test
185     public void testRoutesContainerClass() {
186         assertEquals(RouteTargetConstrainRoutes.class, this.ribSupport.routesContainerClass());
187     }
188
189     @Test
190     public void testRoutesListClass() {
191         assertEquals(RouteTargetConstrainRoute.class, this.ribSupport.routesListClass());
192     }
193
194     @Test
195     public void testChangedRoutes() {
196         final Routes emptyCase = new RouteTargetConstrainRoutesCaseBuilder().build();
197         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
198                 createRoutes(emptyCase)).getRootNode();
199         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
200
201         final Routes emptyRoutes = new RouteTargetConstrainRoutesCaseBuilder()
202                 .setRouteTargetConstrainRoutes(new RouteTargetConstrainRoutesBuilder().build()).build();
203         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
204         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
205
206         final Routes routes = new RouteTargetConstrainRoutesCaseBuilder()
207                 .setRouteTargetConstrainRoutes(RT_ROUTES).build();
208         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
209         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
210         assertFalse(result.isEmpty());
211     }
212 }