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.meta.DeclarationReference;
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.api.YangParserConfiguration;
27 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
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 AbstractTypeSupport<IdentityRefSpecification> {
38 AbstractIdentityRefSpecificationSupport(final YangParserConfiguration config) {
43 public final void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
44 EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
45 super.onFullDefinitionDeclared(stmt);
47 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
48 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
49 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
50 final QName baseIdentity = baseStmt.getArgument();
51 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
52 InferenceException.throwIfNull(stmtCtx, stmt,
53 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
54 baseIdentity.getLocalName());
59 protected final IdentityRefSpecification createDeclared(final StmtContext<QName, IdentityRefSpecification, ?> ctx,
60 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
61 if (substatements.isEmpty()) {
64 return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
68 protected IdentityRefSpecification attachDeclarationReference(final IdentityRefSpecification stmt,
69 final DeclarationReference reference) {
70 return new RefIdentityRefSpecification(stmt, reference);
74 protected final EffectiveStatement<QName, IdentityRefSpecification> createEffective(
75 final Current<QName, IdentityRefSpecification> stmt,
76 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
77 if (substatements.isEmpty()) {
81 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
82 for (final EffectiveStatement<?, ?> subStmt : substatements) {
83 if (subStmt instanceof BaseEffectiveStatement) {
84 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
85 final IdentityEffectiveStatement baseIdentity =
86 verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
87 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
89 builder.addIdentity((IdentitySchemaNode) baseIdentity);
93 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
96 private static SourceException noBase(final CommonStmtCtx stmt) {
98 * https://tools.ietf.org/html/rfc7950#section-9.10.2
100 * The "base" statement, which is a substatement to the "type"
101 * statement, MUST be present at least once if the type is
104 return new SourceException("At least one base statement has to be present", stmt);