Do not create empty route list container
[bgpcep.git] / bgp / extensions / 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 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableSet;
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.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.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.rev180329.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.rev180329.PathId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.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.tree.DataTreeCandidateNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
47
48 public final class IPv4RIBSupportTest extends AbstractRIBSupportTest<Ipv4RoutesCase, Ipv4Routes, Ipv4Route,
49         Ipv4RouteKey> {
50     private IPv4RIBSupport ribSupport;
51     private static final PathId PATH_ID = new PathId(1L);
52     private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.2.3.4/32");
53     private static final Ipv4RouteKey ROUTE_KEY = new Ipv4RouteKey(PATH_ID, PREFIX.getValue());
54     private static final Ipv4Prefixes IPV4_PREFIXES = new Ipv4PrefixesBuilder().setPathId(PATH_ID)
55             .setPrefix(PREFIX).build();
56     private static final DestinationIpv4Case REACH_NLRI = new DestinationIpv4CaseBuilder()
57             .setDestinationIpv4(new DestinationIpv4Builder()
58                     .setIpv4Prefixes(Lists.newArrayList(IPV4_PREFIXES)).build()).build();
59
60     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
61             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case UNREACH_NLRI =
62             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
63                     .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
64                     .setDestinationIpv4(new DestinationIpv4Builder()
65                             .setIpv4Prefixes(Collections.singletonList(IPV4_PREFIXES)).build()).build();
66
67     private static final Ipv4Route ROUTE = new Ipv4RouteBuilder()
68             .setRouteKey(PREFIX.getValue())
69             .setAttributes(ATTRIBUTES)
70             .setPathId(PATH_ID)
71             .setPrefix(PREFIX).build();
72     private static final Ipv4Routes ROUTES = new Ipv4RoutesBuilder()
73             .setIpv4Route(Collections.singletonList(ROUTE)).build();
74
75     @Override
76     public void setUp() throws Exception {
77         super.setUp();
78         this.ribSupport = IPv4RIBSupport.getInstance(this.mappingService);
79         setUpTestCustomizer(this.ribSupport);
80     }
81
82     @Test
83     public void testDeleteRoutes() {
84         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
85         final InstanceIdentifier<Ipv4Route> instanceIdentifier = this.deletedRoutes.get(0);
86         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(Ipv4Route.class));
87     }
88
89     @Test
90     public void testPutRoutes() {
91         this.ribSupport.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         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
100     }
101
102     @Test
103     public void testBuildMpUnreachNlriUpdate() {
104         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
105         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class).getMpUnreachNlri()
106                 .getWithdrawnRoutes().getDestinationType());
107         assertNull(update.getAttributes().augmentation(Attributes1.class));
108     }
109
110     @Test
111     public void testBuildMpReachNlriUpdate() {
112         final Update update = this.ribSupport.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
113         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
114                 .getAdvertizedRoutes().getDestinationType());
115         assertNull(update.getAttributes().augmentation(Attributes2.class));
116     }
117
118     @Test
119     public void testCacheableNlriObjects() {
120         assertEquals(ImmutableSet.of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
121                 .message.rev180329.Ipv4PrefixAndPathId.class), this.ribSupport.cacheableNlriObjects());
122     }
123
124     @Test
125     public void testCacheableAttributeObjects() {
126         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
127     }
128
129     @Test
130     public void testRouteIdAddPath() {
131         final Ipv4RouteKey expected = new Ipv4RouteKey(new PathId(1L), PREFIX.getValue());
132         assertEquals(expected, this.ribSupport.createRouteListKey(expected.getPathId().getValue(), PREFIX.getValue()));
133     }
134
135     @Test
136     public void testRoutePath() {
137         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(ROUTES);
138         assertEquals(getRoutePath().node(prefixNii), this.ribSupport.routePath(getTablePath(), prefixNii));
139     }
140
141     @Test
142     public void testRouteAttributesIdentifier() {
143         assertEquals(new NodeIdentifier(QName.create(Ipv4Routes.QNAME, Attributes.QNAME.getLocalName().intern())),
144                 this.ribSupport.routeAttributesIdentifier());
145     }
146
147     @Test
148     public void testRoutesCaseClass() {
149         assertEquals(Ipv4RoutesCase.class, this.ribSupport.routesCaseClass());
150     }
151
152     @Test
153     public void testRoutesContainerClass() {
154         assertEquals(Ipv4Routes.class, this.ribSupport.routesContainerClass());
155     }
156
157     @Test
158     public void testRoutesListClass() {
159         assertEquals(Ipv4Route.class, this.ribSupport.routesListClass());
160     }
161
162     @Test
163     public void testChangedRoutes() {
164         final Routes emptyCase = new Ipv4RoutesCaseBuilder().build();
165         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
166                 createRoutes(emptyCase)).getRootNode();
167         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
168
169         final Routes emptyRoutes = new Ipv4RoutesCaseBuilder().setIpv4Routes(new Ipv4RoutesBuilder().build()).build();
170         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
171         assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
172
173         final Routes routes = new Ipv4RoutesCaseBuilder().setIpv4Routes(ROUTES).build();
174         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
175         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
176         Assert.assertFalse(result.isEmpty());
177     }
178 }