f81050ff8ee74f6a90d3346489884086b07c8754
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.Map;
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.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
38
39 /**
40  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
41  * of this class, hence recursive lookups from them cross this class.
42  */
43 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
44         StatementContextBase<A, D, E> {
45
46     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
47
48     private final SourceSpecificContext sourceContext;
49     private final A argument;
50
51     private YangVersion version;
52     private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
53     private ModuleIdentifier identifier;
54
55     /**
56      * References to RootStatementContext of submodules which are included in this source.
57      */
58     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
59
60     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
61         final StatementSourceReference ref, final String rawArgument) {
62         super(def, ref, rawArgument);
63         this.sourceContext = Preconditions.checkNotNull(sourceContext);
64         this.argument = def.parseArgumentValue(this, rawStatementArgument());
65     }
66
67     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
68             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
69             final ModuleIdentifier identifier) {
70         this(sourceContext, def, ref, rawArgument);
71         this.setRootVersion(version);
72         this.setRootIdentifier(identifier);
73     }
74
75     RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
76         final CopyType typeOfCopy) {
77         super(original);
78
79         sourceContext = Preconditions.checkNotNull(original.sourceContext);
80         this.argument = original.argument;
81
82         final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
83         final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
84         final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
85
86         for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
87             if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
88                 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
89             }
90         }
91         for (final StmtContext<?, ?, ?> stmtContext : effective) {
92             buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
93         }
94
95         addEffectiveSubstatements(buffer);
96     }
97
98     /**
99      * @return null as root cannot have parent
100      */
101     @Override
102     public StatementContextBase<?, ?, ?> getParentContext() {
103         return null;
104     }
105
106     /**
107      * @return namespace storage of source context
108      */
109     @Override
110     public NamespaceStorageNode getParentNamespaceStorage() {
111         return sourceContext;
112     }
113
114     @Override
115     public Registry getBehaviourRegistry() {
116         return sourceContext;
117     }
118
119     @Override
120     public StorageNodeType getStorageNodeType() {
121         return StorageNodeType.ROOT_STATEMENT_LOCAL;
122     }
123     /**
124      * @return this as its own root
125      */
126     @Nonnull
127     @Override
128     public RootStatementContext<?, ?, ?> getRoot() {
129         return this;
130     }
131
132     SourceSpecificContext getSourceContext() {
133         return sourceContext;
134     }
135
136     @Override
137     public A getStatementArgument() {
138         return argument;
139     }
140
141     /**
142      * @return copy of this considering {@link CopyType} (augment, uses)
143      *
144      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
145      */
146     @Override
147     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
148             final CopyType typeOfCopy) {
149         return createCopy(null, newParent, typeOfCopy);
150     }
151
152     /**
153      * @return copy of this considering {@link CopyType} (augment, uses)
154      *
155      * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
156      */
157     @Override
158     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
159             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
160         final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
161
162         copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
163
164         if (this.getOriginalCtx() != null) {
165             copy.setOriginalCtx(this.getOriginalCtx());
166         } else {
167             copy.setOriginalCtx(this);
168         }
169         definition().onStatementAdded(copy);
170         return copy;
171     }
172
173     @Nonnull
174     @Override
175     public Optional<SchemaPath> getSchemaPath() {
176         return Optional.of(SchemaPath.ROOT);
177     }
178
179     /**
180      * @return true
181      */
182     @Override
183     public boolean isRootContext() {
184         return true;
185     }
186
187     @Override
188     public boolean isConfiguration() {
189         return true;
190     }
191
192     @Override
193     public boolean isEnabledSemanticVersioning() {
194         return sourceContext.isEnabledSemanticVersioning();
195     }
196
197     @Override
198     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
199             final V value) {
200         if (IncludedModuleContext.class.isAssignableFrom(type)) {
201             if (includedContexts.isEmpty()) {
202                 includedContexts = new ArrayList<>(1);
203             }
204             Verify.verify(value instanceof RootStatementContext);
205             includedContexts.add((RootStatementContext<?, ?, ?>) value);
206         }
207         return super.putToLocalStorage(type, key, value);
208     }
209
210     @Nullable
211     @Override
212     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
213         return getFromLocalStorage(type, key, new HashSet<>());
214     }
215
216     /*
217      * We need to track already checked RootStatementContexts due to possible
218      * circular chains of includes between submodules
219      */
220     @Nullable
221     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
222             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
223         final V potentialLocal = super.getFromLocalStorage(type, key);
224         if (potentialLocal != null) {
225             return potentialLocal;
226         }
227
228         alreadyChecked.add(this);
229         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
230             if (alreadyChecked.contains(includedSource)) {
231                 continue;
232             }
233             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
234             if (potential != null) {
235                 return potential;
236             }
237         }
238         return null;
239     }
240
241     @Nullable
242     @Override
243     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
244         return getAllFromLocalStorage(type, new HashSet<>());
245     }
246
247     /*
248      * We need to track already checked RootStatementContexts due to possible
249      * circular chains of includes between submodules
250      */
251     @Nullable
252     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
253             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
254         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
255         if (potentialLocal != null) {
256             return potentialLocal;
257         }
258
259         alreadyChecked.add(this);
260         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
261             if (alreadyChecked.contains(includedSource)) {
262                 continue;
263             }
264             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
265             if (potential != null) {
266                 return potential;
267             }
268         }
269         return null;
270     }
271
272     @Override
273     public YangVersion getRootVersion() {
274         return version == null ? DEFAULT_VERSION : version;
275     }
276
277     @Override
278     public void setRootVersion(final YangVersion version) {
279         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
280                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
281         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
282                 this.version);
283         this.version = Preconditions.checkNotNull(version);
284     }
285
286     @Override
287     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
288         sourceContext.addMutableStmtToSeal(mutableStatement);
289     }
290
291     @Override
292     public void addRequiredModule(final ModuleIdentifier dependency) {
293         Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
294                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
295         if (requiredModules.isEmpty()) {
296             requiredModules = new HashSet<>();
297         }
298         requiredModules.add(dependency);
299     }
300
301     Collection<ModuleIdentifier> getRequiredModules() {
302         return ImmutableSet.copyOf(requiredModules);
303     }
304
305     @Override
306     public void setRootIdentifier(final ModuleIdentifier identifier) {
307         Preconditions.checkNotNull(identifier);
308         this.identifier = identifier;
309     }
310
311     ModuleIdentifier getRootIdentifier() {
312         return identifier;
313     }
314 }