Migrate tests away from Optional.get()
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / SetExtCommunityTest.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.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
13 import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.EncapsulationTunnelType.Vxlan;
14
15 import java.util.List;
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.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ExtendedCommunitiesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.as._4.spec.common.As4SpecificCommonBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.As4RouteOriginExtendedCommunityCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.EncapsulationCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.as._4.route.origin.extended.community._case.As4RouteOriginExtendedCommunityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunityBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint16;
35 import org.opendaylight.yangtools.yang.common.Uint32;
36
37 public class SetExtCommunityTest extends AbstractStatementRegistryConsumerTest {
38     private final Attributes multipleExtCom = new AttributesBuilder().setExtendedCommunities(List.of(
39             new ExtendedCommunitiesBuilder().setExtendedCommunity(new EncapsulationCaseBuilder()
40                     .setEncapsulationExtendedCommunity(new EncapsulationExtendedCommunityBuilder()
41                             .setTunnelType(Vxlan).build()).build()).build(),
42             new ExtendedCommunitiesBuilder().setExtendedCommunity(new As4RouteOriginExtendedCommunityCaseBuilder()
43                     .setAs4RouteOriginExtendedCommunity(new As4RouteOriginExtendedCommunityBuilder()
44                             .setAs4SpecificCommon(new As4SpecificCommonBuilder()
45                                     .setLocalAdministrator(Uint16.valueOf(123))
46                                     .setAsNumber(new AsNumber(Uint32.valueOf(65000))).build())
47                             .build()).build()).build())).build();
48     private final Attributes emptyExtCom = new AttributesBuilder().setExtendedCommunities(List.of()).build();
49     @Mock
50     private BGPRouteEntryExportParameters exportParameters;
51     private List<Statement> basicStatements;
52     private PolicyRIBBaseParametersImpl baseAttributes;
53
54     @Before
55     @Override
56     public void setUp() throws Exception {
57         super.setUp();
58         basicStatements = loadStatement("set-ext-community-statements-test");
59         baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
60         doReturn(PeerRole.Ibgp).when(exportParameters).getFromPeerRole();
61     }
62
63     @Test
64     public void testInlineAdd() {
65         Statement statement = basicStatements.stream()
66                 .filter(st -> st.getName().equals("set-ext-community-inline-add-test")).findFirst().orElseThrow();
67         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
68                 new AttributesBuilder().build());
69         RouteAttributeContainer result = statementRegistry.applyExportStatement(
70                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
71
72         assertEquals(multipleExtCom, result.getAttributes());
73     }
74
75     @Test
76     public void testInlineReplace() {
77         Statement statement = basicStatements.stream()
78                 .filter(st -> st.getName().equals("set-ext-community-inline-replace-test")).findFirst().orElseThrow();
79         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
80                 new AttributesBuilder().build());
81         RouteAttributeContainer result = statementRegistry.applyExportStatement(
82                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
83
84         assertEquals(multipleExtCom, result.getAttributes());
85     }
86
87     @Test
88     public void testInlineRemove() {
89         Statement statement = basicStatements.stream()
90                 .filter(st -> st.getName().equals("set-ext-community-inline-remove-test")).findFirst().orElseThrow();
91
92         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleExtCom);
93         RouteAttributeContainer result = statementRegistry.applyExportStatement(
94                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
95
96         assertEquals(emptyExtCom, result.getAttributes());
97     }
98
99     @Test
100     public void testReferenceAdd() {
101         Statement statement = basicStatements.stream()
102                 .filter(st -> st.getName().equals("set-ext-community-reference-add-test")).findFirst().orElseThrow();
103         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
104                 new AttributesBuilder().build());
105         RouteAttributeContainer result = statementRegistry.applyExportStatement(
106                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
107
108         assertEquals(multipleExtCom, result.getAttributes());
109     }
110
111     @Test
112     public void testReferenceReplace() {
113         Statement statement = basicStatements.stream()
114             .filter(st -> st.getName().equals("set-ext-community-reference-replace-test")).findFirst().orElseThrow();
115         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
116                 new AttributesBuilder().build());
117         RouteAttributeContainer result = statementRegistry.applyExportStatement(
118                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
119
120         assertEquals(multipleExtCom, result.getAttributes());
121     }
122
123     @Test
124     public void testReferenceRemove() {
125         Statement statement = basicStatements.stream()
126             .filter(st -> st.getName().equals("set-ext-community-reference-remove-test")).findFirst().orElseThrow();
127
128         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleExtCom);
129         RouteAttributeContainer result = statementRegistry.applyExportStatement(
130                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
131
132         assertEquals(emptyExtCom, result.getAttributes());
133     }
134 }