2ff4f5a2979ddcd3806a16fd61343b5f27dad0a5
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / SetExtCommunityTest.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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.EncapsulationTunnelType.Vxlan;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
22 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
23 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ExtendedCommunitiesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommonBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.As4RouteOriginExtendedCommunityCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.EncapsulationCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.as._4.route.origin.extended.community._case.As4RouteOriginExtendedCommunityBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunityBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.opendaylight.yangtools.yang.common.Uint32;
38
39 public class SetExtCommunityTest extends AbstractStatementRegistryConsumerTest {
40     private final Attributes multipleExtCom = new AttributesBuilder().setExtendedCommunities(Arrays.asList(
41             new ExtendedCommunitiesBuilder().setExtendedCommunity(new EncapsulationCaseBuilder()
42                     .setEncapsulationExtendedCommunity(new EncapsulationExtendedCommunityBuilder()
43                             .setTunnelType(Vxlan).build()).build()).build(),
44             new ExtendedCommunitiesBuilder().setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
45                     .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
46                             .setAs4SpecificCommon(new As4SpecificCommonBuilder()
47                                     .setLocalAdministrator(Uint16.valueOf(123))
48                                     .setAsNumber(new AsNumber(Uint32.valueOf(65000))).build())
49                             .build()).build()).build())).build();
50     private final Attributes emptyExtCom = new AttributesBuilder()
51             .setExtendedCommunities(Collections.emptyList()).build();
52     @Mock
53     private BGPRouteEntryExportParameters exportParameters;
54     private List<Statement> basicStatements;
55     private PolicyRIBBaseParametersImpl baseAttributes;
56
57     @Before
58     @Override
59     public void setUp() throws Exception {
60         super.setUp();
61         basicStatements = loadStatement("set-ext-community-statements-test");
62         baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
63         doReturn(PeerRole.Ibgp).when(exportParameters).getFromPeerRole();
64     }
65
66     @Test
67     public void testInlineAdd() {
68         Statement statement = basicStatements.stream()
69                 .filter(st -> st.getName().equals("set-ext-community-inline-add-test")).findFirst().get();
70         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
71                 new AttributesBuilder().build());
72         RouteAttributeContainer result = statementRegistry.applyExportStatement(
73                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
74
75         assertEquals(multipleExtCom, result.getAttributes());
76     }
77
78     @Test
79     public void testInlineReplace() {
80         Statement statement = basicStatements.stream()
81                 .filter(st -> st.getName().equals("set-ext-community-inline-replace-test")).findFirst().get();
82         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
83                 new AttributesBuilder().build());
84         RouteAttributeContainer result = statementRegistry.applyExportStatement(
85                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
86
87         assertEquals(multipleExtCom, result.getAttributes());
88     }
89
90     @Test
91     public void testInlineRemove() {
92         Statement statement = basicStatements.stream()
93                 .filter(st -> st.getName().equals("set-ext-community-inline-remove-test")).findFirst().get();
94
95         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleExtCom);
96         RouteAttributeContainer result = statementRegistry.applyExportStatement(
97                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
98
99         assertEquals(emptyExtCom, result.getAttributes());
100     }
101
102     @Test
103     public void testReferenceAdd() {
104         Statement statement = basicStatements.stream()
105                 .filter(st -> st.getName().equals("set-ext-community-reference-add-test")).findFirst().get();
106         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
107                 new AttributesBuilder().build());
108         RouteAttributeContainer result = statementRegistry.applyExportStatement(
109                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
110
111         assertEquals(multipleExtCom, result.getAttributes());
112     }
113
114     @Test
115     public void testReferenceReplace() {
116         Statement statement = basicStatements.stream()
117                 .filter(st -> st.getName().equals("set-ext-community-reference-replace-test")).findFirst().get();
118         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
119                 new AttributesBuilder().build());
120         RouteAttributeContainer result = statementRegistry.applyExportStatement(
121                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
122
123         assertEquals(multipleExtCom, result.getAttributes());
124     }
125
126     @Test
127     public void testReferenceRemove() {
128         Statement statement = basicStatements.stream()
129                 .filter(st -> st.getName().equals("set-ext-community-reference-remove-test")).findFirst().get();
130
131         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleExtCom);
132         RouteAttributeContainer result = statementRegistry.applyExportStatement(
133                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
134
135         assertEquals(emptyExtCom, result.getAttributes());
136     }
137 }