307a1ed191041f41eec97d6e498fe38767528f2f
[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.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 import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.EncapsulationTunnelType.Vxlan;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
26 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
27 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as._4.spec.common.As4SpecificCommonBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.As4RouteOriginExtendedCommunityCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.EncapsulationCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as._4.route.origin.extended.community._case.As4RouteOriginExtendedCommunityBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunityBuilder;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42
43 public class SetExtCommunityTest extends AbstractStatementRegistryConsumerTest {
44     private final Attributes multipleExtCom = new AttributesBuilder().setExtendedCommunities(Arrays.asList(
45             new ExtendedCommunitiesBuilder().setExtendedCommunity(new EncapsulationCaseBuilder()
46                     .setEncapsulationExtendedCommunity(new EncapsulationExtendedCommunityBuilder()
47                             .setTunnelType(Vxlan).build()).build()).build(),
48             new ExtendedCommunitiesBuilder().setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
49                     .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
50                             .setAs4SpecificCommon(new As4SpecificCommonBuilder()
51                                     .setLocalAdministrator(123)
52                                     .setAsNumber(new AsNumber(65000L)).build())
53                             .build()).build()).build())).build();
54     private final Attributes emptyExtCom = new AttributesBuilder()
55             .setExtendedCommunities(Collections.emptyList()).build();
56     @Mock
57     private BGPRouteEntryExportParameters exportParameters;
58     private List<Statement> basicStatements;
59     private PolicyRIBBaseParametersImpl baseAttributes;
60
61     @Before
62     @Override
63     public void setUp() throws Exception {
64         super.setUp();
65         this.basicStatements = loadStatement("set-ext-community-statements-test");
66         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
67         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
68     }
69
70     @Test
71     public void testInlineAdd() {
72         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
73                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
74                 .when(this.exportParameters).getRouteId();
75         Statement statement = this.basicStatements.stream()
76                 .filter(st -> st.getName().equals("set-ext-community-inline-add-test")).findFirst().get();
77         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
78                 new AttributesBuilder().build());
79         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
80                 this.baseAttributes,
81                 this.exportParameters,
82                 attributeContainer,
83                 statement);
84
85         assertEquals(this.multipleExtCom, result.getAttributes());
86     }
87
88     @Test
89     public void testInlineReplace() {
90         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
91                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
92                 .when(this.exportParameters).getRouteId();
93         Statement statement = this.basicStatements.stream()
94                 .filter(st -> st.getName().equals("set-ext-community-inline-replace-test")).findFirst().get();
95         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
96                 new AttributesBuilder().build());
97         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
98                 this.baseAttributes,
99                 this.exportParameters,
100                 attributeContainer,
101                 statement);
102
103         assertEquals(this.multipleExtCom, result.getAttributes());
104     }
105
106     @Test
107     public void testInlineRemove() {
108         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
109                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
110                 .when(this.exportParameters).getRouteId();
111         Statement statement = this.basicStatements.stream()
112                 .filter(st -> st.getName().equals("set-ext-community-inline-remove-test")).findFirst().get();
113
114         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleExtCom);
115         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
116                 this.baseAttributes,
117                 this.exportParameters,
118                 attributeContainer,
119                 statement);
120
121         assertEquals(this.emptyExtCom, result.getAttributes());
122     }
123
124     @Test
125     public void testReferenceAdd() {
126         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
127                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
128                 .when(this.exportParameters).getRouteId();
129         Statement statement = this.basicStatements.stream()
130                 .filter(st -> st.getName().equals("set-ext-community-reference-add-test")).findFirst().get();
131         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
132                 new AttributesBuilder().build());
133         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
134                 this.baseAttributes,
135                 this.exportParameters,
136                 attributeContainer,
137                 statement);
138
139         assertEquals(this.multipleExtCom, result.getAttributes());
140     }
141
142     @Test
143     public void testReferenceReplace() {
144         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
145                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
146                 .when(this.exportParameters).getRouteId();
147         Statement statement = this.basicStatements.stream()
148                 .filter(st -> st.getName().equals("set-ext-community-reference-replace-test")).findFirst().get();
149         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
150                 new AttributesBuilder().build());
151         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
152                 this.baseAttributes,
153                 this.exportParameters,
154                 attributeContainer,
155                 statement);
156
157         assertEquals(this.multipleExtCom, result.getAttributes());
158     }
159
160     @Test
161     public void testReferenceRemove() {
162         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
163                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
164                 .when(this.exportParameters).getRouteId();
165         Statement statement = this.basicStatements.stream()
166                 .filter(st -> st.getName().equals("set-ext-community-reference-remove-test")).findFirst().get();
167
168         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleExtCom);
169         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
170                 this.baseAttributes,
171                 this.exportParameters,
172                 attributeContainer,
173                 statement);
174
175         assertEquals(this.emptyExtCom, result.getAttributes());
176     }
177 }