Retain DeclarationReference in DeclaredStatements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviate / AbstractDeviateStatementSupport.java
index 9250774c8bccecdffe802197442cedcfa8c303d6..219e46bb974115fe2a9e430032a204f2991178f4 100644 (file)
@@ -22,15 +22,20 @@ import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 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.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
 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 +57,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 =
@@ -99,13 +104,13 @@ abstract class AbstractDeviateStatementSupport
     private static final ImmutableSet<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
             YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
 
-    AbstractDeviateStatementSupport() {
-        super(YangStmtMapping.DEVIATE);
+    AbstractDeviateStatementSupport(final YangParserConfiguration config) {
+        super(YangStmtMapping.DEVIATE, StatementPolicy.contextIndependent(), config);
     }
 
     @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);
     }
 
@@ -162,8 +167,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,19 +196,19 @@ 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());
+    protected final DeviateStatement attachDeclarationReference(final DeviateStatement stmt,
+            final DeclarationReference reference) {
+        return DeclaredStatementDecorators.decorateDeviate(stmt, reference);
     }
 
     @Override
-    protected DeviateEffectiveStatement createEffective(final Current<DeviateKind, DeviateStatement> stmt,
+    protected final DeviateEffectiveStatement createEffective(final Current<DeviateKind, DeviateStatement> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        return new DeviateEffectiveStatementImpl(stmt.declared(), substatements);
+        return EffectiveStatements.createDeviate(stmt.declared(), substatements);
     }
 
     protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
@@ -261,7 +266,7 @@ 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.argument());
@@ -318,9 +323,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.argument());
+        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",
@@ -364,7 +369,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 +383,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 +391,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) {