Bug 3670 (part 2/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.java
index 54dd02353de6eef7e5ca57ee4dede45965284a56..b79f75709eb8d4903f58e785ce3ee1e6f20c1700 100644 (file)
@@ -7,13 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL;
-
+import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import java.util.Collection;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import javax.annotation.Nonnull;
-
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
@@ -29,27 +28,37 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
 
-public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
+public class AugmentStatementImpl extends
+        AbstractDeclaredStatement<SchemaNodeIdentifier> implements
+        AugmentStatement {
 
-    private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
+    private static final Logger LOG = LoggerFactory
+            .getLogger(AugmentStatementImpl.class);
 
-    protected AugmentStatementImpl(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
+    protected AugmentStatementImpl(
+            StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
         super(context);
     }
 
-    public static class Definition extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
+    public static class Definition
+            extends
+            AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
 
         public Definition() {
             super(Rfc6020Mapping.AUGMENT);
         }
 
         @Override
-        public SchemaNodeIdentifier parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException {
-            return SchemaNodeIdentifier.create(AugmentUtils.parseAugmentPath(ctx, value), Utils.isXPathAbsolute(value));
+        public SchemaNodeIdentifier parseArgumentValue(
+                StmtContext<?, ?, ?> ctx, String value) throws SourceException {
+            return SchemaNodeIdentifier.create(
+                    AugmentUtils.parseAugmentPath(ctx, value),
+                    Utils.isXPathAbsolute(ctx, value));
         }
 
         @Override
-        public AugmentStatement createDeclared(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
+        public AugmentStatement createDeclared(
+                StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
             return new AugmentStatementImpl(ctx);
         }
 
@@ -64,31 +73,72 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
                 throws SourceException {
 
-            final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(EFFECTIVE_MODEL);
+            if (StmtContextUtils.isInExtensionBody(augmentNode)) {
+                return;
+            }
+
+            final ModelActionBuilder augmentAction = augmentNode
+                    .newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
-                    .requiresCtx(augmentNode, ModelProcessingPhase.FULL_DECLARATION);
+                    .requiresCtx(augmentNode,
+                            ModelProcessingPhase.FULL_DECLARATION);
 
             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
 
                 @Override
                 public void apply() throws InferenceException {
 
-                    final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils.getAugmentTargetCtx(augmentNode);
-                    final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) sourceCtxPrereq.get();
+                    final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
+                            .getAugmentTargetCtx(augmentNode);
+
+                    if (augmentTargetCtx == null) {
+                        throw new InferenceException(
+                                "Augment target not found: "
+                                        + augmentNode.getStatementArgument(),
+                                augmentNode.getStatementSourceReference());
+                    }
+
+                    if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
+                        augmentNode.setIsSupportedToBuildEffective(false);
+                        return;
+                    }
+
+                    final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
 
                     try {
-                        AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
+                        AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
+                                augmentTargetCtx);
+                        augmentTargetCtx
+                                .addEffectiveSubstatement(augmentSourceCtx);
+                        updateAugmentOrder(augmentSourceCtx);
                     } catch (SourceException e) {
                         LOG.warn(e.getMessage(), e);
                     }
+
+                }
+
+                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);
                 }
 
                 @Override
-                public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
+                public void prerequisiteFailed(
+                        final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
                         throws InferenceException {
-                    if (failed.contains(augmentAction)) {
-                        throw new InferenceException("Augment action failed", augmentNode.getStatementSourceReference());
-                    }
+                    throw new InferenceException("Augment target not found: "
+                            + augmentNode.getStatementArgument(), augmentNode
+                            .getStatementSourceReference());
                 }
             });
         }