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.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 abstract class AbstractIdentityRefSpecificationSupport
33 extends BaseStatementSupport<String, IdentityRefSpecification,
34 EffectiveStatement<String, IdentityRefSpecification>> {
35 AbstractIdentityRefSpecificationSupport() {
36 super(YangStmtMapping.TYPE);
40 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45 public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
46 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
47 super.onFullDefinitionDeclared(stmt);
49 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
50 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
51 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
52 final QName baseIdentity = baseStmt.coerceStatementArgument();
53 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
54 InferenceException.throwIfNull(stmtCtx, stmt.getStatementSourceReference(),
55 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
56 baseIdentity.getLocalName());
61 protected final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx,
62 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
63 return new IdentityRefSpecificationImpl(ctx, substatements);
67 protected final IdentityRefSpecification createEmptyDeclared(
68 final StmtContext<String, IdentityRefSpecification, ?> ctx) {
73 protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
74 final StmtContext<String, IdentityRefSpecification,
75 EffectiveStatement<String, IdentityRefSpecification>> ctx,
76 final IdentityRefSpecification declared,
77 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
78 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(ctx.getSchemaPath().get());
79 for (final EffectiveStatement<?, ?> stmt : substatements) {
80 if (stmt instanceof BaseEffectiveStatement) {
81 final QName identityQName = ((BaseEffectiveStatement) stmt).argument();
82 final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
83 ctx.getFromNamespace(IdentityNamespace.class, identityQName);
84 builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
88 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
92 protected final EffectiveStatement<String, IdentityRefSpecification> createEmptyEffective(
93 final StmtContext<String, IdentityRefSpecification,
94 EffectiveStatement<String, IdentityRefSpecification>> ctx,
95 final IdentityRefSpecification declared) {
99 private static SourceException noBase(final StmtContext<?, ?, ?> ctx) {
101 * https://tools.ietf.org/html/rfc7950#section-9.10.2
103 * The "base" statement, which is a substatement to the "type"
104 * statement, MUST be present at least once if the type is
107 return new SourceException("At least one base statement has to be present",
108 ctx.getStatementSourceReference());