Migrate IdentityRefSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractIdentityRefSpecificationSupport.java
index 91ea4029ee0c6bf6ec722a2cf1fef5b9a795a475..dcb380573b8c694384b33f80a4b30d6ac54a738e 100644 (file)
@@ -7,21 +7,30 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+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.parser.spi.IdentityNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 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.source.SourceException;
 
 abstract class AbstractIdentityRefSpecificationSupport
-        extends AbstractStatementSupport<String, IdentityRefSpecification,
+        extends BaseStatementSupport<String, IdentityRefSpecification,
             EffectiveStatement<String, IdentityRefSpecification>> {
     AbstractIdentityRefSpecificationSupport() {
         super(YangStmtMapping.TYPE);
@@ -32,18 +41,6 @@ abstract class AbstractIdentityRefSpecificationSupport
         return value;
     }
 
-    @Override
-    public final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx) {
-        return new IdentityRefSpecificationImpl(ctx);
-    }
-
-    @Override
-    public final EffectiveStatement<String, IdentityRefSpecification> createEffective(
-            final StmtContext<String, IdentityRefSpecification,
-            EffectiveStatement<String, IdentityRefSpecification>> ctx) {
-        return new IdentityRefSpecificationEffectiveStatement(ctx);
-    }
-
     @Override
     public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
             EffectiveStatement<String, IdentityRefSpecification>> stmt) {
@@ -59,4 +56,55 @@ abstract class AbstractIdentityRefSpecificationSupport
                     baseIdentity.getLocalName());
         }
     }
+
+    @Override
+    protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new IdentityRefSpecificationImpl(ctx, substatements);
+    }
+
+    @Override
+    protected final IdentityRefSpecification createEmptyDeclared(
+            final StmtContext<String, IdentityRefSpecification, ?> ctx) {
+        throw noBase(ctx);
+    }
+
+    @Override
+    protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
+            final StmtContext<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> ctx,
+            final IdentityRefSpecification declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(ctx.getSchemaPath().get());
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BaseEffectiveStatement) {
+                final QName identityQName = ((BaseEffectiveStatement) stmt).argument();
+                final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
+                        ctx.getFromNamespace(IdentityNamespace.class, identityQName);
+                builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected final EffectiveStatement<String, IdentityRefSpecification> createEmptyEffective(
+            final StmtContext<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> ctx,
+            final IdentityRefSpecification declared) {
+        throw noBase(ctx);
+    }
+
+    private static SourceException noBase(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.10.2
+         *
+         *     The "base" statement, which is a substatement to the "type"
+         *     statement, MUST be present at least once if the type is
+         *     "identityref".
+         */
+        return new SourceException("At least one base statement has to be present",
+            ctx.getStatementSourceReference());
+    }
 }
\ No newline at end of file