BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / ExportDefaultStatementTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.openconfig.routing.policy.statement;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
13 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createClusterInput;
14 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createInputWithOriginator;
15 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createPathInput;
16 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createPathInputWithAs;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
24 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
25 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32
33 public class ExportDefaultStatementTest extends AbstractStatementRegistryConsumerTest {
34     private static final YangInstanceIdentifier.NodeIdentifierWithPredicates ROUTE_ID_PA
35             = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
36             ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "1.2.3.4/32"));
37     @Mock
38     private BGPRouteEntryExportParameters exportParameters;
39     private List<Statement> defaultExportStatements;
40     private PolicyRIBBaseParametersImpl baseAttributes;
41
42     @Before
43     @Override
44     public void setUp() throws Exception {
45         super.setUp();
46         this.defaultExportStatements = loadStatement("default-odl-export-policy");
47         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
48     }
49
50     /**
51      * To eBGP.
52      */
53     @Test
54     public void testToEbgp() {
55         final Statement statement = getStatementAndSetToRole("to-external", PeerRole.Ebgp);
56
57         final Attributes expectedOutput = createPathInputWithAs();
58         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createPathInput(null));
59
60         assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, expectedOutput);
61         assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, expectedOutput);
62         assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, expectedOutput);
63         assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, expectedOutput);
64     }
65
66
67     /**
68      * From iBGP To iBGP.
69      */
70     @Test
71     public void testFromInternalToInternal() {
72         final Statement statement = getStatementAndSetToRole("from-internal-to-internal", PeerRole.Ibgp);
73         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
74         assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, null);
75     }
76
77     /**
78      * From iBGP To iBGP.
79      */
80     @Test
81     public void testFromExternalToInternal() {
82         final Statement statement = getStatementAndSetToRole("from-external-to-internal", PeerRole.Ibgp);
83         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
84         assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, attributeContainer.getAttributes());
85     }
86
87     /**
88      * From Internal To iBGP.
89      */
90     @Test
91     public void testFromOdlInternalToInternal() {
92         final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client",
93                 PeerRole.Ibgp);
94         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
95         assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, createClusterInput());
96     }
97
98     /**
99      * From RR-Client To iBGP.
100      */
101     @Test
102     public void testFromRRclientToInternal() {
103         final Statement statement = getStatementAndSetToRole("from-rr-client-to-internal", PeerRole.Ibgp);
104         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
105         assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, createInputWithOriginator());
106     }
107
108     /**
109      * Any role To Internal.
110      */
111     @Test
112     public void testOdlInternal() {
113         final Statement statement = getStatementAndSetToRole("to-odl-internal", PeerRole.Internal);
114         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
115
116         assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, null);
117         assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, null);
118         assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, null);
119         assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, null);
120     }
121
122     /**
123      * To RrClient.
124      */
125     @Test
126     public void testFromExternalToRRClient() {
127         final Statement statement = getStatementAndSetToRole("from-external-to-route-reflector", PeerRole.RrClient);
128         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
129         assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, attributeContainer.getAttributes());
130     }
131
132     /**
133      * To RrClient.
134      */
135     @Test
136     public void testFromInternalOrRRClientToRRClient() {
137         final Statement statement = getStatementAndSetToRole("from-internal-or-rr-client-to-route-reflector",
138                 PeerRole.RrClient);
139         final Attributes expectedOutput = createInputWithOriginator();
140         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
141
142         assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, expectedOutput);
143         assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, expectedOutput);
144     }
145
146     /**
147      * To RrClient.
148      */
149     @Test
150     public void testFromOdlInternalRRClient() {
151         final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client",
152                 PeerRole.RrClient);
153         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
154         assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, createClusterInput());
155     }
156
157     private Statement getStatementAndSetToRole(final String statementName, final PeerRole toPeerRole) {
158         doReturn(toPeerRole).when(this.exportParameters).getToPeerRole();
159         return this.defaultExportStatements.stream()
160                 .filter(st -> st.getName().equals(statementName)).findFirst().get();
161     }
162
163     private void assertApplyExportStatement(
164             final Statement statement, final PeerRole fromPeerRole,
165             final RouteAttributeContainer attInput,
166             final Attributes attExpected) {
167         doReturn(fromPeerRole).when(this.exportParameters).getFromPeerRole();
168
169         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
170                 this.baseAttributes,
171                 this.exportParameters,
172                 attInput,
173                 statement);
174         assertEquals(attExpected, result.getAttributes());
175     }
176 }