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.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
26 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
27 import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
28 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
29 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 final class IdentityRefSpecificationSupport extends AbstractTypeSupport<IdentityRefSpecification> {
41 private static final SubstatementValidator RFC6020_VALIDATOR =
42 SubstatementValidator.builder(YangStmtMapping.TYPE).addMandatory(YangStmtMapping.BASE).build();
43 private static final SubstatementValidator RFC7950_VALIDATOR =
44 SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.BASE).build();
46 private IdentityRefSpecificationSupport(final YangParserConfiguration config,
47 final SubstatementValidator validator) {
48 super(config, validator);
51 static @NonNull IdentityRefSpecificationSupport rfc6020Instance(final YangParserConfiguration config) {
52 return new IdentityRefSpecificationSupport(config, RFC6020_VALIDATOR);
55 static @NonNull IdentityRefSpecificationSupport rfc7950Instance(final YangParserConfiguration config) {
56 return new IdentityRefSpecificationSupport(config, RFC7950_VALIDATOR);
60 public void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
61 EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
62 super.onFullDefinitionDeclared(stmt);
64 final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
65 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
66 for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
67 final QName baseIdentity = baseStmt.getArgument();
68 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
69 InferenceException.throwIfNull(stmtCtx, stmt,
70 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
71 baseIdentity.getLocalName());
76 protected IdentityRefSpecification createDeclared(final BoundStmtCtx<QName> ctx,
77 final ImmutableList<DeclaredStatement<?>> substatements) {
78 if (substatements.isEmpty()) {
81 return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
85 protected IdentityRefSpecification attachDeclarationReference(final IdentityRefSpecification stmt,
86 final DeclarationReference reference) {
87 return new RefIdentityRefSpecification(stmt, reference);
91 protected EffectiveStatement<QName, IdentityRefSpecification> createEffective(
92 final Current<QName, IdentityRefSpecification> stmt,
93 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
94 if (substatements.isEmpty()) {
98 final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
99 for (final EffectiveStatement<?, ?> subStmt : substatements) {
100 if (subStmt instanceof BaseEffectiveStatement) {
101 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
102 final IdentityEffectiveStatement baseIdentity =
103 verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
104 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
106 builder.addIdentity((IdentitySchemaNode) baseIdentity);
110 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
113 private static SourceException noBase(final CommonStmtCtx stmt) {
115 * https://tools.ietf.org/html/rfc7950#section-9.10.2
117 * The "base" statement, which is a substatement to the "type"
118 * statement, MUST be present at least once if the type is
121 return new SourceException("At least one base statement has to be present", stmt);