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.spi.registry;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
14 import java.util.HashMap;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
18 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.ActionsAugPolicy;
19 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionAugPolicy;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionPolicy;
21 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
22 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.Actions1;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.BgpNextHopType;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.BgpSetMedType;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.BgpActions;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetAsPathPrepend;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetCommunity;
29 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;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.BgpOriginAttrType;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.generic.actions.route.disposition.RejectRoute;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.statement.Actions;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.LocalPrefBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.MultiExitDiscBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.OriginBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
45 import org.opendaylight.yangtools.concepts.AbstractRegistration;
46 import org.opendaylight.yangtools.yang.binding.Augmentation;
47 import org.opendaylight.yangtools.yang.binding.ChildOf;
48 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
50 final class ActionsRegistryImpl {
52 private final Map<Class<? extends Augmentation<Actions>>, ActionsAugPolicy> actionsRegistry = new HashMap<>();
54 private final Map<Class<? extends ChildOf<BgpActions>>, BgpActionPolicy> bgpActions = new HashMap<>();
56 private final Map<Class<? extends Augmentation<BgpActions>>, BgpActionAugPolicy> bgpAugActionsRegistry
59 AbstractRegistration registerActionPolicy(
60 final Class<? extends Augmentation<Actions>> actionPolicyClass,
61 final ActionsAugPolicy actionPolicy) {
62 synchronized (this.actionsRegistry) {
63 final ActionsAugPolicy prev = this.actionsRegistry.putIfAbsent(actionPolicyClass, actionPolicy);
64 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
65 actionPolicyClass, prev);
66 return new AbstractRegistration() {
68 protected void removeRegistration() {
69 synchronized (ActionsRegistryImpl.this.actionsRegistry) {
70 ActionsRegistryImpl.this.actionsRegistry.remove(actionPolicyClass);
77 public AbstractRegistration registerBgpActionPolicy(
78 final Class<? extends ChildOf<BgpActions>> bgpActionPolicyClass,
79 final BgpActionPolicy bgpActionPolicy) {
80 synchronized (this.bgpActions) {
81 final BgpActionPolicy prev = this.bgpActions.putIfAbsent(bgpActionPolicyClass, bgpActionPolicy);
82 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
83 bgpActionPolicyClass, prev);
84 return new AbstractRegistration() {
86 protected void removeRegistration() {
87 synchronized (ActionsRegistryImpl.this.bgpActions) {
88 ActionsRegistryImpl.this.bgpActions.remove(bgpActionPolicyClass);
95 public AbstractRegistration registerBgpActionAugmentationPolicy(
96 final Class<? extends Augmentation<BgpActions>> bgpActionPolicyClass,
97 final BgpActionAugPolicy bgpActionPolicy) {
98 synchronized (this.bgpAugActionsRegistry) {
99 final BgpActionAugPolicy prev = this.bgpAugActionsRegistry
100 .putIfAbsent(bgpActionPolicyClass, bgpActionPolicy);
101 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
102 bgpActionPolicyClass, prev);
103 return new AbstractRegistration() {
105 protected void removeRegistration() {
106 synchronized (ActionsRegistryImpl.this.bgpAugActionsRegistry) {
107 ActionsRegistryImpl.this.bgpAugActionsRegistry.remove(bgpActionPolicyClass);
114 @SuppressWarnings("unchecked")
115 Attributes applyExportAction(
116 final RouteEntryBaseAttributes routeEntryInfo,
117 final BGPRouteEntryExportParameters routeEntryExportParameters,
118 final Attributes attributes,
119 final Actions actions) {
120 requireNonNull(attributes);
121 if (actions.getRouteDisposition() instanceof RejectRoute) {
124 Attributes attributesUpdated = attributes;
125 final Actions1 augmentation = actions.getAugmentation(Actions1.class);
126 if (augmentation != null && augmentation.getBgpActions() != null) {
127 final BgpActions bgpAction = augmentation.getBgpActions();
129 final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
130 final Long localPrefPrependAction = bgpAction.getSetLocalPref();
131 final BgpOriginAttrType localOriginAction = bgpAction.getSetRouteOrigin();
132 final BgpSetMedType medAction = bgpAction.getSetMed();
133 final BgpNextHopType nhAction = bgpAction.getSetNextHop();
134 final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
135 final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
137 if (asPrependAction != null) {
138 attributesUpdated = this.bgpActions.get(SetAsPathPrepend.class)
139 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
143 if (attributesUpdated == null) {
147 if (setCommunityAction != null) {
148 attributesUpdated = this.bgpActions.get(SetCommunity.class)
149 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
153 if (attributesUpdated == null) {
157 if (setExtCommunityAction != null) {
158 attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
159 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
160 setExtCommunityAction);
163 boolean updated = false;
164 if (localPrefPrependAction != null || localOriginAction != null
165 || medAction != null || nhAction != null) {
170 final AttributesBuilder attributesUpdatedBuilder = new AttributesBuilder(attributes);
171 if (localPrefPrependAction != null) {
172 attributesUpdatedBuilder.setLocalPref(new LocalPrefBuilder()
173 .setPref(localPrefPrependAction).build());
176 if (localOriginAction != null) {
177 attributesUpdatedBuilder.setOrigin(new OriginBuilder()
178 .setValue(BgpOrigin.forValue(localOriginAction.getIntValue())).build());
181 if (medAction != null) {
182 attributesUpdatedBuilder.setMultiExitDisc(new MultiExitDiscBuilder()
183 .setMed(medAction.getUint32()).build());
186 if (nhAction != null) {
187 final IpAddress address = nhAction.getIpAddress();
188 if (address != null) {
190 if (address.getIpv4Address() != null) {
191 nhNew = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
192 .setGlobal(address.getIpv4Address()).build()).build();
194 nhNew = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
195 .setGlobal(address.getIpv6Address()).build()).build();
198 attributesUpdatedBuilder.setCNextHop(nhNew);
201 attributesUpdated = attributesUpdatedBuilder.build();
204 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> bgpConditionsAug = BindingReflections
205 .getAugmentations(bgpAction);
207 if (bgpConditionsAug != null) {
208 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry
209 : bgpConditionsAug.entrySet()) {
210 final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
211 if (handler == null) {
213 } else if (attributesUpdated == null) {
216 attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
217 attributesUpdated, entry.getValue());
222 if (attributesUpdated == null) {
226 // Export Actions Aug
227 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
228 .getAugmentations(actions);
230 if (conditionsAug == null) {
234 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
235 final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
236 if (attributesUpdated == null) {
238 } else if (handler == null) {
241 attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
242 attributesUpdated, (Augmentation<Actions>) entry.getValue());
245 return attributesUpdated;
248 @SuppressWarnings("unchecked")
249 Attributes applyImportAction(
250 final RouteEntryBaseAttributes routeEntryInfo,
251 final BGPRouteEntryImportParameters routeParameters,
252 final Attributes attributes,
253 final Actions actions) {
254 if (actions.getRouteDisposition() instanceof RejectRoute) {
257 Attributes attributesUpdated = attributes;
258 final Actions1 augmentation = actions.getAugmentation(Actions1.class);
260 if (augmentation != null && augmentation.getBgpActions() != null) {
261 final BgpActions bgpAction = augmentation.getBgpActions();
262 final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
263 final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
264 final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
266 if (asPrependAction != null) {
267 attributesUpdated = this.bgpActions.get(asPrependAction.getClass())
268 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, asPrependAction);
271 if (attributesUpdated == null) {
275 if (setCommunityAction != null) {
276 attributesUpdated = this.bgpActions.get(SetCommunity.class)
277 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
281 if (attributesUpdated == null) {
285 if (setExtCommunityAction != null) {
286 attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
287 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, setExtCommunityAction);
290 if (attributesUpdated == null) {
294 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> bgpConditionsAug = BindingReflections
295 .getAugmentations(bgpAction);
297 if (bgpConditionsAug == null) {
301 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry
302 : bgpConditionsAug.entrySet()) {
303 final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
304 if (handler == null) {
306 } else if (attributesUpdated == null) {
309 attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
312 if (attributesUpdated == null) {
317 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
318 .getAugmentations(actions);
320 if (conditionsAug == null) {
321 return attributesUpdated;
323 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
324 final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
325 if (handler == null) {
327 } else if (attributesUpdated == null) {
330 attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
331 (Augmentation<Actions>) entry.getValue());
333 return attributesUpdated;