YANGTOOLS-706: split out yang-parser-reactor
[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.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Optional;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
37
38 /**
39  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
40  * of this class, hence recursive lookups from them cross this class.
41  */
42 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
43         StatementContextBase<A, D, E> {
44
45     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
46
47     private final SourceSpecificContext sourceContext;
48     private final A argument;
49
50     private YangVersion version;
51     private Collection<SourceIdentifier> requiredSources = ImmutableSet.of();
52     private SourceIdentifier identifier;
53
54     /**
55      * References to RootStatementContext of submodules which are included in this source.
56      */
57     private Collection<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
58
59     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
60         final StatementSourceReference ref, final String rawArgument) {
61         super(def, ref, rawArgument);
62         this.sourceContext = requireNonNull(sourceContext);
63         this.argument = def.parseArgumentValue(this, rawStatementArgument());
64     }
65
66     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
67             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
68             final SourceIdentifier identifier) {
69         this(sourceContext, def, ref, rawArgument);
70         this.setRootVersion(version);
71         this.setRootIdentifier(identifier);
72     }
73
74     @Override
75     public StatementContextBase<?, ?, ?> getParentContext() {
76         // null as root cannot have parent
77         return null;
78     }
79
80     @Override
81     public NamespaceStorageNode getParentNamespaceStorage() {
82         // namespace storage of source context
83         return sourceContext;
84     }
85
86     @Override
87     public Registry getBehaviourRegistry() {
88         return sourceContext;
89     }
90
91     @Override
92     public StorageNodeType getStorageNodeType() {
93         return StorageNodeType.ROOT_STATEMENT_LOCAL;
94     }
95
96     @Nonnull
97     @Override
98     public RootStatementContext<?, ?, ?> getRoot() {
99         // this as its own root
100         return this;
101     }
102
103     SourceSpecificContext getSourceContext() {
104         return sourceContext;
105     }
106
107     @Override
108     public A getStatementArgument() {
109         return argument;
110     }
111
112     @Nonnull
113     @Override
114     public Optional<SchemaPath> getSchemaPath() {
115         return Optional.of(SchemaPath.ROOT);
116     }
117
118     @Override
119     public boolean isConfiguration() {
120         return true;
121     }
122
123     @Override
124     public boolean isEnabledSemanticVersioning() {
125         return sourceContext.isEnabledSemanticVersioning();
126     }
127
128     @Override
129     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
130             final V value) {
131         if (IncludedModuleContext.class.isAssignableFrom(type)) {
132             if (includedContexts.isEmpty()) {
133                 includedContexts = new ArrayList<>(1);
134             }
135             verify(value instanceof RootStatementContext);
136             includedContexts.add((RootStatementContext<?, ?, ?>) value);
137         }
138         return super.putToLocalStorage(type, key, value);
139     }
140
141     @Nullable
142     @Override
143     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
144         return getFromLocalStorage(type, key, new HashSet<>());
145     }
146
147     /*
148      * We need to track already checked RootStatementContexts due to possible
149      * circular chains of includes between submodules
150      */
151     @Nullable
152     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
153             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
154         final V potentialLocal = super.getFromLocalStorage(type, key);
155         if (potentialLocal != null) {
156             return potentialLocal;
157         }
158
159         alreadyChecked.add(this);
160         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
161             if (alreadyChecked.contains(includedSource)) {
162                 continue;
163             }
164             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
165             if (potential != null) {
166                 return potential;
167             }
168         }
169         return null;
170     }
171
172     @Nullable
173     @Override
174     public <K, V, N extends IdentifierNamespace<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     @Nullable
183     private <K, V, N extends IdentifierNamespace<K, V>> 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     @Override
204     public YangVersion getRootVersion() {
205         return version == null ? DEFAULT_VERSION : version;
206     }
207
208     @Override
209     public void setRootVersion(final YangVersion version) {
210         checkArgument(sourceContext.getSupportedVersions().contains(version),
211                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
212         checkState(this.version == null, "Version of root %s has been already set to %s", argument,
213                 this.version);
214         this.version = requireNonNull(version);
215     }
216
217     @Override
218     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
219         sourceContext.addMutableStmtToSeal(mutableStatement);
220     }
221
222     @Override
223     public void addRequiredSource(final SourceIdentifier dependency) {
224         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
225                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
226         if (requiredSources.isEmpty()) {
227             requiredSources = new HashSet<>();
228         }
229         requiredSources.add(dependency);
230     }
231
232     /**
233      * Return the set of required sources.
234      *
235      * @return Required sources.
236      */
237     Collection<SourceIdentifier> getRequiredSources() {
238         return ImmutableSet.copyOf(requiredSources);
239     }
240
241     @Override
242     public void setRootIdentifier(final SourceIdentifier identifier) {
243         this.identifier = requireNonNull(identifier);
244     }
245
246     SourceIdentifier getRootIdentifier() {
247         return identifier;
248     }
249
250     @Override
251     protected boolean isIgnoringIfFeatures() {
252         return false;
253     }
254
255     @Override
256     protected boolean isIgnoringConfig() {
257         return false;
258     }
259
260     @Override
261     protected boolean isParentSupportedByFeatures() {
262         return true;
263     }
264 }