Remove SchemaPath from TypeDefinition implementations
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / EffectiveStmtCtx.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 static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.VerifyException;
14 import java.util.Objects;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.common.QName;
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;
24
25 /**
26  * Effective view of a {@link StmtContext} for the purposes of creating an {@link EffectiveStatement}.
27  */
28 @Beta
29 public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immutable {
30     /**
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}.
33      *
34      * @return Parent context, or null if this statement is the root
35      */
36     @Nullable Parent effectiveParent();
37
38     /**
39      * Return parent of this context.
40      *
41      * @return Parent context
42      * @throws VerifyException if this context is already the root
43      */
44     default @NonNull Parent getEffectiveParent() {
45         return verifyNotNull(effectiveParent(), "Attempted to access beyond root context");
46     }
47
48     /**
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}.
51      */
52     @Beta
53     interface Parent extends EffectiveStmtCtx {
54         /**
55          * Effective {@code config} statement value.
56          */
57         @Beta
58         enum EffectiveConfig {
59             /**
60              * We have an effective {@code config true} statement.
61              */
62             TRUE(Boolean.TRUE),
63             /**
64              * We have an effective {@code config false} statement.
65              */
66             FALSE(Boolean.FALSE),
67             /**
68              * We are in a context where {@code config} statements are ignored.
69              */
70             IGNORED(null),
71             /**
72              * We are in a context where {@code config} is not determined, such as within a {@code grouping}.
73              */
74             UNDETERMINED(null);
75
76             private final Boolean config;
77
78             EffectiveConfig(final @Nullable Boolean config) {
79                 this.config = config;
80             }
81
82             /**
83              * Return this value as a {@link Boolean} for use with {@link DataSchemaNode#effectiveConfig()}.
84              *
85              * @return A boolean or null
86              */
87             public @Nullable Boolean asNullable() {
88                 return config;
89             }
90         }
91
92         /**
93          * Return the effective {@code config} statement value.
94          *
95          * @return This statement's effective config
96          */
97         @NonNull EffectiveConfig effectiveConfig();
98
99         /**
100          * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
101          * {@link Optional#empty()} is returned.
102          *
103          * @return Optional SchemaPath
104          * @deprecated Use of SchemaPath in the context of effective statements is going away. Consider not providing
105          *             this information, if your users can exist without it.
106          */
107         // FIXME: 7.0.0: this needs to be a tri-state present/absent/disabled
108         @Deprecated
109         @NonNull Optional<SchemaPath> schemaPath();
110
111         @Deprecated
112         default @NonNull SchemaPath getSchemaPath() {
113             return schemaPath().orElseThrow();
114         }
115
116         @Deprecated
117         default @Nullable SchemaPath wrapSchemaPath() {
118             return SchemaPathSupport.wrap(getSchemaPath());
119         }
120     }
121
122     /**
123      * Minimum amount of state required to build an accurate effective view of a statement. This is a strict superset
124      * of information available in {@link Parent}.
125      *
126      * @param <A> Argument type
127      * @param <D> Class representing declared version of this statement
128      */
129     @Beta
130     interface Current<A, D extends DeclaredStatement<A>> extends Parent, NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
131
132         @NonNull QName moduleName();
133
134         @Nullable EffectiveStatement<?, ?> original();
135
136         // FIXME: YANGTOOLS-1216: implement near ReactorStmtCtx.createSchemaPath(), this should always be a 'String'
137         //                        sub-case as implied by all callers in stmt.type
138         // FIXME: 7.0.0: this method should be moved to stmt.type in some shape or form
139         default @NonNull QName argumentAsTypeQName() {
140             return verifyNotNull(getSchemaPath().getLastComponent());
141         }
142
143         /**
144          * Summon the <a href="https://en.wikipedia.org/wiki/Rabbit_of_Caerbannog">Rabbit of Caerbannog</a>.
145          *
146          * @param <E> Effective Statement representation
147          * @return The {@code Legendary Black Beast of Arrrghhh}.
148          */
149         // FIXME: YANGTOOLS-1186: lob the Holy Hand Grenade of Antioch
150         @Deprecated
151         <E extends EffectiveStatement<A, D>> @NonNull StmtContext<A, D, E> caerbannog();
152
153         /**
154          * Compare another context for equality of {@code getEffectiveParent().getSchemaPath()}, just in a safer manner.
155          *
156          * @param other Other {@link Current}
157          * @return True if {@code other} has parent path equal to this context's parent path.
158          */
159         // FIXME: 8.0.0: Remove this method
160         default boolean equalParentPath(final Current<A, D> other) {
161             final Parent ours = effectiveParent();
162             final Parent theirs = other.effectiveParent();
163             return ours == theirs
164                 || ours != null && theirs != null && Objects.equals(ours.schemaPath(), theirs.schemaPath());
165         }
166     }
167 }