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.spi.registry;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
15 import org.checkerframework.checker.lock.qual.GuardedBy;
16 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
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.rev180329.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.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.rev180329.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;
49 final class ActionsRegistryImpl {
51 private final Map<Class<? extends Augmentation<Actions>>, ActionsAugPolicy> actionsRegistry = new HashMap<>();
53 private final Map<Class<? extends ChildOf<BgpActions>>, BgpActionPolicy> bgpActions = new HashMap<>();
55 private final Map<Class<? extends Augmentation<BgpActions>>, BgpActionAugPolicy> bgpAugActionsRegistry
58 AbstractRegistration registerActionPolicy(
59 final Class<? extends Augmentation<Actions>> actionPolicyClass,
60 final ActionsAugPolicy actionPolicy) {
61 synchronized (this.actionsRegistry) {
62 final ActionsAugPolicy prev = this.actionsRegistry.putIfAbsent(actionPolicyClass, actionPolicy);
63 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
64 actionPolicyClass, prev);
65 return new AbstractRegistration() {
67 protected void removeRegistration() {
68 synchronized (ActionsRegistryImpl.this.actionsRegistry) {
69 ActionsRegistryImpl.this.actionsRegistry.remove(actionPolicyClass);
76 public AbstractRegistration registerBgpActionPolicy(
77 final Class<? extends ChildOf<BgpActions>> bgpActionPolicyClass,
78 final BgpActionPolicy bgpActionPolicy) {
79 synchronized (this.bgpActions) {
80 final BgpActionPolicy prev = this.bgpActions.putIfAbsent(bgpActionPolicyClass, bgpActionPolicy);
81 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
82 bgpActionPolicyClass, prev);
83 return new AbstractRegistration() {
85 protected void removeRegistration() {
86 synchronized (ActionsRegistryImpl.this.bgpActions) {
87 ActionsRegistryImpl.this.bgpActions.remove(bgpActionPolicyClass);
94 public AbstractRegistration registerBgpActionAugmentationPolicy(
95 final Class<? extends Augmentation<BgpActions>> bgpActionPolicyClass,
96 final BgpActionAugPolicy bgpActionPolicy) {
97 synchronized (this.bgpAugActionsRegistry) {
98 final BgpActionAugPolicy prev = this.bgpAugActionsRegistry
99 .putIfAbsent(bgpActionPolicyClass, bgpActionPolicy);
100 Preconditions.checkState(prev == null, "Action Policy %s already registered %s",
101 bgpActionPolicyClass, prev);
102 return new AbstractRegistration() {
104 protected void removeRegistration() {
105 synchronized (ActionsRegistryImpl.this.bgpAugActionsRegistry) {
106 ActionsRegistryImpl.this.bgpAugActionsRegistry.remove(bgpActionPolicyClass);
113 @SuppressWarnings("unchecked")
114 Attributes applyExportAction(
115 final RouteEntryBaseAttributes routeEntryInfo,
116 final BGPRouteEntryExportParameters routeEntryExportParameters,
117 final Attributes attributes,
118 final Actions actions) {
119 requireNonNull(attributes);
120 if (actions.getRouteDisposition() instanceof RejectRoute) {
123 Attributes attributesUpdated = attributes;
124 final Actions1 augmentation = actions.augmentation(Actions1.class);
125 if (augmentation != null && augmentation.getBgpActions() != null) {
126 final BgpActions bgpAction = augmentation.getBgpActions();
128 final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
129 final Long localPrefPrependAction = bgpAction.getSetLocalPref();
130 final BgpOriginAttrType localOriginAction = bgpAction.getSetRouteOrigin();
131 final BgpSetMedType medAction = bgpAction.getSetMed();
132 final BgpNextHopType nhAction = bgpAction.getSetNextHop();
133 final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
134 final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
136 if (asPrependAction != null) {
137 attributesUpdated = this.bgpActions.get(SetAsPathPrepend.class)
138 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
142 if (attributesUpdated == null) {
146 if (setCommunityAction != null) {
147 attributesUpdated = this.bgpActions.get(SetCommunity.class)
148 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
152 if (attributesUpdated == null) {
156 if (setExtCommunityAction != null) {
157 attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
158 .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
159 setExtCommunityAction);
162 boolean updated = false;
163 if (localPrefPrependAction != null || localOriginAction != null
164 || medAction != null || nhAction != null) {
169 final AttributesBuilder attributesUpdatedBuilder = new AttributesBuilder(attributes);
170 if (localPrefPrependAction != null) {
171 attributesUpdatedBuilder.setLocalPref(new LocalPrefBuilder()
172 .setPref(localPrefPrependAction).build());
175 if (localOriginAction != null) {
176 attributesUpdatedBuilder.setOrigin(new OriginBuilder()
177 .setValue(BgpOrigin.forValue(localOriginAction.getIntValue())).build());
180 if (medAction != null) {
181 attributesUpdatedBuilder.setMultiExitDisc(new MultiExitDiscBuilder()
182 .setMed(medAction.getUint32()).build());
185 if (nhAction != null) {
186 final IpAddress address = nhAction.getIpAddress();
188 if (address != null) {
189 if (address.getIpv4Address() != null) {
190 nhNew = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
191 .setGlobal(address.getIpv4Address()).build()).build();
193 nhNew = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
194 .setGlobal(address.getIpv6Address()).build()).build();
197 attributesUpdatedBuilder.setCNextHop(nhNew);
198 } else if (nhAction.getEnumeration() != null
199 && BgpNextHopType.Enumeration.SELF == nhAction.getEnumeration()) {
200 nhNew = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
201 .setGlobal(routeEntryInfo.getOriginatorId()).build()).build();
202 attributesUpdatedBuilder.setCNextHop(nhNew);
205 attributesUpdated = attributesUpdatedBuilder.build();
208 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> bgpConditionsAug = BindingReflections
209 .getAugmentations(bgpAction);
211 if (bgpConditionsAug != null) {
212 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry
213 : bgpConditionsAug.entrySet()) {
214 final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
215 if (handler == null) {
217 } else if (attributesUpdated == null) {
220 attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
221 attributesUpdated, entry.getValue());
226 if (attributesUpdated == null) {
230 // Export Actions Aug
231 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
232 .getAugmentations(actions);
234 if (conditionsAug == null) {
238 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
239 final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
240 if (attributesUpdated == null) {
242 } else if (handler == null) {
245 attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
246 attributesUpdated, (Augmentation<Actions>) entry.getValue());
249 return attributesUpdated;
252 @SuppressWarnings("unchecked")
253 Attributes applyImportAction(
254 final RouteEntryBaseAttributes routeEntryInfo,
255 final BGPRouteEntryImportParameters routeParameters,
256 final Attributes attributes,
257 final Actions actions) {
258 if (actions.getRouteDisposition() instanceof RejectRoute) {
261 Attributes attributesUpdated = attributes;
262 final Actions1 augmentation = actions.augmentation(Actions1.class);
264 if (augmentation != null && augmentation.getBgpActions() != null) {
265 final BgpActions bgpAction = augmentation.getBgpActions();
266 final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
267 final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
268 final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
270 if (asPrependAction != null) {
271 attributesUpdated = this.bgpActions.get(asPrependAction.getClass())
272 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, asPrependAction);
275 if (attributesUpdated == null) {
279 if (setCommunityAction != null) {
280 attributesUpdated = this.bgpActions.get(SetCommunity.class)
281 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
285 if (attributesUpdated == null) {
289 if (setExtCommunityAction != null) {
290 attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
291 .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, setExtCommunityAction);
294 if (attributesUpdated == null) {
298 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> bgpConditionsAug = BindingReflections
299 .getAugmentations(bgpAction);
301 if (bgpConditionsAug == null) {
305 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry
306 : bgpConditionsAug.entrySet()) {
307 final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
308 if (handler == null) {
310 } else if (attributesUpdated == null) {
313 attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
316 if (attributesUpdated == null) {
321 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
322 .getAugmentations(actions);
324 if (conditionsAug == null) {
325 return attributesUpdated;
327 for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
328 final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
329 if (handler == null) {
331 } else if (attributesUpdated == null) {
334 attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
335 (Augmentation<Actions>) entry.getValue());
337 return attributesUpdated;