2d4d0084c9667fd2556b8d6f2bb7cc9d07853586
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / MatchBgpNeighborSetTest.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
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
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.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
26
27 public class MatchBgpNeighborSetTest extends AbstractStatementRegistryConsumerTest {
28     @Mock
29     private BGPRouteEntryExportParameters exportParameters;
30     private List<Statement> basicStatements;
31     private PolicyRIBBaseParametersImpl baseAttributes;
32
33     @Before
34     @Override
35     public void setUp() throws Exception {
36         super.setUp();
37         this.basicStatements = loadStatement("bgp-neighbor-statements-test");
38         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
39     }
40
41
42     @Test
43     public void testMatchFromBgpNeighborAny() {
44         Statement statement = this.basicStatements.stream()
45                 .filter(st -> st.getName().equals("reject-from-neighbor-test")).findFirst().get();
46         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
47                 new AttributesBuilder().build());
48
49         doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getFromPeerId();
50
51         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
52                 this.baseAttributes,
53                 this.exportParameters,
54                 attributeContainer,
55                 statement);
56         assertNotNull(result.getAttributes());
57
58
59         doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getFromPeerId();
60         result = this.statementRegistry.applyExportStatement(
61                 this.baseAttributes,
62                 this.exportParameters,
63                 attributeContainer,
64                 statement);
65         assertNull(result.getAttributes());
66     }
67
68     @Test
69     public void testMatchFromBgpNeighborInvert() {
70         Statement statement = this.basicStatements.stream()
71                 .filter(st -> st.getName().equals("reject-from-neighbor-invert-test")).findFirst().get();
72         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
73                 new AttributesBuilder().build());
74
75         doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getFromPeerId();
76
77         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
78                 this.baseAttributes,
79                 this.exportParameters,
80                 attributeContainer,
81                 statement);
82         assertNull(result.getAttributes());
83
84         doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getFromPeerId();
85         result = this.statementRegistry.applyExportStatement(
86                 this.baseAttributes,
87                 this.exportParameters,
88                 attributeContainer,
89                 statement);
90         assertNotNull(result.getAttributes());
91     }
92
93     @Test
94     public void testMatchToBgpNeighborAny() {
95         Statement statement = this.basicStatements.stream()
96                 .filter(st -> st.getName().equals("reject-to-neighbor-test")).findFirst().get();
97         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
98                 new AttributesBuilder().build());
99
100         doReturn(new PeerId("bgp://127.0.0.2")).when(this.exportParameters).getFromPeerId();
101         doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getToPeerId();
102
103         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
104                 this.baseAttributes,
105                 this.exportParameters,
106                 attributeContainer,
107                 statement);
108         assertNotNull(result.getAttributes());
109
110
111         doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getToPeerId();
112         result = this.statementRegistry.applyExportStatement(
113                 this.baseAttributes,
114                 this.exportParameters,
115                 attributeContainer,
116                 statement);
117         assertNull(result.getAttributes());
118     }
119
120     @Test
121     public void testMatchToBgpNeighborInvert() {
122         Statement statement = this.basicStatements.stream()
123                 .filter(st -> st.getName().equals("reject-to-neighbor-invert-test")).findFirst().get();
124         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
125                 new AttributesBuilder().build());
126
127         doReturn(new PeerId("bgp://127.0.0.2")).when(this.exportParameters).getFromPeerId();
128         doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getToPeerId();
129
130         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
131                 this.baseAttributes,
132                 this.exportParameters,
133                 attributeContainer,
134                 statement);
135         assertNull(result.getAttributes());
136
137         doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getToPeerId();
138         result = this.statementRegistry.applyExportStatement(
139                 this.baseAttributes,
140                 this.exportParameters,
141                 attributeContainer,
142                 statement);
143         assertNotNull(result.getAttributes());
144     }
145 }