Mass-convert all compontents to use -no-zone addresses
[bgpcep.git] / bgp / extensions / route-target / src / test / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / route / policy / ClientAttributePrependHandlerTest.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.route.targetcontrain.impl.route.policy;
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
14 import java.util.Collections;
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.spi.AbstractStatementRegistryTest;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
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.protocol.bgp.route.targetcontrain.impl.activators.StatementActivator;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
33
34 public class ClientAttributePrependHandlerTest extends AbstractStatementRegistryTest {
35     @Mock
36     private BGPRouteEntryExportParameters exportParameters;
37     private List<Statement> basicStatements;
38     @Mock
39     private RouteEntryBaseAttributes baseAttributes;
40
41     @Before
42     @Override
43     public void setUp() throws Exception {
44         super.setUp();
45         final StatementActivator act = new StatementActivator();
46         act.start(this.statementRegistry);
47         this.basicStatements = loadStatement("basic-statements-test");
48         doReturn(CLUSTER).when(this.baseAttributes).getClusterId();
49         doReturn(LOCAL_AS).when(this.baseAttributes).getLocalAs();
50         doReturn(IPV4).when(this.baseAttributes).getOriginatorId();
51     }
52
53
54     @Test
55     public void testPreprendClientAttribute() {
56         Statement statement = this.basicStatements.stream()
57                 .filter(st -> st.getName().equals("client-attribute-append-test")).findFirst().get();
58         final Attributes att = new AttributesBuilder()
59                 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
60                         .setGlobal(IPV4).build()).build())
61                 .build();
62         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(att);
63         doReturn(Collections.emptyList()).when(this.exportParameters).getClientRouteTargetContrainCache();
64
65         RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
66                 this.baseAttributes,
67                 IPV4UNICAST.class,
68                 this.exportParameters,
69                 attributeContainer,
70                 statement);
71         assertEquals(att, result.getAttributes());
72
73         final Attributes expected = new AttributesBuilder()
74                 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
75                         .setGlobal(new Ipv4AddressNoZone("2.2.2.2")).build()).build())
76                 .build();
77         final String rk = "testRoute";
78         final Route rtRoute = new RouteTargetConstrainRouteBuilder()
79                 .setRouteKey(rk)
80                 .setAttributes(expected)
81                 .build();
82         doReturn(Collections.singletonList(rtRoute)).when(this.exportParameters).getClientRouteTargetContrainCache();
83         doReturn(rk).when(this.exportParameters).getRouteKey();
84
85         result = this.statementRegistry.applyExportStatement(
86                 this.baseAttributes,
87                 IPV4UNICAST.class,
88                 this.exportParameters,
89                 attributeContainer,
90                 statement);
91         assertEquals(expected, result.getAttributes());
92     }
93
94 }