630d7eefa2dff752237f7c99c76b6421cfc790bb
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / PrefixMatchTest.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 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createClusterInput;
19
20 import com.google.common.collect.ImmutableMap;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
26 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
27 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32
33 public class PrefixMatchTest extends AbstractStatementRegistryConsumerTest {
34     @Mock
35     private BGPRouteEntryExportParameters exportParameters;
36     private List<Statement> basicStatements;
37     private PolicyRIBBaseParametersImpl baseAttributes;
38
39     @Before
40     @Override
41     public void setUp() throws Exception {
42         super.setUp();
43         this.basicStatements = loadStatement("basic-statements-test");
44         this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
45     }
46
47     @Test
48     public void testPrefixRange() {
49         //RANGE
50         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
51                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22")))
52                 .when(this.exportParameters).getRouteId();
53         Statement statement = this.basicStatements.stream()
54                 .filter(st -> st.getName().equals("reject-prefix-test")).findFirst().get();
55         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
56         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
57                 this.baseAttributes,
58                 this.exportParameters,
59                 attributeContainer,
60                 statement);
61         assertNull(result.getAttributes());
62
63         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
64                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "14.3.191.0/22")))
65                 .when(this.exportParameters).getRouteId();
66         result = this.statementRegistry.applyExportStatement(
67                 this.baseAttributes,
68                 this.exportParameters,
69                 attributeContainer,
70                 statement);
71         assertNotNull(result.getAttributes());
72     }
73
74     @Test
75     public void testPrefixExact() {
76         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
77         Statement statement = this.basicStatements.stream()
78                 .filter(st -> st.getName().equals("reject-prefix-test")).findFirst().get();
79
80         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
81                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.192.0/21")))
82                 .when(this.exportParameters).getRouteId();
83         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
84                 this.baseAttributes,
85                 this.exportParameters,
86                 attributeContainer,
87                 statement);
88         assertNull(result.getAttributes());
89
90         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
91                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "11.3.192.0/21")))
92                 .when(this.exportParameters).getRouteId();
93         result = this.statementRegistry.applyExportStatement(
94                 this.baseAttributes,
95                 this.exportParameters,
96                 attributeContainer,
97                 statement);
98         assertNotNull(result.getAttributes());
99     }
100
101     @Test
102     public void testPrefixInverse() {
103         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput());
104
105         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
106                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.192.0/21")))
107                 .when(this.exportParameters).getRouteId();
108         final Statement statement = this.basicStatements.stream()
109                 .filter(st -> st.getName().equals("reject-prefix-inverse-test")).findFirst().get();
110         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
111                 this.baseAttributes,
112                 this.exportParameters,
113                 attributeContainer,
114                 statement);
115         assertNotNull(result.getAttributes());
116
117         doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME,
118                 ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "11.3.192.0/21")))
119                 .when(this.exportParameters).getRouteId();
120         result = this.statementRegistry.applyExportStatement(
121                 this.baseAttributes,
122                 this.exportParameters,
123                 attributeContainer,
124                 statement);
125         assertNull(result.getAttributes());
126     }
127 }