MVPN RFC6514 Extendend communities
[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.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
25
26 public class ImportDefaultStatementTest extends AbstractStatementRegistryConsumerTest {
27     @Mock
28     private BGPRouteEntryImportParameters importParameters;
29     private List<Statement> defaultImportStatements;
30     private PolicyRIBBaseParametersImpl baseAttributes;
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     @Test
41     public void testFromEbgp() {
42         final Statement statement = getStatement("from-external");
43
44         final RouteAttributeContainer attributeContainer
45                 = routeAttributeContainerFalse(ImportAttributeTestUtil.createInput());
46
47         assertApplyImportStatement(statement, PeerRole.Ebgp, attributeContainer,
48                 ImportAttributeTestUtil.createOutput());
49     }
50
51     @Test
52     public void testFromNonExternal() {
53         final Statement statement = getStatement("from-non-external");
54
55         final Attributes expected = ImportAttributeTestUtil.createInput();
56         final RouteAttributeContainer attributeContainer
57                 = routeAttributeContainerFalse(expected);
58
59         assertApplyImportStatement(statement, PeerRole.Ibgp, attributeContainer, expected);
60         assertApplyImportStatement(statement, PeerRole.RrClient, attributeContainer, expected);
61         assertApplyImportStatement(statement, PeerRole.Internal, attributeContainer, expected);
62     }
63
64     private Statement getStatement(final String statementName) {
65         return this.defaultImportStatements.stream()
66                 .filter(st -> st.getName().equals(statementName)).findFirst().get();
67     }
68
69     private void assertApplyImportStatement(
70             final Statement statement,
71             final PeerRole fromPeerRole,
72             final RouteAttributeContainer attInput,
73             final Attributes attExpected) {
74
75         doReturn(fromPeerRole).when(this.importParameters).getFromPeerRole();
76         doReturn(AS).when(this.importParameters).getFromPeerLocalAs();
77
78         RouteAttributeContainer result = this.statementRegistry.applyImportStatement(
79                 this.baseAttributes,
80                 this.importParameters,
81                 attInput,
82                 statement);
83         assertEquals(attExpected, result.getAttributes());
84     }
85 }