Cleanup deprecation warnings in bgp/extensions/flowspec
[bgpcep.git] / bgp / extensions / 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
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
23 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv6.DestinationFlowspecIpv6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv6.DestinationFlowspecIpv6Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRoute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRouteBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRouteKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.routes.FlowspecIpv6Routes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.routes.FlowspecIpv6RoutesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6Case;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6CaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
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 FlowspecIpv6RIBSupportTest extends AbstractRIBSupportTest<FlowspecIpv6RoutesCase, FlowspecIpv6Routes,
55         FlowspecRoute, FlowspecRouteKey> {
56
57     private static final PathId PATH_ID = new PathId(Uint32.ONE);
58     private static final DestinationIpv6PrefixCase DEST_PREFIX = new DestinationIpv6PrefixCaseBuilder()
59             .setDestinationPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build();
60     private static final List<Flowspec> FLOW_LIST = Collections.singletonList(new FlowspecBuilder()
61             .setFlowspecType(DEST_PREFIX).build());
62     private static final DestinationFlowspecIpv6 DEST_FLOW = new DestinationFlowspecIpv6Builder()
63             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
64     private static final DestinationFlowspecIpv6Case REACH_NLRI = new DestinationFlowspecIpv6CaseBuilder()
65             .setDestinationFlowspecIpv6(DEST_FLOW).build();
66     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
67             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6Case UNREACH_NLRI
68             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
69             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6CaseBuilder()
70             .setDestinationFlowspecIpv6(DEST_FLOW).build();
71     private FlowspecRoute route;
72     private FlowspecRouteKey routeKey;
73     private FlowspecIpv6Routes routes;
74     private FlowspecIpv6RIBSupport ribSupport;
75
76     @Override
77     public void setUp() throws Exception {
78         super.setUp();
79         final SimpleFlowspecExtensionProviderContext fsContext = new SimpleFlowspecExtensionProviderContext();
80         final FlowspecActivator activator = new FlowspecActivator(fsContext);
81         final BGPActivator act = new BGPActivator(activator);
82         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
83         act.start(context);
84
85         final SimpleFlowspecIpv6NlriParser parser = new SimpleFlowspecIpv6NlriParser(
86                 fsContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4,
87                         SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
88         this.routeKey = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
89         this.route = new FlowspecRouteBuilder().withKey(this.routeKey).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
90                 .setAttributes(new AttributesBuilder().build()).build();
91         this.routes = new FlowspecIpv6RoutesBuilder().setFlowspecRoute(Map.of(this.route.key(), this.route)).build();
92         this.ribSupport = FlowspecIpv6RIBSupport.getInstance(fsContext, this.adapter.currentSerializer());
93         setUpTestCustomizer(this.ribSupport);
94     }
95
96     @Test
97     public void testDeleteRoutes() {
98         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
99         final InstanceIdentifier<FlowspecRoute> instanceIdentifier = this.deletedRoutes.get(0);
100         assertEquals(this.routeKey, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
101     }
102
103     @Test
104     public void testPutRoutes() {
105         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
106         assertEquals(this.route, this.insertedRoutes.get(0).getValue());
107     }
108
109     @Test
110     public void testEmptyRoute() {
111         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
112     }
113
114     @Test
115     public void testBuildMpUnreachNlriUpdate() {
116         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(),
117                 createRoutes(this.routes), ATTRIBUTES);
118         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
119             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
120         assertNull(update.getAttributes().augmentation(Attributes1.class));
121     }
122
123     @Test
124     public void testBuildMpReachNlriUpdate() {
125         final Update update = this.ribSupport.buildUpdate(createRoutes(this.routes),
126                 Collections.emptyList(), ATTRIBUTES);
127         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
128                 .getAdvertizedRoutes().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(this.routeKey, this.ribSupport.createRouteListKey(routeKey.getPathId(), routeKey.getRouteKey()));
145     }
146
147     @Test
148     public void testRoutePath() {
149         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(this.routes);
150         assertEquals(getRoutePath().node(prefixNii),
151                 this.ribSupport.routePath(getTablePath(), prefixNii));
152     }
153
154     @Test
155     public void testRouteAttributesIdentifier() {
156         assertEquals(new NodeIdentifier(QName.create(FlowspecIpv6Routes.QNAME,
157             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes
158                     .QNAME.getLocalName().intern())),
159             this.ribSupport.routeAttributesIdentifier());
160     }
161
162     @Test
163     public void testRoutesCaseClass() {
164         assertEquals(FlowspecIpv6RoutesCase.class, this.ribSupport.routesCaseClass());
165     }
166
167     @Test
168     public void testRoutesContainerClass() {
169         assertEquals(FlowspecIpv6Routes.class, this.ribSupport.routesContainerClass());
170     }
171
172     @Test
173     public void testRoutesListClass() {
174         assertEquals(FlowspecRoute.class, this.ribSupport.routesListClass());
175     }
176
177     @Test
178     public void testChangedRoutes() {
179         final Routes emptyCase = new FlowspecIpv6RoutesCaseBuilder().build();
180         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
181                 createRoutes(emptyCase)).getRootNode();
182         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
183
184         final Routes emptyRoutes = new FlowspecIpv6RoutesCaseBuilder()
185                 .setFlowspecIpv6Routes(new FlowspecIpv6RoutesBuilder().build()).build();
186         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
187         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
188
189         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(new FlowspecIpv6RoutesCaseBuilder()
190             .setFlowspecIpv6Routes(new FlowspecIpv6RoutesBuilder()
191                 .setFlowspecRoute(Map.of(this.route.key(), this.route)).build()).build())).getRootNode();
192         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
193         Assert.assertFalse(result.isEmpty());
194     }
195 }