8dcc73356c6e6f452d44ca9bb4e99e7833c1847f
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / actions / SetOriginatorIdPrependHandler.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.actions;
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.message.rev180329.path.attributes.attributes.OriginatorIdBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.SetOriginatorIdPrepend;
20
21 /**
22  * Prepend Originator Id.
23  */
24 public final class SetOriginatorIdPrependHandler implements BgpActionAugPolicy<SetOriginatorIdPrepend> {
25     private static final SetOriginatorIdPrependHandler INSTANCE = new SetOriginatorIdPrependHandler();
26
27     private SetOriginatorIdPrependHandler() {
28
29     }
30
31     public static SetOriginatorIdPrependHandler getInstance() {
32         return INSTANCE;
33     }
34
35     @Override
36     public Attributes applyImportAction(
37             final RouteEntryBaseAttributes routeEntryInfo,
38             final BGPRouteEntryImportParameters routeEntryImportParameters,
39             final Attributes attributes,
40             final SetOriginatorIdPrepend bgpActions) {
41
42         final Ipv4Address defOri = bgpActions.getSetOriginatorIdPrepend().getOriginatorId();
43         return prependOriginatorId(attributes, defOri == null
44                 ? routeEntryInfo.getOriginatorId() : defOri);
45     }
46
47     private static Attributes prependOriginatorId(final Attributes attributes, final Ipv4Address originatorId) {
48         if (attributes.getOriginatorId() != null) {
49             return attributes;
50         }
51         final AttributesBuilder newAtt = new AttributesBuilder(attributes);
52         return newAtt.setOriginatorId(new OriginatorIdBuilder().setOriginator(originatorId).build()).build();
53     }
54
55     @Override
56     public Attributes applyExportAction(
57             final RouteEntryBaseAttributes routeEntryInfo,
58             final BGPRouteEntryExportParameters routeEntryExportParameters,
59             final Attributes attributes,
60             final SetOriginatorIdPrepend bgpActions) {
61         final Ipv4Address defOri = bgpActions.getSetOriginatorIdPrepend().getOriginatorId();
62         return prependOriginatorId(attributes, defOri == null
63                 ? routeEntryInfo.getOriginatorId() : defOri);
64     }
65 }