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 867afc24e7cd525692a11f3b0539f73adcf5cbed..edf67dca168a905941b40abe1613e83b99e3125a 100644 (file)
@@ -7,21 +7,22 @@
  */
 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.parser.rfc7950.stmt.BaseStatementSupport;
+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.CommonStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
@@ -32,60 +33,53 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractIdentityRefSpecificationSupport
-        extends BaseStatementSupport<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> {
-    AbstractIdentityRefSpecificationSupport() {
-        super(YangStmtMapping.TYPE);
+        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 =
                 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
         for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
-            final QName baseIdentity = baseStmt.coerceStatementArgument();
+            final QName baseIdentity = baseStmt.getArgument();
             final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
-            InferenceException.throwIfNull(stmtCtx, stmt.sourceReference(),
+            InferenceException.throwIfNull(stmtCtx, stmt,
                 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
-                    baseIdentity.getLocalName());
+                baseIdentity.getLocalName());
         }
     }
 
     @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.coerceRawStatementArgument(), 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.getSchemaPath());
+        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);
             }
         }
 
@@ -100,6 +94,6 @@ abstract class AbstractIdentityRefSpecificationSupport
          *     statement, MUST be present at least once if the type is
          *     "identityref".
          */
-        return new SourceException("At least one base statement has to be present", stmt.sourceReference());
+        return new SourceException("At least one base statement has to be present", stmt);
     }
 }
\ No newline at end of file