Create common parent for extensions families
[bgpcep.git] / bgp / extensions / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / FlowspecL3vpnIpv4RIBSupportTest.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 package org.opendaylight.protocol.bgp.flowspec;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.flowspec.l3vpn.ipv4.FlowspecL3vpnIpv4RIBSupport;
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.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.Flowspec;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.FlowspecBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4Routes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4RoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4Case;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4CaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RdTwoOctetAs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
52
53 public class FlowspecL3vpnIpv4RIBSupportTest extends AbstractRIBSupportTest<FlowspecL3vpnIpv4RoutesCase,
54         FlowspecL3vpnIpv4Routes, FlowspecL3vpnRoute, FlowspecL3vpnRouteKey> {
55
56     private static final FlowspecL3vpnRoute ROUTE;
57     private static final FlowspecL3vpnRouteKey ROUTE_KEY;
58     private static final PathId PATH_ID = new PathId(1L);
59     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdTwoOctetAs("0:5:3"));
60     private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
61             .setDestinationPrefix(new Ipv4Prefix("172.17.1.0/24")).build();
62     private static final List<Flowspec> FLOW_LIST
63             = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
64     private static final DestinationFlowspecL3vpnIpv4 DEST_FLOW
65             = new DestinationFlowspecL3vpnIpv4Builder().setRouteDistinguisher(RD)
66             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
67     private static final DestinationFlowspecL3vpnIpv4Case REACH_NLRI
68             = new DestinationFlowspecL3vpnIpv4CaseBuilder().setDestinationFlowspecL3vpnIpv4(DEST_FLOW).build();
69     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
70             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecL3vpnIpv4Case UNREACH_NLRI
71             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
72             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
73             DestinationFlowspecL3vpnIpv4CaseBuilder().setDestinationFlowspecL3vpnIpv4(DEST_FLOW).build();
74     private FlowspecL3vpnIpv4RIBSupport ribSupport;
75
76     static {
77         ROUTE_KEY = new FlowspecL3vpnRouteKey(PATH_ID,
78                 "[l3vpn with route-distinguisher 0:5:3] all packets to 172.17.1.0/24");
79         ROUTE = new FlowspecL3vpnRouteBuilder().withKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
80             .setAttributes(new AttributesBuilder().build()).setRouteDistinguisher(RD).build();
81     }
82
83     @Override
84     public void setUp() throws Exception {
85         super.setUp();
86         this.ribSupport = FlowspecL3vpnIpv4RIBSupport
87                 .getInstance(new SimpleFlowspecExtensionProviderContext(), this.mappingService);
88         setUpTestCustomizer(this.ribSupport);
89     }
90
91     @Test
92     public void testDeleteRoutes() {
93         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
94         final InstanceIdentifier<FlowspecL3vpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
95         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecL3vpnRoute.class));
96     }
97
98     @Test
99     public void testPutRoutes() {
100         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
101         final FlowspecL3vpnRoute route = (FlowspecL3vpnRoute) this.insertedRoutes.get(0).getValue();
102         assertEquals(ROUTE, route);
103     }
104
105     @Test
106     public void testEmptyRoute() {
107         final Routes empty = new FlowspecL3vpnIpv4RoutesCaseBuilder().setFlowspecL3vpnIpv4Routes(
108             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.emptyList()).build()).build();
109         assertEquals(createEmptyTable(empty), this.ribSupport.emptyTable());
110     }
111
112     @Test
113     public void testBuildMpUnreachNlriUpdate() {
114         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(
115             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()), ATTRIBUTES);
116         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
117             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
118         assertNull(update.getAttributes().augmentation(Attributes1.class));
119     }
120
121     @Test
122     public void testBuildMpReachNlriUpdate() {
123         final Update update = this.ribSupport.buildUpdate(createRoutes(
124             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()), Collections.emptyList(), ATTRIBUTES);
125         final AdvertizedRoutes advertised = update.getAttributes().augmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes();
126         assertEquals(REACH_NLRI, advertised.getDestinationType());
127         assertNull(update.getAttributes().augmentation(Attributes2.class));
128     }
129
130     @Test
131     public void testCacheableNlriObjects() {
132         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
133     }
134
135     @Test
136     public void testCacheableAttributeObjects() {
137         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
138     }
139
140     @Test
141     public void testRouteIdAddPath() {
142         assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(1L, ROUTE_KEY.getRouteKey()));
143     }
144
145     @Test
146     public void testRoutePath() {
147         final NodeIdentifierWithPredicates prefixNii
148                 = createRouteNIWP(new FlowspecL3vpnIpv4RoutesBuilder()
149                 .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build());
150         assertEquals(getRoutePath().node(prefixNii),
151                 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
152     }
153
154     @Test
155     public void testRouteAttributesIdentifier() {
156         assertEquals(new NodeIdentifier(QName.create(FlowspecL3vpnIpv4Routes.QNAME,
157             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
158                     .Attributes.QNAME.getLocalName().intern())),
159             this.ribSupport.routeAttributesIdentifier());
160     }
161
162     @Test
163     public void testRoutesCaseClass() {
164         assertEquals(FlowspecL3vpnIpv4RoutesCase.class, this.ribSupport.routesCaseClass());
165     }
166
167     @Test
168     public void testRoutesContainerClass() {
169         assertEquals(FlowspecL3vpnIpv4Routes.class, this.ribSupport.routesContainerClass());
170     }
171
172     @Test
173     public void testRoutesListClass() {
174         assertEquals(FlowspecL3vpnRoute.class, this.ribSupport.routesListClass());
175     }
176
177     @Test
178     public void testChangedRoutes() {
179         final Routes emptyCase = new FlowspecL3vpnIpv4RoutesCaseBuilder().build();
180         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
181                 createRoutes(emptyCase)).getRootNode();
182         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
183
184         final Routes emptyRoutes = new FlowspecL3vpnIpv4RoutesCaseBuilder()
185                 .setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder().build()).build();
186         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
187         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
188
189         final Routes routes = new FlowspecL3vpnIpv4RoutesCaseBuilder()
190                 .setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder()
191             .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()).build();
192         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
193         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
194         Assert.assertFalse(result.isEmpty());
195     }
196 }