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