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.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
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.CommonStmtCtx;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 abstract class AbstractIdentityRefSpecificationSupport
36 extends AbstractTypeSupport<IdentityRefSpecification> {
37 AbstractIdentityRefSpecificationSupport(final YangParserConfiguration config) {
42 public final void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
43 EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
44 super.onFullDefinitionDeclared(stmt);
46 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
47 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
48 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
49 final QName baseIdentity = baseStmt.getArgument();
50 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
51 InferenceException.throwIfNull(stmtCtx, stmt,
52 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
53 baseIdentity.getLocalName());
58 protected final IdentityRefSpecification createDeclared(final StmtContext<QName, IdentityRefSpecification, ?> ctx,
59 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
60 if (substatements.isEmpty()) {
63 return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
67 protected final EffectiveStatement<QName, IdentityRefSpecification> createEffective(
68 final Current<QName, IdentityRefSpecification> stmt,
69 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
70 if (substatements.isEmpty()) {
74 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
75 for (final EffectiveStatement<?, ?> subStmt : substatements) {
76 if (subStmt instanceof BaseEffectiveStatement) {
77 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
78 final IdentityEffectiveStatement baseIdentity =
79 verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
80 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
82 builder.addIdentity((IdentitySchemaNode) baseIdentity);
86 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
89 private static SourceException noBase(final CommonStmtCtx stmt) {
91 * https://tools.ietf.org/html/rfc7950#section-9.10.2
93 * The "base" statement, which is a substatement to the "type"
94 * statement, MUST be present at least once if the type is
97 return new SourceException("At least one base statement has to be present", stmt);