Populate xpath/ hierarchy
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractSchemaTreeStatementSupport.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 com.google.common.annotations.Beta;
11 import java.util.Collection;
12 import java.util.Objects;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
20 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
21 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24
25 /**
26  * Specialization of {@link AbstractQNameStatementSupport} for {@link SchemaTreeEffectiveStatement} implementations.
27  * Every statement automatically participates in {@link SchemaTreeNamespace}.
28  *
29  * @param <D> Declared Statement representation
30  * @param <E> Effective Statement representation
31  */
32 @Beta
33 public abstract class AbstractSchemaTreeStatementSupport<D extends DeclaredStatement<QName>,
34         E extends SchemaTreeEffectiveStatement<D>> extends AbstractQNameStatementSupport<D, E> {
35     private static class SchemaTreeEquality<D extends DeclaredStatement<QName>>
36             implements StatementEquality<QName, D> {
37         private static final class Instantiated<D extends DeclaredStatement<QName>> extends SchemaTreeEquality<D> {
38             @Override
39             public boolean canReuseCurrent(final Current<QName, D> copy, final Current<QName, D> current,
40                     final Collection<? extends EffectiveStatement<?, ?>> substatements) {
41                 return copy.effectiveConfig() == current.effectiveConfig()
42                     && super.canReuseCurrent(copy, current, substatements)
43                     // This weird quirk is needed for ... something somewhere
44                     && Objects.equals(copy.original(), current.original());
45             }
46         }
47
48         @Override
49         public boolean canReuseCurrent(final Current<QName, D> copy, final Current<QName, D> current,
50                 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
51             return equalHistory(copy.history(), current.history())
52                 && copy.getArgument().equals(current.getArgument())
53                 // FIXME: 8.0.0: eliminate this call
54                 && copy.equalParentPath(current);
55         }
56
57         private static boolean equalHistory(final CopyHistory copy, final CopyHistory current) {
58             return copy.isAugmenting() == current.isAugmenting() && copy.isAddedByUses() == current.isAddedByUses();
59         }
60     }
61
62     private static final StatementPolicy<QName, ?> INSTANTIATED_POLICY =
63         StatementPolicy.copyDeclared(new SchemaTreeEquality.Instantiated<>());
64     private static final StatementPolicy<QName, ?> UNINSTANTIATED_POLICY =
65         StatementPolicy.copyDeclared(new SchemaTreeEquality<>());
66
67     protected AbstractSchemaTreeStatementSupport(final StatementDefinition publicDefinition,
68             final StatementPolicy<QName, D> policy, final YangParserConfiguration config,
69             final @Nullable SubstatementValidator validator) {
70         super(publicDefinition, policy, config, validator);
71     }
72
73     /**
74      * Return the {@link StatementPolicy} corresponding to a potentially-instantiated YANG statement. Statements are
75      * reused as long as:
76      * <ul>
77      *   <li>{@link Current#schemaPath()} does not change</li>
78      *   <li>{@link Current#argument()} does not change</li>
79      *   <li>{@link Current#history()} does not change as far as {@link CopyableNode} is concerned</li>
80      *   <li>{@link Current#effectiveConfig()} does not change</li>
81      *   <li>{@link Current#original()} does not change</li>
82      * </ul>
83      *
84      * <p>
85      * Typical users include {@code container} and {@code leaf}.
86      *
87      * @param <D> Declared Statement representation
88      * @return A StatementPolicy
89      */
90     @SuppressWarnings("unchecked")
91     public static final <D extends DeclaredStatement<QName>> StatementPolicy<QName, D> instantiatedPolicy() {
92         return (StatementPolicy<QName, D>) INSTANTIATED_POLICY;
93     }
94
95     /**
96      * Return the {@link StatementPolicy} corresponding to an uninstantiated YANG statement. Statements are
97      * reused as long as:
98      * <ul>
99      *   <li>{@link Current#schemaPath()} does not change</li>
100      *   <li>{@link Current#argument()} does not change</li>
101      *   <li>{@link Current#history()} does not change as far as {@link CopyableNode} is concerned</li>
102      * </ul>
103      *
104      * <p>
105      * Typical users include {@code action} and {@code notification} (in its YANG 1.1 form).
106      *
107      * @param <D> Declared Statement representation
108      * @return A StatementPolicy
109      */
110     @SuppressWarnings("unchecked")
111     public static final <D extends DeclaredStatement<QName>> StatementPolicy<QName, D> uninstantiatedPolicy() {
112         return (StatementPolicy<QName, D>) UNINSTANTIATED_POLICY;
113     }
114
115     /**
116      * {@inheritDoc}
117      *
118      * <p>
119      * This method ensures the statement is added to its parent {@link SchemaTreeNamespace}.
120      */
121     @Override
122     public void onStatementAdded(final Mutable<QName, D, E> stmt) {
123         stmt.coerceParentContext().addToNs(SchemaTreeNamespace.class, stmt.getArgument(), stmt);
124     }
125
126     // Non-final because {@code input} and {@code output} are doing their own thing.
127     @Override
128     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
129         return StmtContextUtils.parseIdentifier(ctx, value);
130     }
131 }