1a4bb8a3c95bb9395d9e2679c3ec84befa6ad02d
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / MatchAsPathSetTest.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
19 import com.google.common.collect.ImmutableMap;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
27 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
28 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
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.AsPathBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37
38 public class MatchAsPathSetTest extends AbstractStatementRegistryConsumerTest {
39     @Mock
40     private BGPRouteEntryExportParameters exportParameters;
41     private List<Statement> basicStatements;
42     private PolicyRIBBaseParametersImpl baseAttributes;
43
44     @Before
45     @Override
46     public void setUp() throws Exception {
47         super.setUp();
48         this.basicStatements = loadStatement("match-as-path-set-test");
49         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
50     }
51
52
53     @Test
54     public void testMatchAsPathAny() {
55         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
56                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
57                 .when(this.exportParameters).getRouteId();
58         Statement statement = this.basicStatements.stream()
59                 .filter(st -> st.getName().equals("reject-match-as-path-any-set")).findFirst().get();
60         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
61                 new AttributesBuilder().build());
62         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
63                 this.baseAttributes,
64                 this.exportParameters,
65                 attributeContainer,
66                 statement);
67         assertNotNull(result.getAttributes());
68
69
70         attributeContainer = routeAttributeContainerFalse(
71                 new AttributesBuilder()
72                         .setAsPath(new AsPathBuilder().setSegments(Collections.singletonList(
73                                 new SegmentsBuilder().setAsSequence(Collections.singletonList(
74                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
75         result = this.statementRegistry.applyExportStatement(
76                 this.baseAttributes,
77                 this.exportParameters,
78                 attributeContainer,
79                 statement);
80         assertNull(result.getAttributes());
81     }
82
83     @Test
84     public void testMatchAsPathAll() {
85         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
86                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
87                 .when(this.exportParameters).getRouteId();
88         Statement statement = this.basicStatements.stream()
89                 .filter(st -> st.getName().equals("reject-match-as-path-all-set")).findFirst().get();
90         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
91                 .setAsPath(new AsPathBuilder().setSegments(Collections.singletonList(
92                         new SegmentsBuilder().setAsSequence(Collections.singletonList(
93                                 AsNumber.getDefaultInstance("65"))).build())).build()).build());
94         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
95                 this.baseAttributes,
96                 this.exportParameters,
97                 attributeContainer,
98                 statement);
99         assertNotNull(result.getAttributes());
100
101
102         attributeContainer = routeAttributeContainerFalse(
103                 new AttributesBuilder()
104                         .setAsPath(new AsPathBuilder().setSegments(Arrays.asList(
105                                 new SegmentsBuilder().setAsSet(Arrays.asList(
106                                         AsNumber.getDefaultInstance("65"),
107                                         AsNumber.getDefaultInstance("64")
108                                 )).build(),
109                                 new SegmentsBuilder().setAsSet(Collections.singletonList(
110                                         AsNumber.getDefaultInstance("63")
111                                 )).build()
112                         )).build()).build());
113         result = this.statementRegistry.applyExportStatement(
114                 this.baseAttributes,
115                 this.exportParameters,
116                 attributeContainer,
117                 statement);
118         assertNull(result.getAttributes());
119     }
120
121     @Test
122     public void testMatchAsPathInverse() {
123         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
124                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
125                 .when(this.exportParameters).getRouteId();
126         Statement statement = this.basicStatements.stream()
127                 .filter(st -> st.getName().equals("reject-match-as-path-inverse-set")).findFirst().get();
128         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
129                 new AttributesBuilder()
130                         .setAsPath(new AsPathBuilder().setSegments(Collections.singletonList(
131                                 new SegmentsBuilder().setAsSequence(Collections.singletonList(
132                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
133         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
134                 this.baseAttributes,
135                 this.exportParameters,
136                 attributeContainer,
137                 statement);
138         assertNotNull(result.getAttributes());
139
140
141         attributeContainer = routeAttributeContainerFalse(
142                 new AttributesBuilder()
143                         .setAsPath(new AsPathBuilder().setSegments(Collections.singletonList(
144                                 new SegmentsBuilder().setAsSequence(Collections.singletonList(
145                                         AsNumber.getDefaultInstance("200"))).build())).build()).build());
146         result = this.statementRegistry.applyExportStatement(
147                 this.baseAttributes,
148                 this.exportParameters,
149                 attributeContainer,
150                 statement);
151         assertNull(result.getAttributes());
152     }
153 }