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