Migrate tests away from Optional.get()
[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     @Test
44     public void testMatchAsPathAny() {
45         Statement statement = basicStatements.stream()
46                 .filter(st -> st.getName().equals("reject-match-as-path-any-set")).findFirst().orElseThrow();
47         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
48                 new AttributesBuilder().build());
49         RouteAttributeContainer result = statementRegistry.applyExportStatement(
50                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
51         assertNotNull(result.getAttributes());
52
53
54         attributeContainer = routeAttributeContainerFalse(
55                 new AttributesBuilder()
56                         .setAsPath(new AsPathBuilder().setSegments(List.of(
57                                 new SegmentsBuilder().setAsSequence(List.of(
58                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
59         result = statementRegistry.applyExportStatement(
60                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
61         assertNull(result.getAttributes());
62     }
63
64     @Test
65     public void testMatchAsPathAll() {
66         Statement statement = basicStatements.stream()
67                 .filter(st -> st.getName().equals("reject-match-as-path-all-set")).findFirst().orElseThrow();
68         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(new AttributesBuilder()
69                 .setAsPath(new AsPathBuilder().setSegments(List.of(
70                         new SegmentsBuilder().setAsSequence(List.of(
71                                 AsNumber.getDefaultInstance("65"))).build())).build()).build());
72         RouteAttributeContainer result = statementRegistry.applyExportStatement(
73                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
74         assertNotNull(result.getAttributes());
75
76         attributeContainer = routeAttributeContainerFalse(
77                 new AttributesBuilder()
78                         .setAsPath(new AsPathBuilder().setSegments(List.of(
79                                 new SegmentsBuilder().setAsSet(Set.of(
80                                         AsNumber.getDefaultInstance("65"),
81                                         AsNumber.getDefaultInstance("64")
82                                 )).build(),
83                                 new SegmentsBuilder().setAsSet(Set.of(
84                                         AsNumber.getDefaultInstance("63")
85                                 )).build()
86                         )).build()).build());
87         result = statementRegistry.applyExportStatement(
88                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
89         assertNull(result.getAttributes());
90     }
91
92     @Test
93     public void testMatchAsPathInverse() {
94         Statement statement = basicStatements.stream()
95                 .filter(st -> st.getName().equals("reject-match-as-path-inverse-set")).findFirst().orElseThrow();
96         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
97                 new AttributesBuilder()
98                         .setAsPath(new AsPathBuilder().setSegments(List.of(
99                                 new SegmentsBuilder().setAsSequence(List.of(
100                                         AsNumber.getDefaultInstance("65"))).build())).build()).build());
101         RouteAttributeContainer result = statementRegistry.applyExportStatement(
102                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
103         assertNotNull(result.getAttributes());
104
105
106         attributeContainer = routeAttributeContainerFalse(
107                 new AttributesBuilder()
108                         .setAsPath(new AsPathBuilder().setSegments(List.of(
109                                 new SegmentsBuilder().setAsSequence(List.of(
110                                         AsNumber.getDefaultInstance("200"))).build())).build()).build());
111         result = statementRegistry.applyExportStatement(
112                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
113         assertNull(result.getAttributes());
114     }
115 }