Convert LocRibWriter to DOM APIs
[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.flowspec.FlowspecTypeRegistries.SAFI;
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.Ipv6Prefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecIpv6RoutesCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv6.DestinationFlowspecIpv6;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv6.DestinationFlowspecIpv6Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRoute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRouteBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.route.FlowspecRouteKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.routes.FlowspecIpv6Routes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv6.routes.FlowspecIpv6RoutesBuilder;
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.DestinationFlowspecIpv6Case;
40 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;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.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.common.Uint32;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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<FlowspecIpv6RoutesCase, FlowspecIpv6Routes,
56         FlowspecRoute, FlowspecRouteKey> {
57
58     private static final PathId PATH_ID = new PathId(Uint32.ONE);
59     private static final DestinationIpv6PrefixCase DEST_PREFIX = new DestinationIpv6PrefixCaseBuilder()
60             .setDestinationPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build();
61     private static final List<Flowspec> FLOW_LIST = Collections.singletonList(new FlowspecBuilder()
62             .setFlowspecType(DEST_PREFIX).build());
63     private static final DestinationFlowspecIpv6 DEST_FLOW = new DestinationFlowspecIpv6Builder()
64             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
65     private static final DestinationFlowspecIpv6Case REACH_NLRI = new DestinationFlowspecIpv6CaseBuilder()
66             .setDestinationFlowspecIpv6(DEST_FLOW).build();
67     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
68             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6Case UNREACH_NLRI
69             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
70             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6CaseBuilder()
71             .setDestinationFlowspecIpv6(DEST_FLOW).build();
72     private FlowspecRoute route;
73     private FlowspecRouteKey routeKey;
74     private FlowspecIpv6Routes routes;
75     private FlowspecIpv6RIBSupport ribSupport;
76
77     @Override
78     public void setUp() throws Exception {
79         super.setUp();
80         final BGPActivator act = new BGPActivator();
81         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
82         act.start(context);
83
84         final SimpleFlowspecIpv6NlriParser parser = new SimpleFlowspecIpv6NlriParser(SAFI.FLOWSPEC);
85         this.routeKey = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
86         this.route = new FlowspecRouteBuilder().withKey(this.routeKey).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
87                 .setAttributes(new AttributesBuilder().build()).build();
88         this.routes = new FlowspecIpv6RoutesBuilder().setFlowspecRoute(Map.of(this.route.key(), this.route)).build();
89         this.ribSupport = new FlowspecIpv6RIBSupport(this.adapter.currentSerializer());
90         setUpTestCustomizer(this.ribSupport);
91     }
92
93     @Test
94     public void testDeleteRoutes() {
95         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
96         final InstanceIdentifier<FlowspecRoute> instanceIdentifier = this.deletedRoutes.get(0);
97         assertEquals(this.routeKey, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
98     }
99
100     @Test
101     public void testPutRoutes() {
102         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
103         assertEquals(this.route, this.insertedRoutes.get(0).getValue());
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(),
114                 createRoutes(this.routes), ATTRIBUTES);
115         assertEquals(UNREACH_NLRI, update.getAttributes().augmentation(Attributes2.class)
116             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
117         assertNull(update.getAttributes().augmentation(Attributes1.class));
118     }
119
120     @Test
121     public void testBuildMpReachNlriUpdate() {
122         final Update update = this.ribSupport.buildUpdate(createRoutes(this.routes),
123                 Collections.emptyList(), ATTRIBUTES);
124         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
125                 .getAdvertizedRoutes().getDestinationType());
126         assertNull(update.getAttributes().augmentation(Attributes2.class));
127     }
128
129     @Test
130     public void testCacheableNlriObjects() {
131         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
132     }
133
134     @Test
135     public void testCacheableAttributeObjects() {
136         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
137     }
138
139     @Test
140     public void testRoutePath() {
141         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(this.routes);
142         assertEquals(getRoutePath().node(prefixNii),
143                 this.ribSupport.routePath(getTablePath(), prefixNii));
144     }
145
146     @Test
147     public void testRouteAttributesIdentifier() {
148         assertEquals(new NodeIdentifier(QName.create(FlowspecIpv6Routes.QNAME,
149             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes
150                     .QNAME.getLocalName().intern())),
151             this.ribSupport.routeAttributesIdentifier());
152     }
153
154     @Test
155     public void testRoutesCaseClass() {
156         assertEquals(FlowspecIpv6RoutesCase.class, this.ribSupport.routesCaseClass());
157     }
158
159     @Test
160     public void testRoutesContainerClass() {
161         assertEquals(FlowspecIpv6Routes.class, this.ribSupport.routesContainerClass());
162     }
163
164     @Test
165     public void testRoutesListClass() {
166         assertEquals(FlowspecRoute.class, this.ribSupport.routesListClass());
167     }
168
169     @Test
170     public void testChangedRoutes() {
171         final Routes emptyCase = new FlowspecIpv6RoutesCaseBuilder().build();
172         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
173                 createRoutes(emptyCase)).getRootNode();
174         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
175
176         final Routes emptyRoutes = new FlowspecIpv6RoutesCaseBuilder()
177                 .setFlowspecIpv6Routes(new FlowspecIpv6RoutesBuilder().build()).build();
178         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
179         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
180
181         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(new FlowspecIpv6RoutesCaseBuilder()
182             .setFlowspecIpv6Routes(new FlowspecIpv6RoutesBuilder()
183                 .setFlowspecRoute(Map.of(this.route.key(), this.route)).build()).build())).getRootNode();
184         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
185         Assert.assertFalse(result.isEmpty());
186     }
187 }