Split out yang-model-ri
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / leaf / LeafStatementSupport.java
index 488009e37007169d82508eccbc7f04dac17e46a4..b92301b4e938d59e9c734d281edee1e3a6e375c1 100644 (file)
@@ -7,19 +7,33 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf;
 
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Status;
 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.DefaultEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
-import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
+import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractSchemaTreeStatementSupport;
+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;
 
 public final class LeafStatementSupport
-        extends AbstractQNameStatementSupport<LeafStatement, EffectiveStatement<QName, LeafStatement>> {
+        extends AbstractSchemaTreeStatementSupport<LeafStatement, LeafEffectiveStatement> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .LEAF)
         .addOptional(YangStmtMapping.CONFIG)
@@ -37,7 +51,7 @@ public final class LeafStatementSupport
     private static final LeafStatementSupport INSTANCE = new LeafStatementSupport();
 
     private LeafStatementSupport() {
-        super(YangStmtMapping.LEAF);
+        super(YangStmtMapping.LEAF, instantiatedPolicy());
     }
 
     public static LeafStatementSupport getInstance() {
@@ -45,35 +59,59 @@ public final class LeafStatementSupport
     }
 
     @Override
-    public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return StmtContextUtils.parseIdentifier(ctx,value);
+    public void onFullDefinitionDeclared(final Mutable<QName, LeafStatement, LeafEffectiveStatement> ctx) {
+        super.onFullDefinitionDeclared(ctx);
+        StmtContextUtils.validateIfFeatureAndWhenOnListKeys(ctx);
     }
 
     @Override
-    public void onStatementAdded(final Mutable<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> stmt) {
-        stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public void onFullDefinitionDeclared(
-            final Mutable<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
-        super.onFullDefinitionDeclared(ctx);
-        StmtContextUtils.validateIfFeatureAndWhenOnListKeys(ctx);
+    protected LeafStatement createDeclared(final StmtContext<QName, LeafStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return DeclaredStatements.createLeaf(ctx.getArgument(), substatements);
     }
 
     @Override
-    public LeafStatement createDeclared(final StmtContext<QName, LeafStatement, ?> ctx) {
-        return new LeafStatementImpl(ctx);
+    protected LeafStatement createEmptyDeclared(final StmtContext<QName, LeafStatement, ?> ctx) {
+        return DeclaredStatements.createLeaf(ctx.getArgument());
     }
 
     @Override
-    public EffectiveStatement<QName, LeafStatement> createEffective(
-            final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
-        return new LeafEffectiveStatementImpl(ctx);
+    public LeafEffectiveStatement copyEffective(final Current<QName, LeafStatement> stmt,
+            final LeafEffectiveStatement original) {
+        return new RegularLeafEffectiveStatement((AbstractLeafEffectiveStatement) original, stmt.effectivePath(),
+            computeFlags(stmt, original.effectiveSubstatements()), (LeafSchemaNode) stmt.original());
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected LeafEffectiveStatement createEffective(final Current<QName, LeafStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
+            findFirstStatement(substatements, TypeEffectiveStatement.class), stmt,
+            "Leaf is missing a 'type' statement");
+        final String dflt = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
+        SourceException.throwIf(
+            EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(stmt.yangVersion(), typeStmt, dflt), stmt,
+            "Leaf '%s' has default value '%s' marked with an if-feature statement.", stmt.argument(), dflt);
+
+        final LeafSchemaNode original = (LeafSchemaNode) stmt.original();
+        final int flags = computeFlags(stmt, substatements);
+        final LeafStatement declared = stmt.declared();
+        return original == null ? new EmptyLeafEffectiveStatement(declared, stmt.effectivePath(), flags, substatements)
+                : new RegularLeafEffectiveStatement(declared, stmt.effectivePath(), flags, substatements, original);
+    }
+
+    private static int computeFlags(final Current<?, ?> stmt,
+        final Collection<? extends EffectiveStatement<?, ?>> substatements) {
+        return new FlagsBuilder()
+            .setHistory(stmt.history())
+            .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
+            .setConfiguration(stmt.effectiveConfig().asNullable())
+            .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
+            .toFlags();
     }
-}
\ No newline at end of file
+}