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