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