BUG-6497: Do not lose augmentation statement order
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.java
index 3280c9d0f9495d303318ee3535912b1d18ad5db0..d6adbe039a410c31677ad7613b66ec414391f799 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
@@ -90,72 +91,79 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
 
         @Override
         public void onFullDefinitionDeclared(
-                final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
-                throws SourceException {
+                final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
+            if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
+                return;
+            }
+
             SUBSTATEMENT_VALIDATOR.validate(augmentNode);
 
             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
                 return;
             }
 
-            final ModelActionBuilder augmentAction = augmentNode
-                    .newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
-            final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
-                    .requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
-            final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
+            final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
+                ModelProcessingPhase.EFFECTIVE_MODEL);
+            final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
+                    augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
+            final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
+                    augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
 
                 @Override
-                public void apply() throws InferenceException {
+                public void apply() {
                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
-
-                    if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
+                    if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx)
+                            || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
                         augmentNode.setIsSupportedToBuildEffective(false);
                         return;
                     }
+                    /**
+                     * Marks case short hand in augment
+                     */
+                    if (augmentTargetCtx.getPublicDefinition() == Rfc6020Mapping.CHOICE) {
+                        augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, true);
+                    }
+
+                    // FIXME: this is a workaround for models which augment a node which is added via an extension
+                    //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
                     try {
-                        AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
-                                augmentTargetCtx);
-                        augmentTargetCtx
-                                .addEffectiveSubstatement(augmentSourceCtx);
+                        AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
+                        augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
                         updateAugmentOrder(augmentSourceCtx);
                     } catch (SourceException e) {
-                        LOG.warn(e.getMessage(), e);
+                        LOG.debug("Failed to add augmentation {} defined at {}",
+                            augmentTargetCtx.getStatementSourceReference(),
+                                augmentSourceCtx.getStatementSourceReference(), e);
                     }
-
                 }
 
-                private void updateAugmentOrder(
-                        final StatementContextBase<?, ?, ?> augmentSourceCtx) {
-                    Integer currentOrder = augmentSourceCtx
-                            .getFromNamespace(StmtOrderingNamespace.class,
-                                    Rfc6020Mapping.AUGMENT);
+                private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
+                    Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
+                        Rfc6020Mapping.AUGMENT);
                     if (currentOrder == null) {
                         currentOrder = 1;
                     } else {
                         currentOrder++;
                     }
+
                     augmentSourceCtx.setOrder(currentOrder);
-                    augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
-                            Rfc6020Mapping.AUGMENT, currentOrder);
+                    augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
                 }
 
                 @Override
-                public void prerequisiteFailed(
-                        final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
-                        throws InferenceException {
-                    throw new InferenceException("Augment target not found: "
-                            + augmentNode.getStatementArgument(), augmentNode
-                            .getStatementSourceReference());
+                public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
+                    throw new InferenceException(augmentNode.getStatementSourceReference(),
+                        "Augment target '%s' not found", augmentNode.getStatementArgument());
                 }
             });
         }
 
-        private Mutable<?, ?, ?> getSearchRoot(Mutable<?, ?, ?> augmentContext) {
+        private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
             Mutable<?, ?, ?> parent = augmentContext.getParentContext();
             // Augment is in uses - we need to augment instantiated nodes in parent.
-            if(Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
+            if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
                 return parent.getParentContext();
             }
             return parent;