2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.stream.Collectors;
13 import org.apache.commons.lang3.StringUtils;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
16 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionPolicy;
17 import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.AbstractExtCommunityHandler;
18 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
19 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.BgpSetCommunityOptionType;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetExtCommunity;
22 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.set.ext.community.SetExtCommunityMethod;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.set.ext.community.set.ext.community.method.Inline;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.set.ext.community.set.ext.community.method.Reference;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
31 * Prepend External Community.
33 public final class SetExtCommunityHandler extends AbstractExtCommunityHandler
34 implements BgpActionPolicy<SetExtCommunity> {
35 public SetExtCommunityHandler(final DataBroker dataBroker) {
40 public Attributes applyImportAction(
41 final RouteEntryBaseAttributes routeEntryInfo,
42 final BGPRouteEntryImportParameters routeEntryImportParameters,
43 final Attributes attributes,
44 final SetExtCommunity bgpActions) {
45 return setExtComm(attributes, bgpActions.getSetExtCommunityMethod(), bgpActions.getOptions());
49 public Attributes applyExportAction(
50 final RouteEntryBaseAttributes routeEntryInfo,
51 final BGPRouteEntryExportParameters routeEntryExportParameters,
52 final Attributes attributes,
53 final SetExtCommunity bgpActions) {
54 return setExtComm(attributes, bgpActions.getSetExtCommunityMethod(), bgpActions.getOptions());
57 private Attributes setExtComm(
58 final Attributes attributes,
59 final SetExtCommunityMethod setExtCommunityMethod,
60 final BgpSetCommunityOptionType options) {
61 if (setExtCommunityMethod instanceof Inline) {
62 final Inline inline = (Inline) setExtCommunityMethod;
63 final List<ExtendedCommunities> list = inline.getExtCommunityMember()
64 .stream().map(ge -> new ExtendedCommunitiesBuilder().setExtendedCommunity(ge.getExtendedCommunity())
65 .setTransitive(ge.isTransitive()).build()).collect(Collectors.toList());
66 return inlineSetExtComm(attributes, list, options);
68 return referenceSetExtComm(attributes, ((Reference) setExtCommunityMethod).getExtCommunitySetRef(), options);
71 private Attributes inlineSetExtComm(
72 final Attributes attributes,
73 final List<ExtendedCommunities> actionExtCommunities,
74 final BgpSetCommunityOptionType options) {
75 final AttributesBuilder newAtt = new AttributesBuilder(attributes);
77 if (options.equals(BgpSetCommunityOptionType.REPLACE)) {
78 return newAtt.setExtendedCommunities(actionExtCommunities).build();
81 final List<ExtendedCommunities> actualComm;
82 if (attributes.getCommunities() != null) {
83 actualComm = new ArrayList<>(attributes.getExtendedCommunities());
85 actualComm = new ArrayList<>();
90 actualComm.addAll(actionExtCommunities);
93 actualComm.removeAll(actionExtCommunities);
96 throw new IllegalArgumentException("Option Type not Recognized!");
99 return newAtt.setExtendedCommunities(actualComm).build();
103 private Attributes referenceSetExtComm(
104 final Attributes attributes,
105 final String extCommunitySetName,
106 final BgpSetCommunityOptionType options) {
107 final String setKey = StringUtils.substringBetween(extCommunitySetName, "=\"", "\"");
108 return inlineSetExtComm(attributes, this.extCommunitySets.getUnchecked(setKey), options);