Retain DeclarationReference in DeclaredStatements
[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.DeclarationReference;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
25 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
26 import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
27 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35
36 abstract class AbstractIdentityRefSpecificationSupport
37         extends AbstractTypeSupport<IdentityRefSpecification> {
38     AbstractIdentityRefSpecificationSupport(final YangParserConfiguration config) {
39         super(config);
40     }
41
42     @Override
43     public final void onFullDefinitionDeclared(final Mutable<QName, IdentityRefSpecification,
44             EffectiveStatement<QName, IdentityRefSpecification>> stmt) {
45         super.onFullDefinitionDeclared(stmt);
46
47         final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
48                 StmtContextUtils.findAllDeclaredSubstatements(stmt, BaseStatement.class);
49         for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
50             final QName baseIdentity = baseStmt.getArgument();
51             final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
52             InferenceException.throwIfNull(stmtCtx, stmt,
53                 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
54                 baseIdentity.getLocalName());
55         }
56     }
57
58     @Override
59     protected final IdentityRefSpecification createDeclared(final StmtContext<QName, IdentityRefSpecification, ?> ctx,
60             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
61         if (substatements.isEmpty()) {
62             throw noBase(ctx);
63         }
64         return new IdentityRefSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
65     }
66
67     @Override
68     protected IdentityRefSpecification attachDeclarationReference(final IdentityRefSpecification stmt,
69             final DeclarationReference reference) {
70         return new RefIdentityRefSpecification(stmt, reference);
71     }
72
73     @Override
74     protected final EffectiveStatement<QName, IdentityRefSpecification> createEffective(
75             final Current<QName, IdentityRefSpecification> stmt,
76             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
77         if (substatements.isEmpty()) {
78             throw noBase(stmt);
79         }
80
81         final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
82         for (final EffectiveStatement<?, ?> subStmt : substatements) {
83             if (subStmt instanceof BaseEffectiveStatement) {
84                 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
85                 final IdentityEffectiveStatement baseIdentity =
86                     verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
87                 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
88                     baseIdentity);
89                 builder.addIdentity((IdentitySchemaNode) baseIdentity);
90             }
91         }
92
93         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
94     }
95
96     private static SourceException noBase(final CommonStmtCtx stmt) {
97         /*
98          *  https://tools.ietf.org/html/rfc7950#section-9.10.2
99          *
100          *     The "base" statement, which is a substatement to the "type"
101          *     statement, MUST be present at least once if the type is
102          *     "identityref".
103          */
104         return new SourceException("At least one base statement has to be present", stmt);
105     }
106 }