BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / FlowspecL3vpnIpv4RIBSupportTest.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 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
13
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.flowspec.l3vpn.ipv4.FlowspecL3vpnIpv4RIBSupport;
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.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.bgp.rib.rib.loc.rib.tables.routes.FlowspecL3vpnIpv4RoutesCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.Flowspec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.FlowspecBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.destination.ipv4.DestinationFlowspecL3vpnIpv4Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4Routes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.ipv4.routes.FlowspecL3vpnIpv4RoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRoute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.l3vpn.ipv4.route.FlowspecL3vpnRouteKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4Case;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv4CaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdTwoOctetAs;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 FlowspecL3vpnIpv4RIBSupportTest extends AbstractRIBSupportTest {
56
57     private static final FlowspecL3vpnIpv4RIBSupport RIB_SUPPORT;
58     private static final FlowspecL3vpnRoute ROUTE;
59     private static final FlowspecL3vpnRouteKey ROUTE_KEY;
60     private static final PathId PATH_ID = new PathId(1L);
61     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdTwoOctetAs("0:5:3"));
62
63     private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
64         .setDestinationPrefix(new Ipv4Prefix("172.17.1.0/24")).build();
65     private static final List<Flowspec> FLOW_LIST = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
66     private static final DestinationFlowspecL3vpnIpv4 DEST_FLOW = new DestinationFlowspecL3vpnIpv4Builder().setRouteDistinguisher(RD)
67         .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
68     private static final DestinationFlowspecL3vpnIpv4Case REACH_NLRI = new DestinationFlowspecL3vpnIpv4CaseBuilder().setDestinationFlowspecL3vpnIpv4(DEST_FLOW).build();
69     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
70         DestinationFlowspecL3vpnIpv4Case UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
71         DestinationFlowspecL3vpnIpv4CaseBuilder().setDestinationFlowspecL3vpnIpv4(DEST_FLOW).build();
72
73     static {
74         final SimpleFlowspecExtensionProviderContext providerContext = new SimpleFlowspecExtensionProviderContext();
75         RIB_SUPPORT = FlowspecL3vpnIpv4RIBSupport.getInstance(providerContext);
76         ROUTE_KEY = new FlowspecL3vpnRouteKey(PATH_ID, "[l3vpn with route-distinguisher 0:5:3] all packets to 172.17.1.0/24");
77         ROUTE = new FlowspecL3vpnRouteBuilder().setKey(ROUTE_KEY).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
78             .setAttributes(new AttributesBuilder().build()).setRouteDistinguisher(RD).build();
79     }
80
81     @Override
82     public void setUp() throws Exception {
83         super.setUp();
84         setUpTestCustomizer(RIB_SUPPORT);
85     }
86
87     @Test
88     public void testDeleteRoutes() {
89         RIB_SUPPORT.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
90         final InstanceIdentifier<FlowspecL3vpnRoute> instanceIdentifier = (InstanceIdentifier<FlowspecL3vpnRoute>) this.deletedRoutes.get(0);
91         assertEquals(ROUTE_KEY, instanceIdentifier.firstKeyOf(FlowspecL3vpnRoute.class));
92     }
93
94     @Test
95     public void testPutRoutes() {
96         RIB_SUPPORT.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
97         final FlowspecL3vpnRoute route = (FlowspecL3vpnRoute) this.insertedRoutes.get(0).getValue();
98         assertEquals(ROUTE, route);
99     }
100
101     @Test
102     public void testEmptyRoute() throws Exception {
103         final Routes empty = new FlowspecL3vpnIpv4RoutesCaseBuilder().setFlowspecL3vpnIpv4Routes(
104             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.emptyList()).build()).build();
105         final ChoiceNode emptyRoutes = RIB_SUPPORT.emptyRoutes();
106         assertEquals(createRoutes(empty), emptyRoutes);
107     }
108
109     @Test
110     public void testBuildMpUnreachNlriUpdate() {
111         final Update update = RIB_SUPPORT.buildUpdate(Collections.emptyList(), createRoutes(
112             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()), ATTRIBUTES);
113         assertEquals(UNREACH_NLRI, update.getAttributes().getAugmentation(Attributes2.class)
114             .getMpUnreachNlri().getWithdrawnRoutes().getDestinationType());
115         assertNull(update.getAttributes().getAugmentation(Attributes1.class));
116     }
117
118     @Test
119     public void testBuildMpReachNlriUpdate() {
120         final Update update = RIB_SUPPORT.buildUpdate(createRoutes(
121             new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()), Collections.emptyList(), ATTRIBUTES);
122         final AdvertizedRoutes advertised = update.getAttributes().getAugmentation(Attributes1.class).getMpReachNlri().getAdvertizedRoutes();
123         assertEquals(REACH_NLRI, advertised.getDestinationType());
124         assertNull(update.getAttributes().getAugmentation(Attributes2.class));
125     }
126
127     @Test
128     public void testIsComplexRoute() {
129         Assert.assertTrue(RIB_SUPPORT.isComplexRoute());
130     }
131
132     @Test
133     public void testCacheableNlriObjects() {
134         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableNlriObjects());
135     }
136
137     @Test
138     public void testCacheableAttributeObjects() {
139         Assert.assertEquals(ImmutableSet.of(), RIB_SUPPORT.cacheableAttributeObjects());
140     }
141
142     @Test
143     public void testRouteIdAddPath() {
144         final FlowspecL3vpnRouteKey oldKey = new FlowspecL3vpnRouteKey(new PathId(100L), ROUTE_KEY.getRouteKey());
145         Assert.assertEquals(ROUTE_KEY, RIB_SUPPORT.createNewRouteKey(AbstractRIBSupportTest.PATH_ID, oldKey));
146     }
147
148     @Test
149     public void testRoutePath() {
150         final YangInstanceIdentifier.NodeIdentifierWithPredicates prefixNii = createRouteNIWP(new FlowspecL3vpnIpv4RoutesBuilder().setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build());
151         Assert.assertEquals(getRoutePath().node(prefixNii), RIB_SUPPORT.routePath(getTablePath().node(Routes.QNAME), prefixNii));
152     }
153
154     @Test
155     public void testExtractPathId() {
156         Assert.assertEquals(NON_PATH_ID, RIB_SUPPORT.extractPathId(null));
157     }
158
159     @Test
160     public void testRouteAttributesIdentifier() {
161         Assert.assertEquals(new YangInstanceIdentifier.NodeIdentifier(QName.create(FlowspecL3vpnIpv4Routes.QNAME,
162             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Attributes.QNAME.getLocalName().intern())),
163             RIB_SUPPORT.routeAttributesIdentifier());
164     }
165
166     @Test
167     public void testRoutesCaseClass() {
168         Assert.assertEquals(FlowspecL3vpnIpv4RoutesCase.class, RIB_SUPPORT.routesCaseClass());
169     }
170
171     @Test
172     public void testRoutesContainerClass() {
173         Assert.assertEquals(FlowspecL3vpnIpv4Routes.class, RIB_SUPPORT.routesContainerClass());
174     }
175
176     @Test
177     public void testRoutesListClass() {
178         Assert.assertEquals(FlowspecL3vpnRoute.class, RIB_SUPPORT.routesListClass());
179     }
180
181     @Test
182     public void testChangedRoutes() {
183         final Routes emptyCase = new FlowspecL3vpnIpv4RoutesCaseBuilder().build();
184         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyCase)).getRootNode();
185         Assert.assertTrue(RIB_SUPPORT.changedDOMRoutes(tree).isEmpty());
186
187         final Routes emptyRoutes = new FlowspecL3vpnIpv4RoutesCaseBuilder().setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder().build()).build();
188         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
189         Assert.assertTrue(RIB_SUPPORT.changedDOMRoutes(tree).isEmpty());
190
191         final Routes routes = new FlowspecL3vpnIpv4RoutesCaseBuilder().setFlowspecL3vpnIpv4Routes(new FlowspecL3vpnIpv4RoutesBuilder()
192             .setFlowspecL3vpnRoute(Collections.singletonList(ROUTE)).build()).build();
193         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(routes)).getRootNode();
194         final Collection<DataTreeCandidateNode> result = RIB_SUPPORT.changedDOMRoutes(tree);
195         Assert.assertFalse(result.isEmpty());
196     }
197 }