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; ...@@ -42,6 +42,7 @@ import org.onosproject.net.intent.IntentStore;
42 import org.onosproject.net.intent.IntentStoreDelegate; 42 import org.onosproject.net.intent.IntentStoreDelegate;
43 import org.onosproject.net.intent.Key; 43 import org.onosproject.net.intent.Key;
44 import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; 44 import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
45 +import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
45 import org.onosproject.net.intent.impl.phase.IntentWorker; 46 import org.onosproject.net.intent.impl.phase.IntentWorker;
46 import org.slf4j.Logger; 47 import org.slf4j.Logger;
47 48
...@@ -61,6 +62,7 @@ import static org.onlab.util.Tools.groupedThreads; ...@@ -61,6 +62,7 @@ import static org.onlab.util.Tools.groupedThreads;
61 import static org.onosproject.net.intent.IntentState.FAILED; 62 import static org.onosproject.net.intent.IntentState.FAILED;
62 import static org.onosproject.net.intent.IntentState.INSTALL_REQ; 63 import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
63 import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; 64 import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ;
65 +import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase;
64 import static org.slf4j.LoggerFactory.getLogger; 66 import static org.slf4j.LoggerFactory.getLogger;
65 67
66 /** 68 /**
...@@ -278,7 +280,8 @@ public class IntentManager ...@@ -278,7 +280,8 @@ public class IntentManager
278 280
279 private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { 281 private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) {
280 IntentData current = store.getIntentData(data.key()); 282 IntentData current = store.getIntentData(data.key());
281 - return workerExecutor.submit(new IntentWorker(processor, data, current)); 283 + IntentProcessPhase initial = newInitialPhase(processor, data, current);
284 + return workerExecutor.submit(new IntentWorker(initial));
282 } 285 }
283 286
284 private class IntentBatchPreprocess implements Runnable { 287 private class IntentBatchPreprocess implements Runnable {
......
...@@ -15,8 +15,14 @@ ...@@ -15,8 +15,14 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl.phase; 16 package org.onosproject.net.intent.impl.phase;
17 17
18 +import org.onosproject.net.intent.IntentData;
19 +import org.onosproject.net.intent.impl.IntentProcessor;
20 +
18 import java.util.Optional; 21 import java.util.Optional;
19 22
23 +import static org.onlab.util.Tools.isNullOrEmpty;
24 +import static org.onosproject.net.intent.IntentState.WITHDRAWN;
25 +
20 /** 26 /**
21 * Represents a phase of processing an intent. 27 * Represents a phase of processing an intent.
22 */ 28 */
...@@ -29,4 +35,31 @@ public interface IntentProcessPhase { ...@@ -29,4 +35,31 @@ public interface IntentProcessPhase {
29 * @return next update 35 * @return next update
30 */ 36 */
31 Optional<IntentProcessPhase> execute(); 37 Optional<IntentProcessPhase> execute();
38 +
39 + /**
40 + * Create a starting intent process phase according to intent data this class holds.
41 + *
42 + * @param processor intent processor to be passed to intent process phases
43 + * generated while this instance is working
44 + * @param data intent data to be processed
45 + * @param current intent date that is stored in the store
46 + * @return starting intent process phase
47 + */
48 + static IntentProcessPhase newInitialPhase(IntentProcessor processor,
49 + IntentData data, IntentData current) {
50 + switch (data.state()) {
51 + case INSTALL_REQ:
52 + return new InstallRequest(processor, data, Optional.ofNullable(current));
53 + case WITHDRAW_REQ:
54 + if (current == null || isNullOrEmpty(current.installables())) {
55 + return new Withdrawn(data, WITHDRAWN);
56 + } else {
57 + return new WithdrawRequest(processor, data, current);
58 + }
59 + default:
60 + // illegal state
61 + return new CompilingFailed(data);
62 + }
63 + }
64 +
32 } 65 }
......
...@@ -15,42 +15,31 @@ ...@@ -15,42 +15,31 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl.phase; 16 package org.onosproject.net.intent.impl.phase;
17 17
18 -import org.onosproject.net.intent.IntentData;
19 -import org.onosproject.net.intent.impl.IntentProcessor;
20 18
21 import java.util.Optional; 19 import java.util.Optional;
22 import java.util.concurrent.Callable; 20 import java.util.concurrent.Callable;
23 21
24 import static com.google.common.base.Preconditions.checkNotNull; 22 import static com.google.common.base.Preconditions.checkNotNull;
25 -import static org.onlab.util.Tools.isNullOrEmpty;
26 -import static org.onosproject.net.intent.IntentState.WITHDRAWN;
27 23
28 /** 24 /**
29 * Worker to process a submitted intent. {@link #call()} method generates 25 * Worker to process a submitted intent. {@link #call()} method generates
30 */ 26 */
31 public final class IntentWorker implements Callable<FinalIntentProcessPhase> { 27 public final class IntentWorker implements Callable<FinalIntentProcessPhase> {
32 28
33 - private final IntentProcessor processor; 29 + private final IntentProcessPhase initial;
34 - private final IntentData data;
35 - private final IntentData current;
36 30
37 /** 31 /**
38 * Create an instance with the specified arguments. 32 * Create an instance with the specified arguments.
39 * 33 *
40 - * @param processor intent processor to be passed to intent process phases 34 + * @param initial initial intent process phase
41 - * generated while this instance is working
42 - * @param data intent data to be processed
43 - * @param current intent date that is stored in the store
44 */ 35 */
45 - public IntentWorker(IntentProcessor processor, IntentData data, IntentData current) { 36 + public IntentWorker(IntentProcessPhase initial) {
46 - this.processor = checkNotNull(processor); 37 + this.initial = checkNotNull(initial);
47 - this.data = checkNotNull(data);
48 - this.current = current;
49 } 38 }
50 39
51 @Override 40 @Override
52 public FinalIntentProcessPhase call() throws Exception { 41 public FinalIntentProcessPhase call() throws Exception {
53 - IntentProcessPhase update = createInitialPhase(); 42 + IntentProcessPhase update = initial;
54 Optional<IntentProcessPhase> currentPhase = Optional.of(update); 43 Optional<IntentProcessPhase> currentPhase = Optional.of(update);
55 IntentProcessPhase previousPhase = update; 44 IntentProcessPhase previousPhase = update;
56 45
...@@ -60,25 +49,4 @@ public final class IntentWorker implements Callable<FinalIntentProcessPhase> { ...@@ -60,25 +49,4 @@ public final class IntentWorker implements Callable<FinalIntentProcessPhase> {
60 } 49 }
61 return (FinalIntentProcessPhase) previousPhase; 50 return (FinalIntentProcessPhase) previousPhase;
62 } 51 }
63 -
64 - /**
65 - * Create a starting intent process phase according to intent data this class holds.
66 - *
67 - * @return starting intent process phase
68 - */
69 - private IntentProcessPhase createInitialPhase() {
70 - switch (data.state()) {
71 - case INSTALL_REQ:
72 - return new InstallRequest(processor, data, Optional.ofNullable(current));
73 - case WITHDRAW_REQ:
74 - if (current == null || isNullOrEmpty(current.installables())) {
75 - return new Withdrawn(data, WITHDRAWN);
76 - } else {
77 - return new WithdrawRequest(processor, data, current);
78 - }
79 - default:
80 - // illegal state
81 - return new CompilingFailed(data);
82 - }
83 - }
84 } 52 }
......