Defer copy decisions to StatementSupport
[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 java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
21
22 /**
23  * Class providing necessary support for processing a YANG statement. This class is intended to be subclassed
24  * by developers who want to add semantic support for a statement to a parser reactor.
25  *
26  * @param <A> Argument type
27  * @param <D> Declared Statement representation
28  * @param <E> 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 @NonNull StatementDefinition type;
34
35     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
36         this.type = requireNonNull(publicDefinition);
37         checkArgument(publicDefinition != this);
38     }
39
40     @Override
41     public final StatementDefinition getPublicView() {
42         return type;
43     }
44
45     @Override
46     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
47         // NOOP for most implementations
48     }
49
50     /**
51      * {@inheritDoc}.
52      *
53      * <p>
54      * Subclasses of this class may override this method to perform actions on this event or register a modification
55      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
56      */
57     @Override
58     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
59         // NOOP for most implementations
60     }
61
62     /**
63      * {@inheritDoc}.
64      *
65      * <p>
66      * Subclasses of this class may override this method to perform actions on this event or register a modification
67      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
68      */
69     @Override
70     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
71         // NOOP for most implementations
72     }
73
74     /**
75      * {@inheritDoc}.
76      *
77      * <p>
78      * Subclasses of this class may override this method to perform actions on this event or register a modification
79      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
80      */
81     @Override
82     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
83         // NOOP for most implementations
84     }
85
86     /**
87      * {@inheritDoc}.
88      *
89      * <p>
90      * Subclasses of this class may override this method to perform actions on this event or register a modification
91      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
92      */
93     @Override
94     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
95         final SubstatementValidator validator = getSubstatementValidator();
96         if (validator != null) {
97             validator.validate(stmt);
98         }
99     }
100
101     @Override
102     public boolean hasArgumentSpecificSupports() {
103         // Most of statement supports don't have any argument specific supports, so return 'false'.
104         return false;
105     }
106
107     @Override
108     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
109         // Most of statement supports don't have any argument specific supports, so return null.
110         return null;
111     }
112
113     @Override
114     public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> stmt,
115             final Mutable<?, ?, ?> parent, final CopyType copyType, final QNameModule targetModule) {
116         // Most of statement supports will just want to copy the statement
117         // FIXME: YANGTOOLS-694: that is not strictly true. Subclasses of this should indicate if they are themselves
118         //                       copy-sensitive:
119         //                       1) if they are not and cannot be targeted by inference, and all their current
120         //                          substatements are also non-sensitive, we want to return the same context.
121         //                       2) if they are not and their current substatements are sensitive, we want to copy
122         //                          as a lazily-instantiated interceptor to let it deal with substatements when needed
123         //                          (YANGTOOLS-1067 prerequisite)
124         //                       3) otherwise perform this eager copy
125         return Optional.of(parent.childCopyOf(stmt, copyType, targetModule));
126     }
127
128     /**
129      * Returns corresponding substatement validator of a statement support.
130      *
131      * @return substatement validator or null, if substatement validator is not defined
132      */
133     protected abstract @Nullable SubstatementValidator getSubstatementValidator();
134 }