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