2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.reactor;
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;
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.source.IncludedModuleContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
39 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
40 * of this class, hence recursive lookups from them cross this class.
42 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
43 StatementContextBase<A, D, E> {
45 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
47 private final SourceSpecificContext sourceContext;
48 private final A argument;
50 private YangVersion version;
51 private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
52 private ModuleIdentifier identifier;
55 * References to RootStatementContext of submodules which are included in this source.
57 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
59 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
60 final StatementSourceReference ref, final String rawArgument) {
61 super(def, ref, rawArgument);
62 this.sourceContext = Preconditions.checkNotNull(sourceContext);
63 this.argument = def.parseArgumentValue(this, rawStatementArgument());
66 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
67 final StatementSourceReference ref, final String rawArgument, final YangVersion version,
68 final ModuleIdentifier identifier) {
69 this(sourceContext, def, ref, rawArgument);
70 this.setRootVersion(version);
71 this.setRootIdentifier(identifier);
74 private RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
75 final CopyType typeOfCopy) {
78 sourceContext = Preconditions.checkNotNull(original.sourceContext);
79 this.argument = original.argument;
81 final Collection<? extends Mutable<?, ?, ?>> declared = original.mutableDeclaredSubstatements();
82 final Collection<? extends Mutable<?, ?, ?>> effective = original.mutableEffectiveSubstatements();
83 final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
85 for (final Mutable<?, ?, ?> stmtContext : declared) {
86 if (stmtContext.isSupportedByFeatures()) {
87 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
90 for (final StmtContext<?, ?, ?> stmtContext : effective) {
91 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
94 addEffectiveSubstatements(buffer);
98 * @return null as root cannot have parent
101 public StatementContextBase<?, ?, ?> getParentContext() {
106 * @return namespace storage of source context
109 public NamespaceStorageNode getParentNamespaceStorage() {
110 return sourceContext;
114 public Registry getBehaviourRegistry() {
115 return sourceContext;
119 public StorageNodeType getStorageNodeType() {
120 return StorageNodeType.ROOT_STATEMENT_LOCAL;
123 * @return this as its own root
127 public RootStatementContext<?, ?, ?> getRoot() {
131 SourceSpecificContext getSourceContext() {
132 return sourceContext;
136 public A getStatementArgument() {
141 * @return copy of this considering {@link CopyType} (augment, uses)
143 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
146 public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
147 final CopyType typeOfCopy) {
148 return createCopy(null, newParent, typeOfCopy);
152 * @return copy of this considering {@link CopyType} (augment, uses)
154 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
157 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
158 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
159 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
161 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
163 if (this.getOriginalCtx() != null) {
164 copy.setOriginalCtx(this.getOriginalCtx());
166 copy.setOriginalCtx(this);
168 definition().onStatementAdded(copy);
174 public Optional<SchemaPath> getSchemaPath() {
175 return Optional.of(SchemaPath.ROOT);
179 public boolean isConfiguration() {
184 public boolean isInYangDataExtensionBody() {
189 public boolean isEnabledSemanticVersioning() {
190 return sourceContext.isEnabledSemanticVersioning();
194 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
196 if (IncludedModuleContext.class.isAssignableFrom(type)) {
197 if (includedContexts.isEmpty()) {
198 includedContexts = new ArrayList<>(1);
200 Verify.verify(value instanceof RootStatementContext);
201 includedContexts.add((RootStatementContext<?, ?, ?>) value);
203 return super.putToLocalStorage(type, key, value);
208 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
209 return getFromLocalStorage(type, key, new HashSet<>());
213 * We need to track already checked RootStatementContexts due to possible
214 * circular chains of includes between submodules
217 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
218 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
219 final V potentialLocal = super.getFromLocalStorage(type, key);
220 if (potentialLocal != null) {
221 return potentialLocal;
224 alreadyChecked.add(this);
225 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
226 if (alreadyChecked.contains(includedSource)) {
229 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
230 if (potential != null) {
239 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
240 return getAllFromLocalStorage(type, new HashSet<>());
244 * We need to track already checked RootStatementContexts due to possible
245 * circular chains of includes between submodules
248 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
249 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
250 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
251 if (potentialLocal != null) {
252 return potentialLocal;
255 alreadyChecked.add(this);
256 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
257 if (alreadyChecked.contains(includedSource)) {
260 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
261 if (potential != null) {
269 public YangVersion getRootVersion() {
270 return version == null ? DEFAULT_VERSION : version;
274 public void setRootVersion(final YangVersion version) {
275 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
276 "Unsupported yang version %s in %s", version, getStatementSourceReference());
277 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
279 this.version = Preconditions.checkNotNull(version);
283 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
284 sourceContext.addMutableStmtToSeal(mutableStatement);
288 public void addRequiredModule(final ModuleIdentifier dependency) {
289 Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
290 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
291 if (requiredModules.isEmpty()) {
292 requiredModules = new HashSet<>();
294 requiredModules.add(dependency);
297 Collection<ModuleIdentifier> getRequiredModules() {
298 return ImmutableSet.copyOf(requiredModules);
302 public void setRootIdentifier(final ModuleIdentifier identifier) {
303 Preconditions.checkNotNull(identifier);
304 this.identifier = identifier;
307 ModuleIdentifier getRootIdentifier() {