MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / openconfig-rp-spi / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / spi / AsPathLengthTest.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 java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
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.AsPathBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
28
29 public class AsPathLengthTest extends AbstractStatementRegistryTest {
30     @Mock
31     private BGPRouteEntryExportParameters exportParameters;
32     private List<Statement> basicStatements;
33     @Mock
34     private RouteEntryBaseAttributes baseAttributes;
35
36     @Before
37     @Override
38     public void setUp() throws Exception {
39         super.setUp();
40         this.basicStatements = loadStatement("basic-statements-test");
41         doReturn(CLUSTER).when(this.baseAttributes).getClusterId();
42         doReturn(LOCAL_AS).when(this.baseAttributes).getLocalAs();
43         doReturn(IPV4).when(this.baseAttributes).getOriginatorId();
44     }
45
46     @Test
47     public void testASPathLengthEq() {
48         final AsPathBuilder asPath = new AsPathBuilder();
49         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
50                 .setAsSequence(Arrays.asList(
51                         AsNumber.getDefaultInstance("1"),
52                         AsNumber.getDefaultInstance("2"),
53                         AsNumber.getDefaultInstance("3")))
54                 .build()));
55
56         Statement statement = this.basicStatements.stream()
57                 .filter(st -> st.getName().equals("as-path-eq-length-test")).findFirst().get();
58         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
59                 .setAsPath(asPath.build())
60                 .build());
61
62         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
63                 this.baseAttributes,
64                 this.exportParameters,
65                 attributeContainer,
66                 statement);
67         assertNotNull(result.getAttributes());
68
69         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
70                 .setAsSequence(Arrays.asList(
71                         AsNumber.getDefaultInstance("1"),
72                         AsNumber.getDefaultInstance("3")))
73                 .build()));
74
75         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
76         result = this.statementRegistry.applyExportStatement(
77                 this.baseAttributes,
78                 this.exportParameters,
79                 attributeContainer,
80                 statement);
81         assertNull(result.getAttributes());
82     }
83
84     @Test
85     public void testASPathLengthGe() {
86         final AsPathBuilder asPath = new AsPathBuilder();
87         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
88                 .setAsSequence(Arrays.asList(
89                         AsNumber.getDefaultInstance("1"),
90                         AsNumber.getDefaultInstance("2"),
91                         AsNumber.getDefaultInstance("3")))
92                 .build()));
93
94         Statement statement = this.basicStatements.stream()
95                 .filter(st -> st.getName().equals("as-path-ge-length-test")).findFirst().get();
96         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
97                 .setAsPath(asPath.build())
98                 .build());
99
100         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
101                 this.baseAttributes,
102                 this.exportParameters,
103                 attributeContainer,
104                 statement);
105         assertNull(result.getAttributes());
106
107         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
108                 .setAsSequence(Collections.singletonList(
109                         AsNumber.getDefaultInstance("3")))
110                 .build()));
111
112         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
113         result = this.statementRegistry.applyExportStatement(
114                 this.baseAttributes,
115                 this.exportParameters,
116                 attributeContainer,
117                 statement);
118         assertNotNull(result.getAttributes());
119     }
120
121     @Test
122     public void testASPathLengthLe() {
123         final AsPathBuilder asPath = new AsPathBuilder();
124         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
125                 .setAsSequence(Arrays.asList(
126                         AsNumber.getDefaultInstance("1"),
127                         AsNumber.getDefaultInstance("2"),
128                         AsNumber.getDefaultInstance("3")))
129                 .build()));
130
131         Statement statement = this.basicStatements.stream()
132                 .filter(st -> st.getName().equals("as-path-le-length-test")).findFirst().get();
133         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
134                 .setAsPath(asPath.build())
135                 .build());
136
137         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
138                 this.baseAttributes,
139                 this.exportParameters,
140                 attributeContainer,
141                 statement);
142         assertNotNull(result.getAttributes());
143
144         asPath.setSegments(Collections.singletonList(new SegmentsBuilder()
145                 .setAsSequence(Collections.singletonList(AsNumber.getDefaultInstance("3"))).build()));
146
147         attributeContainer = routeAttributeContainerFalse(new AttributesBuilder().setAsPath(asPath.build()).build());
148         result = this.statementRegistry.applyExportStatement(
149                 this.baseAttributes,
150                 this.exportParameters,
151                 attributeContainer,
152                 statement);
153         assertNull(result.getAttributes());
154     }
155 }