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