Allow derived context to be reused
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / action / ActionStatementSupport.java
index b0acf7ef1de97e7e3b1bc0c7bf5daf743de5f951..7a661f2fd40479e0d03e3b2241526aed6f3d29c1 100644 (file)
@@ -7,26 +7,36 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.action;
 
+import static com.google.common.base.Verify.verify;
+
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 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.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.rfc7950.stmt.BaseSchemaTreeStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.input.InputStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.output.OutputStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 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.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
-public final class ActionStatementSupport
-        extends AbstractQNameStatementSupport<ActionStatement, EffectiveStatement<QName, ActionStatement>> {
+public final class ActionStatementSupport extends
+        BaseSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
+
     private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
             YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
 
@@ -44,7 +54,7 @@ public final class ActionStatementSupport
     private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
 
     private ActionStatementSupport() {
-        super(YangStmtMapping.ACTION);
+        super(YangStmtMapping.ACTION, uninstantiatedPolicy());
     }
 
     public static ActionStatementSupport getInstance() {
@@ -52,56 +62,68 @@ public final class ActionStatementSupport
     }
 
     @Override
-    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return StmtContextUtils.parseIdentifier(ctx, value);
+    public void onStatementAdded(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
+        final QName argument = stmt.getArgument();
+        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), stmt,
+            "Action %s is defined within a notification, rpc or another action", argument);
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), stmt,
+            "Action %s is defined within a case statement", argument);
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.MODULE), stmt,
+            "Action %s is defined at the top level of a module", stmt.getArgument());
+        StmtContextUtils.validateNoKeylessListAncestorOf(stmt, "Action");
+
+        super.onStatementAdded(stmt);
     }
 
     @Override
-    public void onStatementAdded(
-            final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
-        stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
+    public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
+        super.onFullDefinitionDeclared(stmt);
+
+        verify(stmt instanceof StatementContextBase);
+        if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
+            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
+                InputStatementSupport.rfc7950Instance(), null);
+        }
+        if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
+            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
+                OutputStatementSupport.rfc7950Instance(), null);
+        }
     }
 
     @Override
-    public ActionStatement createDeclared(
-            final StmtContext<QName, ActionStatement, ?> ctx) {
-        return new ActionStatementImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @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);
+    protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularActionStatement(ctx.getArgument(), substatements);
     }
 
     @Override
-    public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement,
-            EffectiveStatement<QName, ActionStatement>> stmt) {
-        super.onFullDefinitionDeclared(stmt);
+    protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
+        return new EmptyActionStatement(ctx.getArgument());
+    }
 
-        if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
-            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(InputStatementRFC7950Support.getInstance());
-        }
+    @Override
+    protected ActionEffectiveStatement createEffective(final Current<QName, ActionStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final StatementSourceReference ref = stmt.sourceReference();
+        verify(!substatements.isEmpty(), "Missing implicit input/output statements at %s", ref);
 
-        if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
-            ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(OutputStatementRFC7950Support.getInstance());
+        try {
+            return new ActionEffectiveStatementImpl(stmt.declared(), stmt.wrapSchemaPath(),
+                EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements), substatements);
+        } catch (SubstatementIndexingException e) {
+            throw new SourceException(e.getMessage(), stmt, e);
         }
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    public ActionEffectiveStatement copyEffective(final Current<QName, ActionStatement> stmt,
+            final ActionEffectiveStatement original) {
+        return new ActionEffectiveStatementImpl((ActionEffectiveStatementImpl) original, stmt.wrapSchemaPath(),
+            EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), original.effectiveSubstatements()));
     }
-}
\ No newline at end of file
+}