X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FModifierImpl.java;h=e2778d90bcc9a6078aa9b1e1193f303d2dee0146;hb=936f49c81f8e69826ef2c5a5923dfe697853675f;hp=225ce471106174bf30983c23c8f57e702bc9d684;hpb=ed4fee64bc9bdb4005d11aa35355e9367065998f;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 225ce47110..e2778d90bc 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, @@ -11,6 +11,8 @@ import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPha 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; @@ -24,62 +26,56 @@ 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 { + 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 ModelProcessingPhase phase; private InferenceAction action; private boolean actionApplied = false; - ModifierImpl(ModelProcessingPhase phase) { + ModifierImpl(final 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) { - // Action was not yet defined - return; - } - 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(); + Iterator> it = unsatisfied.iterator(); boolean allSatisfied = true; - while(prereq.hasNext()) { - if(prereq.next().isDone()) { + while (it.hasNext()) { + final AbstractPrerequisite prereq = it.next(); + if (prereq.isDone()) { // We are removing current prerequisite from list. - prereq.remove(); + LOG.trace("Modifier {} prerequisite {} satisfied", this, prereq); + it.remove(); } else { allSatisfied = false; } @@ -92,28 +88,26 @@ class ModifierImpl implements ModelActionBuilder { } boolean isApplied() { - return actionApplied; } - void failModifier() throws InferenceException { + void failModifier() { removeSatisfied(); action.prerequisiteFailed(unsatisfied); action = null; } - private void applyAction() throws InferenceException { - + private void applyAction() { + Preconditions.checkState(!actionApplied); action.apply(); - // Mark all mutations as performed, so context node could move to next. 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); return addedToNs; @@ -122,10 +116,10 @@ class ModifierImpl implements ModelActionBuilder { } } - private > AbstractPrerequisite requiresCtxImpl(C context, ModelProcessingPhase phase) { + private > AbstractPrerequisite requiresCtxImpl(final C context, final ModelProcessingPhase phase) { Preconditions.checkState(action == null, "Action was already registered."); try { - PhaseFinished phaseFin = new PhaseFinished(); + PhaseFinished phaseFin = new PhaseFinished<>(); addReq(phaseFin); contextImpl(context).addPhaseCompletedListener(phase,phaseFin); return phaseFin; @@ -135,10 +129,11 @@ class ModifierImpl implements ModelActionBuilder { } @SuppressWarnings({ "rawtypes", "unchecked" }) - private , N extends StatementNamespace> AbstractPrerequisite mutatesCtxImpl( - StmtContext context, Class namespace, K key, ModelProcessingPhase phase) { + private , N extends IdentifierNamespace>> AbstractPrerequisite mutatesCtxImpl( + final StmtContext context, final Class namespace, final K key, final ModelProcessingPhase phase) { try { - PhaseModificationInNamespace mod = new PhaseModificationInNamespace(phase); + PhaseModificationInNamespace mod = new PhaseModificationInNamespace<>(phase); + addReq(mod); addMutation(mod); contextImpl(context).onNamespaceItemAddedAction((Class) namespace,key,mod); return mod; @@ -147,125 +142,129 @@ class ModifierImpl implements ModelActionBuilder { } } - private static StatementContextBase contextImpl(StmtContext context) { - Preconditions.checkArgument(context instanceof StatementContextBase,"Supplied context was not provided by this reactor."); - return StatementContextBase.class.cast(context); + 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; } @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); } } @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); } @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); } @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); } @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); } @Override public , N extends StatementNamespace> Prerequisite requiresDeclared( - StmtContext context, Class namespace, K key) { + final StmtContext context, final Class namespace, final K key) { final AbstractPrerequisite> rawContext = requiresCtxImpl(context, namespace, key, FULL_DECLARATION); - return rawContext.transform(StmtContextUtils.buildDeclared()); + return rawContext.transform(StmtContext::buildDeclared); } @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); } @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); } @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); } @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); } } @Override - public > Prerequisite mutatesEffectiveCtx(T stmt) { + public > Prerequisite mutatesEffectiveCtx(final 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() { + public final T get() { Preconditions.checkState(isDone()); return value; } @Override - public boolean isDone() { + public 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) { - + final Prerequisite transform(final Function transformation) { return new Prerequisite() { - @Override public O get() { return transformation.apply(AbstractPrerequisite.this.get()); @@ -275,16 +274,22 @@ class ModifierImpl implements ModelActionBuilder { public boolean isDone() { return AbstractPrerequisite.this.isDone(); } - }; } + @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); } @@ -293,64 +298,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 { + public NamespaceMutation(final StatementContextBase ctx, final Class namespace) { resolvePrereq(ctx); } - } 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 @@ -358,5 +362,4 @@ class ModifierImpl implements ModelActionBuilder { return isApplied(); } } - }