2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Multimap;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Objects;
25 import java.util.Optional;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.opendaylight.yangtools.concepts.Mutable;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
31 import org.opendaylight.yangtools.yang.common.YangVersion;
32 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
33 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
35 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
36 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementDefinitions;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
48 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixResolver;
49 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
50 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinitionMap;
51 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
52 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 final class SourceSpecificContext implements NamespaceStorage, Mutable {
57 enum PhaseCompletionProgress {
63 private static final class SupportedStatements extends NamespaceAccess<QName, StatementSupport<?, ?, ?>> {
64 private final QNameToStatementDefinitionMap statementDefinitions;
66 SupportedStatements(final QNameToStatementDefinitionMap statementDefinitions) {
67 this.statementDefinitions = requireNonNull(statementDefinitions);
71 ParserNamespace<QName, StatementSupport<?, ?, ?>> namespace() {
72 return StatementSupport.NAMESPACE;
76 StatementSupport<?, ?, ?> valueFrom(final NamespaceStorage storage, final QName key) {
77 return statementDefinitions.getSupport(key);
81 void valueTo(final NamespaceStorage storage, final QName key, final StatementSupport<?, ?, ?> value) {
86 Map<QName, StatementSupport<?, ?, ?>> allFrom(final NamespaceStorage storage) {
91 Entry<QName, StatementSupport<?, ?, ?>> entryFrom(final NamespaceStorage storage,
92 final NamespaceKeyCriterion<QName> criterion) {
97 void addListener(final QName key, final KeyedValueAddedListener<QName, StatementSupport<?, ?, ?>> listener) {
102 void addListener(final PredicateValueAddedListener<QName, StatementSupport<?, ?, ?>> listener) {
106 private static UnsupportedOperationException uoe() {
107 return new UnsupportedOperationException("StatementSupportNamespace is immutable");
111 private static final Logger LOG = LoggerFactory.getLogger(SourceSpecificContext.class);
113 // TODO: consider keying by Byte equivalent of ExecutionOrder
114 private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
115 private final QNameToStatementDefinitionMap qnameToStmtDefMap = new QNameToStatementDefinitionMap();
116 private final @NonNull SupportedStatements statementSupports = new SupportedStatements(qnameToStmtDefMap);
117 private final HashMapPrefixResolver prefixToModuleMap = new HashMapPrefixResolver();
118 private final @NonNull BuildGlobalContext globalContext;
120 // Freed as soon as we complete ModelProcessingPhase.EFFECTIVE_MODEL
121 private StatementStreamSource source;
124 * "imported" namespaces in this source -- this points to RootStatementContexts of
125 * - modules imported via 'import' statement
126 * - parent module, declared via 'belongs-to' statement
128 private List<RootStatementContext<?, ?, ?>> importedNamespaces = ImmutableList.of();
129 private RootStatementContext<?, ?, ?> root;
130 // TODO: consider using ExecutionOrder byte for these two
131 private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
132 private ModelProcessingPhase inProgressPhase;
134 // If not null, do not add anything to modifiers, but record it here.
135 private List<Entry<ModelProcessingPhase, ModifierImpl>> delayedModifiers;
137 SourceSpecificContext(final BuildGlobalContext globalContext, final StatementStreamSource source) {
138 this.globalContext = requireNonNull(globalContext);
139 this.source = requireNonNull(source);
142 @NonNull BuildGlobalContext globalContext() {
143 return globalContext;
146 ModelProcessingPhase getInProgressPhase() {
147 return inProgressPhase;
150 AbstractResumedStatement<?, ?, ?> createDeclaredChild(final AbstractResumedStatement<?, ?, ?> current,
151 final int childId, final QName name, final String argument, final StatementSourceReference ref) {
152 StatementDefinitionContext<?, ?, ?> def = globalContext.getStatementDefinition(getRootVersion(), name);
154 def = globalContext.getModelDefinedStatementDefinition(name);
156 final StatementSupport<?, ?, ?> extension = qnameToStmtDefMap.getSupport(name);
157 if (extension != null) {
158 def = new StatementDefinitionContext<>(extension);
159 globalContext.putModelDefinedStatementDefinition(name, def);
162 } else if (current != null) {
163 def = current.definition().overrideDefinition(def);
166 if (InferenceException.throwIfNull(def, ref, "Statement %s does not have type mapping defined.", name)
167 .getArgumentDefinition().isPresent()) {
168 SourceException.throwIfNull(argument, ref, "Statement %s requires an argument", name);
170 SourceException.throwIf(argument != null, ref, "Statement %s does not take argument", name);
174 * If the current statement definition has argument specific
175 * sub-definitions, get argument specific sub-definition based on given
176 * argument (e.g. type statement need to be specialized based on its
179 if (def.hasArgumentSpecificSubDefinitions()) {
180 def = def.getSubDefinitionSpecificForArgument(argument);
183 if (current != null) {
184 return current.createSubstatement(childId, def, ref, argument);
188 * If root is null or root version is other than default,
189 * we need to create new root.
192 root = new RootStatementContext<>(this, def, ref, argument);
193 } else if (!RootStatementContext.DEFAULT_VERSION.equals(root.yangVersion())
194 && inProgressPhase == ModelProcessingPhase.SOURCE_LINKAGE) {
195 root = new RootStatementContext<>(this, def, ref, argument, root.yangVersion(),
196 root.getRootIdentifier());
198 final QName rootStatement = root.definition().getStatementName();
199 final String rootArgument = root.rawArgument();
201 checkState(Objects.equals(def.getStatementName(), rootStatement) && Objects.equals(argument, rootArgument),
202 "Root statement was already defined as '%s %s'.", rootStatement, rootArgument);
207 @NonNull SourceIdentifier identifySource() {
208 final var arg = root.getArgument();
209 verify(arg instanceof Unqualified, "Unexpected argument %s", arg);
210 final var unqualified = (Unqualified) arg;
212 final var module = root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, root);
213 if (module != null) {
214 // creates SourceIdentifier for a module
215 return new SourceIdentifier(unqualified, module.revision());
218 // creates SourceIdentifier for a submodule
219 return new SourceIdentifier(unqualified,
220 StmtContextUtils.getLatestRevision(root.declaredSubstatements()).orElse(null));
223 @NonNull DeclaredStatement<?> declaredRoot() {
224 return root.declared();
227 @NonNull EffectiveStatement<?, ?> effectiveRoot() {
228 return root.buildEffective();
232 * Return version of root statement context.
234 * @return version of root statement context
236 private YangVersion getRootVersion() {
237 return root != null ? root.yangVersion() : RootStatementContext.DEFAULT_VERSION;
240 void startPhase(final ModelProcessingPhase phase) {
241 final ModelProcessingPhase previousPhase = phase.getPreviousPhase();
242 verify(Objects.equals(previousPhase, finishedPhase),
243 "Phase sequencing violation: previous phase should be %s, source %s has %s", previousPhase, source,
246 final Collection<ModifierImpl> previousModifiers = modifiers.get(previousPhase);
247 checkState(previousModifiers.isEmpty(), "Previous phase %s has unresolved modifiers %s in source %s",
248 previousPhase, previousModifiers, source);
250 inProgressPhase = phase;
251 LOG.debug("Source {} started phase {}", source, phase);
254 private void updateImportedNamespaces(final ParserNamespace<?, ?> type, final Object value) {
255 if (ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX.equals(type)
256 || ParserNamespaces.IMPORTED_MODULE.equals(type)) {
257 verify(value instanceof RootStatementContext, "Unexpected imported value %s", value);
259 if (importedNamespaces.isEmpty()) {
260 importedNamespaces = new ArrayList<>(1);
262 importedNamespaces.add((RootStatementContext<?, ?, ?>) value);
267 public <K, V> V putToLocalStorage(final ParserNamespace<K, V> type, final K key, final V value) {
268 // RootStatementContext takes care of IncludedModuleContext and the rest...
269 final V ret = root.putToLocalStorage(type, key, value);
270 // FIXME: what about duplicates?
271 updateImportedNamespaces(type, value);
276 public <K, V> V putToLocalStorageIfAbsent(final ParserNamespace<K, V> type, final K key, final V value) {
277 // RootStatementContext takes care of IncludedModuleContext and the rest...
278 final V ret = root.putToLocalStorageIfAbsent(type, key, value);
280 updateImportedNamespaces(type, value);
286 public StorageType getStorageType() {
287 return StorageType.SOURCE_LOCAL_SPECIAL;
291 public <K, V> V getFromLocalStorage(final ParserNamespace<K, V> type, final K key) {
292 final V potentialLocal = root.getFromLocalStorage(type, key);
293 if (potentialLocal != null) {
294 return potentialLocal;
297 for (final NamespaceStorage importedSource : importedNamespaces) {
298 final V potential = importedSource.getFromLocalStorage(type, key);
299 if (potential != null) {
307 public <K, V> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type) {
308 final Map<K, V> potentialLocal = root.getAllFromLocalStorage(type);
309 if (potentialLocal != null) {
310 return potentialLocal;
313 for (final NamespaceStorage importedSource : importedNamespaces) {
314 final Map<K, V> potential = importedSource.getAllFromLocalStorage(type);
316 if (potential != null) {
323 <K, V> NamespaceAccess<K, V> accessNamespace(final ParserNamespace<K, V> type) {
324 if (StatementSupport.NAMESPACE.equals(type)) {
325 @SuppressWarnings("unchecked")
326 final var ret = (NamespaceAccess<K, V>) statementSupports;
329 return globalContext.accessNamespace(type);
333 public GlobalStorage getParentStorage() {
334 return globalContext;
337 PhaseCompletionProgress tryToCompletePhase(final byte executionOrder) {
338 final ModelProcessingPhase phase = verifyNotNull(ModelProcessingPhase.ofExecutionOrder(executionOrder));
339 final Collection<ModifierImpl> currentPhaseModifiers = modifiers.get(phase);
341 boolean hasProgressed = tryToProgress(currentPhaseModifiers);
342 final boolean phaseCompleted = requireNonNull(root, "Malformed source. Valid root element is missing.")
343 .tryToCompletePhase(executionOrder);
345 hasProgressed |= tryToProgress(currentPhaseModifiers);
347 // TODO: use executionOrder instead?
348 if (phaseCompleted && currentPhaseModifiers.isEmpty()) {
349 finishedPhase = phase;
350 LOG.debug("Source {} finished phase {}", source, phase);
351 if (phase == ModelProcessingPhase.EFFECTIVE_MODEL) {
352 // We have the effective model acquired, which is the final phase of source interaction.
353 LOG.trace("Releasing source {}", source);
356 return PhaseCompletionProgress.FINISHED;
359 return hasProgressed ? PhaseCompletionProgress.PROGRESS : PhaseCompletionProgress.NO_PROGRESS;
362 private boolean tryToProgress(final Collection<ModifierImpl> currentPhaseModifiers) {
363 boolean hasProgressed = false;
365 // We are about to iterate over the modifiers and invoke callbacks. Those callbacks can end up circling back
366 // and modifying the same collection. This asserts that modifiers should not be modified.
367 delayedModifiers = List.of();
369 // Try making forward progress ...
370 final Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
371 while (modifier.hasNext()) {
372 if (modifier.next().tryApply()) {
374 hasProgressed = true;
378 // We have finished iterating, if we have any delayed modifiers, put them back. This may seem as if we want
379 // to retry the loop, but we do not have to, as we will be circling back anyway.
381 // The thing is, we are inherently single-threaded and therefore if we observe non-empty delayedModifiers, the
382 // only way that could happen is through a callback, which in turn means we have made progress.
383 if (!delayedModifiers.isEmpty()) {
384 verify(hasProgressed, "Delayed modifiers encountered without making progress in %s", this);
385 for (Entry<ModelProcessingPhase, ModifierImpl> entry : delayedModifiers) {
386 modifiers.put(entry.getKey(), entry.getValue());
389 delayedModifiers = null;
391 return hasProgressed;
394 @NonNull ModelActionBuilder newInferenceAction(final @NonNull ModelProcessingPhase phase) {
395 final ModifierImpl action = new ModifierImpl();
397 if (delayedModifiers != null) {
398 if (delayedModifiers.isEmpty()) {
399 delayedModifiers = new ArrayList<>(2);
401 delayedModifiers.add(Map.entry(phase,action));
403 modifiers.put(phase, action);
410 public String toString() {
411 return "SourceSpecificContext [source=" + source + ", current=" + inProgressPhase + ", finished="
412 + finishedPhase + "]";
415 Optional<StatementSourceException> failModifiers(final ModelProcessingPhase identifier) {
416 final var exceptions = new ArrayList<StatementSourceException>();
417 for (var mod : modifiers.get(identifier)) {
420 } catch (StatementSourceException e) {
425 return switch (exceptions.size()) {
426 case 0 -> Optional.empty();
427 case 1 -> Optional.of(exceptions.get(0));
429 final var ex = new InferenceException("Yang model processing phase " + identifier + " failed", root,
431 exceptions.listIterator(1).forEachRemaining(ex::addSuppressed);
432 yield Optional.of(ex);
437 void loadStatements() {
438 LOG.trace("Source {} loading statements for phase {}", source, inProgressPhase);
440 switch (inProgressPhase) {
441 case SOURCE_PRE_LINKAGE:
442 source.writePreLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef());
445 source.writeLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef(), preLinkagePrefixes(),
448 case STATEMENT_DEFINITION:
449 source.writeLinkageAndStatementDefinitions(new StatementContextWriter(this, inProgressPhase), stmtDef(),
450 prefixes(), getRootVersion());
452 case FULL_DECLARATION:
453 source.writeFull(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes(),
461 private PrefixResolver preLinkagePrefixes() {
462 final HashMapPrefixResolver preLinkagePrefixes = new HashMapPrefixResolver();
463 final var prefixToNamespaceMap = getAllFromLocalStorage(ParserNamespaces.IMP_PREFIX_TO_NAMESPACE);
464 if (prefixToNamespaceMap == null) {
465 //:FIXME if it is a submodule without any import, the map is null. Handle also submodules and includes...
469 prefixToNamespaceMap.forEach((key, value) -> preLinkagePrefixes.put(key, QNameModule.of(value)));
470 return preLinkagePrefixes;
473 private PrefixResolver prefixes() {
474 final var allImports = root.namespace(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX);
475 if (allImports != null) {
476 allImports.forEach((key, value) ->
477 prefixToModuleMap.put(key, root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, value)));
480 final var allBelongsTo = root.namespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
481 if (allBelongsTo != null) {
482 allBelongsTo.forEach((key, value) ->
483 prefixToModuleMap.put(key, root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, value)));
486 return prefixToModuleMap;
489 private QNameToStatementDefinition stmtDef() {
490 // regular YANG statements and extension supports added
491 final StatementSupportBundle supportsForPhase = globalContext.getSupportsForPhase(inProgressPhase);
492 qnameToStmtDefMap.putAll(supportsForPhase.getCommonDefinitions());
493 qnameToStmtDefMap.putAll(supportsForPhase.getDefinitionsSpecificForVersion(getRootVersion()));
495 // No further actions needed
496 if (inProgressPhase != ModelProcessingPhase.FULL_DECLARATION) {
497 return qnameToStmtDefMap;
500 // We need to any and all extension statements which have been declared in the context
501 final Map<QName, StatementSupport<?, ?, ?>> extensions = globalContext.getNamespace(
502 StatementDefinitions.NAMESPACE);
503 if (extensions != null) {
504 extensions.forEach((qname, support) -> {
505 final StatementSupport<?, ?, ?> existing = qnameToStmtDefMap.putIfAbsent(qname, support);
506 if (existing != null) {
507 LOG.debug("Source {} already defines statement {} as {}", source, qname, existing);
509 LOG.debug("Source {} defined statement {} as {}", source, qname, support);
514 return qnameToStmtDefMap;
517 Collection<SourceIdentifier> getRequiredSources() {
518 return root.getRequiredSources();
521 SourceIdentifier getRootIdentifier() {
522 return root.getRootIdentifier();