ecb8fbacf55b3170cc54be01d30545ef9f2b1784
[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 java.util.Collection;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
16 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
22
23 abstract class AbstractIdentityRefSpecificationSupport
24         extends AbstractStatementSupport<String, IdentityRefSpecification,
25             EffectiveStatement<String, IdentityRefSpecification>> {
26     AbstractIdentityRefSpecificationSupport() {
27         super(YangStmtMapping.TYPE);
28     }
29
30     @Override
31     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
32         return value;
33     }
34
35     @Override
36     public final IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx) {
37         return new IdentityRefSpecificationImpl(ctx);
38     }
39
40     @Override
41     public final EffectiveStatement<String, IdentityRefSpecification> createEffective(
42             final StmtContext<String, IdentityRefSpecification,
43             EffectiveStatement<String, IdentityRefSpecification>> ctx) {
44         return new IdentityRefSpecificationEffectiveStatement(ctx);
45     }
46
47     @Override
48     public final void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
49             EffectiveStatement<String, IdentityRefSpecification>> stmt) {
50         super.onFullDefinitionDeclared(stmt);
51
52         final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
53                 StmtContextUtils.<QName, BaseStatement>findAllDeclaredSubstatements(stmt, BaseStatement.class);
54         for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
55             final QName baseIdentity = baseStmt.getStatementArgument();
56             final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
57             InferenceException.throwIfNull(stmtCtx, stmt.getStatementSourceReference(),
58                 "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
59                     baseIdentity.getLocalName());
60         }
61     }
62 }