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