Merge branch 'master' of ../controller
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
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> Argument type
24  * @param <D> Declared Statement representation
25  * @param <E> Effective Statement representation
26  */
27 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
28         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
29
30     private final @NonNull StatementDefinition type;
31
32     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
33         this.type = requireNonNull(publicDefinition);
34         checkArgument(publicDefinition != this);
35     }
36
37     @Override
38     public final StatementDefinition getPublicView() {
39         return type;
40     }
41
42     @Override
43     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
44         // NOOP for most implementations
45     }
46
47     /**
48      * {@inheritDoc}.
49      *
50      * <p>
51      * Subclasses of this class may override this method to perform actions on this event or register a modification
52      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
53      */
54     @Override
55     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
56         // NOOP for most implementations
57     }
58
59     /**
60      * {@inheritDoc}.
61      *
62      * <p>
63      * Subclasses of this class may override this method to perform actions on this event or register a modification
64      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
65      */
66     @Override
67     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
68         // NOOP for most implementations
69     }
70
71     /**
72      * {@inheritDoc}.
73      *
74      * <p>
75      * Subclasses of this class may override this method to perform actions on this event or register a modification
76      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
77      */
78     @Override
79     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
80         // NOOP for most implementations
81     }
82
83     /**
84      * {@inheritDoc}.
85      *
86      * <p>
87      * Subclasses of this class may override this method to perform actions on this event or register a modification
88      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
89      */
90     @Override
91     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
92         final SubstatementValidator validator = getSubstatementValidator();
93         if (validator != null) {
94             validator.validate(stmt);
95         }
96     }
97
98     @Override
99     public boolean hasArgumentSpecificSupports() {
100         // Most of statement supports don't have any argument specific
101         // supports, so return 'false'.
102         return false;
103     }
104
105     @Override
106     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
107         // Most of statement supports don't have any argument specific
108         // supports, so return null.
109         return null;
110     }
111
112     /**
113      * Returns corresponding substatement validator of a statement support.
114      *
115      * @return substatement validator or null, if substatement validator is not defined
116      */
117     protected abstract @Nullable SubstatementValidator getSubstatementValidator();
118 }