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