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 com.google.common.annotations.Beta;
14 import com.google.common.base.VerifyException;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 * Support for processing concrete YANG statement.
34 * This interface is intended to be implemented by developers, which want to introduce support of statement to parser.
35 * Consider subclassing {@link AbstractStatementSupport} for easier implementation of this interface.
37 * @param <A> Argument type
38 * @param <D> Declared Statement representation
39 * @param <E> Effective Statement representation
41 public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
42 implements StatementDefinition, StatementFactory<A, D, E> {
44 * A baseline class for implementing the {@link StatementFactory#canReuseCurrent(Current, Current, Collection)}
45 * contract in a manner which is consistent with a statement's {@link CopyPolicy}.
47 * @param <A> Argument type
48 * @param <D> Declared Statement representation
50 public abstract static class StatementPolicy<A, D extends DeclaredStatement<A>> implements Immutable {
51 final @NonNull CopyPolicy copyPolicy;
53 StatementPolicy(final CopyPolicy copyPolicy) {
54 this.copyPolicy = requireNonNull(copyPolicy);
58 * Return a {@link StatementPolicy} for {@link CopyPolicy#CONTEXT_INDEPENDENT}.
60 * @param <A> Argument type
61 * @param <D> Declared Statement representation
62 * @return Context-independent policy
64 @SuppressWarnings("unchecked")
65 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> contextIndependent() {
66 return (StatementPolicy<A, D>) EqualSemantics.CONTEXT_INDEPENDENT;
70 * Return a {@link StatementPolicy} for {@link CopyPolicy#EXACT_REPLICA}.
72 * @param <A> Argument type
73 * @param <D> Declared Statement representation
74 * @return Exact-replica policy
76 @SuppressWarnings("unchecked")
77 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> exactReplica() {
78 return (StatementPolicy<A, D>) EqualSemantics.EXACT_REPLICA;
82 * Return a {@link StatementPolicy} for {@link CopyPolicy#IGNORE}.
84 * @param <A> Argument type
85 * @param <D> Declared Statement representation
86 * @return Ignoring policy
88 @SuppressWarnings("unchecked")
89 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> ignore() {
90 return (StatementPolicy<A, D>) AlwaysFail.IGNORE;
94 * Return a {@link StatementPolicy} for {@link CopyPolicy#REJECT}.
96 * @param <A> Argument type
97 * @param <D> Declared Statement representation
98 * @return Rejecting statement policy
100 @SuppressWarnings("unchecked")
101 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> reject() {
102 return (StatementPolicy<A, D>) AlwaysFail.REJECT;
106 * Return a {@link StatementPolicy} for {@link CopyPolicy#DECLARED_COPY}, deferring to a
107 * {@link StatementEquality} for individual decisions.
109 * @param <A> Argument type
110 * @param <D> Declared Statement representation
111 * @param equality {@link StatementEquality} to apply to effective statements
112 * @return Equality-based statement policy
114 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> copyDeclared(
115 final @NonNull StatementEquality<A, D> equality) {
116 return new EqualSemantics<>(equality);
120 * Return a {@link StatementPolicy} for {@link CopyPolicy#DECLARED_COPY}, always performing a copy operation.
122 * @param <A> Argument type
123 * @param <D> Declared Statement representation
124 * @return Rejecting statement policy
126 @SuppressWarnings("unchecked")
127 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> alwaysCopyDeclared() {
128 return (StatementPolicy<A, D>) EqualSemantics.ALWAYS_COPY;
131 @Deprecated(forRemoval = true)
132 // FIXME: 7.0.0: remove this method
133 public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> legacyDeclaredCopy() {
134 return alwaysCopyDeclared();
137 abstract boolean canReuseCurrent(@NonNull Current<A, D> copy, @NonNull Current<A, D> current,
138 @NonNull Collection<? extends EffectiveStatement<?, ?>> substatements);
140 private static final class AlwaysFail<A, D extends DeclaredStatement<A>> extends StatementPolicy<A, D> {
141 static final @NonNull AlwaysFail<?, ?> IGNORE = new AlwaysFail<>(CopyPolicy.IGNORE);
142 static final @NonNull AlwaysFail<?, ?> REJECT = new AlwaysFail<>(CopyPolicy.REJECT);
144 private AlwaysFail(final CopyPolicy copyPolicy) {
149 boolean canReuseCurrent(final Current<A, D> copy, final Current<A, D> current,
150 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
151 throw new VerifyException("This implementation should never be invoked");
155 private static final class EqualSemantics<A, D extends DeclaredStatement<A>> extends StatementPolicy<A, D> {
156 static final @NonNull EqualSemantics<?, ?> ALWAYS_COPY =
157 new EqualSemantics<>((copy, stmt, substatements) -> false);
158 static final @NonNull EqualSemantics<?, ?> CONTEXT_INDEPENDENT =
159 new EqualSemantics<>(CopyPolicy.CONTEXT_INDEPENDENT, (copy, stmt, substatements) -> true);
160 static final @NonNull EqualSemantics<?, ?> EXACT_REPLICA =
161 new EqualSemantics<>(CopyPolicy.EXACT_REPLICA, (copy, stmt, substatements) -> true);
163 private final @NonNull StatementEquality<A, D> equality;
165 private EqualSemantics(final CopyPolicy copyPolicy, final StatementEquality<A, D> equality) {
167 this.equality = requireNonNull(equality);
170 EqualSemantics(final StatementEquality<A, D> equality) {
171 this(CopyPolicy.DECLARED_COPY, equality);
175 boolean canReuseCurrent(final Current<A, D> copy, final Current<A, D> current,
176 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
177 return equality.canReuseCurrent(copy, current, substatements);
183 * Abstract base class for comparators associated with statements with a {@link CopyPolicy#DECLARED_COPY} copy
186 * @param <A> Argument type
187 * @param <D> Declared Statement representation
190 public interface StatementEquality<A, D extends DeclaredStatement<A>> {
192 * Determine whether {@code current} statement has the same semantics as the provided copy. See the contract
193 * specification of {@link StatementFactory#canReuseCurrent(Current, Current, Collection)}.
195 * @param copy Copy of current effective context
196 * @param current Current effective context
197 * @param substatements Current effective substatements
198 * @return True if {@code current} can be reused in place of {@code copy}, false if the copy needs to be used.
200 boolean canReuseCurrent(@NonNull Current<A, D> copy, @NonNull Current<A, D> current,
201 @NonNull Collection<? extends EffectiveStatement<?, ?>> substatements);
204 private final @NonNull StatementPolicy<A, D> policy;
205 private final @NonNull StatementDefinition def;
206 private final @NonNull CopyPolicy copyPolicy;
209 protected StatementSupport(final StatementSupport<A, D, E> delegate) {
210 checkArgument(delegate != this);
211 this.def = delegate.def;
212 this.policy = delegate.policy;
213 this.copyPolicy = delegate.copyPolicy;
217 protected StatementSupport(final StatementDefinition publicDefinition, final StatementPolicy<A, D> policy) {
218 checkArgument(publicDefinition != this);
219 this.def = requireNonNull(publicDefinition);
220 this.policy = requireNonNull(policy);
221 this.copyPolicy = policy.copyPolicy;
225 * Returns public statement definition, which will be present in built statements.
228 * Public statement definition may be used to provide different implementation of statement definition,
229 * which will not retain any build specific data or context.
231 * @return public statement definition, which will be present in built statements.
233 public final @NonNull StatementDefinition getPublicView() {
238 * Return this statement's {@link CopyPolicy}. This is a static value, reflecting how this statement reacts to being
239 * replicated to a different context, without reflecting on behaviour of potential substatements, which would come
240 * into play in something like:
254 * description "Can be used in description/reference statements to attach additional notes";
257 * description "A nice module extending description statement semantics" {
258 * foo:note "We can now attach description/reference a note.";
259 * foo:note "Also another note";
266 * In this scenario, it is the reactor's job to figure out what to do (like talking to substatements).
268 * @return This statement's copy policy
270 public final @NonNull CopyPolicy copyPolicy() {
275 public final boolean canReuseCurrent(final Current<A, D> copy, final Current<A, D> current,
276 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
277 return policy.canReuseCurrent(copy, current, substatements);
281 * Parses textual representation of argument in object representation.
283 * @param ctx Context, which may be used to access source-specific namespaces required for parsing.
284 * @param value String representation of value, as was present in text source.
285 * @return Parsed value
286 * @throws SourceException when an inconsistency is detected.
288 public abstract A parseArgumentValue(StmtContext<?, ?, ?> ctx, String value);
291 * Adapts the argument value to match a new module. Default implementation returns original value stored in context,
292 * which is appropriate for most implementations.
294 * @param ctx Context, which may be used to access source-specific namespaces required for parsing.
295 * @param targetModule Target module, may not be null.
296 * @return Adapted argument value.
298 public A adaptArgumentValue(final @NonNull StmtContext<A, D, E> ctx, final @NonNull QNameModule targetModule) {
299 return ctx.argument();
303 * Invoked when a statement supported by this instance is added to build context. This allows implementations
304 * of this interface to start tracking the statement and perform any modifications to the build context hierarchy,
305 * accessible via {@link StmtContext#getParentContext()}. One such use is populating the parent's namespaces to
306 * allow it to locate this child statement.
308 * @param stmt Context of added statement. No substatements are available.
310 public void onStatementAdded(final @NonNull Mutable<A, D, E> stmt) {
311 // NOOP for most implementations
315 * Invoked when statement is closed during {@link ModelProcessingPhase#SOURCE_PRE_LINKAGE} phase, only substatements
316 * from this and previous phase are available.
319 * Implementation may use method to perform actions on this event or register modification action using
320 * {@link Mutable#newInferenceAction(ModelProcessingPhase)}.
322 * @param stmt Context of added statement.
324 public void onPreLinkageDeclared(final @NonNull Mutable<A, D, E> stmt) {
325 // NOOP for most implementations
329 * Invoked when statement is closed during {@link ModelProcessingPhase#SOURCE_LINKAGE} phase, only substatements
330 * from this and previous phase are available.
333 * Implementation may use method to perform actions on this event or register modification action using
334 * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
336 * @param stmt Context of added statement.
337 * @throws SourceException when an inconsistency is detected.
339 public void onLinkageDeclared(final @NonNull Mutable<A, D, E> stmt) {
340 // NOOP for most implementations
344 * Invoked when statement is closed during {@link ModelProcessingPhase#STATEMENT_DEFINITION} phase,
345 * only substatements from this phase are available.
348 * Implementation may use method to perform actions on this event or register modification action using
349 * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
351 * @param stmt Context of added statement. Argument and statement parent is accessible.
352 * @throws SourceException when an inconsistency is detected.
354 public void onStatementDefinitionDeclared(final Mutable<A, D, E> stmt) {
355 // NOOP for most implementations
359 * Invoked when statement is closed during {@link ModelProcessingPhase#FULL_DECLARATION} phase,
360 * only substatements from this phase are available.
363 * Implementation may use method to perform actions on this event or register modification action using
364 * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
366 * @param stmt Context of added statement. Argument and statement parent is accessible.
367 * @throws SourceException when an inconsistency is detected.
369 public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
370 final SubstatementValidator validator = getSubstatementValidator();
371 if (validator != null) {
372 validator.validate(stmt);
377 * Returns corresponding substatement validator of a statement support.
379 * @return substatement validator or null, if substatement validator is not defined
381 // FIXME: rename to 'substatementValidator' and perhaps let it be passed in?
382 protected abstract @Nullable SubstatementValidator getSubstatementValidator();
385 * Returns true if this support has argument specific supports.
387 public boolean hasArgumentSpecificSupports() {
388 // Most of statement supports don't have any argument specific supports, so return 'false'.
393 * If this support has argument specific supports, the method returns support specific for given argument
394 * (e.g. type statement support need to be specialized based on its argument), otherwise returns null.
396 * @param argument argument of statement
397 * @return statement support specific for supplied argument or null
399 public @Nullable StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
400 // Most of statement supports don't have any argument specific supports, so return null.
405 * Given a raw string representation of an argument, try to use a shared representation. Default implementation
408 * @param rawArgument Argument string
409 * @return A potentially-shard instance
411 public String internArgument(final String rawArgument) {
416 * Returns unknown statement form of a regular YANG statement supplied as a parameter to the method. Default
417 * implementation does nothing.
419 * @param yangStmtDef statement definition of a regular YANG statement
420 * @return Optional of unknown statement form of a regular YANG statement or empty() if it is not supported by this
423 public Optional<StatementSupport<?, ?, ?>> getUnknownStatementDefinitionOf(final StatementDefinition yangStmtDef) {
424 return Optional.empty();
428 * Returns true if this statement support and all its substatements ignore if-feature statements (e.g. yang-data
429 * extension defined in <a href="https://tools.ietf.org/html/rfc8040#section-8">RFC 8040</a>). Default
430 * implementation returns false.
432 * @return true if this statement support ignores if-feature statements, otherwise false.
435 public boolean isIgnoringIfFeatures() {
440 * Returns true if this statement support and all its substatements ignore config statements (e.g. yang-data
441 * extension defined in <a href="https://tools.ietf.org/html/rfc8040#section-8">RFC 8040</a>). Default
442 * implementation returns false.
444 * @return true if this statement support ignores config statements,
448 public boolean isIgnoringConfig() {
453 public final QName getStatementName() {
454 return def.getStatementName();
458 public final Optional<ArgumentDefinition> getArgumentDefinition() {
459 return def.getArgumentDefinition();
463 // Non-final for compatible extensions
464 public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
465 return def.getDeclaredRepresentationClass();
469 // Non-final for compatible extensions
470 public Class<? extends EffectiveStatement<?,?>> getEffectiveRepresentationClass() {
471 return def.getEffectiveRepresentationClass();
475 * Statement context copy policy, indicating how should reactor handle statement copy operations. Every statement
476 * copied by the reactor is subject to this policy.
478 public enum CopyPolicy {
480 * Reuse the source statement context in the new place, as it cannot be affected by any further operations. This
481 * implies that the semantics of the effective statement are not affected by any of its substatements. Each
482 * of the substatements is free to make its own policy.
485 * This policy is typically used by static constant statements such as {@code description} or {@code length},
486 * where the baseline RFC7950 does not allow any impact. A {@code description} could hold an extension statement
487 * in which case this interaction would come into play. Normal YANG will see empty substatements, so the reactor
488 * will be free to complete reuse the context.
491 * In case any substatement is of stronger policy, it is up to the reactor to handle correct handling of
492 * resulting subobjects.
494 // TODO: does this mean source must have transitioned to ModelProcessingPhase.EFFECTIVE_MODEL?
497 * Reuse the source statement context in the new place completely. This policy is more stringent than
498 * {@link #CONTEXT_INDEPENDENT} in that the statement is dependent on circumstances of its original definition
499 * and any copy operation must replicate it exactly as is. This implies ignoring the usual policy of its
500 * substatements. A typical example of such a statement is {@code type}.
504 * Create a copy sharing declared instance, but otherwise having a separate disconnected lifecycle.
506 // TODO: will the copy transition to ModelProcessingPhase.FULL_DECLARATION or which phase?
509 * Reject any attempt to copy this statement. This is useful for statements that are defined as top-level
510 * constructs, such as {@code contact}, {@code deviation} and similar.
514 * Ignore this statement's existence for the purposes of the new place -- it is not impacted. This guidance
515 * is left here for completeness, as it can have justifiable uses (but I can't think of any). Any substatements
516 * need to be ignored, too.