Make getOriginalCtx() give out an Optional
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.reactor;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
34 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
36
37 /**
38  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
39  * of this class, hence recursive lookups from them cross this class.
40  */
41 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
42         StatementContextBase<A, D, E> {
43
44     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
45
46     private final SourceSpecificContext sourceContext;
47     private final A argument;
48
49     private YangVersion version;
50     private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
51     private ModuleIdentifier identifier;
52
53     /**
54      * References to RootStatementContext of submodules which are included in this source.
55      */
56     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
57
58     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
59         final StatementSourceReference ref, final String rawArgument) {
60         super(def, ref, rawArgument);
61         this.sourceContext = Preconditions.checkNotNull(sourceContext);
62         this.argument = def.parseArgumentValue(this, rawStatementArgument());
63     }
64
65     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
66             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
67             final ModuleIdentifier identifier) {
68         this(sourceContext, def, ref, rawArgument);
69         this.setRootVersion(version);
70         this.setRootIdentifier(identifier);
71     }
72
73     /**
74      * @return null as root cannot have parent
75      */
76     @Override
77     public StatementContextBase<?, ?, ?> getParentContext() {
78         return null;
79     }
80
81     /**
82      * @return namespace storage of source context
83      */
84     @Override
85     public NamespaceStorageNode getParentNamespaceStorage() {
86         return sourceContext;
87     }
88
89     @Override
90     public Registry getBehaviourRegistry() {
91         return sourceContext;
92     }
93
94     @Override
95     public StorageNodeType getStorageNodeType() {
96         return StorageNodeType.ROOT_STATEMENT_LOCAL;
97     }
98     /**
99      * @return this as its own root
100      */
101     @Nonnull
102     @Override
103     public RootStatementContext<?, ?, ?> getRoot() {
104         return this;
105     }
106
107     SourceSpecificContext getSourceContext() {
108         return sourceContext;
109     }
110
111     @Override
112     public A getStatementArgument() {
113         return argument;
114     }
115
116     @Override
117     public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
118             final CopyType typeOfCopy) {
119         throw new UnsupportedOperationException("Root context cannot be copied");
120     }
121
122     @Override
123     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
124             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
125         throw new UnsupportedOperationException("Root context cannot be copied");
126     }
127
128     @Nonnull
129     @Override
130     public Optional<SchemaPath> getSchemaPath() {
131         return Optional.of(SchemaPath.ROOT);
132     }
133
134     @Override
135     public boolean isConfiguration() {
136         return true;
137     }
138
139     @Override
140     public boolean isInYangDataExtensionBody() {
141         return false;
142     }
143
144     @Override
145     public boolean isEnabledSemanticVersioning() {
146         return sourceContext.isEnabledSemanticVersioning();
147     }
148
149     @Override
150     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
151             final V value) {
152         if (IncludedModuleContext.class.isAssignableFrom(type)) {
153             if (includedContexts.isEmpty()) {
154                 includedContexts = new ArrayList<>(1);
155             }
156             Verify.verify(value instanceof RootStatementContext);
157             includedContexts.add((RootStatementContext<?, ?, ?>) value);
158         }
159         return super.putToLocalStorage(type, key, value);
160     }
161
162     @Nullable
163     @Override
164     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
165         return getFromLocalStorage(type, key, new HashSet<>());
166     }
167
168     /*
169      * We need to track already checked RootStatementContexts due to possible
170      * circular chains of includes between submodules
171      */
172     @Nullable
173     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
174             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
175         final V potentialLocal = super.getFromLocalStorage(type, key);
176         if (potentialLocal != null) {
177             return potentialLocal;
178         }
179
180         alreadyChecked.add(this);
181         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
182             if (alreadyChecked.contains(includedSource)) {
183                 continue;
184             }
185             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
186             if (potential != null) {
187                 return potential;
188             }
189         }
190         return null;
191     }
192
193     @Nullable
194     @Override
195     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
196         return getAllFromLocalStorage(type, new HashSet<>());
197     }
198
199     /*
200      * We need to track already checked RootStatementContexts due to possible
201      * circular chains of includes between submodules
202      */
203     @Nullable
204     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
205             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
206         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
207         if (potentialLocal != null) {
208             return potentialLocal;
209         }
210
211         alreadyChecked.add(this);
212         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
213             if (alreadyChecked.contains(includedSource)) {
214                 continue;
215             }
216             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
217             if (potential != null) {
218                 return potential;
219             }
220         }
221         return null;
222     }
223
224     @Override
225     public YangVersion getRootVersion() {
226         return version == null ? DEFAULT_VERSION : version;
227     }
228
229     @Override
230     public void setRootVersion(final YangVersion version) {
231         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
232                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
233         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
234                 this.version);
235         this.version = Preconditions.checkNotNull(version);
236     }
237
238     @Override
239     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
240         sourceContext.addMutableStmtToSeal(mutableStatement);
241     }
242
243     @Override
244     public void addRequiredModule(final ModuleIdentifier dependency) {
245         Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
246                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
247         if (requiredModules.isEmpty()) {
248             requiredModules = new HashSet<>();
249         }
250         requiredModules.add(dependency);
251     }
252
253     Collection<ModuleIdentifier> getRequiredModules() {
254         return ImmutableSet.copyOf(requiredModules);
255     }
256
257     @Override
258     public void setRootIdentifier(final ModuleIdentifier identifier) {
259         Preconditions.checkNotNull(identifier);
260         this.identifier = identifier;
261     }
262
263     ModuleIdentifier getRootIdentifier() {
264         return identifier;
265     }
266 }