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