2ba1b3b3169c128fba9ae19c8b11366bdbbafdd8
[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 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.CLUSTER;
15 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.IPV4;
16 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.LOCAL_AS;
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.CommunitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37
38 public class SetCommunityTest extends AbstractStatementRegistryConsumerTest {
39     private final Attributes multipleCom = new AttributesBuilder().setCommunities(Arrays.asList(
40             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("65")).setSemantics(10).build(),
41             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("66")).setSemantics(11).build()
42     )).build();
43     private final Attributes emptyCom = new AttributesBuilder().setCommunities(Collections.emptyList()).build();
44     @Mock
45     private BGPRouteEntryExportParameters exportParameters;
46     private List<Statement> basicStatements;
47     private PolicyRIBBaseParametersImpl baseAttributes;
48
49     @Before
50     @Override
51     public void setUp() throws Exception {
52         super.setUp();
53         this.basicStatements = loadStatement("set-community-statements-test");
54         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
55         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
56     }
57
58     @Test
59     public void testInlineAdd() {
60         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
61                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
62                 .when(this.exportParameters).getRouteId();
63         Statement statement = this.basicStatements.stream()
64                 .filter(st -> st.getName().equals("set-community-inline-add-test")).findFirst().get();
65         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
66                 new AttributesBuilder().build());
67         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
68                 this.baseAttributes,
69                 this.exportParameters,
70                 attributeContainer,
71                 statement);
72
73         assertEquals(this.multipleCom, result.getAttributes());
74     }
75
76     @Test
77     public void testInlineReplace() {
78         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
79                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
80                 .when(this.exportParameters).getRouteId();
81         Statement statement = this.basicStatements.stream()
82                 .filter(st -> st.getName().equals("set-community-inline-replace-test")).findFirst().get();
83         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
84                 new AttributesBuilder().build());
85         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
86                 this.baseAttributes,
87                 this.exportParameters,
88                 attributeContainer,
89                 statement);
90
91         assertEquals(this.multipleCom, result.getAttributes());
92     }
93
94     @Test
95     public void testInlineRemove() {
96         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
97                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
98                 .when(this.exportParameters).getRouteId();
99         Statement statement = this.basicStatements.stream()
100                 .filter(st -> st.getName().equals("set-community-inline-remove-test")).findFirst().get();
101
102         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleCom);
103         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
104                 this.baseAttributes,
105                 this.exportParameters,
106                 attributeContainer,
107                 statement);
108
109         assertEquals(this.emptyCom, result.getAttributes());
110     }
111
112     @Test
113     public void testReferenceAdd() {
114         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
115                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
116                 .when(this.exportParameters).getRouteId();
117         Statement statement = this.basicStatements.stream()
118                 .filter(st -> st.getName().equals("set-community-reference-add-test")).findFirst().get();
119         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
120                 new AttributesBuilder().build());
121         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
122                 this.baseAttributes,
123                 this.exportParameters,
124                 attributeContainer,
125                 statement);
126
127         assertEquals(this.multipleCom, result.getAttributes());
128     }
129
130     @Test
131     public void testReferenceReplace() {
132         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
133                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
134                 .when(this.exportParameters).getRouteId();
135         Statement statement = this.basicStatements.stream()
136                 .filter(st -> st.getName().equals("set-community-reference-replace-test")).findFirst().get();
137         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
138                 new AttributesBuilder().build());
139         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
140                 this.baseAttributes,
141                 this.exportParameters,
142                 attributeContainer,
143                 statement);
144
145         assertEquals(this.multipleCom, result.getAttributes());
146     }
147
148     @Test
149     public void testReferenceRemove() {
150         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
151                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
152                 .when(this.exportParameters).getRouteId();
153         Statement statement = this.basicStatements.stream()
154                 .filter(st -> st.getName().equals("set-community-reference-remove-test")).findFirst().get();
155
156         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(this.multipleCom);
157         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
158                 this.baseAttributes,
159                 this.exportParameters,
160                 attributeContainer,
161                 statement);
162
163         assertEquals(this.emptyCom, result.getAttributes());
164     }
165 }