MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / MatchCommunityTest.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.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
13
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
21 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
22 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.CommunitiesBuilder;
27
28 public class MatchCommunityTest extends AbstractStatementRegistryConsumerTest {
29     @Mock
30     private BGPRouteEntryExportParameters exportParameters;
31     private List<Statement> basicStatements;
32     private PolicyRIBBaseParametersImpl baseAttributes;
33
34     @Before
35     @Override
36     public void setUp() throws Exception {
37         super.setUp();
38         this.basicStatements = loadStatement("community-statements-test");
39         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
40     }
41
42     @Test
43     public void testComAny() {
44         Statement statement = this.basicStatements.stream()
45                 .filter(st -> st.getName().equals("community-any-test")).findFirst().get();
46         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
47                 new AttributesBuilder().build());
48         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
49                 this.baseAttributes,
50                 this.exportParameters,
51                 attributeContainer,
52                 statement);
53         assertNotNull(result.getAttributes());
54
55         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setCommunities(
56                 Collections.singletonList(new CommunitiesBuilder()
57                         .setAsNumber(AsNumber.getDefaultInstance("65"))
58                         .setSemantics(10)
59                         .build())).build());
60
61         result = this.statementRegistry.applyExportStatement(
62                 this.baseAttributes,
63                 this.exportParameters,
64                 attributeContainer,
65                 statement);
66         assertNull(result.getAttributes());
67     }
68
69     @Test
70     public void testComInvert() {
71         Statement statement = this.basicStatements.stream()
72                 .filter(st -> st.getName().equals("community-invert-test")).findFirst().get();
73         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
74                 new AttributesBuilder().build());
75         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
76                 this.baseAttributes,
77                 this.exportParameters,
78                 attributeContainer,
79                 statement);
80         assertNull(result.getAttributes());
81
82         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setCommunities(
83                 Collections.singletonList(new CommunitiesBuilder()
84                         .setAsNumber(AsNumber.getDefaultInstance("65"))
85                         .setSemantics(10)
86                         .build())).build());
87
88         result = this.statementRegistry.applyExportStatement(
89                 this.baseAttributes,
90                 this.exportParameters,
91                 attributeContainer,
92                 statement);
93         assertNotNull(result.getAttributes());
94     }
95
96     @Test
97     public void testComAll() {
98         Statement statement = this.basicStatements.stream()
99                 .filter(st -> st.getName().equals("community-all-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         assertNotNull(result.getAttributes());
108
109         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setCommunities(Arrays.asList(
110                 new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("65"))
111                         .setSemantics(10).build(),
112                 new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("66"))
113                         .setSemantics(11).build())).build());
114
115         result = this.statementRegistry.applyExportStatement(
116                 this.baseAttributes,
117                 this.exportParameters,
118                 attributeContainer,
119                 statement);
120         assertNull(result.getAttributes());
121     }
122 }