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.NamespaceBehaviour.NamespaceStorageNode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
34 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
35 * of this class, hence recursive lookups from them cross this class.
37 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
38 StatementContextBase<A, D, E> {
40 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
42 private final SourceSpecificContext sourceContext;
43 private final A argument;
45 private YangVersion version;
48 * References to RootStatementContext of submodules which are included in this source.
50 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
52 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) {
54 this.sourceContext = Preconditions.checkNotNull(sourceContext);
55 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
58 RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext,
59 final YangVersion version) {
60 this(builder, sourceContext);
61 this.setRootVersion(version);
64 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
65 final CopyType typeOfCopy) {
68 sourceContext = Preconditions.checkNotNull(original.sourceContext);
69 this.argument = original.argument;
71 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
72 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
73 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
75 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
76 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
77 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
80 for (final StmtContext<?, ?, ?> stmtContext : effective) {
81 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
84 addEffectiveSubstatements(buffer);
88 * @return null as root cannot have parent
91 public StatementContextBase<?, ?, ?> getParentContext() {
96 * @return namespace storage of source context
99 public NamespaceStorageNode getParentNamespaceStorage() {
100 return sourceContext;
104 * @return registry of source context
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);