/*
 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.rfc8040.parser;

import com.google.common.annotations.Beta;
import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatement;
import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatements;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
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;

@Beta
public final class YangDataStatementSupport extends AbstractStatementSupport<String, YangDataStatement,
        EffectiveStatement<String, YangDataStatement>> {
    /**
     * Declared statement representation of 'yang-data' extension defined in
     * <a href="https://tools.ietf.org/html/rfc8040#section-8">RFC 8040</a>.
     */
    private static final class Declared extends AbstractDeclaredStatement<String> implements YangDataStatement {
        Declared(final StmtContext<String, YangDataStatement, ?> ctx) {
            super(ctx);
        }

        @Override
        public String getArgument() {
            return argument();
        }
    }

    private static final YangDataStatementSupport INSTANCE = new YangDataStatementSupport(YangDataStatements.YANG_DATA);

    private final SubstatementValidator validator;

    private YangDataStatementSupport(final StatementDefinition definition) {
        super(definition);
        validator = SubstatementValidator.builder(definition)
                .addMandatory(YangStmtMapping.CONTAINER)
                .addOptional(YangStmtMapping.USES)
                .build();
    }

    public static YangDataStatementSupport getInstance() {
        return INSTANCE;
    }

    @Override
    protected SubstatementValidator getSubstatementValidator() {
        return validator;
    }

    @Override
    public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
        return value;
    }

    @Override
    public YangDataStatement createDeclared(final StmtContext<String, YangDataStatement, ?> ctx) {
        return new Declared(ctx);
    }

    @Override
    public EffectiveStatement<String, YangDataStatement> createEffective(final StmtContext<String,
            YangDataStatement, EffectiveStatement<String, YangDataStatement>> ctx) {
        // in case of yang-data node we need to perform substatement validation at the point when we have
        // effective substatement contexts already available - if the node has only a uses statement declared in it,
        // one top-level container node may very well be added to the yang-data as an effective statement
        validator.validate(ctx);
        return new YangDataEffectiveStatementImpl(ctx);
    }

    @Override
    public void onFullDefinitionDeclared(final Mutable<String, YangDataStatement,
            EffectiveStatement<String, YangDataStatement>> ctx) {
        // as per https://tools.ietf.org/html/rfc8040#section-8,
        // yang-data is ignored unless it appears as a top-level statement
        if (ctx.coerceParentContext().getParentContext() != null) {
            ctx.setIsSupportedToBuildEffective(false);
        }
    }

    @Override
    public boolean isIgnoringIfFeatures() {
        return true;
    }

    @Override
    public boolean isIgnoringConfig() {
        return true;
    }
}
@@ -406,16 +406,96 @@
 s.Beta;%0A
+import org.opendaylight.yangtools.rfc8040.model.api.YangDataEffectiveStatement;%0A
 import o
@@ -549,32 +549,32 @@
 gDataStatement;%0A
-
 import org.opend
@@ -699,82 +699,8 @@
 ng;%0A
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;%0A
 impo
@@ -1205,16 +1205,24 @@
 tSupport
+%0A       
  extends
@@ -1273,25 +1273,25 @@
 atement,
-%0A        
+ YangData
 Effectiv
@@ -1304,35 +1304,8 @@
 ment
-%3CString, YangDataStatement%3E
 %3E %7B%0A
@@ -2795,16 +2795,24 @@
  public 
+YangData
 Effectiv
@@ -2825,35 +2825,8 @@
 ment
-%3CString, YangDataStatement%3E
  cre
@@ -2838,16 +2838,29 @@
 fective(
+%0A            
 final St
@@ -2876,28 +2876,16 @@
 %3CString,
-%0A           
  YangDat
@@ -2896,16 +2896,24 @@
 tement, 
+YangData
 Effectiv
@@ -2926,35 +2926,8 @@
 ment
-%3CString, YangDataStatement%3E
 %3E ct
@@ -3451,29 +3451,25 @@
 atement,
-%0A            
+ YangData
 Effectiv
@@ -3482,35 +3482,8 @@
 ment
-%3CString, YangDataStatement%3E
 %3E ct

Fix YangDataStatementSupport declaration