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