MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / SetCommunityTest.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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
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.rev180329.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.CommunitiesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
30
31 public class SetCommunityTest extends AbstractStatementRegistryConsumerTest {
32     private final Attributes multipleCom = new AttributesBuilder().setCommunities(Arrays.asList(
33             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("65")).setSemantics(10).build(),
34             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("66")).setSemantics(11).build()
35     )).build();
36     private final Attributes emptyCom = new AttributesBuilder().setCommunities(Collections.emptyList()).build();
37     @Mock
38     private BGPRouteEntryExportParameters exportParameters;
39     private List<Statement> basicStatements;
40     private PolicyRIBBaseParametersImpl baseAttributes;
41
42     @Before
43     @Override
44     public void setUp() throws Exception {
45         super.setUp();
46         this.basicStatements = loadStatement("set-community-statements-test");
47         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
48         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
49     }
50
51     @Test
52     public void testInlineAdd() {
53         Statement statement = this.basicStatements.stream()
54                 .filter(st -> st.getName().equals("set-community-inline-add-test")).findFirst().get();
55         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
56                 new AttributesBuilder().build());
57         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
58                 this.baseAttributes,
59                 this.exportParameters,
60                 attributeContainer,
61                 statement);
62
63         assertEquals(this.multipleCom, result.getAttributes());
64     }
65
66     @Test
67     public void testInlineReplace() {
68         Statement statement = this.basicStatements.stream()
69                 .filter(st -> st.getName().equals("set-community-inline-replace-test")).findFirst().get();
70         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
71                 new AttributesBuilder().build());
72         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
73                 this.baseAttributes,
74                 this.exportParameters,
75                 attributeContainer,
76                 statement);
77
78         assertEquals(this.multipleCom, result.getAttributes());
79     }
80
81     @Test
82     public void testInlineRemove() {
83         Statement statement = this.basicStatements.stream()
84                 .filter(st -> st.getName().equals("set-community-inline-remove-test")).findFirst().get();
85
86         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleCom);
87         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
88                 this.baseAttributes,
89                 this.exportParameters,
90                 attributeContainer,
91                 statement);
92
93         assertEquals(this.emptyCom, result.getAttributes());
94     }
95
96     @Test
97     public void testReferenceAdd() {
98         Statement statement = this.basicStatements.stream()
99                 .filter(st -> st.getName().equals("set-community-reference-add-test")).findFirst().get();
100         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
101                 new AttributesBuilder().build());
102         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
103                 this.baseAttributes,
104                 this.exportParameters,
105                 attributeContainer,
106                 statement);
107
108         assertEquals(this.multipleCom, result.getAttributes());
109     }
110
111     @Test
112     public void testReferenceReplace() {
113         Statement statement = this.basicStatements.stream()
114                 .filter(st -> st.getName().equals("set-community-reference-replace-test")).findFirst().get();
115         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
116                 new AttributesBuilder().build());
117         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
118                 this.baseAttributes,
119                 this.exportParameters,
120                 attributeContainer,
121                 statement);
122
123         assertEquals(this.multipleCom, result.getAttributes());
124     }
125
126     @Test
127     public void testReferenceRemove() {
128         Statement statement = this.basicStatements.stream()
129                 .filter(st -> st.getName().equals("set-community-reference-remove-test")).findFirst().get();
130
131         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleCom);
132         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
133                 this.baseAttributes,
134                 this.exportParameters,
135                 attributeContainer,
136                 statement);
137
138         assertEquals(this.emptyCom, result.getAttributes());
139     }
140 }