/* * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.stmt.reactor; import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL; import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.FULL_DECLARATION; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Preconditions; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.function.Function; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.ContextMutation; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.OnNamespaceItemAdded; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase.OnPhaseFinished; import org.slf4j.Logger; import org.slf4j.LoggerFactory; final class ModifierImpl implements ModelActionBuilder { private static final Logger LOG = LoggerFactory.getLogger(ModifierImpl.class); private final Set> unsatisfied = new HashSet<>(1); private final Set> mutations = new HashSet<>(1); private InferenceAction action; private boolean actionApplied = false; private AbstractPrerequisite addReq(final AbstractPrerequisite prereq) { LOG.trace("Modifier {} adding prerequisite {}", this, prereq); unsatisfied.add(prereq); return prereq; } private AbstractPrerequisite addMutation(final AbstractPrerequisite mutation) { LOG.trace("Modifier {} adding mutation {}", this, mutation); mutations.add(mutation); return mutation; } private void checkNotRegistered() { Preconditions.checkState(action == null, "Action was already registered."); } private static IllegalStateException shouldNotHappenProbablyBug(final SourceException e) { return new IllegalStateException("Source exception during registering prerequisite. This is probably bug.", e); } private boolean removeSatisfied() { final Iterator> it = unsatisfied.iterator(); while (it.hasNext()) { final AbstractPrerequisite prereq = it.next(); if (prereq.isDone()) { // We are removing current prerequisite from list. LOG.trace("Modifier {} prerequisite {} satisfied", this, prereq); it.remove(); } } return unsatisfied.isEmpty(); } boolean isApplied() { return actionApplied; } void failModifier() { removeSatisfied(); action.prerequisiteFailed(unsatisfied); action = null; } private void applyAction() { Preconditions.checkState(!actionApplied); action.apply(); actionApplied = true; } @SuppressWarnings({ "unchecked", "rawtypes" }) private , N extends StatementNamespace> AbstractPrerequisite requiresCtxImpl(final StmtContext context, final Class namespace, final K key, final ModelProcessingPhase phase) { checkNotRegistered(); try { AddedToNamespace addedToNs = new AddedToNamespace<>(phase); addReq(addedToNs); contextImpl(context).onNamespaceItemAddedAction((Class) namespace, key, addedToNs); return addedToNs; } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } private > AbstractPrerequisite requiresCtxImpl(final C context, final ModelProcessingPhase phase) { checkNotRegistered(); try { PhaseFinished phaseFin = new PhaseFinished<>(); addReq(phaseFin); contextImpl(context).addPhaseCompletedListener(phase, phaseFin); return phaseFin; } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } @SuppressWarnings({ "rawtypes", "unchecked" }) private , N extends IdentifierNamespace>> AbstractPrerequisite mutatesCtxImpl(final StmtContext context, final Class namespace, final K key, final ModelProcessingPhase phase) { checkNotRegistered(); try { PhaseModificationInNamespace mod = new PhaseModificationInNamespace<>(phase); addReq(mod); addMutation(mod); contextImpl(context).onNamespaceItemAddedAction((Class) namespace, key, mod); return mod; } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } private static StatementContextBase contextImpl(final Object value) { Preconditions.checkArgument(value instanceof StatementContextBase,"Supplied context was not provided by this reactor."); return StatementContextBase.class.cast(value); } boolean tryApply() { Preconditions.checkState(action != null, "Action was not defined yet."); if (removeSatisfied()) { applyAction(); return true; } return false; } @Nonnull @Override public , CT extends C> Prerequisite mutatesCtx(final CT context, final ModelProcessingPhase phase) { try { return addMutation(new PhaseMutation<>(contextImpl(context), phase)); } catch (InferenceException e) { throw shouldNotHappenProbablyBug(e); } } @Nonnull @Override public ,E extends EffectiveStatement> AbstractPrerequisite> requiresCtx(final StmtContext context, final ModelProcessingPhase phase) { return requiresCtxImpl(context, phase); } @Nonnull @Override public > Prerequisite> requiresCtx(final StmtContext context, final Class namespace, final K key, final ModelProcessingPhase phase) { return requiresCtxImpl(context, namespace, key, phase); } @Nonnull @Override public > Prerequisite requiresDeclared(final StmtContext context) { return requiresCtxImpl(context, FULL_DECLARATION).transform(StmtContext::buildDeclared); } @Nonnull @Override public , N extends StatementNamespace> AbstractPrerequisite> requiresDeclaredCtx( final StmtContext context, final Class namespace, final K key) { return requiresCtxImpl(context, namespace, key, FULL_DECLARATION); } @Nonnull @Override public , N extends StatementNamespace> Prerequisite requiresDeclared( final StmtContext context, final Class namespace, final K key) { final AbstractPrerequisite> rawContext = requiresCtxImpl(context, namespace, key, FULL_DECLARATION); return rawContext.transform(StmtContext::buildDeclared); } @Nonnull @Override public > Prerequisite requiresEffective(final StmtContext stmt) { return requiresCtxImpl(stmt, EFFECTIVE_MODEL).transform(StmtContext::buildEffective); } @Nonnull @Override public , N extends StatementNamespace> AbstractPrerequisite> requiresEffectiveCtx( final StmtContext context, final Class namespace, final K key) { return requiresCtxImpl(contextImpl(context), namespace, key, EFFECTIVE_MODEL); } @Nonnull @Override public , N extends StatementNamespace> Prerequisite requiresEffective( final StmtContext context, final Class namespace, final K key) { final AbstractPrerequisite> rawContext = requiresCtxImpl(context, namespace, key, EFFECTIVE_MODEL); return rawContext.transform(StmtContext::buildEffective); } @Nonnull @Override public > Prerequisite> mutatesNs(final Mutable context, final Class namespace) { try { return addMutation(new NamespaceMutation<>(contextImpl(context), namespace)); } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } @Nonnull @Override public , N extends IdentifierNamespace>> AbstractPrerequisite> mutatesEffectiveCtx( final StmtContext context, final Class namespace, final K key) { return mutatesCtxImpl(context, namespace, key, EFFECTIVE_MODEL); } @Override public void apply(final InferenceAction action) { Preconditions.checkState(this.action == null, "Action already defined to %s", this.action); this.action = Preconditions.checkNotNull(action); } private abstract class AbstractPrerequisite implements Prerequisite { private boolean done = false; private T value; @Override public final T get() { Preconditions.checkState(isDone()); return value; } final boolean isDone() { return done; } final boolean resolvePrereq(final T value) { this.value = value; this.done = true; return isApplied(); } final Prerequisite transform(final Function transformation) { return () -> transformation.apply(get()); } @Override public final String toString() { return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString(); } ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { return toStringHelper.add("value", value); } } private class PhaseMutation extends AbstractPrerequisite implements ContextMutation { @SuppressWarnings("unchecked") public PhaseMutation(final StatementContextBase context, final ModelProcessingPhase phase) { context.addMutation(phase, this); resolvePrereq((C) context); } @Override public boolean isFinished() { return isApplied(); } } private class PhaseFinished> extends AbstractPrerequisite implements OnPhaseFinished { @SuppressWarnings("unchecked") @Override public boolean phaseFinished(final StatementContextBase context, final ModelProcessingPhase phase) { return resolvePrereq((C) context); } } private class NamespaceMutation> extends AbstractPrerequisite> { public NamespaceMutation(final StatementContextBase ctx, final Class namespace) { resolvePrereq(ctx); } } private class AddedToNamespace> extends AbstractPrerequisite implements OnNamespaceItemAdded,OnPhaseFinished { private final ModelProcessingPhase phase; public > AddedToNamespace(final ModelProcessingPhase phase) { this.phase = phase; } @Override public void namespaceItemAdded(final StatementContextBase context, final Class namespace, final Object key, final Object value) { StatementContextBase targetContext = (StatementContextBase) value; targetContext.addPhaseCompletedListener(phase, this); } @SuppressWarnings("unchecked") @Override public boolean phaseFinished(final StatementContextBase context, final ModelProcessingPhase phase) { return resolvePrereq((C) context); } @Override ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { return super.addToStringAttributes(toStringHelper).add("phase", phase); } } private class PhaseModificationInNamespace> extends AbstractPrerequisite implements OnNamespaceItemAdded, ContextMutation { private final ModelProcessingPhase modPhase; public > PhaseModificationInNamespace(final ModelProcessingPhase phase) { Preconditions.checkArgument(phase != null, "Model processing phase must not be null"); this.modPhase = phase; } @SuppressWarnings("unchecked") @Override public void namespaceItemAdded(final StatementContextBase context, final Class namespace, final Object key, final Object value) { StatementContextBase targetCtx = contextImpl(value); targetCtx.addMutation(modPhase, this); resolvePrereq((C) targetCtx); } @Override public boolean isFinished() { return isApplied(); } } }