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 com.google.common.collect.ImmutableList;
11 import java.util.Collection;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
22 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
23 import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 abstract class AbstractIdentityRefSpecificationSupport
35 extends BaseStatementSupport<String, IdentityRefSpecification,
36 EffectiveStatement<String, IdentityRefSpecification>> {
37 AbstractIdentityRefSpecificationSupport() {
38 super(YangStmtMapping.TYPE);
42 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47 public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
48 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
49 super.onFullDefinitionDeclared(stmt);
51 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
52 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
53 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
54 final QName baseIdentity = baseStmt.coerceStatementArgument();
55 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
56 InferenceException.throwIfNull(stmtCtx, stmt.sourceReference(),
57 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
58 baseIdentity.getLocalName());
63 protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
64 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
65 return new IdentityRefSpecificationImpl(ctx.getRawArgument(), substatements);
69 protected final IdentityRefSpecification createEmptyDeclared(
70 final StmtContext<String, IdentityRefSpecification, ?> ctx) {
75 protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
76 final Current<String, IdentityRefSpecification> stmt,
77 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
78 if (substatements.isEmpty()) {
82 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.getSchemaPath());
83 for (final EffectiveStatement<?, ?> subStmt : substatements) {
84 if (subStmt instanceof BaseEffectiveStatement) {
85 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
86 final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
87 stmt.getFromNamespace(IdentityNamespace.class, identityQName);
88 builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
92 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
95 private static SourceException noBase(final CommonStmtCtx stmt) {
97 * https://tools.ietf.org/html/rfc7950#section-9.10.2
99 * The "base" statement, which is a substatement to the "type"
100 * statement, MUST be present at least once if the type is
103 return new SourceException("At least one base statement has to be present", stmt.sourceReference());