Bump upstreams
[bgpcep.git] / bgp / openconfig-rp-spi / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / spi / registry / ActionsRegistryImpl.java
index d8f887cf27bbc8009851baeb7eb37296dac61c2d..a9025ddf99c86ac0f65a06fbe1c6c6f88a6e2dd1 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry;
 
 import static java.util.Objects.requireNonNull;
@@ -13,8 +12,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.Preconditions;
 import java.util.HashMap;
 import java.util.Map;
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.GuardedBy;
+import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.ActionsAugPolicy;
 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionAugPolicy;
@@ -31,22 +29,23 @@ import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev15100
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.BgpOriginAttrType;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.generic.actions.route.disposition.RejectRoute;
 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;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.LocalPrefBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.MultiExitDiscBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.OriginBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
-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;
-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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPrefBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.MultiExitDiscBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.CNextHop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
 import org.opendaylight.yangtools.concepts.AbstractRegistration;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
-import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 final class ActionsRegistryImpl {
     @GuardedBy("this")
@@ -112,6 +111,7 @@ final class ActionsRegistryImpl {
         }
     }
 
+    @SuppressWarnings("unchecked")
     Attributes applyExportAction(
             final RouteEntryBaseAttributes routeEntryInfo,
             final BGPRouteEntryExportParameters routeEntryExportParameters,
@@ -122,234 +122,197 @@ final class ActionsRegistryImpl {
             return null;
         }
         Attributes attributesUpdated = attributes;
-        attributesUpdated = applyExportBGPActions(routeEntryInfo, routeEntryExportParameters,
-                attributesUpdated, actions.getAugmentation(Actions1.class));
-        if (attributesUpdated == null) {
-            return null;
-        }
-        attributesUpdated = applyExportAugActions(routeEntryInfo, routeEntryExportParameters,
-                attributesUpdated, actions);
-        return attributesUpdated;
-    }
-
-    @SuppressWarnings("unchecked")
-    private Attributes applyExportAugActions(
-            final RouteEntryBaseAttributes routeEntryInfo,
-            final BGPRouteEntryExportParameters routeEntryExportParameters,
-            @Nonnull final Attributes attributes,
-            final Actions actions) {
-        final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
-                .getAugmentations(actions);
+        final Actions1 augmentation = actions.augmentation(Actions1.class);
+        if (augmentation != null && augmentation.getBgpActions() != null) {
+            final BgpActions bgpAction = augmentation.getBgpActions();
+
+            final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
+            final Uint32 localPrefPrependAction = bgpAction.getSetLocalPref();
+            final BgpOriginAttrType localOriginAction = bgpAction.getSetRouteOrigin();
+            final BgpSetMedType medAction = bgpAction.getSetMed();
+            final BgpNextHopType nhAction = bgpAction.getSetNextHop();
+            final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
+            final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
+
+            if (asPrependAction != null) {
+                attributesUpdated = this.bgpActions.get(SetAsPathPrepend.class)
+                        .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
+                                asPrependAction);
+            }
 
-        if (conditionsAug == null) {
-            return attributes;
-        }
-        Attributes attributesUpdated = attributes;
-        for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
-            final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
             if (attributesUpdated == null) {
                 return null;
-            } else if (handler == null) {
-                continue;
             }
-            attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
-                    attributesUpdated, (Augmentation<Actions>) entry.getValue());
-        }
-        return attributesUpdated;
-    }
-
-    @SuppressWarnings("unchecked")
-    private Attributes applyExportBGPActions(
-            final RouteEntryBaseAttributes routeEntryInfo,
-            final BGPRouteEntryExportParameters routeEntryExportParameters,
-            @Nonnull final Attributes attributes,
-            final Actions1 augmentation) {
-        if (augmentation == null || augmentation.getBgpActions() == null) {
-            return attributes;
-        }
-        Attributes attributesUpdated = attributes;
-        final BgpActions actions = augmentation.getBgpActions();
-
-        final SetAsPathPrepend asPrependAction = actions.getSetAsPathPrepend();
-        final Long localPrefPrependAction = actions.getSetLocalPref();
-        final BgpOriginAttrType localOriginAction = actions.getSetRouteOrigin();
-        final BgpSetMedType medAction = actions.getSetMed();
-        final BgpNextHopType nhAction = actions.getSetNextHop();
-        final SetCommunity setCommunityAction =  actions.getSetCommunity();
-        final SetExtCommunity setExtCommunityAction =  actions.getSetExtCommunity();
-
-        if (asPrependAction != null) {
-            attributesUpdated = this.bgpActions.get(SetAsPathPrepend.class)
-                    .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated, asPrependAction);
-        }
 
-        if (setCommunityAction != null) {
-            attributesUpdated = this.bgpActions.get(SetCommunity.class)
-                    .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
-                            setCommunityAction);
-        }
-
-        if (setExtCommunityAction != null) {
-            attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
-                    .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
-                            setExtCommunityAction);
-        }
-
-        boolean updated = false;
-        if (localPrefPrependAction != null || localOriginAction != null
-                || medAction != null || nhAction != null) {
-            updated = true;
-        }
+            if (setCommunityAction != null) {
+                attributesUpdated = this.bgpActions.get(SetCommunity.class)
+                        .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
+                                setCommunityAction);
+            }
 
-        if (updated) {
-            final AttributesBuilder attributesUpdatedBuilder = new AttributesBuilder(attributes);
-            if (localPrefPrependAction != null) {
-                attributesUpdatedBuilder.setLocalPref(new LocalPrefBuilder().setPref(localPrefPrependAction).build());
+            if (attributesUpdated == null) {
+                return null;
             }
 
-            if (localOriginAction != null) {
-                attributesUpdatedBuilder.setOrigin(new OriginBuilder()
-                        .setValue(BgpOrigin.forValue(localOriginAction.getIntValue())).build());
+            if (setExtCommunityAction != null) {
+                attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
+                        .applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
+                                setExtCommunityAction);
             }
 
-            if (medAction != null) {
-                attributesUpdatedBuilder.setMultiExitDisc(new MultiExitDiscBuilder()
-                        .setMed(medAction.getUint32()).build());
+            boolean updated = false;
+            if (localPrefPrependAction != null || localOriginAction != null
+                    || medAction != null || nhAction != null) {
+                updated = true;
             }
 
-            if (nhAction != null) {
-                final IpAddress address = nhAction.getIpAddress();
-                if (address != null) {
+            if (updated) {
+                final AttributesBuilder attributesUpdatedBuilder = new AttributesBuilder(attributes);
+                if (localPrefPrependAction != null) {
+                    attributesUpdatedBuilder.setLocalPref(new LocalPrefBuilder()
+                            .setPref(localPrefPrependAction).build());
+                }
+
+                if (localOriginAction != null) {
+                    attributesUpdatedBuilder.setOrigin(new OriginBuilder()
+                            .setValue(BgpOrigin.forValue(localOriginAction.getIntValue())).build());
+                }
+
+                if (medAction != null) {
+                    attributesUpdatedBuilder.setMultiExitDisc(new MultiExitDiscBuilder()
+                            .setMed(medAction.getUint32()).build());
+                }
+
+                if (nhAction != null) {
+                    final IpAddress address = nhAction.getIpAddress();
                     CNextHop nhNew;
-                    if (address.getIpv4Address() != null) {
+                    if (address != null) {
+                        if (address.getIpv4Address() != null) {
+                            nhNew = new Ipv4NextHopCaseBuilder()
+                                    .setIpv4NextHop(new Ipv4NextHopBuilder()
+                                        .setGlobal(IetfInetUtil.ipv4AddressNoZoneFor(address.getIpv4Address()))
+                                        .build())
+                                    .build();
+                        } else {
+                            nhNew = new Ipv6NextHopCaseBuilder()
+                                    .setIpv6NextHop(new Ipv6NextHopBuilder()
+                                        .setGlobal(IetfInetUtil.ipv6AddressNoZoneFor(address.getIpv6Address()))
+                                        .build())
+                                    .build();
+                        }
+
+                        attributesUpdatedBuilder.setCNextHop(nhNew);
+                    } else if (nhAction.getEnumeration() != null
+                            && BgpNextHopType.Enumeration.SELF == nhAction.getEnumeration()) {
                         nhNew = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
-                                .setGlobal(address.getIpv4Address()).build()).build();
-                    } else {
-                        nhNew = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
-                                .setGlobal(address.getIpv6Address()).build()).build();
+                                .setGlobal(routeEntryInfo.getOriginatorId()).build()).build();
+                        attributesUpdatedBuilder.setCNextHop(nhNew);
                     }
-
-                    attributesUpdatedBuilder.setCNextHop(nhNew);
                 }
+                attributesUpdated = attributesUpdatedBuilder.build();
             }
-            attributesUpdated = attributesUpdatedBuilder.build();
-        }
-
-        final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
-                .getAugmentations(actions);
 
-        if (conditionsAug != null) {
-            for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
-                final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
+            for (final Augmentation<BgpActions> action : bgpAction.augmentations().values()) {
+                final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(action.implementedInterface());
                 if (handler == null) {
                     continue;
                 } else if (attributesUpdated == null) {
                     return null;
                 }
                 attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters,
-                        attributesUpdated, entry.getValue());
+                    attributesUpdated, action);
             }
         }
 
-        return attributesUpdated;
-    }
-
-    Attributes applyImportAction(
-            final RouteEntryBaseAttributes routeEntryInfo,
-            final BGPRouteEntryImportParameters routeBaseParameters,
-            final Attributes attributes,
-            final Actions actions) {
-        requireNonNull(attributes);
-        if (actions.getRouteDisposition() instanceof RejectRoute) {
-            return null;
-        }
-        Attributes attributesUpdated = attributes;
-        attributesUpdated = applyImportBGPActions(routeEntryInfo, routeBaseParameters,
-                attributesUpdated, actions.getAugmentation(Actions1.class));
         if (attributesUpdated == null) {
             return null;
         }
-        attributesUpdated = applyImportAugActions(routeEntryInfo, routeBaseParameters, attributesUpdated,
-                actions);
+
+        // Export Actions Aug
+        for (final Augmentation<Actions> entry : actions.augmentations().values()) {
+            final ActionsAugPolicy handler = this.actionsRegistry.get(entry.implementedInterface());
+            if (attributesUpdated == null) {
+                return null;
+            } else if (handler == null) {
+                continue;
+            }
+            attributesUpdated = handler.applyExportAction(routeEntryInfo, routeEntryExportParameters, attributesUpdated,
+                entry);
+        }
+
         return attributesUpdated;
     }
 
     @SuppressWarnings("unchecked")
-    private Attributes applyImportBGPActions(
+    Attributes applyImportAction(
             final RouteEntryBaseAttributes routeEntryInfo,
             final BGPRouteEntryImportParameters routeParameters,
             final Attributes attributes,
-            final Actions1 augmentation) {
-        if (augmentation == null || augmentation.getBgpActions() == null) {
-            return attributes;
+            final Actions actions) {
+        if (actions.getRouteDisposition() instanceof RejectRoute) {
+            return null;
         }
         Attributes attributesUpdated = attributes;
-        final BgpActions actions = augmentation.getBgpActions();
-        final SetCommunity setCommunityAction =  actions.getSetCommunity();
-        final SetExtCommunity setExtCommunityAction =  actions.getSetExtCommunity();
-        final SetAsPathPrepend asPrependAction = actions.getSetAsPathPrepend();
-
-        if (asPrependAction != null) {
-            attributesUpdated = this.bgpActions.get(asPrependAction.getClass())
-                    .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, asPrependAction);
-        }
+        final Actions1 augmentation = actions.augmentation(Actions1.class);
 
-        if (setCommunityAction != null) {
-            attributesUpdated = this.bgpActions.get(SetCommunity.class)
-                    .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
-                            setCommunityAction);
-        }
+        if (augmentation != null && augmentation.getBgpActions() != null) {
+            final BgpActions bgpAction = augmentation.getBgpActions();
+            final SetCommunity setCommunityAction = bgpAction.getSetCommunity();
+            final SetExtCommunity setExtCommunityAction = bgpAction.getSetExtCommunity();
+            final SetAsPathPrepend asPrependAction = bgpAction.getSetAsPathPrepend();
 
-        if (setExtCommunityAction != null) {
-            attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
-                    .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, setExtCommunityAction);
-        }
-
-        if (attributesUpdated == null) {
-            return null;
-        }
+            if (asPrependAction != null) {
+                attributesUpdated = this.bgpActions.get(asPrependAction.getClass())
+                        .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, asPrependAction);
+            }
 
-        final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
-                .getAugmentations(actions);
+            if (attributesUpdated == null) {
+                return null;
+            }
 
-        if (conditionsAug == null) {
-            return attributes;
-        }
+            if (setCommunityAction != null) {
+                attributesUpdated = this.bgpActions.get(SetCommunity.class)
+                        .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
+                                setCommunityAction);
+            }
 
-        for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
-            final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(entry.getKey());
-            if (handler == null) {
-                continue;
-            } else if (attributesUpdated == null) {
+            if (attributesUpdated == null) {
                 return null;
             }
-            attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
-                    entry.getValue());
-        }
-        return attributesUpdated;
-    }
 
-    @SuppressWarnings("unchecked")
-    private Attributes applyImportAugActions(final RouteEntryBaseAttributes routeEntryInfo,
-            final BGPRouteEntryImportParameters routeBaseParameters,
-            @Nonnull final Attributes attributes,
-            final Actions actions) {
-        final Map<Class<? extends Augmentation<?>>, Augmentation<?>> conditionsAug = BindingReflections
-                .getAugmentations(actions);
+            if (setExtCommunityAction != null) {
+                attributesUpdated = this.bgpActions.get(SetExtCommunity.class)
+                        .applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, setExtCommunityAction);
+            }
+
+            if (attributesUpdated == null) {
+                return null;
+            }
 
-        if (conditionsAug == null) {
-            return attributes;
+            for (final Augmentation<BgpActions> action : bgpAction.augmentations().values()) {
+                final BgpActionAugPolicy handler = this.bgpAugActionsRegistry.get(action.implementedInterface());
+                if (handler == null) {
+                    continue;
+                } else if (attributesUpdated == null) {
+                    return null;
+                }
+                attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated,
+                    action);
+            }
+            if (attributesUpdated == null) {
+                return null;
+            }
         }
-        Attributes attributesUpdated = attributes;
-        for (final Map.Entry<Class<? extends Augmentation<?>>, Augmentation<?>> entry : conditionsAug.entrySet()) {
-            final ActionsAugPolicy handler = this.actionsRegistry.get(entry.getKey());
+
+        // Augmented Actions
+        for (final Augmentation<Actions> action : actions.augmentations().values()) {
+            final ActionsAugPolicy handler = this.actionsRegistry.get(action.implementedInterface());
             if (handler == null) {
                 continue;
             } else if (attributesUpdated == null) {
                 return null;
             }
-            attributesUpdated = handler.applyImportAction(routeEntryInfo, routeBaseParameters, attributesUpdated,
-                    (Augmentation<Actions>) entry.getValue());
+            attributesUpdated = handler.applyImportAction(routeEntryInfo, routeParameters, attributesUpdated, action);
         }
         return attributesUpdated;
     }