2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
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.
26 * @param <A> Argument type
27 * @param <D> Declared Statement representation
28 * @param <E> Effective Statement representation
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> {
33 private final @NonNull StatementDefinition type;
35 protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
36 this.type = requireNonNull(publicDefinition);
37 checkArgument(publicDefinition != this);
41 public final StatementDefinition getPublicView() {
46 public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
47 // NOOP for most implementations
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)}.
58 public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
59 // NOOP for most implementations
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)}.
70 public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
71 // NOOP for most implementations
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)}.
82 public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
83 // NOOP for most implementations
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)}.
94 public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
95 final SubstatementValidator validator = getSubstatementValidator();
96 if (validator != null) {
97 validator.validate(stmt);
102 public boolean hasArgumentSpecificSupports() {
103 // Most of statement supports don't have any argument specific supports, so return 'false'.
108 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
109 // Most of statement supports don't have any argument specific supports, so return null.
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
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));
129 * Returns corresponding substatement validator of a statement support.
131 * @return substatement validator or null, if substatement validator is not defined
133 protected abstract @Nullable SubstatementValidator getSubstatementValidator();