Add AbstractIdentityRefSpecificationSupport defences
[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         return new IdentityRefSpecificationImpl(ctx.getRawArgument(), substatements);
63     }
64
65     @Override
66     protected final IdentityRefSpecification createEmptyDeclared(
67             final StmtContext<String, IdentityRefSpecification, ?> ctx) {
68         throw noBase(ctx);
69     }
70
71     @Override
72     protected final EffectiveStatement<String, IdentityRefSpecification> createEffective(
73             final Current<String, IdentityRefSpecification> stmt,
74             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
75         if (substatements.isEmpty()) {
76             throw noBase(stmt);
77         }
78
79         final IdentityrefTypeBuilder builder = BaseTypes.identityrefTypeBuilder(stmt.argumentAsTypeQName());
80         for (final EffectiveStatement<?, ?> subStmt : substatements) {
81             if (subStmt instanceof BaseEffectiveStatement) {
82                 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
83                 final IdentityEffectiveStatement baseIdentity =
84                     verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
85                 verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
86                     baseIdentity);
87                 builder.addIdentity((IdentitySchemaNode) baseIdentity);
88             }
89         }
90
91         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
92     }
93
94     private static SourceException noBase(final CommonStmtCtx stmt) {
95         /*
96          *  https://tools.ietf.org/html/rfc7950#section-9.10.2
97          *
98          *     The "base" statement, which is a substatement to the "type"
99          *     statement, MUST be present at least once if the type is
100          *     "identityref".
101          */
102         return new SourceException("At least one base statement has to be present", stmt);
103     }
104 }