Sho SHIMIZU
Committed by Gerrit Code Review

Remove dependency on IntentData and IntentStore from IntentWorker

- Make createInitialPhase() static and move it to IntentProcessPhase
  - Additionally, rename to newInitialPhase()
- IntentWorker's constructor receives an initial intent process phase

Change-Id: I4774493230a008fc5530d9d90b262e1eb441f946
......@@ -42,6 +42,7 @@ import org.onosproject.net.intent.IntentStore;
import org.onosproject.net.intent.IntentStoreDelegate;
import org.onosproject.net.intent.Key;
import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
import org.onosproject.net.intent.impl.phase.IntentWorker;
import org.slf4j.Logger;
......@@ -61,6 +62,7 @@ import static org.onlab.util.Tools.groupedThreads;
import static org.onosproject.net.intent.IntentState.FAILED;
import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ;
import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -278,7 +280,8 @@ public class IntentManager
private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) {
IntentData current = store.getIntentData(data.key());
return workerExecutor.submit(new IntentWorker(processor, data, current));
IntentProcessPhase initial = newInitialPhase(processor, data, current);
return workerExecutor.submit(new IntentWorker(initial));
}
private class IntentBatchPreprocess implements Runnable {
......
......@@ -15,8 +15,14 @@
*/
package org.onosproject.net.intent.impl.phase;
import org.onosproject.net.intent.IntentData;
import org.onosproject.net.intent.impl.IntentProcessor;
import java.util.Optional;
import static org.onlab.util.Tools.isNullOrEmpty;
import static org.onosproject.net.intent.IntentState.WITHDRAWN;
/**
* Represents a phase of processing an intent.
*/
......@@ -29,4 +35,31 @@ public interface IntentProcessPhase {
* @return next update
*/
Optional<IntentProcessPhase> execute();
/**
* Create a starting intent process phase according to intent data this class holds.
*
* @param processor intent processor to be passed to intent process phases
* generated while this instance is working
* @param data intent data to be processed
* @param current intent date that is stored in the store
* @return starting intent process phase
*/
static IntentProcessPhase newInitialPhase(IntentProcessor processor,
IntentData data, IntentData current) {
switch (data.state()) {
case INSTALL_REQ:
return new InstallRequest(processor, data, Optional.ofNullable(current));
case WITHDRAW_REQ:
if (current == null || isNullOrEmpty(current.installables())) {
return new Withdrawn(data, WITHDRAWN);
} else {
return new WithdrawRequest(processor, data, current);
}
default:
// illegal state
return new CompilingFailed(data);
}
}
}
......
......@@ -15,42 +15,31 @@
*/
package org.onosproject.net.intent.impl.phase;
import org.onosproject.net.intent.IntentData;
import org.onosproject.net.intent.impl.IntentProcessor;
import java.util.Optional;
import java.util.concurrent.Callable;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.isNullOrEmpty;
import static org.onosproject.net.intent.IntentState.WITHDRAWN;
/**
* Worker to process a submitted intent. {@link #call()} method generates
*/
public final class IntentWorker implements Callable<FinalIntentProcessPhase> {
private final IntentProcessor processor;
private final IntentData data;
private final IntentData current;
private final IntentProcessPhase initial;
/**
* Create an instance with the specified arguments.
*
* @param processor intent processor to be passed to intent process phases
* generated while this instance is working
* @param data intent data to be processed
* @param current intent date that is stored in the store
* @param initial initial intent process phase
*/
public IntentWorker(IntentProcessor processor, IntentData data, IntentData current) {
this.processor = checkNotNull(processor);
this.data = checkNotNull(data);
this.current = current;
public IntentWorker(IntentProcessPhase initial) {
this.initial = checkNotNull(initial);
}
@Override
public FinalIntentProcessPhase call() throws Exception {
IntentProcessPhase update = createInitialPhase();
IntentProcessPhase update = initial;
Optional<IntentProcessPhase> currentPhase = Optional.of(update);
IntentProcessPhase previousPhase = update;
......@@ -60,25 +49,4 @@ public final class IntentWorker implements Callable<FinalIntentProcessPhase> {
}
return (FinalIntentProcessPhase) previousPhase;
}
/**
* Create a starting intent process phase according to intent data this class holds.
*
* @return starting intent process phase
*/
private IntentProcessPhase createInitialPhase() {
switch (data.state()) {
case INSTALL_REQ:
return new InstallRequest(processor, data, Optional.ofNullable(current));
case WITHDRAW_REQ:
if (current == null || isNullOrEmpty(current.installables())) {
return new Withdrawn(data, WITHDRAWN);
} else {
return new WithdrawRequest(processor, data, current);
}
default:
// illegal state
return new CompilingFailed(data);
}
}
}
......