BUG-6757: revert fix for BUG-4456
[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 javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
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.IdentityStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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(Rfc6020Mapping
29             .TYPE)
30             .add(Rfc6020Mapping.BASE, 1, 1)
31             .build();
32
33     protected IdentityRefSpecificationImpl(
34             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(Rfc6020Mapping.TYPE);
44         }
45
46         @Override
47         public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value)
48                 throws SourceException {
49             return value;
50         }
51
52         @Override
53         public TypeStatement.IdentityRefSpecification createDeclared(
54                 StmtContext<String, TypeStatement.IdentityRefSpecification, ?> ctx) {
55             return new IdentityRefSpecificationImpl(ctx);
56         }
57
58         @Override
59         public EffectiveStatement<String, TypeStatement.IdentityRefSpecification> createEffective(
60                 StmtContext<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement
61                         .IdentityRefSpecification>> ctx) {
62             return new IdentityRefSpecificationEffectiveStatementImpl(ctx);
63         }
64
65         @Override
66         public void onFullDefinitionDeclared(StmtContext.Mutable<String, IdentityRefSpecification,
67                 EffectiveStatement<String, IdentityRefSpecification>> stmt) throws SourceException {
68             final StmtContext<QName, ?, ?> baseStmt = StmtContextUtils.findFirstDeclaredSubstatement(stmt,
69                     BaseStatement.class);
70             Preconditions.checkArgument(baseStmt != null, "The \"base\" statement, which is a substatement to the " +
71                     "\"type\"\n statement, MUST be present if the type is \"identityref\" in source '%s'", stmt
72                     .getStatementSourceReference());
73             final QName baseIdentity = baseStmt.getStatementArgument();
74             final StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> stmtCtx = stmt
75                     .getFromNamespace(IdentityNamespace.class, baseIdentity);
76             Preconditions.checkArgument(stmtCtx != null, "Referenced base identity '%s' doesn't exist " +
77                         "in " + "given scope " + "(module, imported submodules), source: '%s'", baseIdentity
78                     .getLocalName(), stmt.getStatementSourceReference());
79         }
80
81         @Override
82         public void onStatementDefinitionDeclared(StmtContext.Mutable<String, IdentityRefSpecification,
83                 EffectiveStatement<String, IdentityRefSpecification>> stmt) throws SourceException {
84             super.onStatementDefinitionDeclared(stmt);
85             SUBSTATEMENT_VALIDATOR.validate(stmt);
86         }
87     }
88
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 }