Propagate @Nonnull and @Nullable annotations
[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.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl;
25
26 public class IdentityRefSpecificationImpl extends AbstractDeclaredStatement<String> implements TypeStatement.IdentityRefSpecification {
27     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
28             .TYPE)
29             .addMandatory(Rfc6020Mapping.BASE)
30             .build();
31
32     protected IdentityRefSpecificationImpl(
33             final StmtContext<String, TypeStatement.IdentityRefSpecification, ?> context) {
34         super(context);
35     }
36
37     public static class Definition
38             extends
39             AbstractStatementSupport<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement.IdentityRefSpecification>> {
40
41         public Definition() {
42             super(Rfc6020Mapping.TYPE);
43         }
44
45         @Override
46         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47             return value;
48         }
49
50         @Override
51         public TypeStatement.IdentityRefSpecification createDeclared(
52                 final StmtContext<String, TypeStatement.IdentityRefSpecification, ?> ctx) {
53             return new IdentityRefSpecificationImpl(ctx);
54         }
55
56         @Override
57         public EffectiveStatement<String, TypeStatement.IdentityRefSpecification> createEffective(
58                 final StmtContext<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement
59                         .IdentityRefSpecification>> ctx) {
60             return new IdentityRefSpecificationEffectiveStatementImpl(ctx);
61         }
62
63         @Override
64         public void onFullDefinitionDeclared(final StmtContext.Mutable<String, IdentityRefSpecification,
65                 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
66             final StmtContext<QName, ?, ?> baseStmt = StmtContextUtils.findFirstDeclaredSubstatement(stmt,
67                     BaseStatement.class);
68             Preconditions.checkArgument(baseStmt != null, "The \"base\" statement, which is a substatement to the " +
69                     "\"type\"\n statement, MUST be present if the type is \"identityref\" in source '%s'", stmt
70                     .getStatementSourceReference());
71             final QName baseIdentity = baseStmt.getStatementArgument();
72             final StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> stmtCtx = stmt
73                     .getFromNamespace(IdentityNamespace.class, baseIdentity);
74             Preconditions.checkArgument(stmtCtx != null, "Referenced base identity '%s' doesn't exist " +
75                         "in " + "given scope " + "(module, imported submodules), source: '%s'", baseIdentity
76                     .getLocalName(), stmt.getStatementSourceReference());
77         }
78
79         @Override
80         public void onStatementDefinitionDeclared(final StmtContext.Mutable<String, IdentityRefSpecification,
81                 EffectiveStatement<String, IdentityRefSpecification>> stmt) {
82             super.onStatementDefinitionDeclared(stmt);
83             SUBSTATEMENT_VALIDATOR.validate(stmt);
84         }
85     }
86
87     @Nonnull
88     @Override
89     public String getName() {
90         return argument();
91     }
92
93     @Nonnull
94     @Override
95     public BaseStatement getBase() {
96         return firstDeclared(BaseStatement.class);
97     }
98
99 }