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