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