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.meta.StmtContextUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
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.
43 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
44 StatementContextBase<A, D, E> {
46 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
48 private final SourceSpecificContext sourceContext;
49 private final A argument;
51 private YangVersion version;
52 private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
53 private ModuleIdentifier identifier;
56 * References to RootStatementContext of submodules which are included in this source.
58 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
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());
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);
75 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
76 final CopyType typeOfCopy) {
79 sourceContext = Preconditions.checkNotNull(original.sourceContext);
80 this.argument = original.argument;
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());
86 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
87 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
88 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
91 for (final StmtContext<?, ?, ?> stmtContext : effective) {
92 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
95 addEffectiveSubstatements(buffer);
99 * @return null as root cannot have parent
102 public StatementContextBase<?, ?, ?> getParentContext() {
107 * @return namespace storage of source context
110 public NamespaceStorageNode getParentNamespaceStorage() {
111 return sourceContext;
115 public Registry getBehaviourRegistry() {
116 return sourceContext;
120 public StorageNodeType getStorageNodeType() {
121 return StorageNodeType.ROOT_STATEMENT_LOCAL;
124 * @return this as its own root
128 public RootStatementContext<?, ?, ?> getRoot() {
132 SourceSpecificContext getSourceContext() {
133 return sourceContext;
137 public A getStatementArgument() {
142 * @return copy of this considering {@link CopyType} (augment, uses)
144 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
147 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
148 final CopyType typeOfCopy) {
149 return createCopy(null, newParent, typeOfCopy);
153 * @return copy of this considering {@link CopyType} (augment, uses)
155 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
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);
162 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
164 if (this.getOriginalCtx() != null) {
165 copy.setOriginalCtx(this.getOriginalCtx());
167 copy.setOriginalCtx(this);
169 definition().onStatementAdded(copy);
175 public Optional<SchemaPath> getSchemaPath() {
176 return Optional.of(SchemaPath.ROOT);
183 public boolean isRootContext() {
188 public boolean isConfiguration() {
193 public boolean isEnabledSemanticVersioning() {
194 return sourceContext.isEnabledSemanticVersioning();
198 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
200 if (IncludedModuleContext.class.isAssignableFrom(type)) {
201 if (includedContexts.isEmpty()) {
202 includedContexts = new ArrayList<>(1);
204 Verify.verify(value instanceof RootStatementContext);
205 includedContexts.add((RootStatementContext<?, ?, ?>) value);
207 return super.putToLocalStorage(type, key, value);
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<>());
217 * We need to track already checked RootStatementContexts due to possible
218 * circular chains of includes between submodules
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;
228 alreadyChecked.add(this);
229 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
230 if (alreadyChecked.contains(includedSource)) {
233 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
234 if (potential != null) {
243 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
244 return getAllFromLocalStorage(type, new HashSet<>());
248 * We need to track already checked RootStatementContexts due to possible
249 * circular chains of includes between submodules
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;
259 alreadyChecked.add(this);
260 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
261 if (alreadyChecked.contains(includedSource)) {
264 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
265 if (potential != null) {
273 public YangVersion getRootVersion() {
274 return version == null ? DEFAULT_VERSION : version;
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,
283 this.version = Preconditions.checkNotNull(version);
287 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
288 sourceContext.addMutableStmtToSeal(mutableStatement);
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<>();
298 requiredModules.add(dependency);
301 Collection<ModuleIdentifier> getRequiredModules() {
302 return ImmutableSet.copyOf(requiredModules);
306 public void setRootIdentifier(final ModuleIdentifier identifier) {
307 Preconditions.checkNotNull(identifier);
308 this.identifier = identifier;
311 ModuleIdentifier getRootIdentifier() {