Teach YANG parser to retain DeclarationReference
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractIdentityRefSpecificationSupport.java
index 4eb169076680ec5c933fb28e6a204cf4ac8a0db9..edf67dca168a905941b40abe1613e83b99e3125a 100644 (file)
@@ -7,22 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
-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.BaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
-import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
@@ -32,20 +33,14 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractIdentityRefSpecificationSupport
-        extends AbstractStatementSupport<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> {
-    AbstractIdentityRefSpecificationSupport() {
-        super(YangStmtMapping.TYPE, CopyPolicy.DECLARED_COPY);
+        extends AbstractTypeSupport<IdentityRefSpecification> {
+    AbstractIdentityRefSpecificationSupport(final YangParserConfiguration config) {
+        super(config);
     }
 
     @Override
-    public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return value;
-    }
-
-    @Override
-    public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> stmt) {
+    public final void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
+            EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
         super.onFullDefinitionDeclared(stmt);
 
         final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
@@ -60,32 +55,31 @@ abstract class AbstractIdentityRefSpecificationSupport
     }
 
     @Override
-    protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
+    protected final IdentityRefSpecification createDeclared(final StmtContext<QName, IdentityRefSpecification, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new IdentityRefSpecificationImpl(ctx.getRawArgument(), substatements);
-    }
-
-    @Override
-    protected final IdentityRefSpecification createEmptyDeclared(
-            final StmtContext<String, IdentityRefSpecification, ?> ctx) {
-        throw noBase(ctx);
+        if (substatements.isEmpty()) {
+            throw noBase(ctx);
+        }
+        return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
     }
 
     @Override
-    protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
-            final Current<String, IdentityRefSpecification> stmt,
+    protected final EffectiveStatement<QName, IdentityRefSpecification> createEffective(
+            final Current<QName, IdentityRefSpecification> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
         if (substatements.isEmpty()) {
             throw noBase(stmt);
         }
 
-        final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.wrapSchemaPath());
+        final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
         for (final EffectiveStatement<?, ?> subStmt : substatements) {
             if (subStmt instanceof BaseEffectiveStatement) {
                 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
-                final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
-                        stmt.getFromNamespace(IdentityNamespace.class, identityQName);
-                builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
+                final IdentityEffectiveStatement baseIdentity =
+                    verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
+                verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
+                    baseIdentity);
+                builder.addIdentity((IdentitySchemaNode) baseIdentity);
             }
         }