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 com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Throwables;
14 import com.google.common.collect.ImmutableCollection;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMultimap;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.Multimaps;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.EnumMap;
23 import java.util.EventListener;
24 import java.util.Iterator;
25 import javax.annotation.Nonnull;
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.api.meta.StatementDefinition;
30 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
41 import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.ValueAddedListener;
43 public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
44 extends NamespaceStorageSupport implements StmtContext.Mutable<A, D, E> {
47 * event listener when an item is added to model namespace
49 interface OnNamespaceItemAdded extends EventListener {
51 * @throws SourceException
53 void namespaceItemAdded(StatementContextBase<?, ?, ?> context, Class<?> namespace, Object key, Object value);
57 * event listener when a parsing {@link ModelProcessingPhase} is completed
59 interface OnPhaseFinished extends EventListener {
61 * @throws SourceException
63 boolean phaseFinished(StatementContextBase<?, ?, ?> context, ModelProcessingPhase phase);
67 * interface for all mutations within an {@link ModelActionBuilder.InferenceAction}
69 interface ContextMutation {
74 private final StatementDefinitionContext<A, D, E> definition;
75 private final StatementSourceReference statementDeclSource;
76 private final String rawArgument;
78 private Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = ImmutableMultimap.of();
79 private Multimap<ModelProcessingPhase, ContextMutation> phaseMutation = ImmutableMultimap.of();
80 private Collection<StatementContextBase<?, ?, ?>> effective = ImmutableList.of();
81 private Collection<StatementContextBase<?, ?, ?>> effectOfStatement = ImmutableList.of();
82 private StatementMap substatements = StatementMap.empty();
84 private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
85 private CopyHistory copyHistory = CopyHistory.original();
86 private boolean isSupportedToBuildEffective = true;
87 private ModelProcessingPhase completedPhase = null;
88 private StatementContextBase<?, ?, ?> originalCtx;
89 private D declaredInstance;
90 private E effectiveInstance;
91 private int order = 0;
93 StatementContextBase(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
94 final String rawArgument) {
95 this.definition = Preconditions.checkNotNull(def);
96 this.statementDeclSource = Preconditions.checkNotNull(ref);
97 this.rawArgument = rawArgument;
100 StatementContextBase(final StatementContextBase<A, D, E> original) {
101 this.definition = Preconditions.checkNotNull(original.definition,
102 "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
103 this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
104 "Statement context statementDeclSource cannot be null copying from: %s",
105 original.getStatementSourceReference());
106 this.rawArgument = original.rawArgument;
110 public Collection<StatementContextBase<?, ?, ?>> getEffectOfStatement() {
111 return effectOfStatement;
115 public void addAsEffectOfStatement(final StatementContextBase<?, ?, ?> ctx) {
116 if (effectOfStatement.isEmpty()) {
117 effectOfStatement = new ArrayList<>(1);
119 effectOfStatement.add(ctx);
123 public void addAsEffectOfStatement(final Collection<StatementContextBase<?, ?, ?>> ctxs) {
124 if (ctxs.isEmpty()) {
128 if (effectOfStatement.isEmpty()) {
129 effectOfStatement = new ArrayList<>(ctxs.size());
131 effectOfStatement.addAll(ctxs);
135 public SupportedByFeatures getSupportedByFeatures() {
136 return supportedByFeatures;
140 public void setSupportedByFeatures(final boolean isSupported) {
141 this.supportedByFeatures = isSupported ? SupportedByFeatures.SUPPORTED : SupportedByFeatures.NOT_SUPPORTED;
145 public boolean isSupportedToBuildEffective() {
146 return isSupportedToBuildEffective;
150 public void setIsSupportedToBuildEffective(final boolean isSupportedToBuildEffective) {
151 this.isSupportedToBuildEffective = isSupportedToBuildEffective;
155 public CopyHistory getCopyHistory() {
160 public void appendCopyHistory(final CopyType typeOfCopy, final CopyHistory toAppend) {
161 copyHistory = copyHistory.append(typeOfCopy, toAppend);
165 public StatementContextBase<?, ?, ?> getOriginalCtx() {
170 public void setOriginalCtx(final StatementContextBase<?, ?, ?> originalCtx) {
171 this.originalCtx = originalCtx;
175 public void setOrder(final int order) {
180 public int getOrder() {
185 public ModelProcessingPhase getCompletedPhase() {
186 return completedPhase;
190 public void setCompletedPhase(final ModelProcessingPhase completedPhase) {
191 this.completedPhase = completedPhase;
195 public abstract StatementContextBase<?, ?, ?> getParentContext();
198 * @return root context of statement
202 public abstract RootStatementContext<?, ?, ?> getRoot();
205 * @return origin of statement
209 public StatementSource getStatementSource() {
210 return statementDeclSource.getStatementSource();
214 * @return reference of statement source
218 public StatementSourceReference getStatementSourceReference() {
219 return statementDeclSource;
223 public final String rawStatementArgument() {
229 public Collection<StatementContextBase<?, ?, ?>> declaredSubstatements() {
230 return substatements.values();
235 public Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements() {
236 if (effective instanceof ImmutableCollection) {
240 return Collections.unmodifiableCollection(effective);
243 public void removeStatementsFromEffectiveSubstatements(final Collection<StatementContextBase<?, ?, ?>> substatements) {
244 if (!effective.isEmpty()) {
245 effective.removeAll(substatements);
250 private void shrinkEffective() {
251 if (effective.isEmpty()) {
252 effective = ImmutableList.of();
256 public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
257 if (effective.isEmpty()) {
261 final Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
262 while (iterator.hasNext()) {
263 final StatementContextBase<?, ?, ?> next = iterator.next();
264 if (statementDef.equals(next.getPublicDefinition())) {
273 * Removes a statement context from the effective substatements
274 * based on its statement definition (i.e statement keyword) and raw (in String form) statement argument.
275 * The statement context is removed only if both statement definition and statement argument match with
276 * one of the effective substatements' statement definition and argument.
278 * If the statementArg parameter is null, the statement context is removed based only on its statement definition.
280 * @param statementDef statement definition of the statement context to remove
281 * @param statementArg statement argument of the statement context to remove
283 public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
284 final String statementArg) {
285 if (statementArg == null) {
286 removeStatementFromEffectiveSubstatements(statementDef);
289 if (effective.isEmpty()) {
293 final Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
294 while (iterator.hasNext()) {
295 final StatementContextBase<?, ?, ?> next = iterator.next();
296 if (statementDef.equals(next.getPublicDefinition()) && statementArg.equals(next.rawStatementArgument())) {
305 * adds effective statement to collection of substatements
307 * @param substatement substatement
308 * @throws IllegalStateException
309 * if added in declared phase
310 * @throws NullPointerException
311 * if statement parameter is null
313 public void addEffectiveSubstatement(final StatementContextBase<?, ?, ?> substatement) {
314 Preconditions.checkNotNull(substatement, "StatementContextBase effective substatement cannot be null at: %s",
315 getStatementSourceReference());
316 beforeAddEffectiveStatement(1);
317 effective.add(substatement);
321 * adds effective statement to collection of substatements
323 * @param substatements substatements
324 * @throws IllegalStateException
325 * if added in declared phase
326 * @throws NullPointerException
327 * if statement parameter is null
329 public void addEffectiveSubstatements(final Collection<StatementContextBase<?, ?, ?>> substatements) {
330 if (substatements.isEmpty()) {
334 substatements.forEach(Preconditions::checkNotNull);
335 beforeAddEffectiveStatement(substatements.size());
336 effective.addAll(substatements);
339 private void beforeAddEffectiveStatement(final int toAdd) {
340 final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
341 Preconditions.checkState(inProgressPhase == ModelProcessingPhase.FULL_DECLARATION
342 || inProgressPhase == ModelProcessingPhase.EFFECTIVE_MODEL,
343 "Effective statement cannot be added in declared phase at: %s", getStatementSourceReference());
345 if (effective.isEmpty()) {
346 effective = new ArrayList<>(toAdd);
351 * Create a new substatement at the specified offset.
353 * @param offset Substatement offset
354 * @param def definition context
355 * @param ref source reference
356 * @param argument statement argument
357 * @return A new substatement
359 public final <CA, CD extends DeclaredStatement<CA>, CE extends EffectiveStatement<CA, CD>> StatementContextBase<CA, CD, CE>
360 createSubstatement(final int offset, final StatementDefinitionContext<CA, CD, CE> def,
361 final StatementSourceReference ref, final String argument) {
362 final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
363 Preconditions.checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL,
364 "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference());
366 final StatementContextBase<CA, CD, CE> ret = new SubstatementContext<>(this, def, ref, argument);
367 substatements = substatements.put(offset, ret);
368 def.onStatementAdded(ret);
373 * Lookup substatement by its offset in this statement.
375 * @param offset Substatement offset
376 * @return Substatement, or null if substatement does not exist.
378 final StatementContextBase<?, ?, ?> lookupSubstatement(final int offset) {
379 return substatements.get(offset);
383 * builds {@link DeclaredStatement} for statement context
386 public D buildDeclared() {
387 Preconditions.checkArgument(completedPhase == ModelProcessingPhase.FULL_DECLARATION
388 || completedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
389 if (declaredInstance == null) {
390 declaredInstance = definition().getFactory().createDeclared(this);
392 return declaredInstance;
396 * builds {@link EffectiveStatement} for statement context
399 public E buildEffective() {
400 if (effectiveInstance == null) {
401 effectiveInstance = definition().getFactory().createEffective(this);
403 return effectiveInstance;
407 * tries to execute current {@link ModelProcessingPhase} of source parsing
410 * to be executed (completed)
411 * @return if phase was successfully completed
412 * @throws SourceException
413 * when an error occured in source parsing
415 boolean tryToCompletePhase(final ModelProcessingPhase phase) {
417 boolean finished = true;
418 final Collection<ContextMutation> openMutations = phaseMutation.get(phase);
419 if (!openMutations.isEmpty()) {
420 final Iterator<ContextMutation> it = openMutations.iterator();
421 while (it.hasNext()) {
422 final ContextMutation current = it.next();
423 if (current.isFinished()) {
430 if (openMutations.isEmpty()) {
431 phaseMutation.removeAll(phase);
432 if (phaseMutation.isEmpty()) {
433 phaseMutation = ImmutableMultimap.of();
438 for (final StatementContextBase<?, ?, ?> child : substatements.values()) {
439 finished &= child.tryToCompletePhase(phase);
441 for (final StatementContextBase<?, ?, ?> child : effective) {
442 finished &= child.tryToCompletePhase(phase);
446 onPhaseCompleted(phase);
453 * Occurs on end of {@link ModelProcessingPhase} of source parsing
456 * that was to be completed (finished)
457 * @throws SourceException
458 * when an error occurred in source parsing
460 private void onPhaseCompleted(final ModelProcessingPhase phase) {
461 completedPhase = phase;
463 final Collection<OnPhaseFinished> listeners = phaseListeners.get(phase);
464 if (listeners.isEmpty()) {
468 final Iterator<OnPhaseFinished> listener = listeners.iterator();
469 while (listener.hasNext()) {
470 final OnPhaseFinished next = listener.next();
471 if (next.phaseFinished(this, phase)) {
476 if (listeners.isEmpty()) {
477 phaseListeners.removeAll(phase);
478 if (phaseListeners.isEmpty()) {
479 phaseListeners = ImmutableMultimap.of();
485 * Ends declared section of current node.
488 * @throws SourceException
490 void endDeclared(final StatementSourceReference ref, final ModelProcessingPhase phase) {
491 definition().onDeclarationFinished(this, phase);
495 * @return statement definition
497 protected final StatementDefinitionContext<A, D, E> definition() {
502 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
503 definition().checkNamespaceAllowed(type);
507 * occurs when an item is added to model namespace
509 * @throws SourceException instance of SourceException
512 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key, final V value) {
513 // definition().onNamespaceElementAdded(this, type, key, value);
516 <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final K key,
517 final OnNamespaceItemAdded listener) throws SourceException {
518 final Object potential = getFromNamespace(type, key);
519 if (potential != null) {
520 listener.namespaceItemAdded(this, type, key, potential);
523 final NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
524 if (behaviour instanceof NamespaceBehaviourWithListeners) {
525 final NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
526 casted.addValueListener(new ValueAddedListener<K>(this, key) {
528 void onValueAdded(final Object key, final Object value) {
530 listener.namespaceItemAdded(StatementContextBase.this, type, key, value);
531 } catch (final SourceException e) {
532 throw Throwables.propagate(e);
540 * @see StatementSupport#getPublicView()
544 public StatementDefinition getPublicDefinition() {
545 return definition().getPublicView();
549 public ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
550 return getRoot().getSourceContext().newInferenceAction(phase);
553 private static <T> Multimap<ModelProcessingPhase, T> newMultimap() {
554 return Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
558 * adds {@link OnPhaseFinished} listener for a {@link ModelProcessingPhase} end
560 * @throws SourceException
562 void addPhaseCompletedListener(final ModelProcessingPhase phase, final OnPhaseFinished listener) {
564 Preconditions.checkNotNull(phase, "Statement context processing phase cannot be null at: %s",
565 getStatementSourceReference());
566 Preconditions.checkNotNull(listener, "Statement context phase listener cannot be null at: %s",
567 getStatementSourceReference());
569 ModelProcessingPhase finishedPhase = completedPhase;
570 while (finishedPhase != null) {
571 if (phase.equals(finishedPhase)) {
572 listener.phaseFinished(this, finishedPhase);
575 finishedPhase = finishedPhase.getPreviousPhase();
577 if (phaseListeners.isEmpty()) {
578 phaseListeners = newMultimap();
581 phaseListeners.put(phase, listener);
585 * adds {@link ContextMutation} to {@link ModelProcessingPhase}
587 * @throws IllegalStateException
588 * when the mutation was registered after phase was completed
590 void addMutation(final ModelProcessingPhase phase, final ContextMutation mutation) {
591 ModelProcessingPhase finishedPhase = completedPhase;
592 while (finishedPhase != null) {
593 if (phase.equals(finishedPhase)) {
594 throw new IllegalStateException("Mutation registered after phase was completed at: " +
595 getStatementSourceReference());
597 finishedPhase = finishedPhase.getPreviousPhase();
600 if (phaseMutation.isEmpty()) {
601 phaseMutation = newMultimap();
603 phaseMutation.put(phase, mutation);
607 * adds statement to namespace map with the key
610 * {@link StatementNamespace} child that determines namespace to be added to
612 * of type according to namespace class specification
614 * to be added to namespace map
617 public <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(final Class<N> namespace, final KT key,
618 final StmtContext<?, ?, ?> stmt) {
619 addContextToNamespace(namespace, key, stmt);
623 public final String toString() {
624 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
627 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
628 return toStringHelper.add("definition", definition).add("rawArgument", rawArgument);