Move implementation of OnStatementAdded to single class
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / action / ActionStatementSupport.java
index ea00dafb630515b6a0ec8ef44ba1f3a3fc8b8d69..0e0ad7dbb24f87e5670900512d6eb68d731d1444 100644 (file)
@@ -7,34 +7,33 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.action;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 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.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
-import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.input.InputStatementRFC7950Support;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.output.OutputStatementRFC7950Support;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 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.meta.SubstatementValidator;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementDefinitionContext;
 
-public final class ActionStatementSupport
-        extends AbstractQNameStatementSupport<ActionStatement, EffectiveStatement<QName, ActionStatement>> {
-    private static final Set<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(YangStmtMapping.NOTIFICATION,
-            YangStmtMapping.RPC, YangStmtMapping.ACTION);
-    private static final StatementSupport<?, ?, ?> IMPLICIT_INPUT = new InputStatementRFC7950Support();
-    private static final StatementSupport<?, ?, ?> IMPLICIT_OUTPUT = new OutputStatementRFC7950Support();
+public final class ActionStatementSupport extends
+        BaseSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
+
+    private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
+            YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
 
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.ACTION)
@@ -47,57 +46,32 @@ public final class ActionStatementSupport
         .addOptional(YangStmtMapping.STATUS)
         .addAny(YangStmtMapping.TYPEDEF)
         .build();
+    private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
 
-    public ActionStatementSupport() {
+    private ActionStatementSupport() {
         super(YangStmtMapping.ACTION);
     }
 
-    @Override
-    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return StmtContextUtils.qnameFromArgument(ctx, value);
-    }
-
-    @Override
-    public void onStatementAdded(
-            final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
-        stmt.getParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.getStatementArgument(), stmt);
+    public static ActionStatementSupport getInstance() {
+        return INSTANCE;
     }
 
     @Override
-    public ActionStatement createDeclared(
-            final StmtContext<QName, ActionStatement, ?> ctx) {
-        return new ActionStatementImpl(ctx);
-    }
-
-    @Override
-    public EffectiveStatement<QName, ActionStatement> createEffective(
-            final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> ctx) {
-        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS),
-                ctx.getStatementSourceReference(),
-                "Action %s is defined within a notification, rpc or another action", ctx.getStatementArgument());
-        SourceException.throwIf(!StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST,
-                YangStmtMapping.KEY), ctx.getStatementSourceReference(),
-                "Action %s is defined within a list that has no key statement", ctx.getStatementArgument());
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE),
-                ctx.getStatementSourceReference(), "Action %s is defined within a case statement",
-                ctx.getStatementArgument());
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE),
-                ctx.getStatementSourceReference(), "Action %s is defined at the top level of a module",
-                ctx.getStatementArgument());
-        return new ActionEffectiveStatementImpl(ctx);
+    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+        return StmtContextUtils.parseIdentifier(ctx, value);
     }
 
     @Override
-    public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement,
-            EffectiveStatement<QName, ActionStatement>> stmt) {
+    public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
         super.onFullDefinitionDeclared(stmt);
 
         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
-            addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_INPUT);
+            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
+                InputStatementRFC7950Support.getInstance(), null);
         }
-
         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
-            addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_OUTPUT);
+            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
+                OutputStatementRFC7950Support.getInstance(), null);
         }
     }
 
@@ -106,11 +80,41 @@ public final class ActionStatementSupport
         return SUBSTATEMENT_VALIDATOR;
     }
 
-    private static void addImplicitStatement(final StatementContextBase<?, ?, ?> parent,
-        final StatementSupport<?, ?, ?> statementToAdd) {
-        final StatementDefinitionContext<?, ?, ?> stmtDefCtx = new StatementDefinitionContext<>(statementToAdd);
+    @Override
+    protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularActionStatement(ctx.coerceStatementArgument(), substatements);
+    }
+
+    @Override
+    protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
+        return new EmptyActionStatement(ctx.coerceStatementArgument());
+    }
 
-        parent.createSubstatement(parent.declaredSubstatements().size(), stmtDefCtx,
-                ImplicitSubstatement.of(parent.getStatementSourceReference()), null);
+    @Override
+    protected ActionEffectiveStatement createEffective(
+            final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final QName argument = ctx.coerceStatementArgument();
+        final StatementSourceReference ref = ctx.getStatementSourceReference();
+        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS), ref,
+            "Action %s is defined within a notification, rpc or another action", argument);
+        SourceException.throwIf(
+            !StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
+            "Action %s is defined within a list that has no key statement", argument);
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE), ref,
+            "Action %s is defined within a case statement", argument);
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE), ref,
+            "Action %s is defined at the top level of a module", argument);
+
+        return new ActionEffectiveStatementImpl(declared, ctx.getSchemaPath().get(),
+            historyAndStatusFlags(ctx, substatements), ctx, substatements);
+    }
+
+    @Override
+    protected ActionEffectiveStatement createEmptyEffective(
+            final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared) {
+        throw new IllegalStateException("Missing implicit input/output statements at "
+            + ctx.getStatementSourceReference());
     }
-}
\ No newline at end of file
+}