Provide codec Serializer via RibSupport
[bgpcep.git] / bgp / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / FlowspecL3vpnIpv6RIBSupportTest.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 package org.opendaylight.protocol.bgp.flowspec;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.flowspec.l3vpn.ipv6.FlowspecL3vpnIpv6RIBSupport;
20 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv6RoutesCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv6RoutesCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.Flowspec;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.FlowspecBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.destination.ipv6.DestinationFlowspecL3vpnIpv6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.destination.ipv6.DestinationFlowspecL3vpnIpv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv6.route.FlowspecL3vpnRouteKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv6.routes.FlowspecL3vpnIpv6Routes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.l3vpn.ipv6.routes.FlowspecL3vpnIpv6RoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv6Case;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv6CaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RdTwoOctetAs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 FlowspecL3vpnIpv6RIBSupportTest extends AbstractRIBSupportTest<FlowspecL3vpnRoute> {
54
55     private static final FlowspecL3vpnRoute ROUTE;
56     private static final FlowspecL3vpnRouteKey ROUTE_KEY;
57     private static final PathId PATH_ID = new PathId(1L);
58     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdTwoOctetAs("0:5:3"));
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
62             = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
63     private static final DestinationFlowspecL3vpnIpv6 DEST_FLOW
64             = new DestinationFlowspecL3vpnIpv6Builder().setRouteDistinguisher(RD)
65             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
66     private static final DestinationFlowspecL3vpnIpv6Case REACH_NLRI
67             = new DestinationFlowspecL3vpnIpv6CaseBuilder().setDestinationFlowspecL3vpnIpv6(DEST_FLOW).build();
68     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
69             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecL3vpnIpv6Case UNREACH_NLRI
70             = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update
71             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecL3vpnIpv6CaseBuilder()
72             .setDestinationFlowspecL3vpnIpv6(DEST_FLOW).build();
73
74     static {
75         ROUTE_KEY = new FlowspecL3vpnRouteKey(PATH_ID,
76                 "[l3vpn with route-distinguisher 0:5:3] all packets to 2001:db8:1:2::/64");
77         ROUTE = new FlowspecL3vpnRouteBuilder().setKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
78                 .setAttributes(new AttributesBuilder().build()).setRouteDistinguisher(RD).build();
79     }
80
81     private FlowspecL3vpnIpv6RIBSupport ribSupport;
82
83     @Override
84     public void setUp() throws Exception {
85         super.setUp();
86         this.ribSupport = FlowspecL3vpnIpv6RIBSupport
87                 .getInstance(new SimpleFlowspecExtensionProviderContext(), this.mappingService);
88         setUpTestCustomizer(this.ribSupport);
89     }
90
91     @Test
92     public void testDeleteRoutes() {
93         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
94         final InstanceIdentifier<FlowspecL3vpnRoute> instanceIdentifier = this.deletedRoutes.get(0);
95         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecL3vpnRoute.class));
96     }
97
98     @Test
99     public void testPutRoutes() {
100         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
101         final FlowspecL3vpnRoute route = (FlowspecL3vpnRoute) this.insertedRoutes.get(0).getValue();
102         assertEquals(ROUTE, route);
103     }
104
105     @Test
106     public void testEmptyRoute() {
107         final Routes empty = new FlowspecL3vpnIpv6RoutesCaseBuilder().setFlowspecL3vpnIpv6Routes(
108             new FlowspecL3vpnIpv6RoutesBuilder().setFlowspecL3vpnRoute(Collections.emptyList()).build()).build();
109         final ChoiceNode emptyRoutes = this.ribSupport.emptyRoutes();
110         assertEquals(createRoutes(empty), emptyRoutes);
111     }
112
113     @Test
114     public void testBuildMpUnreachNlriUpdate() {
115         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(
116             new FlowspecL3vpnIpv6RoutesBuilder()
117                     .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()), 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 = this.ribSupport.buildUpdate(createRoutes(
126             new FlowspecL3vpnIpv6RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()),
127                 Collections.emptyList(), ATTRIBUTES);
128         final AdvertizedRoutes advertised
129                 = update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes();
130         assertEquals(REACH_NLRI, advertised.getDestinationType());
131         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
132     }
133
134     @Test
135     public void testCacheableNlriObjects() {
136         Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
137     }
138
139     @Test
140     public void testCacheableAttributeObjects() {
141         Assert.assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
142     }
143
144     @Test
145     public void testRouteIdAddPath() {
146         Assert.assertEquals(ROUTE_KEY, this.ribSupport.createRouteListKey(1L, ROUTE_KEY.getRouteKey()));
147     }
148
149     @Test
150     public void testRoutePath() {
151         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii
152                 = createRouteNIWP(new FlowspecL3vpnIpv6RoutesBuilder()
153                 .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build());
154         Assert.assertEquals(getRoutePath().node(prefixNii),
155                 this.ribSupport.routePath(getTablePath().node(Routes.QNAME), prefixNii));
156     }
157
158     @Test
159     public void testRouteAttributesIdentifier() {
160         Assert.assertEquals(new YangInstanceIdentifier.NodeIdentifier(QName.create(FlowspecL3vpnIpv6Routes.QNAME,
161             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
162                     .Attributes.QNAME.getLocalName().intern())),
163             this.ribSupport.routeAttributesIdentifier());
164     }
165
166     @Test
167     public void testRoutesCaseClass() {
168         Assert.assertEquals(FlowspecL3vpnIpv6RoutesCase.class, this.ribSupport.routesCaseClass());
169     }
170
171     @Test
172     public void testRoutesContainerClass() {
173         Assert.assertEquals(FlowspecL3vpnIpv6Routes.class, this.ribSupport.routesContainerClass());
174     }
175
176     @Test
177     public void testRoutesListClass() {
178         Assert.assertEquals(FlowspecL3vpnRoute.class, this.ribSupport.routesListClass());
179     }
180
181     @Test
182     public void testChangedRoutes() {
183         final Routes emptyCase = new FlowspecL3vpnIpv6RoutesCaseBuilder().build();
184         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
185                 createRoutes(emptyCase)).getRootNode();
186         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
187
188         final Routes emptyRoutes = new FlowspecL3vpnIpv6RoutesCaseBuilder()
189                 .setFlowspecL3vpnIpv6Routes(new FlowspecL3vpnIpv6RoutesBuilder().build()).build();
190         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
191         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
192
193         final Routes routes = new FlowspecL3vpnIpv6RoutesCaseBuilder()
194                 .setFlowspecL3vpnIpv6Routes(new FlowspecL3vpnIpv6RoutesBuilder()
195                         .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()).build();
196         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
197         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
198         Assert.assertFalse(result.isEmpty());
199     }
200 }