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