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