Promote more declared statements to yang-parser-spi
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviate / AbstractDeviateStatementSupport.java
index 1c880d8ca3fc3e7e824c40120e738c9d562e8ce4..3b394a12a533a263319f1380211168f499bf9ff0 100644 (file)
@@ -28,9 +28,10 @@ import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.spi.stmt.DeclaredStatements;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
@@ -52,7 +53,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 abstract class AbstractDeviateStatementSupport
-        extends BaseStatementSupport<DeviateKind, DeviateStatement, DeviateEffectiveStatement> {
+        extends AbstractStatementSupport<DeviateKind, DeviateStatement, DeviateEffectiveStatement> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class);
 
     private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR =
@@ -100,23 +101,23 @@ abstract class AbstractDeviateStatementSupport
             YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
 
     AbstractDeviateStatementSupport() {
-        super(YangStmtMapping.DEVIATE);
+        super(YangStmtMapping.DEVIATE, StatementPolicy.contextIndependent());
     }
 
     @Override
     public final DeviateKind parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value), ctx.sourceReference(),
+        return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value), ctx,
             "String '%s' is not valid deviate argument", value);
     }
 
     @Override
     public final void onFullDefinitionDeclared(
             final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx) {
-        final DeviateKind deviateKind = deviateStmtCtx.getStatementArgument();
+        final DeviateKind deviateKind = deviateStmtCtx.argument();
         getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
 
         final SchemaNodeIdentifier deviationTarget =
-                (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().getStatementArgument();
+                (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().argument();
 
         if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
             return;
@@ -162,8 +163,8 @@ abstract class AbstractDeviateStatementSupport
 
             @Override
             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
-                throw new InferenceException(deviateStmtCtx.coerceParentContext().sourceReference(),
-                    "Deviation target '%s' not found.", deviationTarget);
+                throw new InferenceException(deviateStmtCtx.coerceParentContext(), "Deviation target '%s' not found.",
+                    deviationTarget);
             }
         });
     }
@@ -191,13 +192,12 @@ abstract class AbstractDeviateStatementSupport
     @Override
     protected final DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new DeviateStatementImpl(ctx.getRawArgument(), ctx.coerceStatementArgument(), substatements);
+        return DeclaredStatements.createDeviate(ctx.getArgument(), substatements);
     }
 
     @Override
     protected final DeviateStatement createEmptyDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
-        // This is exceedingly unlikely, just reuse the implementation
-        return createDeclared(ctx, ImmutableList.of());
+        return DeclaredStatements.createDeviate(ctx.getArgument());
     }
 
     @Override
@@ -261,10 +261,10 @@ abstract class AbstractDeviateStatementSupport
                     && YangStmtMapping.LEAF.equals(targetCtx.publicDefinition())) {
                 for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
                     InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.publicDefinition()),
-                        stmtCtxToBeAdded.sourceReference(),
+                        stmtCtxToBeAdded,
                         "Deviation cannot add substatement %s to target node %s because it is already defined "
                         + "in target and can appear only once.",
-                        stmtToBeAdded.getStatementName(), targetCtx.getStatementArgument());
+                        stmtToBeAdded.getStatementName(), targetCtx.argument());
                 }
             }
         }
@@ -290,7 +290,7 @@ abstract class AbstractDeviateStatementSupport
                 && YangStmtMapping.LEAF_LIST.equals(targetCtx.publicDefinition())) {
             LOG.error("Deviation cannot replace substatement {} in target leaf-list {} because a leaf-list can "
                     + "have multiple default statements. At line: {}", stmtToBeReplaced.getStatementName(),
-                    targetCtx.getStatementArgument(), stmtCtxToBeReplaced.sourceReference());
+                    targetCtx.argument(), stmtCtxToBeReplaced.sourceReference());
             return;
         }
 
@@ -318,9 +318,9 @@ abstract class AbstractDeviateStatementSupport
             return;
         }
 
-        throw new InferenceException(stmtCtxToBeReplaced.sourceReference(), "Deviation cannot replace "
-                + "substatement %s in target node %s because it does not exist in target node.",
-                stmtToBeReplaced.getStatementName(), targetCtx.getStatementArgument());
+        throw new InferenceException(stmtCtxToBeReplaced,
+            "Deviation cannot replace substatement %s in target node %s because it does not exist in target node.",
+            stmtToBeReplaced.getStatementName(), targetCtx.argument());
     }
 
     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
@@ -356,7 +356,7 @@ abstract class AbstractDeviateStatementSupport
 
         LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does "
                 + "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument,
-                targetCtx.getStatementArgument(), stmtCtxToBeDeleted.sourceReference());
+                targetCtx.argument(), stmtCtxToBeDeleted.sourceReference());
     }
 
     private static void copyStatement(final Mutable<?, ?, ?> stmtCtxToBeCopied,
@@ -364,7 +364,7 @@ abstract class AbstractDeviateStatementSupport
         // we need to make a copy of the statement context only if it is an unknown statement, otherwise
         // we can reuse the original statement context
         if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) {
-            targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied);
+            targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied.replicaAsChildOf(targetCtx));
         } else {
             targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL));
         }
@@ -378,7 +378,7 @@ abstract class AbstractDeviateStatementSupport
     private static void validateDeviationTarget(final StmtContext<?, ?, ?> deviateSubStmtCtx,
             final StmtContext<?, ?, ?> targetCtx) {
         InferenceException.throwIf(!isSupportedDeviationTarget(deviateSubStmtCtx, targetCtx,
-            targetCtx.getRootVersion()), deviateSubStmtCtx.sourceReference(),
+            targetCtx.yangVersion()), deviateSubStmtCtx,
             "%s is not a valid deviation target for substatement %s.", targetCtx.argument(),
             deviateSubStmtCtx.publicDefinition().getStatementName());
     }
@@ -386,7 +386,7 @@ abstract class AbstractDeviateStatementSupport
     private static boolean isSupportedDeviationTarget(final StmtContext<?, ?, ?> deviateSubstatementCtx,
             final StmtContext<?, ?, ?> deviateTargetCtx, final YangVersion yangVersion) {
         Set<StatementDefinition> supportedDeviationTargets =
-                YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(deviateTargetCtx.getRootVersion(),
+                YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(yangVersion,
                         deviateSubstatementCtx.publicDefinition());
 
         if (supportedDeviationTargets == null) {