2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement;
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;
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;
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"));
38 private BGPRouteEntryExportParameters exportParameters;
39 private List<Statement> defaultExportStatements;
40 private PolicyRIBBaseParametersImpl baseAttributes;
44 public void setUp() throws Exception {
46 this.defaultExportStatements = loadStatement("default-odl-export-policy");
47 this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
54 public void testToEbgp() {
55 final Statement statement = getStatementAndSetToRole("to-external", PeerRole.Ebgp);
57 final Attributes expectedOutput = createPathInputWithAs();
58 final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createPathInput(null));
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);
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);
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());
88 * From Internal To iBGP.
91 public void testFromOdlInternalToInternal() {
92 final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client",
94 final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
95 assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, createClusterInput());
99 * From RR-Client To iBGP.
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());
109 * Any role To Internal.
112 public void testOdlInternal() {
113 final Statement statement = getStatementAndSetToRole("to-odl-internal", PeerRole.Internal);
114 final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
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);
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());
136 public void testFromInternalOrRRClientToRRClient() {
137 final Statement statement = getStatementAndSetToRole("from-internal-or-rr-client-to-route-reflector",
139 final Attributes expectedOutput = createInputWithOriginator();
140 final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
142 assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, expectedOutput);
143 assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, expectedOutput);
150 public void testFromOdlInternalRRClient() {
151 final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client",
153 final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
154 assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, createClusterInput());
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();
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();
169 RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
171 this.exportParameters,
174 assertEquals(attExpected, result.getAttributes());