BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / inet / src / test / java / org / opendaylight / protocol / bgp / inet / IPv6RIBSupportTest.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.inet;
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 java.util.Collection;
17 import java.util.Collections;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.DestinationIpv6Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.Ipv6Routes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.Ipv6RoutesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6RouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6RouteKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6Case;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
47
48 public final class IPv6RIBSupportTest extends AbstractRIBSupportTest {
49     private static final IPv6RIBSupport RIB_SUPPORT = IPv6RIBSupport.getInstance();
50     private static final PathId PATH_ID = new PathId(1L);
51     private static final Ipv6Prefix PREFIX = new Ipv6Prefix("2001:db8:1:2::/64");
52     private static final Ipv6RouteKey ROUTE_KEY = new Ipv6RouteKey(PATH_ID, PREFIX);
53     private static final Ipv6Prefixes IPV6_PREFIXES = new Ipv6PrefixesBuilder().setPathId(PATH_ID)
54             .setPrefix(PREFIX).build();
55     private static final DestinationIpv6Case REACH_NLRI = new DestinationIpv6CaseBuilder()
56             .setDestinationIpv6(new DestinationIpv6Builder()
57                     .setIpv6Prefixes(Lists.newArrayList(IPV6_PREFIXES)).build()).build();
58
59     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update
60             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6Case UNREACH_NLRI =
61             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update
62                     .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder()
63                     .setDestinationIpv6(new DestinationIpv6Builder()
64                             .setIpv6Prefixes(Collections.singletonList(IPV6_PREFIXES)).build()).build();
65
66     private static final Ipv6Route ROUTE = new Ipv6RouteBuilder().setAttributes(ATTRIBUTES).setPathId(PATH_ID)
67             .setPrefix(PREFIX).build();
68     private static final Ipv6Routes ROUTES = new Ipv6RoutesBuilder()
69             .setIpv6Route(Collections.singletonList(ROUTE)).build();
70     private static final Ipv6Routes EMPTY_ROUTES = new Ipv6RoutesBuilder()
71             .setIpv6Route(Collections.emptyList()).build();
72
73     @Override
74     public void setUp() throws Exception {
75         super.setUp();
76         setUpTestCustomizer(RIB_SUPPORT);
77     }
78
79     @Test
80     public void testDeleteRoutes() {
81         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
82         @SuppressWarnings("unchecked") final InstanceIdentifier<Ipv6Route> instanceIdentifier =
83                 (InstanceIdentifier<Ipv6Route>) this.deletedRoutes.get(0);
84         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv6Route.class));
85     }
86
87     @Test
88     public void testPutRoutes() {
89         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
90         final Ipv6Route route = (Ipv6Route) this.insertedRoutes.get(0).getValue();
91         assertEquals(ROUTE, route);
92     }
93
94
95     @Test
96     public void testEmptyRoute() throws Exception {
97         final Routes empty = new Ipv6RoutesCaseBuilder().setIpv6Routes(EMPTY_ROUTES).build();
98         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
99         assertEquals(createRoutes(empty), emptyRoutes);
100     }
101
102     @Test
103     public void testBuildMpUnreachNlriUpdate() {
104         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
105         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
106                 .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
107         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
108     }
109
110     @Test
111     public void testBuildMpReachNlriUpdate() {
112         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
113         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class)
114                 .getMpReachNlri().getAdvertizedRoutes().getDestinationType());
115         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
116     }
117
118     @Test
119     public void testIsComplexRoute() {
120         Assert.assertFalse(RIB_SUPPORT.isComplexRoute());
121     }
122
123     @Test
124     public void testCacheableNlriObjects() {
125         assertEquals(ImmutableSet.of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet
126                 .rev171207.Ipv6Prefix.class), RIB_SUPPORT.cacheableNlriObjects());
127     }
128
129     @Test
130     public void testCacheableAttributeObjects() {
131         assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
132     }
133
134     @Test
135     public void testRouteIdAddPath() {
136         final Ipv6RouteKey expected = new Ipv6RouteKey(new PathId(1L), PREFIX);
137         final Ipv6RouteKey oldKey = new Ipv6RouteKey(new PathId(100L), PREFIX);
138         assertEquals(expected, RIB_SUPPORT.createNewRouteKey(AbstractRIBSupportTest.PATH_ID, oldKey));
139     }
140
141     @Test
142     public void testRoutePath() {
143         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
144         assertEquals(getRoutePath().node(prefixNii),
145                 RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
146     }
147
148
149     @Test
150     public void testExtractPathId() {
151        /* final NormalizedNode<?, ?> route = Iterables.getOnlyElement(createRoutes(ROUTES));
152         assertEquals(PATH_ID.getValue(), RIB_SUPPORT.extractPathId(route));*/
153     }
154
155     @Test
156     public void testRouteAttributesIdentifier() {
157         assertEquals(new NodeIdentifier(QName.create(Ipv6Routes.QNAME,
158                 Attributes.QNAME.getLocalName().intern())), RIB_SUPPORT.routeAttributesIdentifier());
159     }
160
161     @Test
162     public void testRoutesCaseClass() {
163         assertEquals(Ipv6RoutesCase.class, RIB_SUPPORT.routesCaseClass());
164     }
165
166     @Test
167     public void testRoutesContainerClass() {
168         assertEquals(Ipv6Routes.class, RIB_SUPPORT.routesContainerClass());
169     }
170
171     @Test
172     public void testRoutesListClass() {
173         assertEquals(Ipv6Route.class, RIB_SUPPORT.routesListClass());
174     }
175
176     @Test
177     public void testChangedRoutes() {
178         final Routes emptyCase = new Ipv6RoutesCaseBuilder().build();
179         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
180                 createRoutes(emptyCase)).getRootNode();
181         Assert.assertTrue(RIB_SUPPORT.changedDOMRoutes(tree).isEmpty());
182
183         final Routes emptyRoutes = new Ipv6RoutesCaseBuilder().setIpv6Routes(new Ipv6RoutesBuilder().build()).build();
184         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
185         Assert.assertTrue(RIB_SUPPORT.changedDOMRoutes(tree).isEmpty());
186
187         final Routes routes = new Ipv6RoutesCaseBuilder().setIpv6Routes(ROUTES).build();
188         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
189         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedDOMRoutes(tree);
190         Assert.assertFalse(result.isEmpty());
191     }
192 }