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.source.IncludedModuleContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
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.
41 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
42 StatementContextBase<A, D, E> {
44 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
46 private final SourceSpecificContext sourceContext;
47 private final A argument;
49 private YangVersion version;
50 private Collection<ModuleIdentifier> requiredModules = ImmutableSet.of();
51 private ModuleIdentifier identifier;
54 * References to RootStatementContext of submodules which are included in this source.
56 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
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());
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);
74 * @return null as root cannot have parent
77 public StatementContextBase<?, ?, ?> getParentContext() {
82 * @return namespace storage of source context
85 public NamespaceStorageNode getParentNamespaceStorage() {
90 public Registry getBehaviourRegistry() {
95 public StorageNodeType getStorageNodeType() {
96 return StorageNodeType.ROOT_STATEMENT_LOCAL;
99 * @return this as its own root
103 public RootStatementContext<?, ?, ?> getRoot() {
107 SourceSpecificContext getSourceContext() {
108 return sourceContext;
112 public A getStatementArgument() {
117 public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
118 final CopyType typeOfCopy) {
119 throw new UnsupportedOperationException("Root context cannot be copied");
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");
130 public Optional<SchemaPath> getSchemaPath() {
131 return Optional.of(SchemaPath.ROOT);
135 public boolean isConfiguration() {
140 public boolean isInYangDataExtensionBody() {
145 public boolean isEnabledSemanticVersioning() {
146 return sourceContext.isEnabledSemanticVersioning();
150 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
152 if (IncludedModuleContext.class.isAssignableFrom(type)) {
153 if (includedContexts.isEmpty()) {
154 includedContexts = new ArrayList<>(1);
156 Verify.verify(value instanceof RootStatementContext);
157 includedContexts.add((RootStatementContext<?, ?, ?>) value);
159 return super.putToLocalStorage(type, key, value);
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<>());
169 * We need to track already checked RootStatementContexts due to possible
170 * circular chains of includes between submodules
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;
180 alreadyChecked.add(this);
181 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
182 if (alreadyChecked.contains(includedSource)) {
185 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
186 if (potential != null) {
195 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
196 return getAllFromLocalStorage(type, new HashSet<>());
200 * We need to track already checked RootStatementContexts due to possible
201 * circular chains of includes between submodules
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;
211 alreadyChecked.add(this);
212 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
213 if (alreadyChecked.contains(includedSource)) {
216 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
217 if (potential != null) {
225 public YangVersion getRootVersion() {
226 return version == null ? DEFAULT_VERSION : version;
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,
235 this.version = Preconditions.checkNotNull(version);
239 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
240 sourceContext.addMutableStmtToSeal(mutableStatement);
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<>();
250 requiredModules.add(dependency);
253 Collection<ModuleIdentifier> getRequiredModules() {
254 return ImmutableSet.copyOf(requiredModules);
258 public void setRootIdentifier(final ModuleIdentifier identifier) {
259 Preconditions.checkNotNull(identifier);
260 this.identifier = identifier;
263 ModuleIdentifier getRootIdentifier() {