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