Mass-convert all compontents to use -no-zone addresses
[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 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
9
10 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
11 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionAugPolicy;
12 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
13 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginatorIdBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.SetOriginatorIdPrepend;
19
20 /**
21  * Prepend Originator Id.
22  */
23 public final class SetOriginatorIdPrependHandler implements BgpActionAugPolicy<SetOriginatorIdPrepend> {
24     private static final SetOriginatorIdPrependHandler INSTANCE = new SetOriginatorIdPrependHandler();
25
26     private SetOriginatorIdPrependHandler() {
27
28     }
29
30     public static SetOriginatorIdPrependHandler getInstance() {
31         return INSTANCE;
32     }
33
34     @Override
35     public Attributes applyImportAction(
36             final RouteEntryBaseAttributes routeEntryInfo,
37             final BGPRouteEntryImportParameters routeEntryImportParameters,
38             final Attributes attributes,
39             final SetOriginatorIdPrepend bgpActions) {
40
41         final Ipv4AddressNoZone defOri = bgpActions.getSetOriginatorIdPrepend().getOriginatorId();
42         return prependOriginatorId(attributes, defOri == null
43                 ? routeEntryInfo.getOriginatorId() : defOri);
44     }
45
46     private static Attributes prependOriginatorId(final Attributes attributes, final Ipv4AddressNoZone originatorId) {
47         if (attributes.getOriginatorId() != null) {
48             return attributes;
49         }
50         final AttributesBuilder newAtt = new AttributesBuilder(attributes);
51         return newAtt.setOriginatorId(new OriginatorIdBuilder().setOriginator(originatorId).build()).build();
52     }
53
54     @Override
55     public Attributes applyExportAction(
56             final RouteEntryBaseAttributes routeEntryInfo,
57             final BGPRouteEntryExportParameters routeEntryExportParameters,
58             final Attributes attributes,
59             final SetOriginatorIdPrepend bgpActions) {
60         final Ipv4AddressNoZone defOri = bgpActions.getSetOriginatorIdPrepend().getOriginatorId();
61         return prependOriginatorId(attributes, defOri == null
62                 ? routeEntryInfo.getOriginatorId() : defOri);
63     }
64 }