Bug 6878: Add support for parsing yang-specific XPath functions
[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 com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
20 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl;
26
27 public class IdentityRefSpecificationImpl extends AbstractDeclaredStatement<String> implements TypeStatement.IdentityRefSpecification {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
29             .TYPE)
30             .addMandatory(YangStmtMapping.BASE)
31             .build();
32
33     protected IdentityRefSpecificationImpl(
34             final StmtContext<String, TypeStatement.IdentityRefSpecification, ?> context) {
35         super(context);
36     }
37
38     public static class Definition
39             extends
40             AbstractStatementSupport<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement.IdentityRefSpecification>> {
41
42         public Definition() {
43             super(YangStmtMapping.TYPE);
44         }
45
46         @Override
47         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
48             return value;
49         }
50
51         @Override
52         public TypeStatement.IdentityRefSpecification createDeclared(
53                 final StmtContext<String, TypeStatement.IdentityRefSpecification, ?> ctx) {
54             return new IdentityRefSpecificationImpl(ctx);
55         }
56
57         @Override
58         public EffectiveStatement<String, TypeStatement.IdentityRefSpecification> createEffective(
59                 final StmtContext<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement
60                         .IdentityRefSpecification>> ctx) {
61             return new IdentityRefSpecificationEffectiveStatementImpl(ctx);
62         }
63
64         @Override
65         public void onFullDefinitionDeclared(final StmtContext.Mutable<String, IdentityRefSpecification,
66                 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
67             super.onFullDefinitionDeclared(stmt);
68
69             final Collection<StmtContext<QName, BaseStatement, ?>> baseStatements =
70                     StmtContextUtils.<QName, BaseStatement>findAllDeclaredSubstatements(stmt, BaseStatement.class);
71
72             for (StmtContext<QName, BaseStatement, ?> baseStmt : baseStatements) {
73                 final QName baseIdentity = baseStmt.getStatementArgument();
74                 final StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> stmtCtx =
75                         stmt.getFromNamespace(IdentityNamespace.class, baseIdentity);
76                 Preconditions.checkArgument(stmtCtx != null, "Referenced base identity '%s' doesn't exist " +
77                         "in given scope (module, imported modules, submodules), source: '%s'",
78                         baseIdentity.getLocalName(), stmt.getStatementSourceReference());
79             }
80         }
81
82         @Override
83         protected SubstatementValidator getSubstatementValidator() {
84             return SUBSTATEMENT_VALIDATOR;
85         }
86     }
87
88     @Nonnull
89     @Override
90     public String getName() {
91         return argument();
92     }
93
94     @Nonnull
95     @Override
96     public BaseStatement getBase() {
97         return firstDeclared(BaseStatement.class);
98     }
99
100     @Nonnull
101     @Override
102     public Collection<? extends BaseStatement> getBases() {
103         return allDeclared(BaseStatement.class);
104     }
105
106 }