2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
24 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
25 import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
26 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36 abstract class AbstractIdentityRefSpecificationSupport
37 extends AbstractStringStatementSupport<IdentityRefSpecification,
38 EffectiveStatement<String, IdentityRefSpecification>> {
39 AbstractIdentityRefSpecificationSupport() {
40 super(YangStmtMapping.TYPE, StatementPolicy.exactReplica());
44 public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
45 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
46 super.onFullDefinitionDeclared(stmt);
48 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
49 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
50 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
51 final QName baseIdentity = baseStmt.getArgument();
52 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
53 InferenceException.throwIfNull(stmtCtx, stmt,
54 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
55 baseIdentity.getLocalName());
60 protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
61 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
62 return new IdentityRefSpecificationImpl(ctx.getRawArgument(), substatements);
66 protected final IdentityRefSpecification createEmptyDeclared(
67 final StmtContext<String, IdentityRefSpecification, ?> ctx) {
72 protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
73 final Current<String, IdentityRefSpecification> stmt,
74 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
75 if (substatements.isEmpty()) {
79 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
80 for (final EffectiveStatement<?, ?> subStmt : substatements) {
81 if (subStmt instanceof BaseEffectiveStatement) {
82 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
83 final IdentityEffectiveStatement baseIdentity =
84 verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
85 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
87 builder.addIdentity((IdentitySchemaNode) baseIdentity);
91 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
94 private static SourceException noBase(final CommonStmtCtx stmt) {
96 * https://tools.ietf.org/html/rfc7950#section-9.10.2
98 * The "base" statement, which is a substatement to the "type"
99 * statement, MUST be present at least once if the type is
102 return new SourceException("At least one base statement has to be present", stmt);