5e4e0c27e8ba08652c3a6a626447b60fa2a00050
[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 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.List;
15 import java.util.Set;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
21 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
22 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
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.rev200120.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.SegmentsBuilder;
28
29 public class MatchAsPathSetTest extends AbstractStatementRegistryConsumerTest {
30     @Mock
31     private BGPRouteEntryExportParameters exportParameters;
32     private List<Statement> basicStatements;
33     private PolicyRIBBaseParametersImpl baseAttributes;
34
35     @Before
36     @Override
37     public void setUp() throws Exception {
38         super.setUp();
39         basicStatements = loadStatement("match-as-path-set-test");
40         baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
41     }
42
43
44     @Test
45     public void testMatchAsPathAny() {
46         Statement statement = basicStatements.stream()
47                 .filter(st -> st.getName().equals("reject-match-as-path-any-set")).findFirst().get();
48         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
49                 new AttributesBuilder().build());
50         RouteAttributeContainer result = statementRegistry.applyExportStatement(
51                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
52         assertNotNull(result.getAttributes());
53
54
55         attributeContainer = routeAttributeContainerFalse(
56                 new AttributesBuilder()
57                         .setAsPath(new AsPathBuilder().setSegments(List.of(
58                                 new SegmentsBuilder().setAsSequence(List.of(
59                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
60         result = statementRegistry.applyExportStatement(
61                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
62         assertNull(result.getAttributes());
63     }
64
65     @Test
66     public void testMatchAsPathAll() {
67         Statement statement = basicStatements.stream()
68                 .filter(st -> st.getName().equals("reject-match-as-path-all-set")).findFirst().get();
69         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
70                 .setAsPath(new AsPathBuilder().setSegments(List.of(
71                         new SegmentsBuilder().setAsSequence(List.of(
72                                 AsNumber.getDefaultInstance("65"))).build())).build()).build());
73         RouteAttributeContainer result = statementRegistry.applyExportStatement(
74                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
75         assertNotNull(result.getAttributes());
76
77         attributeContainer = routeAttributeContainerFalse(
78                 new AttributesBuilder()
79                         .setAsPath(new AsPathBuilder().setSegments(List.of(
80                                 new SegmentsBuilder().setAsSet(Set.of(
81                                         AsNumber.getDefaultInstance("65"),
82                                         AsNumber.getDefaultInstance("64")
83                                 )).build(),
84                                 new SegmentsBuilder().setAsSet(Set.of(
85                                         AsNumber.getDefaultInstance("63")
86                                 )).build()
87                         )).build()).build());
88         result = statementRegistry.applyExportStatement(
89                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
90         assertNull(result.getAttributes());
91     }
92
93     @Test
94     public void testMatchAsPathInverse() {
95         Statement statement = basicStatements.stream()
96                 .filter(st -> st.getName().equals("reject-match-as-path-inverse-set")).findFirst().get();
97         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
98                 new AttributesBuilder()
99                         .setAsPath(new AsPathBuilder().setSegments(List.of(
100                                 new SegmentsBuilder().setAsSequence(List.of(
101                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
102         RouteAttributeContainer result = statementRegistry.applyExportStatement(
103                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
104         assertNotNull(result.getAttributes());
105
106
107         attributeContainer = routeAttributeContainerFalse(
108                 new AttributesBuilder()
109                         .setAsPath(new AsPathBuilder().setSegments(List.of(
110                                 new SegmentsBuilder().setAsSequence(List.of(
111                                         AsNumber.getDefaultInstance("200"))).build())).build()).build());
112         result = statementRegistry.applyExportStatement(
113                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
114         assertNull(result.getAttributes());
115     }
116 }