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 static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.VerifyException;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 * Effective view of a {@link StmtContext} for the purposes of creating an {@link EffectiveStatement}.
29 public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immutable {
31 * Return parent of this context, if there is one. All statements except for top-level source statements, such as
32 * {@code module} and {@code submodule}.
34 * @return Parent context, or null if this statement is the root
36 @Nullable Parent effectiveParent();
39 * Return parent of this context.
41 * @return Parent context
42 * @throws VerifyException if this context is already the root
44 default @NonNull Parent getEffectiveParent() {
45 return verifyNotNull(effectiveParent(), "Attempted to access beyond root context");
49 * Minimum amount of parent state required to build an accurate effective view of a particular child. Child state
50 * is expressed as {@link Current}.
53 interface Parent extends EffectiveStmtCtx {
55 * Effective {@code config} statement value.
58 enum EffectiveConfig {
60 * We have an effective {@code config true} statement.
64 * We have an effective {@code config false} statement.
68 * We are in a context where {@code config} statements are ignored.
72 * We are in a context where {@code config} is not determined, such as within a {@code grouping}.
76 private final Boolean config;
78 EffectiveConfig(final @Nullable Boolean config) {
83 * Return this value as a {@link Boolean} for use with {@link DataSchemaNode#effectiveConfig()}.
85 * @return A boolean or null
87 public @Nullable Boolean asNullable() {
93 * Return the effective {@code config} statement value.
95 * @return This statement's effective config
97 @NonNull EffectiveConfig effectiveConfig();
99 // FIXME: 7.0.0: this is currently only used by AbstractTypeStatement
100 @NonNull QNameModule effectiveNamespace();
102 default @NonNull Object effectivePath() {
103 return SchemaPathSupport.toEffectivePath(getSchemaPath());
106 default @Nullable SchemaPath optionalPath() {
107 return SchemaPathSupport.toOptionalPath(getSchemaPath());
111 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
112 * {@link Optional#empty()} is returned.
114 * @return Optional SchemaPath
115 * @deprecated Use of SchemaPath in the context of effective statements is going away. Consider not providing
116 * this information, if your users can exist without it.
118 // FIXME: 7.0.0: this needs to be a tri-state present/absent/disabled
120 @Nullable SchemaPath schemaPath();
123 default @NonNull SchemaPath getSchemaPath() {
124 return verifyNotNull(schemaPath(), "Missing path for %s", this);
129 * Minimum amount of state required to build an accurate effective view of a statement. This is a strict superset
130 * of information available in {@link Parent}.
132 * @param <A> Argument type
133 * @param <D> Class representing declared version of this statement
136 interface Current<A, D extends DeclaredStatement<A>> extends Parent, NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
138 @NonNull QName moduleName();
140 @Nullable EffectiveStatement<?, ?> original();
142 // FIXME: 7.0.0: this method should be moved to stmt.type in some shape or form
143 @NonNull QName argumentAsTypeQName();
146 * Summon the <a href="https://en.wikipedia.org/wiki/Rabbit_of_Caerbannog">Rabbit of Caerbannog</a>.
148 * @param <E> Effective Statement representation
149 * @return The {@code Legendary Black Beast of Arrrghhh}.
151 // FIXME: YANGTOOLS-1186: lob the Holy Hand Grenade of Antioch
153 <E extends EffectiveStatement<A, D>> @NonNull StmtContext<A, D, E> caerbannog();
156 * Compare another context for equality of {@code getEffectiveParent().getSchemaPath()}, just in a safer manner.
158 * @param other Other {@link Current}
159 * @return True if {@code other} has parent path equal to this context's parent path.
161 // FIXME: 8.0.0: Remove this method
162 default boolean equalParentPath(final Current<A, D> other) {
163 final Parent ours = effectiveParent();
164 final Parent theirs = other.effectiveParent();
165 return ours == theirs
166 || ours != null && theirs != null && SchemaPathSupport.effectivelyEqual(ours.schemaPath(),
167 theirs.schemaPath());