Convert bgp-flowspec to OSGi DS
[bgpcep.git] / bgp / extensions / 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
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.flowspec.l3vpn.ipv6.FlowspecL3vpnIpv6RIBSupport;
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.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv6RoutesCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv6RoutesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.destination.ipv6.DestinationFlowspecL3vpnIpv6;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.destination.ipv6.DestinationFlowspecL3vpnIpv6Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRoute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRouteKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv6.routes.FlowspecL3vpnIpv6Routes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.l3vpn.ipv6.routes.FlowspecL3vpnIpv6RoutesBuilder;
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.DestinationFlowspecL3vpnIpv6Case;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv6CaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RdTwoOctetAs;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.common.Uint32;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 FlowspecL3vpnIpv6RIBSupportTest extends AbstractRIBSupportTest<FlowspecL3vpnIpv6RoutesCase,
55         FlowspecL3vpnIpv6Routes, 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 DestinationIpv6PrefixCase DEST_PREFIX = new DestinationIpv6PrefixCaseBuilder()
62             .setDestinationPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build();
63     private static final List<Flowspec> FLOW_LIST
64             = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
65     private static final DestinationFlowspecL3vpnIpv6 DEST_FLOW
66             = new DestinationFlowspecL3vpnIpv6Builder().setRouteDistinguisher(RD)
67             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
68     private static final DestinationFlowspecL3vpnIpv6Case REACH_NLRI
69             = new DestinationFlowspecL3vpnIpv6CaseBuilder().setDestinationFlowspecL3vpnIpv6(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.DestinationFlowspecL3vpnIpv6Case 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.DestinationFlowspecL3vpnIpv6CaseBuilder()
74             .setDestinationFlowspecL3vpnIpv6(DEST_FLOW).build();
75
76     static {
77         ROUTE_KEY = new FlowspecL3vpnRouteKey(PATH_ID,
78                 "[l3vpn with route-distinguisher 0:5:3] all packets to 2001:db8:1:2::/64");
79         ROUTE = new FlowspecL3vpnRouteBuilder().withKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
80                 .setAttributes(new AttributesBuilder().build()).setRouteDistinguisher(RD).build();
81     }
82
83     private FlowspecL3vpnIpv6RIBSupport ribSupport;
84
85     @Override
86     public void setUp() throws Exception {
87         super.setUp();
88         this.ribSupport = new FlowspecL3vpnIpv6RIBSupport(this.adapter.currentSerializer());
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 FlowspecL3vpnIpv6RoutesBuilder()
115                     .setFlowspecL3vpnRoute(Map.of(ROUTE.key(), 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 FlowspecL3vpnIpv6RoutesBuilder().setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build()),
125                 Collections.emptyList(), ATTRIBUTES);
126         final AdvertizedRoutes advertised
127                 = update.getAttributes().augmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes();
128         assertEquals(REACH_NLRI, advertised.getDestinationType());
129         assertNull(update.getAttributes().augmentation(Attributes2.class));
130     }
131
132     @Test
133     public void testCacheableNlriObjects() {
134         Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
135     }
136
137     @Test
138     public void testCacheableAttributeObjects() {
139         Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
140     }
141
142     @Test
143     public void testRouteIdAddPath() {
144         Assert.assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(ROUTE_KEY.getPathId(),
145             ROUTE_KEY.getRouteKey()));
146     }
147
148     @Test
149     public void testRoutePath() {
150         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(
151             new FlowspecL3vpnIpv6RoutesBuilder().setFlowspecL3vpnRoute(Map.of(ROUTE.key(), ROUTE)).build());
152         Assert.assertEquals(getRoutePath().node(prefixNii),
153                 this.ribSupport.routePath(getTablePath(), prefixNii));
154     }
155
156     @Test
157     public void testRouteAttributesIdentifier() {
158         Assert.assertEquals(new YangInstanceIdentifier.NodeIdentifier(QName.create(FlowspecL3vpnIpv6Routes.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         Assert.assertEquals(FlowspecL3vpnIpv6RoutesCase.class, this.ribSupport.routesCaseClass());
167     }
168
169     @Test
170     public void testRoutesContainerClass() {
171         Assert.assertEquals(FlowspecL3vpnIpv6Routes.class, this.ribSupport.routesContainerClass());
172     }
173
174     @Test
175     public void testRoutesListClass() {
176         Assert.assertEquals(FlowspecL3vpnRoute.class, this.ribSupport.routesListClass());
177     }
178
179     @Test
180     public void testChangedRoutes() {
181         final Routes emptyCase = new FlowspecL3vpnIpv6RoutesCaseBuilder().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 FlowspecL3vpnIpv6RoutesCaseBuilder()
187                 .setFlowspecL3vpnIpv6Routes(new FlowspecL3vpnIpv6RoutesBuilder().build()).build();
188         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
189         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
190
191         final Routes routes = new FlowspecL3vpnIpv6RoutesCaseBuilder()
192                 .setFlowspecL3vpnIpv6Routes(new FlowspecL3vpnIpv6RoutesBuilder()
193                         .setFlowspecL3vpnRoute(Map.of(ROUTE.key(), 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 }