Promote more declared statements to yang-parser-spi
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviation / DeviationStatementSupport.java
index 0fa0f4a2e38b000ec9da50912a58a1ed2ab52375..f492ff90d600656b38ede036d509d17c5be4bcac 100644 (file)
@@ -7,57 +7,56 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.deviation;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+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.stmt.DeviationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.spi.stmt.DeclaredStatements;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 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.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 
-public final class DeviationStatementSupport extends AbstractStatementSupport<SchemaNodeIdentifier, DeviationStatement,
-        EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> {
+public final class DeviationStatementSupport
+        extends AbstractStatementSupport<Absolute, DeviationStatement, DeviationEffectiveStatement> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .DEVIATION)
         .addOptional(YangStmtMapping.DESCRIPTION)
         .addAny(YangStmtMapping.DEVIATE)
         .addOptional(YangStmtMapping.REFERENCE)
         .build();
+    private static final DeviationStatementSupport INSTANCE = new DeviationStatementSupport();
 
-    public DeviationStatementSupport() {
-        super(YangStmtMapping.DEVIATION);
+    private DeviationStatementSupport() {
+        super(YangStmtMapping.DEVIATION, StatementPolicy.reject());
     }
 
-    @Override
-    public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return ArgumentUtils.nodeIdentifierFromPath(ctx, value);
-    }
-
-    @Override
-    public DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
-        return new DeviationStatementImpl(ctx);
+    public static DeviationStatementSupport getInstance() {
+        return INSTANCE;
     }
 
     @Override
-    public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
-            final StmtContext<SchemaNodeIdentifier, DeviationStatement,
-            EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
-        return new DeviationEffectiveStatementImpl(ctx);
+    public Absolute parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+        return ArgumentUtils.parseAbsoluteSchemaNodeIdentifier(ctx, value);
     }
 
     @Override
-    public void onFullDefinitionDeclared(final StmtContext.Mutable<SchemaNodeIdentifier, DeviationStatement,
-            EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
-        final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class,
-                ctx.getRoot());
-        final QNameModule targetModule = ctx.getStatementArgument().getLastComponent().getModule();
+    public void onFullDefinitionDeclared(
+            final Mutable<Absolute, DeviationStatement, DeviationEffectiveStatement> ctx) {
+        final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot());
+        final QNameModule targetModule = Iterables.getLast(ctx.getArgument().getNodeIdentifiers()).getModule();
 
         if (currentModule.equals(targetModule)) {
-            throw new InferenceException(ctx.getStatementSourceReference(),
+            throw new InferenceException(ctx,
                     "Deviation must not target the same module as the one it is defined in: %s", currentModule);
         }
     }
@@ -66,4 +65,21 @@ public final class DeviationStatementSupport extends AbstractStatementSupport<Sc
     protected SubstatementValidator getSubstatementValidator() {
         return SUBSTATEMENT_VALIDATOR;
     }
+
+    @Override
+    protected DeviationStatement createDeclared(final StmtContext<Absolute, DeviationStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return DeclaredStatements.createDeviation(ctx.getRawArgument(), ctx.getArgument(), substatements);
+    }
+
+    @Override
+    protected DeviationStatement createEmptyDeclared(final StmtContext<Absolute, DeviationStatement, ?> ctx) {
+        return DeclaredStatements.createDeviation(ctx.getRawArgument(), ctx.getArgument());
+    }
+
+    @Override
+    protected DeviationEffectiveStatement createEffective(final Current<Absolute, DeviationStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new DeviationEffectiveStatementImpl(stmt.declared(), substatements);
+    }
 }
\ No newline at end of file