Code clean up
[bgpcep.git] / bgp / labeled-unicast / src / test / java / org / opendaylight / protocol / bgp / labeled / unicast / LabeledUnicastIpv4RIBSupportTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.labeled.unicast;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
24 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
25 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.bgp.rib.rib.loc.rib.tables.routes.LabeledUnicastRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.bgp.rib.rib.loc.rib.tables.routes.LabeledUnicastRoutesCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.LabeledUnicastRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.LabeledUnicastRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
54 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
56
57 public class LabeledUnicastIpv4RIBSupportTest extends AbstractRIBSupportTest<LabeledUnicastRoutesCase,
58         LabeledUnicastRoutes, LabeledUnicastRoute, LabeledUnicastRouteKey> {
59
60     private static final IpPrefix IPv4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
61     private static final LabeledUnicastRoute ROUTE;
62     private static final LabeledUnicastRoutes ROUTES;
63     private static final LabeledUnicastRouteKey ROUTE_KEY;
64     private static final String LABEL_KEY;
65     private static final PathId PATH_ID = new PathId(1L);
66     private static final List<LabelStack> LABEL_STACK = Lists.newArrayList(new LabelStackBuilder()
67             .setLabelValue(new MplsLabel(355L)).build());
68     private static final List<CLabeledUnicastDestination> LABELED_DESTINATION_LIST
69             = Collections.singletonList(new CLabeledUnicastDestinationBuilder()
70             .setPathId(PATH_ID).setLabelStack(LABEL_STACK).setPrefix(IPv4_PREFIX).build());
71     private static final DestinationLabeledUnicastCase REACH_NLRI = new DestinationLabeledUnicastCaseBuilder()
72             .setDestinationLabeledUnicast(new DestinationLabeledUnicastBuilder()
73                     .setCLabeledUnicastDestination(LABELED_DESTINATION_LIST).build()).build();
74     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
75             .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
76             .DestinationLabeledUnicastCase UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml
77             .ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
78             .DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(new org.opendaylight.yang.gen.v1.urn
79             .opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.unreach.nlri.withdrawn
80             .routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder()
81             .setCLabeledUnicastDestination(LABELED_DESTINATION_LIST).build()).build();
82
83     static {
84         final BGPActivator act = new BGPActivator();
85         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
86         act.start(context);
87         final ByteBuf buffer = Unpooled.buffer();
88         LUNlriParser.serializeNlri(LABELED_DESTINATION_LIST, false, buffer);
89         LABEL_KEY = ByteArray.encodeBase64(buffer);
90         ROUTE_KEY = new LabeledUnicastRouteKey(PATH_ID, LABEL_KEY);
91         ROUTE = new LabeledUnicastRouteBuilder()
92                 .withKey(ROUTE_KEY)
93                 .setPrefix(IPv4_PREFIX)
94                 .setPathId(PATH_ID)
95                 .setLabelStack(LABEL_STACK)
96                 .setAttributes(new AttributesBuilder().build()).build();
97         ROUTES = new LabeledUnicastRoutesBuilder().setLabeledUnicastRoute(Collections.singletonList(ROUTE)).build();
98     }
99
100     private LabeledUnicastIpv4RIBSupport ribSupport;
101
102     @Override
103     public void setUp() throws Exception {
104         super.setUp();
105         this.ribSupport = LabeledUnicastIpv4RIBSupport.getInstance(this.mappingService);
106         setUpTestCustomizer(this.ribSupport);
107     }
108
109     @Test
110     public void testDeleteRoutes() {
111         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
112         final InstanceIdentifier<LabeledUnicastRoute> instanceIdentifier = this.deletedRoutes.get(0);
113         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(LabeledUnicastRoute.class));
114     }
115
116     @Test
117     public void testPutRoutes() {
118         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
119         final LabeledUnicastRoute route = (LabeledUnicastRoute) this.insertedRoutes.get(0).getValue();
120         assertEquals(ROUTE, route);
121     }
122
123     @Test
124     public void testEmptyRoute() {
125         final Routes empty = new LabeledUnicastRoutesCaseBuilder().setLabeledUnicastRoutes(
126             new LabeledUnicastRoutesBuilder().setLabeledUnicastRoute(Collections.emptyList()).build()).build();
127         assertEquals(createEmptyTable(empty), this.ribSupport.emptyTable());
128     }
129
130     @Test
131     public void testBuildMpUnreachNlriUpdate() {
132         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
133         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
134             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
135         assertNull(update.getAttributes().augmentation(Attributes1.class));
136     }
137
138     @Test
139     public void testBuildMpReachNlriUpdate() {
140         final Update update = this.ribSupport.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
141         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
142                 .getAdvertizedRoutes().getDestinationType());
143         assertNull(update.getAttributes().augmentation(Attributes2.class));
144     }
145
146     @Test
147     public void testCacheableNlriObjects() {
148         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
149     }
150
151     @Test
152     public void testCacheableAttributeObjects() {
153         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
154     }
155
156     @Test
157     public void testRouteIdAddPath() {
158         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(1L, ROUTE_KEY.getRouteKey()));
159     }
160
161     @Test
162     public void testRoutePath() {
163         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
164         assertEquals(getRoutePath().node(prefixNii),
165                 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
166     }
167
168     @Test
169     public void testRouteAttributesIdentifier() {
170         assertEquals(new NodeIdentifier(QName.create(LabeledUnicastRoutes.QNAME,
171             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
172                     .Attributes.QNAME.getLocalName().intern())),
173             this.ribSupport.routeAttributesIdentifier());
174     }
175
176     @Test
177     public void testRoutesCaseClass() {
178         assertEquals(LabeledUnicastRoutesCase.class, this.ribSupport.routesCaseClass());
179     }
180
181     @Test
182     public void testRoutesContainerClass() {
183         assertEquals(LabeledUnicastRoutes.class, this.ribSupport.routesContainerClass());
184     }
185
186     @Test
187     public void testRoutesListClass() {
188         assertEquals(LabeledUnicastRoute.class, this.ribSupport.routesListClass());
189     }
190
191     @Test
192     public void testChangedRoutes() {
193         final Routes emptyCase = new LabeledUnicastRoutesCaseBuilder().build();
194         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
195                 createRoutes(emptyCase)).getRootNode();
196         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
197
198         final Routes emptyRoutes = new LabeledUnicastRoutesCaseBuilder()
199                 .setLabeledUnicastRoutes(new LabeledUnicastRoutesBuilder().build()).build();
200         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
201         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
202
203         final Routes routes = new LabeledUnicastRoutesCaseBuilder().setLabeledUnicastRoutes(ROUTES).build();
204         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
205         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
206         Assert.assertFalse(result.isEmpty());
207     }
208 }