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.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;
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;
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 isEnabledSemanticVersioning() {
141 return sourceContext.isEnabledSemanticVersioning();
145 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
147 if (IncludedModuleContext.class.isAssignableFrom(type)) {
148 if (includedContexts.isEmpty()) {
149 includedContexts = new ArrayList<>(1);
151 Verify.verify(value instanceof RootStatementContext);
152 includedContexts.add((RootStatementContext<?, ?, ?>) value);
154 return super.putToLocalStorage(type, key, value);
159 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
160 return getFromLocalStorage(type, key, new HashSet<>());
164 * We need to track already checked RootStatementContexts due to possible
165 * circular chains of includes between submodules
168 private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
169 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
170 final V potentialLocal = super.getFromLocalStorage(type, key);
171 if (potentialLocal != null) {
172 return potentialLocal;
175 alreadyChecked.add(this);
176 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
177 if (alreadyChecked.contains(includedSource)) {
180 final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
181 if (potential != null) {
190 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
191 return getAllFromLocalStorage(type, new HashSet<>());
195 * We need to track already checked RootStatementContexts due to possible
196 * circular chains of includes between submodules
199 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
200 final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
201 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
202 if (potentialLocal != null) {
203 return potentialLocal;
206 alreadyChecked.add(this);
207 for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
208 if (alreadyChecked.contains(includedSource)) {
211 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
212 if (potential != null) {
220 public YangVersion getRootVersion() {
221 return version == null ? DEFAULT_VERSION : version;
225 public void setRootVersion(final YangVersion version) {
226 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
227 "Unsupported yang version %s in %s", version, getStatementSourceReference());
228 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
230 this.version = Preconditions.checkNotNull(version);
234 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
235 sourceContext.addMutableStmtToSeal(mutableStatement);
239 public void addRequiredModule(final ModuleIdentifier dependency) {
240 Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
241 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
242 if (requiredModules.isEmpty()) {
243 requiredModules = new HashSet<>();
245 requiredModules.add(dependency);
248 Collection<ModuleIdentifier> getRequiredModules() {
249 return ImmutableSet.copyOf(requiredModules);
253 public void setRootIdentifier(final ModuleIdentifier identifier) {
254 Preconditions.checkNotNull(identifier);
255 this.identifier = identifier;
258 ModuleIdentifier getRootIdentifier() {
263 protected boolean isIgnoringIfFeatures() {
268 protected boolean isIgnoringConfig() {
273 protected boolean isParentSupportedByFeatures() {