*/
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.CommonStmtCtx;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
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);
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) {
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.getStatementSourceReference(),
+ 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,
+ final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+ return new IdentityRefSpecificationImpl(ctx.getRawArgument(), substatements);
+ }
+
+ @Override
+ protected final IdentityRefSpecification createEmptyDeclared(
+ final StmtContext<String, IdentityRefSpecification, ?> ctx) {
+ throw noBase(ctx);
+ }
+
+ @Override
+ protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
+ final Current<String, IdentityRefSpecification> stmt,
+ final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ if (substatements.isEmpty()) {
+ throw noBase(stmt);
+ }
+
+ final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.getSchemaPath());
+ 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());
+ }
+ }
+
+ return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
+ }
+
+ private static SourceException noBase(final CommonStmtCtx stmt) {
+ /*
+ * 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", stmt);
+ }
}
\ No newline at end of file