2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 com.google.common.annotations.Beta;
11 import java.util.Collection;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
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 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
20 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
25 * Specialization of {@link AbstractQNameStatementSupport} for {@link SchemaTreeEffectiveStatement} implementations.
26 * Every statement automatically participates in {@link SchemaTreeNamespace}.
28 * @param <D> Declared Statement representation
29 * @param <E> Effective Statement representation
32 public abstract class AbstractSchemaTreeStatementSupport<D extends DeclaredStatement<QName>,
33 E extends SchemaTreeEffectiveStatement<D>> extends AbstractQNameStatementSupport<D, E> {
34 private static class SchemaTreeEquality<D extends DeclaredStatement<QName>>
35 implements StatementEquality<QName, D> {
36 private static final class Instantiated<D extends DeclaredStatement<QName>> extends SchemaTreeEquality<D> {
38 public boolean canReuseCurrent(final Current<QName, D> copy, final Current<QName, D> current,
39 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
40 return copy.effectiveConfig() == current.effectiveConfig()
41 && super.canReuseCurrent(copy, current, substatements);
46 public boolean canReuseCurrent(final Current<QName, D> copy, final Current<QName, D> current,
47 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
48 return equalHistory(copy.history(), current.history())
49 && copy.getArgument().equals(current.getArgument());
52 private static boolean equalHistory(final CopyHistory copy, final CopyHistory current) {
53 return copy.isAugmenting() == current.isAugmenting() && copy.isAddedByUses() == current.isAddedByUses();
57 private static final StatementPolicy<QName, ?> INSTANTIATED_POLICY =
58 StatementPolicy.copyDeclared(new SchemaTreeEquality.Instantiated<>());
59 private static final StatementPolicy<QName, ?> UNINSTANTIATED_POLICY =
60 StatementPolicy.copyDeclared(new SchemaTreeEquality<>());
62 protected AbstractSchemaTreeStatementSupport(final StatementDefinition publicDefinition,
63 final StatementPolicy<QName, D> policy, final YangParserConfiguration config,
64 final @Nullable SubstatementValidator validator) {
65 super(publicDefinition, policy, config, validator);
69 * Return the {@link StatementPolicy} corresponding to a potentially-instantiated YANG statement. Statements are
72 * <li>{@link Current#argument()} does not change</li>
73 * <li>{@link Current#history()} does not change as far as {@link CopyableNode} is concerned</li>
74 * <li>{@link Current#effectiveConfig()} does not change</li>
75 * <li>{@link Current#original()} does not change</li>
79 * Typical users include {@code container} and {@code leaf}.
81 * @param <D> Declared Statement representation
82 * @return A StatementPolicy
84 @SuppressWarnings("unchecked")
85 public static final <D extends DeclaredStatement<QName>> StatementPolicy<QName, D> instantiatedPolicy() {
86 return (StatementPolicy<QName, D>) INSTANTIATED_POLICY;
90 * Return the {@link StatementPolicy} corresponding to an uninstantiated YANG statement. Statements are
93 * <li>{@link Current#argument()} does not change</li>
94 * <li>{@link Current#history()} does not change as far as {@link CopyableNode} is concerned</li>
98 * Typical users include {@code action} and {@code notification} (in its YANG 1.1 form).
100 * @param <D> Declared Statement representation
101 * @return A StatementPolicy
103 @SuppressWarnings("unchecked")
104 public static final <D extends DeclaredStatement<QName>> StatementPolicy<QName, D> uninstantiatedPolicy() {
105 return (StatementPolicy<QName, D>) UNINSTANTIATED_POLICY;
112 * This method ensures the statement is added to its parent {@link SchemaTreeNamespace}.
115 public void onStatementAdded(final Mutable<QName, D, E> stmt) {
116 stmt.coerceParentContext().addToNs(SchemaTreeNamespace.class, stmt.getArgument(), stmt);
119 // Non-final because {@code input} and {@code output} are doing their own thing.
121 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
122 return StmtContextUtils.parseIdentifier(ctx, value);