97be0e67c016225f47b4fc26a8aa848858ade589
[bgpcep.git] / bgp / openconfig-rp-spi / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / spi / AsPathLength.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.spi;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
14
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
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.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33
34 public class AsPathLength extends AbstractStatementRegistryTest {
35     @Mock
36     private BGPRouteEntryExportParameters exportParameters;
37     private List<Statement> basicStatements;
38     @Mock
39     private RouteEntryBaseAttributes baseAttributes;
40
41     @Before
42     @Override
43     public void setUp() throws Exception {
44         super.setUp();
45         this.basicStatements = loadStatement("basic-statements-test");
46         doReturn(CLUSTER).when(this.baseAttributes).getClusterId();
47         doReturn(LOCAL_AS).when(this.baseAttributes).getLocalAs();
48         doReturn(IPV4).when(this.baseAttributes).getOriginatorId();
49     }
50
51     @Test
52     public void testASPathLengthEq() {
53         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
54                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
55                 .when(this.exportParameters).getRouteId();
56         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
57
58         final AsPathBuilder asPath = new AsPathBuilder();
59         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
60                 .setAsSequence(Arrays.asList(
61                         AsNumber.getDefaultInstance("1"),
62                         AsNumber.getDefaultInstance("2"),
63                         AsNumber.getDefaultInstance("3")))
64                 .build()));
65
66         Statement statement = this.basicStatements.stream()
67                 .filter(st -> st.getName().equals("as-path-eq-length-test")).findFirst().get();
68         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
69                 .setAsPath(asPath.build())
70                 .build());
71
72         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
73                 this.baseAttributes,
74                 this.exportParameters,
75                 attributeContainer,
76                 statement);
77         assertNotNull(result.getAttributes());
78
79         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
80                 .setAsSequence(Arrays.asList(
81                         AsNumber.getDefaultInstance("1"),
82                         AsNumber.getDefaultInstance("3")))
83                 .build()));
84
85         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
86         result = this.statementRegistry.applyExportStatement(
87                 this.baseAttributes,
88                 this.exportParameters,
89                 attributeContainer,
90                 statement);
91         assertNull(result.getAttributes());
92     }
93
94     @Test
95     public void testASPathLengthGe() {
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         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
100
101         final AsPathBuilder asPath = new AsPathBuilder();
102         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
103                 .setAsSequence(Arrays.asList(
104                         AsNumber.getDefaultInstance("1"),
105                         AsNumber.getDefaultInstance("2"),
106                         AsNumber.getDefaultInstance("3")))
107                 .build()));
108
109         Statement statement = this.basicStatements.stream()
110                 .filter(st -> st.getName().equals("as-path-ge-length-test")).findFirst().get();
111         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
112                 .setAsPath(asPath.build())
113                 .build());
114
115         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
116                 this.baseAttributes,
117                 this.exportParameters,
118                 attributeContainer,
119                 statement);
120         assertNull(result.getAttributes());
121
122         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
123                 .setAsSequence(Arrays.asList(
124                         AsNumber.getDefaultInstance("3")))
125                 .build()));
126
127         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
128         result = this.statementRegistry.applyExportStatement(
129                 this.baseAttributes,
130                 this.exportParameters,
131                 attributeContainer,
132                 statement);
133         assertNotNull(result.getAttributes());
134     }
135
136     @Test
137     public void testASPathLengthLe() {
138         doReturn(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME,
139                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
140                 .when(this.exportParameters).getRouteId();
141         doReturn(PeerRole.Ibgp).when(this.exportParameters).getFromPeerRole();
142
143         final AsPathBuilder asPath = new AsPathBuilder();
144         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
145                 .setAsSequence(Arrays.asList(
146                         AsNumber.getDefaultInstance("1"),
147                         AsNumber.getDefaultInstance("2"),
148                         AsNumber.getDefaultInstance("3")))
149                 .build()));
150
151         Statement statement = this.basicStatements.stream()
152                 .filter(st -> st.getName().equals("as-path-le-length-test")).findFirst().get();
153         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
154                 .setAsPath(asPath.build())
155                 .build());
156
157         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
158                 this.baseAttributes,
159                 this.exportParameters,
160                 attributeContainer,
161                 statement);
162         assertNotNull(result.getAttributes());
163
164         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
165                 .setAsSequence(Arrays.asList(
166                         AsNumber.getDefaultInstance("3")))
167                 .build()));
168
169         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
170         result = this.statementRegistry.applyExportStatement(
171                 this.baseAttributes,
172                 this.exportParameters,
173                 attributeContainer,
174                 statement);
175         assertNull(result.getAttributes());
176     }
177 }