TypeStatement takes a QName argument
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractIdentityRefSpecificationSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12
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.ri.type.BaseTypes;
24 import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
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;
33
34 abstract class AbstractIdentityRefSpecificationSupport
35         extends AbstractTypeSupport<IdentityRefSpecification> {
36     @Override
37     public final void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
38             EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
39         super.onFullDefinitionDeclared(stmt);
40
41         final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
42                 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
43         for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
44             final QName baseIdentity = baseStmt.getArgument();
45             final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
46             InferenceException.throwIfNull(stmtCtx, stmt,
47                 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
48                 baseIdentity.getLocalName());
49         }
50     }
51
52     @Override
53     protected final IdentityRefSpecification createDeclared(final StmtContext<QName, IdentityRefSpecification, ?> ctx,
54             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
55         if (substatements.isEmpty()) {
56             throw noBase(ctx);
57         }
58         return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
59     }
60
61     @Override
62     protected final EffectiveStatement<QName, IdentityRefSpecification> createEffective(
63             final Current<QName, IdentityRefSpecification> stmt,
64             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
65         if (substatements.isEmpty()) {
66             throw noBase(stmt);
67         }
68
69         final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
70         for (final EffectiveStatement<?, ?> subStmt : substatements) {
71             if (subStmt instanceof BaseEffectiveStatement) {
72                 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
73                 final IdentityEffectiveStatement baseIdentity =
74                     verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
75                 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
76                     baseIdentity);
77                 builder.addIdentity((IdentitySchemaNode) baseIdentity);
78             }
79         }
80
81         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
82     }
83
84     private static SourceException noBase(final CommonStmtCtx stmt) {
85         /*
86          *  https://tools.ietf.org/html/rfc7950#section-9.10.2
87          *
88          *     The "base" statement, which is a substatement to the "type"
89          *     statement, MUST be present at least once if the type is
90          *     "identityref".
91          */
92         return new SourceException("At least one base statement has to be present", stmt);
93     }
94 }