Reformulate StatementContextFactory.createEffective()
[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.Map;
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.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
27
28 /**
29  * Effective view of a {@link StmtContext} for the purposes of creating an {@link EffectiveStatement}.
30  */
31 @Beta
32 public interface EffectiveStmtCtx extends Immutable {
33     /**
34      * Return parent of this context, if there is one. All statements except for top-level source statements, such as
35      * {@code module} and {@code submodule}.
36      *
37      * @return Parent context, or null if this statement is the root
38      */
39     // FIXME: YANGTOOLS-1185: rename to effectiveParent()
40     @Nullable Parent parent();
41
42     /**
43      * Return parent of this context.
44      *
45      * @return Parent context
46      * @throws VerifyException if this context is already the root
47      */
48     // FIXME: YANGTOOLS-1185: rename to getEffectiveParent()
49     default @NonNull Parent getParent() {
50         return verifyNotNull(parent(), "Attempted to access beyond root context");
51     }
52
53     /**
54      * Minimum amount of parent state required to build an accurate effective view of a particular child. Child state
55      * is expressed as {@link Current}.
56      */
57     @Beta
58     interface Parent extends EffectiveStmtCtx {
59         // FIXME: 7.0.0: this should be Optional<Boolean>
60         boolean effectiveConfig();
61
62         // FIXME: 7.0.0: this needs to be a tri-state present/absent/disabled
63         @Deprecated
64         @NonNull Optional<SchemaPath> schemaPath();
65
66         @Deprecated
67         default @NonNull SchemaPath getSchemaPath() {
68             return schemaPath().orElseThrow();
69         }
70
71         @NonNull StatementDefinition publicDefinition();
72     }
73
74     /**
75      * Minimum amount of state required to build an accurate effective view of a statement. This is a strict superset
76      * of information available in {@link Parent}.
77      *
78      * @param <A> Argument type
79      * @param <D> Class representing declared version of this statement
80      */
81     @Beta
82     interface Current<A, D extends DeclaredStatement<A>> extends Parent {
83         default @NonNull StatementSource source() {
84             return sourceReference().getStatementSource();
85         }
86
87         @NonNull StatementSourceReference sourceReference();
88
89         @NonNull CopyHistory history();
90
91         @NonNull D declared();
92
93         <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V getFromNamespace(Class<@NonNull N> type,
94             T key);
95
96         <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
97
98         @Nullable A argument();
99
100         default @NonNull A coerceArgument() {
101             return verifyNotNull(argument(), "Attempted to use non-existent argument");
102         }
103
104         @Nullable String rawArgument();
105
106         @Nullable EffectiveStatement<?, ?> original();
107
108         @NonNull YangVersion yangVersion();
109
110         /**
111          * Summon the <a href="https://en.wikipedia.org/wiki/Rabbit_of_Caerbannog">Rabbit of Caerbannog</a>.
112          *
113          * @param <E> Effective Statement representation
114          * @return The {@code Legendary Black Beast of Arrrghhh}.
115          */
116         // FIXME: YANGTOOLS-1186: lob the Holy Hand Grenade of Antioch
117         @Deprecated
118         <E extends EffectiveStatement<A, D>> @NonNull StmtContext<A, D, E> caerbannog();
119     }
120 }