Route Constrain policies
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / SetLocalAddressAsNextHopHandler.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 org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
12 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionAugPolicy;
13 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
14 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
19 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;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.SetLocalAddressAsNextHop;
21
22 /**
23  * Local Address shall be set as next Hop.
24  * https://tools.ietf.org/html/rfc4684
25  *
26  * @author Claudio D. Gasparini
27  */
28 public final class SetLocalAddressAsNextHopHandler implements BgpActionAugPolicy<SetLocalAddressAsNextHop> {
29     private static final SetLocalAddressAsNextHopHandler INSTANCE = new SetLocalAddressAsNextHopHandler();
30
31     private SetLocalAddressAsNextHopHandler() {
32
33     }
34
35     public static SetLocalAddressAsNextHopHandler getInstance() {
36         return INSTANCE;
37     }
38
39     @Override
40     public Attributes applyImportAction(
41             final RouteEntryBaseAttributes routeEntryInfo,
42             final BGPRouteEntryImportParameters routeBaseParameters,
43             final Attributes attributes,
44             final SetLocalAddressAsNextHop actions) {
45         return setNextHop(attributes, routeEntryInfo.getOriginatorId());
46     }
47
48     private Attributes setNextHop(final Attributes attributes, final Ipv4Address localAddress) {
49         return new AttributesBuilder(attributes)
50                 .setCNextHop(new Ipv4NextHopCaseBuilder()
51                         .setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(localAddress).build())
52                         .build()).build();
53     }
54
55     @Override
56     public Attributes applyExportAction(
57             final RouteEntryBaseAttributes routeEntryInfo,
58             final BGPRouteEntryExportParameters exportParameters,
59             final Attributes attributes,
60             final SetLocalAddressAsNextHop actions) {
61         return setNextHop(attributes, routeEntryInfo.getOriginatorId());
62     }
63 }