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