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