bfddf870c89656e5225a47a09fcdf55b0c54889a
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / actions / SetExtCommunityHandler.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 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;
30
31 /**
32  * Prepend External Community.
33  */
34 public final class SetExtCommunityHandler extends AbstractExtCommunityHandler
35         implements BgpActionPolicy<SetExtCommunity> {
36     public SetExtCommunityHandler(final DataBroker dataBroker) {
37         super(dataBroker);
38     }
39
40     @Override
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());
47     }
48
49     @Override
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());
56     }
57
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);
68         }
69         return referenceSetExtComm(attributes, ((Reference) setExtCommunityMethod).getExtCommunitySetRef(), options);
70     }
71
72     private Attributes inlineSetExtComm(
73             final Attributes attributes,
74             final List<ExtendedCommunities> actionExtCommunities,
75             final BgpSetCommunityOptionType options) {
76         final AttributesBuilder newAtt = new AttributesBuilder(attributes);
77
78         if (options.equals(BgpSetCommunityOptionType.REPLACE)) {
79             return newAtt.setExtendedCommunities(actionExtCommunities).build();
80         }
81
82         final List<ExtendedCommunities> actualComm;
83         if (attributes.getCommunities() != null) {
84             actualComm = new ArrayList<>(attributes.getExtendedCommunities());
85         } else {
86             actualComm = new ArrayList<>();
87         }
88
89         switch (options) {
90             case ADD:
91                 actualComm.addAll(actionExtCommunities);
92                 break;
93             case REMOVE:
94                 actualComm.removeAll(actionExtCommunities);
95                 break;
96             default:
97                 throw new IllegalArgumentException("Option Type not Recognized!");
98         }
99
100         return newAtt.setExtendedCommunities(actualComm).build();
101
102     }
103
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);
110     }
111
112 }