Bug 6897: Adding getSubstatementValidator() method to AbstractStatementSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / MustStatementImpl.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 javax.annotation.Nonnull;
11 import javax.annotation.Nullable;
12 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
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.DescriptionStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorAppTagStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorMessageStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.MustStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
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.stmt.rfc6020.effective.MustEffectiveStatementImpl;
25
26 public class MustStatementImpl extends AbstractDeclaredStatement<RevisionAwareXPath> implements MustStatement {
27     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
28             .MUST)
29             .addOptional(YangStmtMapping.DESCRIPTION)
30             .addOptional(YangStmtMapping.ERROR_APP_TAG)
31             .addOptional(YangStmtMapping.ERROR_MESSAGE)
32             .addOptional(YangStmtMapping.REFERENCE)
33             .build();
34
35     protected MustStatementImpl(final StmtContext<RevisionAwareXPath, MustStatement, ?> context) {
36         super(context);
37     }
38
39     public static class Definition extends
40             AbstractStatementSupport<RevisionAwareXPath, MustStatement, EffectiveStatement<RevisionAwareXPath, MustStatement>> {
41
42         public Definition() {
43             super(YangStmtMapping.MUST);
44         }
45
46         @Override
47         public RevisionAwareXPath parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
48             return Utils.parseXPath(ctx, value);
49         }
50
51         @Override
52         public MustStatement createDeclared(final StmtContext<RevisionAwareXPath, MustStatement, ?> ctx) {
53             return new MustStatementImpl(ctx);
54         }
55
56         @Override
57         public EffectiveStatement<RevisionAwareXPath, MustStatement> createEffective(
58                 final StmtContext<RevisionAwareXPath, MustStatement, EffectiveStatement<RevisionAwareXPath, MustStatement>> ctx) {
59             return new MustEffectiveStatementImpl(ctx);
60         }
61
62         @Override
63         public void onFullDefinitionDeclared(final StmtContext.Mutable<RevisionAwareXPath, MustStatement,
64                 EffectiveStatement<RevisionAwareXPath, MustStatement>> stmt) {
65             super.onFullDefinitionDeclared(stmt);
66             getSubstatementValidator().validate(stmt);
67         }
68
69         @Override
70         protected SubstatementValidator getSubstatementValidator() {
71             return SUBSTATEMENT_VALIDATOR;
72         }
73     }
74
75     @Nonnull
76     @Override
77     public RevisionAwareXPath getCondition() {
78         return argument();
79     }
80
81     @Nullable
82     @Override
83     public ErrorAppTagStatement getErrorAppTagStatement() {
84         return firstDeclared(ErrorAppTagStatement.class);
85     }
86
87     @Nullable
88     @Override
89     public ErrorMessageStatement getErrorMessageStatement() {
90         return firstDeclared(ErrorMessageStatement.class);
91     }
92
93     @Nullable
94     @Override
95     public DescriptionStatement getDescription() {
96         return firstDeclared(DescriptionStatement.class);
97     }
98
99     @Nullable
100     @Override
101     public ReferenceStatement getReference() {
102         return firstDeclared(ReferenceStatement.class);
103     }
104 }