Upgrade ietf-{inet,yang}-types to 2013-07-15
[bgpcep.git] / bgp / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / LinkstateRIBSupportTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.bgp.linkstate;
9
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 io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.math.BigInteger;
19 import java.util.Collection;
20 import java.util.Collections;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.protocol.bgp.linkstate.impl.BGPActivator;
24 import org.opendaylight.protocol.bgp.linkstate.impl.LinkstateRIBSupport;
25 import org.opendaylight.protocol.bgp.linkstate.impl.nlri.LinkstateNlriParser;
26 import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry;
27 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
28 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
29 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AreaIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.DomainIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.RouteDistinguisher;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.isis.lan.identifier.IsIsRouterIdentifierBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.node._case.NodeDescriptorsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.common.QName;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
67 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
69 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
70
71 public final class LinkstateRIBSupportTest extends AbstractRIBSupportTest {
72
73     private static final LinkstateRIBSupport RIB_SUPPORT = LinkstateRIBSupport.getInstance();
74     private static final LinkstateRoute ROUTE;
75     private static final LinkstateRoutes ROUTES;
76     private static final LinkstateRouteKey ROUTE_KEY;
77
78     private static final NodeCase OBJECT_TYPE2 = new NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder()
79         .setAreaId(new AreaIdentifier(2697513L)).setAsNumber(new AsNumber(72L)).setCRouterIdentifier(new IsisPseudonodeCaseBuilder()
80             .setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(new IsIsRouterIdentifierBuilder()
81                 .setIsoSystemId(new IsoSystemIdentifier(new byte[]{0, 0, 0, 0, 0, (byte) 0x39})).build()).setPsn((short) 5).build()).build())
82         .setDomainId(new DomainIdentifier(28282828L)).build()).build();
83
84     private static final RouteDistinguisher RD = new RouteDistinguisher(BigInteger.ONE);
85     private static final Identifier ID = new Identifier(BigInteger.ONE);
86     private static final CLinkstateDestination LINKSTATE_DESTINATION = new CLinkstateDestinationBuilder()
87         .setDistinguisher(RD)
88         .setIdentifier(ID)
89         .setObjectType(OBJECT_TYPE2)
90         .setProtocolId(ProtocolId.IsisLevel1).build();
91
92     private static final DestinationLinkstateCase REACH_NLRI = new DestinationLinkstateCaseBuilder().setDestinationLinkstate(new DestinationLinkstateBuilder()
93         .setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
94     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase UNREACH_NLRI =
95         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn
96             .routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(new org.opendaylight.yang.gen.v1.urn.opendaylight
97             .params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate
98             ._case.DestinationLinkstateBuilder().setCLinkstateDestination(Collections.singletonList(LINKSTATE_DESTINATION)).build()).build();
99
100     static {
101         final BGPActivator act = new BGPActivator();
102         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
103         act.start(context);
104         assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388));
105         assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71));
106         final ByteBuf buffer = Unpooled.buffer();
107         SimpleNlriTypeRegistry.getInstance().serializeNlriType(LINKSTATE_DESTINATION, buffer);
108         final byte[] arrayKey = ByteArray.readAllBytes(buffer);
109         ROUTE_KEY = new LinkstateRouteKey(arrayKey);
110         ROUTE = new LinkstateRouteBuilder().setKey(ROUTE_KEY).setDistinguisher(RD).setIdentifier(ID).setObjectType(OBJECT_TYPE2)
111             .setProtocolId(ProtocolId.IsisLevel1).setAttributes(new AttributesBuilder().build()).build();
112         ROUTES = new LinkstateRoutesBuilder().setLinkstateRoute(Collections.singletonList(ROUTE)).build();
113     }
114
115     @Override
116     public void setUp() throws Exception {
117         super.setUp();
118         setUpTestCustomizer(RIB_SUPPORT);
119     }
120
121     @Test
122     public void testDeleteRoutes() {
123         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
124         final InstanceIdentifier<LinkstateRoute> instanceIdentifier = (InstanceIdentifier<LinkstateRoute>) this.deletedRoutes.get(0);
125         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(LinkstateRoute.class));
126     }
127
128     @Test
129     public void testPutRoutes() {
130         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
131         final LinkstateRoute route = (LinkstateRoute) this.insertedRoutes.get(0).getValue();
132         assertEquals(ROUTE, route);
133     }
134
135     @Test
136     public void testEmptyRoute() throws Exception {
137         final Routes empty = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(
138             new LinkstateRoutesBuilder().setLinkstateRoute(Collections.emptyList()).build()).build();
139         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
140         assertEquals(createRoutes(empty), emptyRoutes);
141     }
142
143     @Test
144     public void testBuildMpUnreachNlriUpdate() {
145         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
146         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
147             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
148         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
149     }
150
151     @Test
152     public void testBuildMpReachNlriUpdate() {
153         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
154         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType());
155         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
156     }
157
158     @Test
159     public void testIsComplexRoute() {
160         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
161     }
162
163     @Test
164     public void testCacheableNlriObjects() {
165         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
166     }
167
168     @Test
169     public void testCacheableAttributeObjects() {
170         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
171     }
172
173     @Test
174     public void testRouteIdAddPath() {
175         Assert.assertNull(RIB_SUPPORT.getRouteIdAddPath(AbstractRIBSupportTest.PATH_ID, null));
176     }
177
178     @Test
179     public void testRoutePath() {
180         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
181         Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
182     }
183
184     @Test
185     public void testExtractPathId() {
186         Assert.assertEquals((Long) NON_PATH_ID, RIB_SUPPORT.extractPathId(null));
187     }
188
189     @Test
190     public void testRouteAttributesIdentifier() {
191         Assert.assertEquals(new NodeIdentifier(QName.create(LinkstateRoutes.QNAME,
192             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes.QNAME.getLocalName().intern())),
193             RIB_SUPPORT.routeAttributesIdentifier());
194     }
195
196     @Test
197     public void testRoutesCaseClass() {
198         Assert.assertEquals(LinkstateRoutesCase.class, RIB_SUPPORT.routesCaseClass());
199     }
200
201     @Test
202     public void testRoutesContainerClass() {
203         Assert.assertEquals(LinkstateRoutes.class, RIB_SUPPORT.routesContainerClass());
204     }
205
206     @Test
207     public void testRoutesListClass() {
208         Assert.assertEquals(LinkstateRoute.class, RIB_SUPPORT.routesListClass());
209     }
210
211     @Test
212     public void testChangedRoutes() {
213         final Routes emptyCase = new LinkstateRoutesCaseBuilder().build();
214         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode();
215         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
216
217         final Routes emptyRoutes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(new LinkstateRoutesBuilder().build()).build();
218         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
219         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
220
221         final Routes routes = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(ROUTES).build();
222         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
223         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedRoutes(tree);
224         Assert.assertFalse(result.isEmpty());
225     }
226 }