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