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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import org.apache.commons.lang3.StringUtils;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
17 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionPolicy;
18 import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.AbstractExtCommunityHandler;
19 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
20 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.BgpSetCommunityOptionType;
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.SetExtCommunity;
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.SetExtCommunityMethod;
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.Inline;
25 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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunities;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
32 * Prepend External Community.
34 public final class SetExtCommunityHandler extends AbstractExtCommunityHandler
35 implements BgpActionPolicy<SetExtCommunity> {
36 public SetExtCommunityHandler(final DataBroker dataBroker) {
41 public Attributes applyImportAction(
42 final RouteEntryBaseAttributes routeEntryInfo,
43 final BGPRouteEntryImportParameters routeEntryImportParameters,
44 final Attributes attributes,
45 final SetExtCommunity bgpActions) {
46 return setExtComm(attributes, bgpActions.getSetExtCommunityMethod(), bgpActions.getOptions());
50 public Attributes applyExportAction(
51 final RouteEntryBaseAttributes routeEntryInfo,
52 final BGPRouteEntryExportParameters routeEntryExportParameters,
53 final Attributes attributes,
54 final SetExtCommunity bgpActions) {
55 return setExtComm(attributes, bgpActions.getSetExtCommunityMethod(), bgpActions.getOptions());
58 private Attributes setExtComm(
59 final Attributes attributes,
60 final SetExtCommunityMethod setExtCommunityMethod,
61 final BgpSetCommunityOptionType options) {
62 if (setExtCommunityMethod instanceof Inline) {
63 final Inline inline = (Inline) setExtCommunityMethod;
64 final List<ExtendedCommunities> list = inline.getExtCommunityMember()
65 .stream().map(ge -> new ExtendedCommunitiesBuilder().setExtendedCommunity(ge.getExtendedCommunity())
66 .setTransitive(ge.isTransitive()).build()).collect(Collectors.toList());
67 return inlineSetExtComm(attributes, list, options);
69 return referenceSetExtComm(attributes, ((Reference) setExtCommunityMethod).getExtCommunitySetRef(), options);
72 private Attributes inlineSetExtComm(
73 final Attributes attributes,
74 final List<ExtendedCommunities> actionExtCommunities,
75 final BgpSetCommunityOptionType options) {
76 final AttributesBuilder newAtt = new AttributesBuilder(attributes);
78 if (options.equals(BgpSetCommunityOptionType.REPLACE)) {
79 return newAtt.setExtendedCommunities(actionExtCommunities).build();
82 final List<ExtendedCommunities> actualComm;
83 if (attributes.getCommunities() != null) {
84 actualComm = new ArrayList<>(attributes.getExtendedCommunities());
86 actualComm = new ArrayList<>();
91 actualComm.addAll(actionExtCommunities);
94 actualComm.removeAll(actionExtCommunities);
97 throw new IllegalArgumentException("Option Type not Recognized!");
100 return newAtt.setExtendedCommunities(actualComm).build();
104 private Attributes referenceSetExtComm(
105 final Attributes attributes,
106 final String extCommunitySetName,
107 final BgpSetCommunityOptionType options) {
108 final String setKey = StringUtils.substringBetween(extCommunitySetName, "=\"", "\"");
109 return inlineSetExtComm(attributes, this.extCommunitySets.getUnchecked(setKey), options);