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;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
36 * of this class, hence recursive lookups from them cross this class.
38 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
39 StatementContextBase<A, D, E> {
41 public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
43 private final SourceSpecificContext sourceContext;
44 private final A argument;
46 private YangVersion version;
49 * References to RootStatementContext of submodules which are included in this source.
51 private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
53 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
54 final StatementSourceReference ref, final String rawArgument) {
55 super(def, ref, rawArgument);
56 this.sourceContext = Preconditions.checkNotNull(sourceContext);
57 this.argument = def.parseArgumentValue(this, rawArgument);
60 RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
61 final StatementSourceReference ref, final String rawArgument, final YangVersion version) {
62 this(sourceContext, def, ref, rawArgument);
63 this.setRootVersion(version);
66 RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
67 final CopyType typeOfCopy) {
70 sourceContext = Preconditions.checkNotNull(original.sourceContext);
71 this.argument = original.argument;
73 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
74 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
75 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
77 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
78 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
79 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
82 for (final StmtContext<?, ?, ?> stmtContext : effective) {
83 buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
86 addEffectiveSubstatements(buffer);
90 * @return null as root cannot have parent
93 public StatementContextBase<?, ?, ?> getParentContext() {
98 * @return namespace storage of source context
101 public NamespaceStorageNode getParentNamespaceStorage() {
102 return sourceContext;
106 public Registry getBehaviourRegistry() {
107 return sourceContext;
111 public StorageNodeType getStorageNodeType() {
112 return StorageNodeType.ROOT_STATEMENT_LOCAL;
115 * @return this as its own root
119 public RootStatementContext<?, ?, ?> getRoot() {
123 SourceSpecificContext getSourceContext() {
124 return sourceContext;
128 public A getStatementArgument() {
133 * @return copy of this considering {@link CopyType} (augment, uses)
135 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
138 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
139 final CopyType typeOfCopy) {
140 return createCopy(null, newParent, typeOfCopy);
144 * @return copy of this considering {@link CopyType} (augment, uses)
146 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
149 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
150 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
151 final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
153 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
155 if (this.getOriginalCtx() != null) {
156 copy.setOriginalCtx(this.getOriginalCtx());
158 copy.setOriginalCtx(this);
160 definition().onStatementAdded(copy);
166 public Optional<SchemaPath> getSchemaPath() {
167 return Optional.of(SchemaPath.ROOT);
174 public boolean isRootContext() {
179 public boolean isConfiguration() {
184 public boolean isEnabledSemanticVersioning() {
185 return sourceContext.isEnabledSemanticVersioning();
189 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
191 if (IncludedModuleContext.class.isAssignableFrom(type)) {
192 if (includedContexts.isEmpty()) {
193 includedContexts = new ArrayList<>(1);
195 Verify.verify(value instanceof RootStatementContext);
196 includedContexts.add((RootStatementContext<?, ?, ?>) value);
198 super.addToLocalStorage(type, key, value);
202 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
203 final V potentialLocal = super.getFromLocalStorage(type, key);
204 if (potentialLocal != null) {
205 return potentialLocal;
207 for (final NamespaceStorageNode includedSource : includedContexts) {
208 final V potential = includedSource.getFromLocalStorage(type, key);
209 if (potential != null) {
218 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
219 final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
220 if (potentialLocal != null) {
221 return potentialLocal;
223 for (final NamespaceStorageNode includedSource : includedContexts) {
224 final Map<K, V> potential = includedSource.getAllFromLocalStorage(type);
225 if (potential != null) {
233 public YangVersion getRootVersion() {
234 return version == null ? DEFAULT_VERSION : version;
238 public void setRootVersion(final YangVersion version) {
239 Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
240 "Unsupported yang version %s in %s", version, getStatementSourceReference());
241 Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
243 this.version = Preconditions.checkNotNull(version);