8ec5a209fbe6f4fdf5c3fd032ecedb2bf4a2d268
[bgpcep.git] / bgp / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / FlowspecIpv4RIBSupportTest.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.rev130715.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.Flowspec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.FlowspecBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.ipv4.DestinationFlowspec;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.ipv4.DestinationFlowspecBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.ipv4.route.FlowspecRoute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.ipv4.route.FlowspecRouteBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.ipv4.route.FlowspecRouteKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.routes.FlowspecRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.routes.FlowspecRoutesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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 FlowspecIpv4RIBSupportTest extends AbstractRIBSupportTest {
56
57     private static final FlowspecIpv4RIBSupport RIB_SUPPORT;
58     private static final FlowspecRoute ROUTE;
59     private static final FlowspecRoutes ROUTES;
60     private static final FlowspecRouteKey ROUTE_KEY;
61     private static final PathId PATH_ID = new PathId(1L);
62
63     private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder().setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
64     private static final List<Flowspec> FLOW_LIST = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
65     private static final DestinationFlowspec DEST_FLOW = new DestinationFlowspecBuilder().setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
66     private static final DestinationFlowspecCase REACH_NLRI = new DestinationFlowspecCaseBuilder().setDestinationFlowspec(DEST_FLOW).build();
67     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update
68         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update
69         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder().setDestinationFlowspec(DEST_FLOW).build();
70
71     static {
72         final SimpleFlowspecExtensionProviderContext fsContext = new SimpleFlowspecExtensionProviderContext();
73         final FlowspecActivator activator = new FlowspecActivator(fsContext);
74         final BGPActivator act = new BGPActivator(activator);
75         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
76         act.start(context);
77         RIB_SUPPORT = FlowspecIpv4RIBSupport.getInstance(fsContext);
78
79         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(
80             fsContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
81
82         ROUTE_KEY = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
83         ROUTE = new FlowspecRouteBuilder().setKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
84             .setAttributes(new AttributesBuilder().build()).build();
85         ROUTES = new FlowspecRoutesBuilder().setFlowspecRoute(Collections.singletonList(ROUTE)).build();
86     }
87
88     @Override
89     public void setUp() throws Exception {
90         super.setUp();
91         setUpTestCustomizer(RIB_SUPPORT);
92     }
93
94     @Test
95     public void testDeleteRoutes() {
96         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
97         final InstanceIdentifier<FlowspecRoute> instanceIdentifier = (InstanceIdentifier<FlowspecRoute>) this.deletedRoutes.get(0);
98         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
99     }
100
101     @Test
102     public void testPutRoutes() {
103         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
104         final FlowspecRoute route = (FlowspecRoute) this.insertedRoutes.get(0).getValue();
105         assertEquals(ROUTE, route);
106     }
107
108     @Test
109     public void testEmptyRoute() throws Exception {
110         final Routes empty = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(
111             new FlowspecRoutesBuilder().setFlowspecRoute(Collections.emptyList()).build()).build();
112         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
113         assertEquals(createRoutes(empty), emptyRoutes);
114     }
115
116     @Test
117     public void testBuildMpUnreachNlriUpdate() {
118         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(ROUTES), ATTRIBUTES);
119         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
120             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
121         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
122     }
123
124     @Test
125     public void testBuildMpReachNlriUpdate() {
126         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(ROUTES), Collections.emptyList(), ATTRIBUTES);
127         assertEquals(REACH_NLRI, update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes().getDestinationType());
128         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
129     }
130
131     @Test
132     public void testIsComplexRoute() {
133         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
134     }
135
136     @Test
137     public void testCacheableNlriObjects() {
138         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
139     }
140
141     @Test
142     public void testCacheableAttributeObjects() {
143         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
144     }
145
146     @Test
147     public void testRouteIdAddPath() {
148         final NodeIdentifierWithPredicates expected = createRouteNIWP(ROUTES);
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(FlowspecRoutes.QNAME,
168             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Attributes.QNAME.getLocalName().intern())),
169             RIB_SUPPORT.routeAttributesIdentifier());
170     }
171
172     @Test
173     public void testRoutesCaseClass() {
174         Assert.assertEquals(FlowspecRoutesCase.class, RIB_SUPPORT.routesCaseClass());
175     }
176
177     @Test
178     public void testRoutesContainerClass() {
179         Assert.assertEquals(FlowspecRoutes.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 FlowspecRoutesCaseBuilder().build();
190         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode();
191         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
192
193         final Routes emptyRoutes = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder().build()).build();
194         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
195         Assert.assertTrue(RIB_SUPPORT.changedRoutes(tree).isEmpty());
196
197         final Routes routes = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder()
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 }