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