2afe1e135ac355cd44e21e16fd6139083019e4a9
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / ImportDefaultStatementTest.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.CLUSTER;
14 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.IPV4;
15 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.LOCAL_AS;
16
17 import com.google.common.collect.ImmutableMap;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
23 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
24 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31
32 public class ImportDefaultStatementTest extends AbstractStatementRegistryConsumerTest {
33     private static final NodeIdentifierWithPredicates ROUTE_ID_PA
34             = new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
35             ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "1.2.3.4/32"));
36     @Mock
37     private BGPRouteEntryImportParameters importParameters;
38     private List<Statement> defaultImportStatements;
39     private PolicyRIBBaseParametersImpl baseAttributes;
40
41
42     @Before
43     @Override
44     public void setUp() throws Exception {
45         super.setUp();
46         this.defaultImportStatements = loadStatement("default-odl-import-policy");
47         doReturn(ROUTE_ID_PA).when(this.importParameters).getRouteId();
48         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
49     }
50
51     /**
52      * From eBGP.
53      */
54     @Test
55     public void testFromEbgp() {
56         final Statement statement = getStatement("from-external");
57
58         final RouteAttributeContainer attributeContainer
59                 = routeAttributeContainerFalse(ImportAttributeTestUtil.createInput());
60
61         assertApplyImportStatement(statement, PeerRole.Ebgp, attributeContainer,
62                 ImportAttributeTestUtil.createOutput());
63     }
64
65     /**
66      * From NonExternal.
67      */
68     @Test
69     public void testFromNonExternal() {
70         final Statement statement = getStatement("from-non-external");
71
72         final Attributes expected = ImportAttributeTestUtil.createInput();
73         final RouteAttributeContainer attributeContainer
74                 = routeAttributeContainerFalse(expected);
75
76         assertApplyImportStatement(statement, PeerRole.Ibgp, attributeContainer, expected);
77         assertApplyImportStatement(statement, PeerRole.RrClient, attributeContainer, expected);
78         assertApplyImportStatement(statement, PeerRole.Internal, attributeContainer, expected);
79     }
80
81     private Statement getStatement(final String statementName) {
82         return this.defaultImportStatements.stream()
83                 .filter(st -> st.getName().equals(statementName)).findFirst().get();
84     }
85
86     private void assertApplyImportStatement(
87             final Statement statement,
88             final PeerRole fromPeerRole,
89             final RouteAttributeContainer attInput,
90             final Attributes attExpected) {
91
92         doReturn(fromPeerRole).when(this.importParameters).getFromPeerRole();
93
94         RouteAttributeContainer result = this.statementRegistry.applyImportStatement(
95                 this.baseAttributes,
96                 this.importParameters,
97                 attInput,
98                 statement);
99         assertEquals(attExpected, result.getAttributes());
100     }
101 }