51ac4cde6c4940289980ea85f32f372f12af57d4
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / MatchExtComTest.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.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
15 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.CLUSTER;
16 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.IPV4;
17 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.LOCAL_AS;
18 import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.EncapsulationTunnelType.Vxlan;
19
20 import com.google.common.collect.ImmutableMap;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
28 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
29 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
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 MatchExtComTest extends AbstractStatementRegistryConsumerTest {
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("ext-community-statements-test");
54         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
55     }
56
57     @Test
58     public void testExtComAny() {
59         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
60                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
61                 .when(this.exportParameters).getRouteId();
62         Statement statement = this.basicStatements.stream()
63                 .filter(st -> st.getName().equals("ext-community-any-test")).findFirst().get();
64         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
65                 new AttributesBuilder().build());
66         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
67                 this.baseAttributes,
68                 this.exportParameters,
69                 attributeContainer,
70                 statement);
71         assertNotNull(result.getAttributes());
72
73
74         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
75                 .setExtendedCommunities(Collections.singletonList(new ExtendedCommunitiesBuilder()
76                         .setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
77                                 .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
78                                         .setAs4SpecificCommon(new As4SpecificCommonBuilder()
79                                                 .setAsNumber(AsNumber.getDefaultInstance("65000"))
80                                                 .setLocalAdministrator(123)
81                                                 .build()).build()).build()).build())).build());
82
83         result = this.statementRegistry.applyExportStatement(
84                 this.baseAttributes,
85                 this.exportParameters,
86                 attributeContainer,
87                 statement);
88         assertNull(result.getAttributes());
89     }
90
91     @Test
92     public void testExtComAll() {
93         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
94                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
95                 .when(this.exportParameters).getRouteId();
96         Statement statement = this.basicStatements.stream()
97                 .filter(st -> st.getName().equals("ext-community-all-test")).findFirst().get();
98         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
99                 new AttributesBuilder().build());
100         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
101                 this.baseAttributes,
102                 this.exportParameters,
103                 attributeContainer,
104                 statement);
105         assertNotNull(result.getAttributes());
106
107
108         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setExtendedCommunities(Arrays.asList(
109                 new ExtendedCommunitiesBuilder().setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
110                         .setAs4RouteOriginExtendedCommunity(
111                                 new As4RouteOriginExtendedCommunityBuilder()
112                                         .setAs4SpecificCommon(new As4SpecificCommonBuilder()
113                                                 .setAsNumber(AsNumber.getDefaultInstance("65000"))
114                                                 .setLocalAdministrator(123)
115                                                 .build()).build()).build()).build(),
116                 new ExtendedCommunitiesBuilder().setExtendedCommunity(new EncapsulationCaseBuilder()
117                         .setEncapsulationExtendedCommunity(new EncapsulationExtendedCommunityBuilder()
118                                 .setTunnelType(Vxlan).build()).build()).build())).build());
119
120         result = this.statementRegistry.applyExportStatement(
121                 this.baseAttributes,
122                 this.exportParameters,
123                 attributeContainer,
124                 statement);
125         assertNull(result.getAttributes());
126     }
127
128     @Test
129     public void testExtComInvert() {
130         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
131                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
132                 .when(this.exportParameters).getRouteId();
133         Statement statement = this.basicStatements.stream()
134                 .filter(st -> st.getName().equals("ext-community-invert-test")).findFirst().get();
135         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
136                 new AttributesBuilder().build());
137         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
138                 this.baseAttributes,
139                 this.exportParameters,
140                 attributeContainer,
141                 statement);
142         assertNull(result.getAttributes());
143
144         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
145                 .setExtendedCommunities(Collections.singletonList(new ExtendedCommunitiesBuilder()
146                         .setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
147                                 .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
148                                         .setAs4SpecificCommon(new As4SpecificCommonBuilder()
149                                                 .setAsNumber(AsNumber.getDefaultInstance("65000"))
150                                                 .setLocalAdministrator(123)
151                                                 .build()).build()).build()).build())).build());
152
153         result = this.statementRegistry.applyExportStatement(
154                 this.baseAttributes,
155                 this.exportParameters,
156                 attributeContainer,
157                 statement);
158         assertNotNull(result.getAttributes());
159     }
160 }