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 java.util.ArrayList;
15 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
36 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
37 * of this class, hence recursive lookups from them cross this class.
39 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
40 StatementContextBase<A, D, E> {
42 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
44 private final SourceSpecificContext sourceContext;
45 private final A argument;
47 private YangVersion version;
50 * References to RootStatementContext of submodules which are included in this source.
52 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
54 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
55 final StatementSourceReference ref, final String rawArgument) {
56 super(def, ref, rawArgument);
57 this.sourceContext = Preconditions.checkNotNull(sourceContext);
58 this.argument = def.parseArgumentValue(this, rawArgument);
61 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
62 final StatementSourceReference ref, final String rawArgument, final YangVersion version) {
63 this(sourceContext, def, ref, rawArgument);
64 this.setRootVersion(version);
67 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
68 final CopyType typeOfCopy) {
71 sourceContext = Preconditions.checkNotNull(original.sourceContext);
72 this.argument = original.argument;
74 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
75 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
76 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
78 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
79 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
80 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
83 for (final StmtContext<?, ?, ?> stmtContext : effective) {
84 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
87 addEffectiveSubstatements(buffer);
91 * @return null as root cannot have parent
94 public StatementContextBase<?, ?, ?> getParentContext() {
99 * @return namespace storage of source context
102 public NamespaceStorageNode getParentNamespaceStorage() {
103 return sourceContext;
107 public Registry getBehaviourRegistry() {
108 return sourceContext;
112 public StorageNodeType getStorageNodeType() {
113 return StorageNodeType.ROOT_STATEMENT_LOCAL;
116 * @return this as its own root
120 public RootStatementContext<?, ?, ?> getRoot() {
124 SourceSpecificContext getSourceContext() {
125 return sourceContext;
129 public A getStatementArgument() {
134 * @return copy of this considering {@link CopyType} (augment, uses)
136 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
139 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
140 final CopyType typeOfCopy) {
141 return createCopy(null, newParent, typeOfCopy);
145 * @return copy of this considering {@link CopyType} (augment, uses)
147 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
150 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
151 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
152 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
154 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
156 if (this.getOriginalCtx() != null) {
157 copy.setOriginalCtx(this.getOriginalCtx());
159 copy.setOriginalCtx(this);
161 definition().onStatementAdded(copy);
167 public Optional<SchemaPath> getSchemaPath() {
168 return Optional.of(SchemaPath.ROOT);
175 public boolean isRootContext() {
180 public boolean isConfiguration() {
185 public boolean isEnabledSemanticVersioning() {
186 return sourceContext.isEnabledSemanticVersioning();
190 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
192 if (IncludedModuleContext.class.isAssignableFrom(type)) {
193 if (includedContexts.isEmpty()) {
194 includedContexts = new ArrayList<>(1);
196 Verify.verify(value instanceof RootStatementContext);
197 includedContexts.add((RootStatementContext<?, ?, ?>) value);
199 super.addToLocalStorage(type, key, value);
203 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
204 final V potentialLocal = super.getFromLocalStorage(type, key);
205 if (potentialLocal != null) {
206 return potentialLocal;
208 for (final NamespaceStorageNode includedSource : includedContexts) {
209 final V potential = includedSource.getFromLocalStorage(type, key);
210 if (potential != null) {
219 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
220 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
221 if (potentialLocal != null) {
222 return potentialLocal;
224 for (final NamespaceStorageNode includedSource : includedContexts) {
225 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
226 if (potential != null) {
234 public YangVersion getRootVersion() {
235 return version == null ? DEFAULT_VERSION : version;
239 public void setRootVersion(final YangVersion version) {
240 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
241 "Unsupported yang version %s in %s", version, getStatementSourceReference());
242 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
244 this.version = Preconditions.checkNotNull(version);
248 public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
249 sourceContext.addMutableStmtToSeal(mutableStatement);