Provide Add Path support for all AFI/SAFI
[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.rev130919.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.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as._4.spec.common.As4SpecificCommonBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.As4RouteOriginExtendedCommunityCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.EncapsulationCaseBuilder;
33 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;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunityBuilder;
35
36 public class SetExtCommunityTest extends AbstractStatementRegistryConsumerTest {
37     private final Attributes multipleExtCom = new AttributesBuilder().setExtendedCommunities(Arrays.asList(
38             new ExtendedCommunitiesBuilder().setExtendedCommunity(new EncapsulationCaseBuilder()
39                     .setEncapsulationExtendedCommunity(new EncapsulationExtendedCommunityBuilder()
40                             .setTunnelType(Vxlan).build()).build()).build(),
41             new ExtendedCommunitiesBuilder().setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
42                     .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
43                             .setAs4SpecificCommon(new As4SpecificCommonBuilder()
44                                     .setLocalAdministrator(123)
45                                     .setAsNumber(new AsNumber(65000L)).build())
46                             .build()).build()).build())).build();
47     private final Attributes emptyExtCom = new AttributesBuilder()
48             .setExtendedCommunities(Collections.emptyList()).build();
49     @Mock
50     private BGPRouteEntryExportParameters exportParameters;
51     private List<Statement> basicStatements;
52     private PolicyRIBBaseParametersImpl baseAttributes;
53
54     @Before
55     @Override
56     public void setUp() throws Exception {
57         super.setUp();
58         this.basicStatements = loadStatement("set-ext-community-statements-test");
59         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
60         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
61     }
62
63     @Test
64     public void testInlineAdd() {
65         Statement statement = this.basicStatements.stream()
66                 .filter(st -> st.getName().equals("set-ext-community-inline-add-test")).findFirst().get();
67         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
68                 new AttributesBuilder().build());
69         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
70                 this.baseAttributes,
71                 this.exportParameters,
72                 attributeContainer,
73                 statement);
74
75         assertEquals(this.multipleExtCom, result.getAttributes());
76     }
77
78     @Test
79     public void testInlineReplace() {
80         Statement statement = this.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 = this.statementRegistry.applyExportStatement(
85                 this.baseAttributes,
86                 this.exportParameters,
87                 attributeContainer,
88                 statement);
89
90         assertEquals(this.multipleExtCom, result.getAttributes());
91     }
92
93     @Test
94     public void testInlineRemove() {
95         Statement statement = this.basicStatements.stream()
96                 .filter(st -> st.getName().equals("set-ext-community-inline-remove-test")).findFirst().get();
97
98         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleExtCom);
99         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
100                 this.baseAttributes,
101                 this.exportParameters,
102                 attributeContainer,
103                 statement);
104
105         assertEquals(this.emptyExtCom, result.getAttributes());
106     }
107
108     @Test
109     public void testReferenceAdd() {
110         Statement statement = this.basicStatements.stream()
111                 .filter(st -> st.getName().equals("set-ext-community-reference-add-test")).findFirst().get();
112         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
113                 new AttributesBuilder().build());
114         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
115                 this.baseAttributes,
116                 this.exportParameters,
117                 attributeContainer,
118                 statement);
119
120         assertEquals(this.multipleExtCom, result.getAttributes());
121     }
122
123     @Test
124     public void testReferenceReplace() {
125         Statement statement = this.basicStatements.stream()
126                 .filter(st -> st.getName().equals("set-ext-community-reference-replace-test")).findFirst().get();
127         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
128                 new AttributesBuilder().build());
129         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
130                 this.baseAttributes,
131                 this.exportParameters,
132                 attributeContainer,
133                 statement);
134
135         assertEquals(this.multipleExtCom, result.getAttributes());
136     }
137
138     @Test
139     public void testReferenceRemove() {
140         Statement statement = this.basicStatements.stream()
141                 .filter(st -> st.getName().equals("set-ext-community-reference-remove-test")).findFirst().get();
142
143         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleExtCom);
144         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
145                 this.baseAttributes,
146                 this.exportParameters,
147                 attributeContainer,
148                 statement);
149
150         assertEquals(this.emptyExtCom, result.getAttributes());
151     }
152 }