Update MRI projects for Aluminium
[bgpcep.git] / bgp / extensions / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / FlowspecIpv4RIBSupportTest.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.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.bgp.rib.rib.loc.rib.tables.routes.FlowspecRoutesCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.Flowspec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.FlowspecBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv4.DestinationFlowspecIpv4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.ipv4.DestinationFlowspecIpv4Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRoute;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.ipv4.route.FlowspecRouteKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.routes.FlowspecRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase;
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.DestinationFlowspecCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.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.common.Uint32;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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 FlowspecIpv4RIBSupportTest extends AbstractRIBSupportTest<FlowspecRoutesCase, FlowspecRoutes,
54         FlowspecRoute, FlowspecRouteKey> {
55
56     private FlowspecIpv4RIBSupport ribSupport;
57     private FlowspecRoute route;
58     private FlowspecRoutes routes;
59     private FlowspecRouteKey routeKey;
60     private static final PathId PATH_ID = new PathId(Uint32.ONE);
61
62     private static final DestinationPrefixCase DEST_PREFIX = new DestinationPrefixCaseBuilder()
63             .setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
64     private static final List<Flowspec> FLOW_LIST
65             = Collections.singletonList(new FlowspecBuilder().setFlowspecType(DEST_PREFIX).build());
66     private static final DestinationFlowspecIpv4 DEST_FLOW = new DestinationFlowspecIpv4Builder()
67             .setFlowspec(FLOW_LIST).setPathId(PATH_ID).build();
68     private static final DestinationFlowspecCase REACH_NLRI = new DestinationFlowspecCaseBuilder()
69             .setDestinationFlowspecIpv4(DEST_FLOW).build();
70     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
71             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase UNREACH_NLRI =
72             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.update
73             .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder()
74             .setDestinationFlowspecIpv4(DEST_FLOW).build();
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         this.ribSupport = FlowspecIpv4RIBSupport.getInstance(fsContext, this.adapter.currentSerializer());
85
86         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(
87                 fsContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4,
88                         SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
89
90         this.routeKey = new FlowspecRouteKey(PATH_ID, parser.stringNlri(FLOW_LIST));
91         this.route = new FlowspecRouteBuilder().withKey(this.routeKey).setPathId(PATH_ID).setFlowspec(FLOW_LIST)
92                 .setAttributes(new AttributesBuilder().build()).build();
93         this.routes = new FlowspecRoutesBuilder().setFlowspecRoute(Collections.singletonList(this.route)).build();
94         setUpTestCustomizer(this.ribSupport);
95     }
96
97     @Test
98     public void testDeleteRoutes() {
99         this.ribSupport.deleteRoutes(this.tx, getTablePath(), createNlriWithDrawnRoute(UNREACH_NLRI));
100         final InstanceIdentifier<FlowspecRoute> instanceIdentifier = this.deletedRoutes.get(0);
101         assertEquals(this.routeKey, instanceIdentifier.firstKeyOf(FlowspecRoute.class));
102     }
103
104     @Test
105     public void testPutRoutes() {
106         this.ribSupport.putRoutes(this.tx, getTablePath(), createNlriAdvertiseRoute(REACH_NLRI), createAttributes());
107         assertEquals(this.route, this.insertedRoutes.get(0).getValue());
108     }
109
110     @Test
111     public void testEmptyRoute() {
112         assertEquals(createEmptyTable(), this.ribSupport.emptyTable());
113     }
114
115     @Test
116     public void testBuildMpUnreachNlriUpdate() {
117         final Update update = this.ribSupport.buildUpdate(Collections.emptyList(), createRoutes(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(routes), Collections.emptyList(), ATTRIBUTES);
126         assertEquals(REACH_NLRI, update.getAttributes().augmentation(Attributes1.class).getMpReachNlri()
127                 .getAdvertizedRoutes().getDestinationType());
128         assertNull(update.getAttributes().augmentation(Attributes2.class));
129     }
130
131     @Test
132     public void testCacheableNlriObjects() {
133         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableNlriObjects());
134     }
135
136     @Test
137     public void testCacheableAttributeObjects() {
138         assertEquals(ImmutableSet.of(), this.ribSupport.cacheableAttributeObjects());
139     }
140
141     @Test
142     public void testRouteIdAddPath() {
143         assertEquals(this.routeKey, this.ribSupport.createRouteListKey(routeKey.getPathId(), routeKey.getRouteKey()));
144     }
145
146     @Test
147     public void testRoutePath() {
148         final NodeIdentifierWithPredicates prefixNii = createRouteNIWP(this.routes);
149         assertEquals(getRoutePath().node(prefixNii),
150                 this.ribSupport.routePath(getTablePath(), prefixNii));
151     }
152
153     @Test
154     public void testRouteAttributesIdentifier() {
155         assertEquals(new NodeIdentifier(QName.create(FlowspecRoutes.QNAME,
156             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables
157                     .Attributes.QNAME.getLocalName().intern())),
158             this.ribSupport.routeAttributesIdentifier());
159     }
160
161     @Test
162     public void testRoutesCaseClass() {
163         assertEquals(FlowspecRoutesCase.class, this.ribSupport.routesCaseClass());
164     }
165
166     @Test
167     public void testRoutesContainerClass() {
168         assertEquals(FlowspecRoutes.class, this.ribSupport.routesContainerClass());
169     }
170
171     @Test
172     public void testRoutesListClass() {
173         assertEquals(FlowspecRoute.class, this.ribSupport.routesListClass());
174     }
175
176     @Test
177     public void testChangedRoutes() {
178         final Routes emptyCase = new FlowspecRoutesCaseBuilder().build();
179         DataTreeCandidateNode tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(),
180                 createRoutes(emptyCase)).getRootNode();
181         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
182
183         final Routes emptyRoutes
184                 = new FlowspecRoutesCaseBuilder().setFlowspecRoutes(new FlowspecRoutesBuilder().build()).build();
185         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(emptyRoutes)).getRootNode();
186         Assert.assertTrue(this.ribSupport.changedRoutes(tree).isEmpty());
187
188         tree = DataTreeCandidates.fromNormalizedNode(getRoutePath(), createRoutes(new FlowspecRoutesCaseBuilder()
189             .setFlowspecRoutes(new FlowspecRoutesBuilder().setFlowspecRoute(Collections.singletonList(this.route))
190                 .build()).build())).getRootNode();
191         final Collection<DataTreeCandidateNode> result = this.ribSupport.changedRoutes(tree);
192         Assert.assertFalse(result.isEmpty());
193     }
194 }