X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FModifierImpl.java;h=e917f958ef65a026d840982fc7866999e79e551d;hb=3eb67390c9fc1826e5db4f97e8bc86f951cd26c0;hp=638952cf3dc44241d0479ede263f6dc16569e693;hpb=f1c55073f8d7631007c01a401346cc83383e5745;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ModifierImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ModifierImpl.java index 638952cf3d..e917f958ef 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ModifierImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ModifierImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * 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, @@ -10,11 +10,14 @@ 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.Function; +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; @@ -24,108 +27,97 @@ 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.meta.StmtContextUtils; 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; -class ModifierImpl implements ModelActionBuilder { +final class ModifierImpl implements ModelActionBuilder { + private static final Logger LOG = LoggerFactory.getLogger(ModifierImpl.class); - private final ModelProcessingPhase phase; - private final Set> unsatisfied = new HashSet<>(); - private final Set> mutations = new HashSet<>(); + private final InferenceContext ctx = new InferenceContext() { }; + + private final Set> unsatisfied = new HashSet<>(1); + private final Set> mutations = new HashSet<>(1); private InferenceAction action; private boolean actionApplied = false; - ModifierImpl(ModelProcessingPhase phase) { - this.phase = Preconditions.checkNotNull(phase); - } - - private AbstractPrerequisite addReq(AbstractPrerequisite prereq) { + private AbstractPrerequisite addReq(final AbstractPrerequisite prereq) { + LOG.trace("Modifier {} adding prerequisite {}", this, prereq); unsatisfied.add(prereq); return prereq; } - private AbstractPrerequisite addMutation(AbstractPrerequisite mutation) { + 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 IllegalStateException shouldNotHappenProbablyBug(SourceException e) { - return new IllegalStateException("Source exception during registering prerequisite. This is probably bug.",e); - } - - private void tryToResolve() throws InferenceException { - if(action == null) { - return; // Action was not yet defined - } - if(removeSatisfied()) { - applyAction(); - } + private static IllegalStateException shouldNotHappenProbablyBug(final SourceException e) { + return new IllegalStateException("Source exception during registering prerequisite. This is probably bug.", e); } private boolean removeSatisfied() { - Iterator> prereq = unsatisfied.iterator(); - boolean allSatisfied = true; - while(prereq.hasNext()) { - if(prereq.next().isDone()) { - prereq.remove(); // We are removing current prerequisite from list. - } else { - allSatisfied = false; + 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 allSatisfied; - } - - ModelProcessingPhase getPhase() { - return phase; + return unsatisfied.isEmpty(); } boolean isApplied() { - return actionApplied; } - void failModifier() throws InferenceException { + void failModifier() { removeSatisfied(); action.prerequisiteFailed(unsatisfied); action = null; } - private void applyAction() throws InferenceException { - - action.apply(); - // Mark all mutations as performed, so context node could move to next. + private void applyAction() { + Preconditions.checkState(!actionApplied); + action.apply(ctx); actionApplied = true; } @SuppressWarnings({ "unchecked", "rawtypes" }) - private , N extends StatementNamespace> AbstractPrerequisite requiresCtxImpl(StmtContext context, Class namespace, K key,ModelProcessingPhase phase) { + 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); + AddedToNamespace addedToNs = new AddedToNamespace<>(phase); addReq(addedToNs); - contextImpl(context).onNamespaceItemAddedAction((Class) namespace,key,addedToNs); + contextImpl(context).onNamespaceItemAddedAction((Class) namespace, key, addedToNs); return addedToNs; } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } - private > AbstractPrerequisite requiresCtxImpl(C context, ModelProcessingPhase phase) { - Preconditions.checkState(action == null, "Action was already registered."); + private > AbstractPrerequisite requiresCtxImpl(final C context, + final ModelProcessingPhase phase) { + checkNotRegistered(); + try { - PhaseFinished phaseFin = new PhaseFinished(); + PhaseFinished phaseFin = new PhaseFinished<>(); addReq(phaseFin); - contextImpl(context).addPhaseCompletedListener(phase,phaseFin); + contextImpl(context).addPhaseCompletedListener(phase, phaseFin); return phaseFin; } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); @@ -133,156 +125,166 @@ class ModifierImpl implements ModelActionBuilder { } @SuppressWarnings({ "rawtypes", "unchecked" }) - private , N extends StatementNamespace> AbstractPrerequisite mutatesCtxImpl( - StmtContext context, Class namespace, K key, ModelProcessingPhase phase) { - try { - PhaseModificationInNamespace mod = new PhaseModificationInNamespace(phase); - addMutation(mod); - contextImpl(context).onNamespaceItemAddedAction((Class) namespace,key,mod); - return mod; - } catch (SourceException e) { - throw shouldNotHappenProbablyBug(e); - } + 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); + } - private static StatementContextBase contextImpl(StmtContext context) { - Preconditions.checkArgument(context instanceof StatementContextBase,"Supplied context was not provided by this reactor."); - return StatementContextBase.class.cast(context); + 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(CT context, ModelProcessingPhase phase) { + public , CT extends C> Prerequisite mutatesCtx(final CT context, final ModelProcessingPhase phase) { try { - return addMutation(new PhaseMutation(contextImpl(context),phase)); + return addMutation(new PhaseMutation<>(contextImpl(context), phase)); } catch (InferenceException e) { throw shouldNotHappenProbablyBug(e); } } + @Nonnull @Override - public ,E extends EffectiveStatement> AbstractPrerequisite> requiresCtx(StmtContext context, ModelProcessingPhase phase) { + public ,E extends EffectiveStatement> AbstractPrerequisite> requiresCtx(final StmtContext context, final ModelProcessingPhase phase) { return requiresCtxImpl(context, phase); } + @Nonnull @Override - public > Prerequisite> requiresCtx(StmtContext context, Class namespace, K key, ModelProcessingPhase phase) { + 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(StmtContext context) { - return requiresCtxImpl(context, FULL_DECLARATION).transform(StmtContextUtils.buildDeclared()); + public > Prerequisite requiresDeclared(final StmtContext context) { + return requiresCtxImpl(context, FULL_DECLARATION).transform(StmtContext::buildDeclared); } + @Nonnull @Override public , N extends StatementNamespace> AbstractPrerequisite> requiresDeclaredCtx( - StmtContext context, Class namespace, K key) { + final StmtContext context, final Class namespace, final K key) { return requiresCtxImpl(context, namespace, key, FULL_DECLARATION); } + @Nonnull @Override public , N extends StatementNamespace> Prerequisite requiresDeclared( - StmtContext context, Class namespace, K key) { - final AbstractPrerequisite> rawContext = requiresCtxImpl(context, namespace, key, FULL_DECLARATION); - return rawContext.transform(StmtContextUtils.buildDeclared()); + 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(StmtContext stmt) { - return requiresCtxImpl(stmt, EFFECTIVE_MODEL).transform(StmtContextUtils.buildEffective()); + public > Prerequisite requiresEffective(final StmtContext stmt) { + return requiresCtxImpl(stmt, EFFECTIVE_MODEL).transform(StmtContext::buildEffective); } + @Nonnull @Override public , N extends StatementNamespace> AbstractPrerequisite> requiresEffectiveCtx( - StmtContext context, Class namespace, K key) { - return requiresCtxImpl(contextImpl(context),namespace,key, EFFECTIVE_MODEL); + 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( - StmtContext context, Class namespace, K key) { + final StmtContext context, final Class namespace, final K key) { final AbstractPrerequisite> rawContext = requiresCtxImpl(context, namespace, key, EFFECTIVE_MODEL); - return rawContext.transform(StmtContextUtils.buildEffective()); + return rawContext.transform(StmtContext::buildEffective); } + @Nonnull @Override - public > Prerequisite> mutatesNs(Mutable context, - Class namespace) { + public > Prerequisite> mutatesNs(final Mutable context, + final Class namespace) { try { - return addMutation(new NamespaceMutation(contextImpl(context),namespace)); + return addMutation(new NamespaceMutation<>(contextImpl(context), namespace)); } catch (SourceException e) { throw shouldNotHappenProbablyBug(e); } } + @Nonnull @Override - public > Prerequisite mutatesEffectiveCtx(T stmt) { - return mutatesCtx(stmt, EFFECTIVE_MODEL); - } - - - @Override - public , N extends StatementNamespace> AbstractPrerequisite> mutatesEffectiveCtx( - StmtContext context, Class namespace, K key) { + 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(InferenceAction action) throws InferenceException { + public void apply(final InferenceAction action) { + Preconditions.checkState(this.action == null, "Action already defined to %s", this.action); this.action = Preconditions.checkNotNull(action); - tryToResolve(); } private abstract class AbstractPrerequisite implements Prerequisite { - private T value; private boolean done = false; + private T value; @Override - public T get() { - Preconditions.checkState(isDone()); + public final T resolve(final InferenceContext ctx) { + Preconditions.checkState(done); + Preconditions.checkArgument(ctx == ModifierImpl.this.ctx); return value; } - @Override - public boolean isDone() { + final boolean isDone() { return done; } - protected void resolvePrereq(T value) throws InferenceException { - Preconditions.checkState(!isDone()); + final boolean resolvePrereq(final T value) { this.value = value; this.done = true; - tryToResolve(); + return isApplied(); } - protected Prerequisite transform(final Function transformation) { - - return new Prerequisite() { - - @Override - public O get() { - return transformation.apply(AbstractPrerequisite.this.get()); - } - - @Override - public boolean isDone() { - return AbstractPrerequisite.this.isDone(); - } + final Prerequisite transform(final Function transformation) { + return ctx -> transformation.apply(resolve(ctx)); + } - }; + @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(StatementContextBase context, ModelProcessingPhase phase) throws InferenceException { + public PhaseMutation(final StatementContextBase context, final ModelProcessingPhase phase) { context.addMutation(phase, this); resolvePrereq((C) context); } @@ -291,64 +293,63 @@ class ModifierImpl implements ModelActionBuilder { public boolean isFinished() { return isApplied(); } - - } - private class PhaseFinished> extends AbstractPrerequisite implements OnPhaseFinished { + private class PhaseFinished> extends AbstractPrerequisite implements OnPhaseFinished { @SuppressWarnings("unchecked") @Override - public void phaseFinished(StatementContextBase context, ModelProcessingPhase phase) throws SourceException { - resolvePrereq((C) (context)); + public boolean phaseFinished(final StatementContextBase context, final ModelProcessingPhase phase) { + return resolvePrereq((C) context); } } - private class NamespaceMutation> extends AbstractPrerequisite> { - - public NamespaceMutation(StatementContextBase ctx, Class namespace) throws InferenceException { + private class NamespaceMutation> extends AbstractPrerequisite> { + public NamespaceMutation(final StatementContextBase ctx, final Class namespace) { resolvePrereq(ctx); } - } - private class AddedToNamespace> extends AbstractPrerequisite implements OnNamespaceItemAdded,OnPhaseFinished { - + private class AddedToNamespace> extends AbstractPrerequisite implements OnNamespaceItemAdded,OnPhaseFinished { private final ModelProcessingPhase phase; - public > AddedToNamespace(ModelProcessingPhase phase) { + public > AddedToNamespace(final ModelProcessingPhase phase) { this.phase = phase; } @Override - public void namespaceItemAdded(StatementContextBase context, Class namespace, Object key, - Object value) throws SourceException { + 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 void phaseFinished(StatementContextBase context, ModelProcessingPhase phase) throws SourceException { - resolvePrereq((C) context); + 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(ModelProcessingPhase phase) throws SourceException { + 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(StatementContextBase context, Class namespace, Object key, - Object value) throws SourceException { - context.addMutation(modPhase,this); - resolvePrereq((C) context); + 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 @@ -356,5 +357,4 @@ class ModifierImpl implements ModelActionBuilder { return isApplied(); } } - }