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