Bug 7051 - moving of SubstatementValidator into spi.meta package
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractStatementSupport.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.spi.meta;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Optional;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementDefinitionContext;
21
22 /**
23  *
24  * Class providing necessary support for processing YANG statement.
25  *
26  * This class is intended to be subclassed by developers, which want to
27  * introduce support of statement to parser.
28  *
29  * @param <A>
30  *            Argument type
31  * @param <D>
32  *            Declared Statement representation
33  * @param <E>
34  *            Effective Statement representation
35  */
36 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
37         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
38
39     private final StatementDefinition type;
40
41     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
42         Preconditions.checkArgument(publicDefinition != this);
43         this.type = Preconditions.checkNotNull(publicDefinition);
44     }
45
46     @Nonnull
47     @Override
48     public final QName getStatementName() {
49         return type.getStatementName();
50     }
51
52     @Override
53     public final QName getArgumentName() {
54         return type.getArgumentName();
55     }
56
57     @Nonnull
58     @Override
59     public final Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
60         return type.getDeclaredRepresentationClass();
61     }
62
63     @Nonnull
64     @Override
65     public final Class<? extends EffectiveStatement<?,?>> getEffectiveRepresentationClass() {
66         return type.getEffectiveRepresentationClass();
67     }
68
69     @Override
70     public final StatementDefinition getPublicView() {
71         return type;
72     }
73
74     @Override
75     public Optional<StatementContextBase<?, ?, ?>> beforeSubStatementCreated(final StmtContext.Mutable<?, ?, ?> stmt,
76             final int offset, final StatementDefinitionContext<?, ?, ?> def, final StatementSourceReference ref,
77             final String argument) {
78         // NOOP for most implementations and also no implicit statements
79         return Optional.empty();
80     }
81
82     @Override
83     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
84         // NOOP for most implementations
85     }
86
87     /**
88      * {@inheritDoc}
89      *
90      * Subclasses of this class may override this method to perform actions on
91      * this event or register modification action using
92      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
93      */
94     @Override
95     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
96         // NOOP for most implementations
97     }
98
99     /**
100      * {@inheritDoc}
101      *
102      * Subclasses of this class may override this method to perform actions on
103      * this event or register modification action using
104      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
105      */
106     @Override
107     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
108         // NOOP for most implementations
109     }
110
111     /**
112      * {@inheritDoc}
113      *
114      * Subclasses of this class may override this method to perform actions on
115      * this event or register modification action using
116      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
117      */
118     @Override
119     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
120         // NOOP for most implementations
121     }
122
123     /**
124      * {@inheritDoc}
125      *
126      * Subclasses of this class may override this method to perform actions on
127      * this event or register modification action using
128      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
129      */
130     @Override
131     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
132         final SubstatementValidator validator = getSubstatementValidator();
133         if (validator != null) {
134             validator.validate(stmt);
135         }
136     }
137
138     @Override
139     public boolean isArgumentYinElement() {
140         return getPublicView().isArgumentYinElement();
141     }
142
143     @Override
144     public boolean hasArgumentSpecificSupports() {
145         // Most of statement supports don't have any argument specific
146         // supports, so return 'false'.
147         return false;
148     }
149
150     @Override
151     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
152         // Most of statement supports don't have any argument specific
153         // supports, so return null.
154         return null;
155     }
156
157     /**
158      * Returns corresponding substatement validator of a statement support
159      *
160      * @return substatement validator or null, if substatement validator is not
161      *         defined
162      */
163     @Nullable
164     protected abstract SubstatementValidator getSubstatementValidator();
165 }