YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IdentityRefSpecificationImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import java.util.Collection;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
17 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl;
26
27 public class IdentityRefSpecificationImpl extends AbstractDeclaredStatement<String>
28         implements IdentityRefSpecification {
29     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
30             .TYPE)
31             .addMandatory(YangStmtMapping.BASE)
32             .build();
33
34     protected IdentityRefSpecificationImpl(final StmtContext<String, IdentityRefSpecification, ?> context) {
35         super(context);
36     }
37
38     public static class Definition extends AbstractStatementSupport<String, IdentityRefSpecification,
39             EffectiveStatement<String, IdentityRefSpecification>> {
40
41         public Definition() {
42             super(YangStmtMapping.TYPE);
43         }
44
45         @Override
46         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47             return value;
48         }
49
50         @Override
51         public IdentityRefSpecification createDeclared(final StmtContext<String, IdentityRefSpecification, ?> ctx) {
52             return new IdentityRefSpecificationImpl(ctx);
53         }
54
55         @Override
56         public EffectiveStatement<String, IdentityRefSpecification> createEffective(
57                 final StmtContext<String, IdentityRefSpecification,
58                 EffectiveStatement<String, IdentityRefSpecification>> ctx) {
59             return new IdentityRefSpecificationEffectiveStatementImpl(ctx);
60         }
61
62         @Override
63         public void onFullDefinitionDeclared(final Mutable<String, IdentityRefSpecification,
64                 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
65             super.onFullDefinitionDeclared(stmt);
66
67             final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
68                     StmtContextUtils.<QName, BaseStatement>findAllDeclaredSubstatements(stmt, BaseStatement.class);
69             for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
70                 final QName baseIdentity = baseStmt.getStatementArgument();
71                 final StmtContext<?, ?, ?> stmtCtx = stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
72                 InferenceException.throwIfNull(stmtCtx, stmt.getStatementSourceReference(),
73                     "Referenced base identity '%s' doesn't exist in given scope (module, imported modules, submodules)",
74                         baseIdentity.getLocalName());
75             }
76         }
77
78         @Override
79         protected SubstatementValidator getSubstatementValidator() {
80             return SUBSTATEMENT_VALIDATOR;
81         }
82     }
83
84     @Nonnull
85     @Override
86     public String getName() {
87         return argument();
88     }
89
90     @Nonnull
91     @Override
92     public Collection<? extends BaseStatement> getBases() {
93         return allDeclared(BaseStatement.class);
94     }
95 }