a917e53d6abe6f4eee1ed3e2eb347183c0edc318
[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     @Override
72     public StatementContextBase<?, ?, ?> getParentContext() {
73         // null as root cannot have parent
74         return null;
75     }
76
77     @Override
78     public NamespaceStorageNode getParentNamespaceStorage() {
79         // namespace storage of source context
80         return sourceContext;
81     }
82
83     @Override
84     public Registry getBehaviourRegistry() {
85         return sourceContext;
86     }
87
88     @Override
89     public StorageNodeType getStorageNodeType() {
90         return StorageNodeType.ROOT_STATEMENT_LOCAL;
91     }
92
93     @Nonnull
94     @Override
95     public RootStatementContext<?, ?, ?> getRoot() {
96         // this as its own root
97         return this;
98     }
99
100     SourceSpecificContext getSourceContext() {
101         return sourceContext;
102     }
103
104     @Override
105     public A getStatementArgument() {
106         return argument;
107     }
108
109     @Nonnull
110     @Override
111     public Optional<SchemaPath> getSchemaPath() {
112         return Optional.of(SchemaPath.ROOT);
113     }
114
115     @Override
116     public boolean isConfiguration() {
117         return true;
118     }
119
120     @Override
121     public boolean isEnabledSemanticVersioning() {
122         return sourceContext.isEnabledSemanticVersioning();
123     }
124
125     @Override
126     public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
127             final V value) {
128         if (IncludedModuleContext.class.isAssignableFrom(type)) {
129             if (includedContexts.isEmpty()) {
130                 includedContexts = new ArrayList<>(1);
131             }
132             Verify.verify(value instanceof RootStatementContext);
133             includedContexts.add((RootStatementContext<?, ?, ?>) value);
134         }
135         return super.putToLocalStorage(type, key, value);
136     }
137
138     @Nullable
139     @Override
140     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
141         return getFromLocalStorage(type, key, new HashSet<>());
142     }
143
144     /*
145      * We need to track already checked RootStatementContexts due to possible
146      * circular chains of includes between submodules
147      */
148     @Nullable
149     private <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key,
150             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
151         final V potentialLocal = super.getFromLocalStorage(type, key);
152         if (potentialLocal != null) {
153             return potentialLocal;
154         }
155
156         alreadyChecked.add(this);
157         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
158             if (alreadyChecked.contains(includedSource)) {
159                 continue;
160             }
161             final V potential = includedSource.getFromLocalStorage(type, key, alreadyChecked);
162             if (potential != null) {
163                 return potential;
164             }
165         }
166         return null;
167     }
168
169     @Nullable
170     @Override
171     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
172         return getAllFromLocalStorage(type, new HashSet<>());
173     }
174
175     /*
176      * We need to track already checked RootStatementContexts due to possible
177      * circular chains of includes between submodules
178      */
179     @Nullable
180     private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type,
181             final HashSet<RootStatementContext<?, ?, ?>> alreadyChecked) {
182         final Map<K, V> potentialLocal = super.getAllFromLocalStorage(type);
183         if (potentialLocal != null) {
184             return potentialLocal;
185         }
186
187         alreadyChecked.add(this);
188         for (final RootStatementContext<?, ?, ?> includedSource : includedContexts) {
189             if (alreadyChecked.contains(includedSource)) {
190                 continue;
191             }
192             final Map<K, V> potential = includedSource.getAllFromLocalStorage(type, alreadyChecked);
193             if (potential != null) {
194                 return potential;
195             }
196         }
197         return null;
198     }
199
200     @Override
201     public YangVersion getRootVersion() {
202         return version == null ? DEFAULT_VERSION : version;
203     }
204
205     @Override
206     public void setRootVersion(final YangVersion version) {
207         Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version),
208                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
209         Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument,
210                 this.version);
211         this.version = Preconditions.checkNotNull(version);
212     }
213
214     @Override
215     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
216         sourceContext.addMutableStmtToSeal(mutableStatement);
217     }
218
219     @Override
220     public void addRequiredModule(final ModuleIdentifier dependency) {
221         Preconditions.checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
222                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
223         if (requiredModules.isEmpty()) {
224             requiredModules = new HashSet<>();
225         }
226         requiredModules.add(dependency);
227     }
228
229     Collection<ModuleIdentifier> getRequiredModules() {
230         return ImmutableSet.copyOf(requiredModules);
231     }
232
233     @Override
234     public void setRootIdentifier(final ModuleIdentifier identifier) {
235         Preconditions.checkNotNull(identifier);
236         this.identifier = identifier;
237     }
238
239     ModuleIdentifier getRootIdentifier() {
240         return identifier;
241     }
242
243     @Override
244     protected boolean isIgnoringIfFeatures() {
245         return false;
246     }
247
248     @Override
249     protected boolean isIgnoringConfig() {
250         return false;
251     }
252
253     @Override
254     protected boolean isParentSupportedByFeatures() {
255         return true;
256     }
257 }