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