Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / actions / SetClusterIdPrependHandler.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 java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
13 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionAugPolicy;
14 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
15 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ClusterIdBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.SetClusterIdPrepend;
21
22 /**
23  * Prepend Cluster Id.
24  */
25 public final class SetClusterIdPrependHandler implements BgpActionAugPolicy<SetClusterIdPrepend> {
26     @Override
27     public Attributes applyImportAction(
28             final RouteEntryBaseAttributes routeEntryInfo,
29             final BGPRouteEntryImportParameters importParameters,
30             final Attributes attributes,
31             final SetClusterIdPrepend bgpActions) {
32         final ClusterIdentifier clusterIdLocal = importParameters.getFromClusterId() == null
33                 ? routeEntryInfo.getClusterId() : importParameters.getFromClusterId();
34         return prependClusterId(attributes, clusterIdLocal);
35     }
36
37     private Attributes prependClusterId(final Attributes attributes, final ClusterIdentifier clusterId) {
38         final AttributesBuilder newAtt = new AttributesBuilder(attributes);
39         final List<ClusterIdentifier> newClusterList = new ArrayList<>();
40         newClusterList.add(clusterId);
41         if (attributes.getClusterId() != null && !attributes.getClusterId().getCluster().isEmpty()) {
42             final List<ClusterIdentifier> oldList = attributes.getClusterId().getCluster();
43             newClusterList.addAll(oldList);
44         }
45         return newAtt.setClusterId(new ClusterIdBuilder().setCluster(newClusterList).build()).build();
46     }
47
48     @Override
49     public Attributes applyExportAction(
50             final RouteEntryBaseAttributes routeEntryInfo,
51             final BGPRouteEntryExportParameters exportParameters,
52             final Attributes attributes,
53             final SetClusterIdPrepend bgpActions) {
54         final ClusterIdentifier clusterIdLocal = exportParameters.getFromClusterId() == null
55                 ? routeEntryInfo.getClusterId() : exportParameters.getFromClusterId();
56         return prependClusterId(attributes, clusterIdLocal);
57     }
58 }