11f6f921e9eefac2ba2f0a625518e79f96c14d58
[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.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Set;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.YangVersion;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
37 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
39
40 /**
41  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
42  * of this class, hence recursive lookups from them cross this class.
43  */
44 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
45         StatementContextBase<A, D, E> {
46
47     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
48
49     private final SourceSpecificContext sourceContext;
50     private final A argument;
51
52     private YangVersion rootVersion;
53     private Set<SourceIdentifier> requiredSources = ImmutableSet.of();
54     private SourceIdentifier rootIdentifier;
55
56     /**
57      * References to RootStatementContext of submodules which are included in this source.
58      */
59     private List<RootStatementContext<?, ?, ?>> includedContexts = ImmutableList.of();
60
61     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
62         final StatementSourceReference ref, final String rawArgument) {
63         super(def, ref, rawArgument);
64         this.sourceContext = requireNonNull(sourceContext);
65         this.argument = def.parseArgumentValue(this, rawStatementArgument());
66     }
67
68     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
69             final StatementSourceReference ref, final String rawArgument, final YangVersion version,
70             final SourceIdentifier identifier) {
71         this(sourceContext, def, ref, rawArgument);
72         this.setRootVersion(version);
73         this.setRootIdentifier(identifier);
74     }
75
76     @Override
77     public StatementContextBase<?, ?, ?> getParentContext() {
78         // null as root cannot have parent
79         return null;
80     }
81
82     @Override
83     public NamespaceStorageNode getParentNamespaceStorage() {
84         // namespace storage of source context
85         return sourceContext;
86     }
87
88     @Override
89     public Registry getBehaviourRegistry() {
90         return sourceContext;
91     }
92
93     @Override
94     public StorageNodeType getStorageNodeType() {
95         return StorageNodeType.ROOT_STATEMENT_LOCAL;
96     }
97
98     @Nonnull
99     @Override
100     public RootStatementContext<?, ?, ?> getRoot() {
101         // this as its own root
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(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 rootVersion == null ? DEFAULT_VERSION : rootVersion;
208     }
209
210     @Override
211     public void setRootVersion(final YangVersion version) {
212         checkArgument(sourceContext.getSupportedVersions().contains(version),
213                 "Unsupported yang version %s in %s", version, getStatementSourceReference());
214         checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
215                 this.rootVersion);
216         this.rootVersion = requireNonNull(version);
217     }
218
219     @Override
220     public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
221         sourceContext.addMutableStmtToSeal(mutableStatement);
222     }
223
224     @Override
225     public void addRequiredSource(final SourceIdentifier dependency) {
226         checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
227                 "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
228         if (requiredSources.isEmpty()) {
229             requiredSources = new HashSet<>();
230         }
231         requiredSources.add(dependency);
232     }
233
234     /**
235      * Return the set of required sources.
236      *
237      * @return Required sources.
238      */
239     Collection<SourceIdentifier> getRequiredSources() {
240         return ImmutableSet.copyOf(requiredSources);
241     }
242
243     @Override
244     public void setRootIdentifier(final SourceIdentifier identifier) {
245         this.rootIdentifier = requireNonNull(identifier);
246     }
247
248     SourceIdentifier getRootIdentifier() {
249         return rootIdentifier;
250     }
251
252     @Override
253     protected boolean isIgnoringIfFeatures() {
254         return false;
255     }
256
257     @Override
258     protected boolean isIgnoringConfig() {
259         return false;
260     }
261
262     @Override
263     protected boolean isParentSupportedByFeatures() {
264         return true;
265     }
266 }