StmtContext should not care about semantic versions
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.YangVersion;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
48  * of this class, hence recursive lookups from them cross this class.
49  */
50 public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
51         extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
52     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
53
54     private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
55     // These namespaces are well-known and not needed after the root is cleaned up
56     private static final Map<Class<?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
57         GroupingNamespace.class, new SweptNamespace(GroupingNamespace.class),
58         SchemaTreeNamespace.class, new SweptNamespace(SchemaTreeNamespace.class),
59         TypeNamespace.class, new SweptNamespace(TypeNamespace.class));
60
61     private final @NonNull SourceSpecificContext sourceContext;
62     private final A argument;
63
64     private YangVersion rootVersion;
65     private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
66     private SourceIdentifier rootIdentifier;
67
68     /**
69      * References to RootStatementContext of submodules which are included in this source.
70      */
71     private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
72
73     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
74             final StatementSourceReference ref, final String rawArgument) {
75         super(def, ref, rawArgument);
76         this.sourceContext = requireNonNull(sourceContext);
77         this.argument = def.parseArgumentValue(this, rawArgument());
78     }
79
80     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
81             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
82             final SourceIdentifier identifier) {
83         this(sourceContext, def, ref, rawArgument);
84         this.setRootVersion(version);
85         this.setRootIdentifier(identifier);
86     }
87
88     @Override
89     public StatementContextBase<?, ?, ?> getParentContext() {
90         // null as root cannot have parent
91         return null;
92     }
93
94     @Override
95     public NamespaceStorageNode getParentNamespaceStorage() {
96         // namespace storage of source context
97         return sourceContext;
98     }
99
100     @Override
101     public StorageNodeType getStorageNodeType() {
102         return StorageNodeType.ROOT_STATEMENT_LOCAL;
103     }
104
105     @Override
106     public RootStatementContext<?, ?, ?> getRoot() {
107         // this as its own root
108         return this;
109     }
110
111     SourceSpecificContext getSourceContext() {
112         return sourceContext;
113     }
114
115     @Override
116     public A argument() {
117         return argument;
118     }
119
120     @Override
121     @Deprecated
122     public SchemaPath schemaPath() {
123         return SchemaPath.ROOT;
124     }
125
126     @Override
127     public EffectiveConfig effectiveConfig() {
128         return EffectiveConfig.UNDETERMINED;
129     }
130
131     @Override
132     public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
133             final V value) {
134         if (IncludedModuleContext.class.isAssignableFrom(type)) {
135             if (includedContexts.isEmpty()) {
136                 includedContexts = new ArrayList<>(1);
137             }
138             verify(value instanceof RootStatementContext);
139             includedContexts.add((RootStatementContext<?, ?, ?>) value);
140         }
141         return super.putToLocalStorage(type, key, value);
142     }
143
144     @Override
145     public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
146         return getFromLocalStorage(type, key, new HashSet<>());
147     }
148
149     /*
150      * We need to track already checked RootStatementContexts due to possible
151      * circular chains of includes between submodules
152      */
153     private <K, V, N extends ParserNamespace<K, V>> @Nullable V getFromLocalStorage(final Class<N> type,
154             final K key, final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
155         final V potentialLocal = super.getFromLocalStorage(type, key);
156         if (potentialLocal != null) {
157             return potentialLocal;
158         }
159
160         alreadyChecked.add(this);
161         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
162             if (alreadyChecked.contains(includedSource)) {
163                 continue;
164             }
165             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
166             if (potential != null) {
167                 return potential;
168             }
169         }
170         return null;
171     }
172
173     @Override
174     public <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
175         return getAllFromLocalStorage(type, new HashSet<>());
176     }
177
178     /*
179      * We need to track already checked RootStatementContexts due to possible
180      * circular chains of includes between submodules
181      */
182     private <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(final Class<N> type,
183             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
184         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
185         if (potentialLocal != null) {
186             return potentialLocal;
187         }
188
189         alreadyChecked.add(this);
190         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
191             if (alreadyChecked.contains(includedSource)) {
192                 continue;
193             }
194             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
195             if (potential != null) {
196                 return potential;
197             }
198         }
199         return null;
200     }
201
202     /**
203      * Return the set of required sources.
204      *
205      * @return Required sources.
206      */
207     Collection<SourceIdentifier> getRequiredSources() {
208         return ImmutableSet.copyOf(requiredSources);
209     }
210
211     SourceIdentifier getRootIdentifier() {
212         return rootIdentifier;
213     }
214
215     @Override
216     protected boolean isIgnoringIfFeatures() {
217         return false;
218     }
219
220     @Override
221     protected boolean isIgnoringConfig() {
222         return false;
223     }
224
225     @Override
226     protected boolean isParentSupportedByFeatures() {
227         return true;
228     }
229
230     void setRootIdentifierImpl(final SourceIdentifier identifier) {
231         this.rootIdentifier = requireNonNull(identifier);
232     }
233
234     @NonNull Registry getBehaviourRegistryImpl() {
235         return sourceContext;
236     }
237
238     @NonNull YangVersion getRootVersionImpl() {
239         return rootVersion == null ? DEFAULT_VERSION : rootVersion;
240     }
241
242     void setRootVersionImpl(final YangVersion version) {
243         checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
244                 "Unsupported yang version %s in %s", version, sourceReference());
245         checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
246                 this.rootVersion);
247         this.rootVersion = requireNonNull(version);
248     }
249
250     /**
251      * Add mutable statement to seal. Each mutable statement must be sealed
252      * as the last step of statement parser processing.
253      *
254      * @param mutableStatement
255      *            mutable statement which should be sealed
256      */
257     void addMutableStmtToSeal(final MutableStatement mutableStatement) {
258         sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
259     }
260
261     void addRequiredSourceImpl(final SourceIdentifier dependency) {
262         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
263                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
264         if (requiredSources.isEmpty()) {
265             requiredSources = new HashSet<>();
266         }
267         requiredSources.add(dependency);
268     }
269
270     @Override
271     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
272         throw new UnsupportedOperationException("Root statement cannot be reparented to " + newParent);
273     }
274
275     @Override
276     void sweepNamespaces() {
277         LOG.trace("Sweeping root {}", this);
278         sweepNamespaces(SWEPT_NAMESPACES);
279     }
280 }