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