Pull out IntentWorker from IntentManager

- IntentWorker is placed in phase package
- Make public IntentProcessPhase subclasses package private now

Change-Id: Ie7d218fe5d8a516f3913ff8881d9d17cfd8e4c46
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/InstallRequest.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/InstallRequest.java
index 90e0fd2..1944aad 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/InstallRequest.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/InstallRequest.java
@@ -25,13 +25,13 @@
 /**
  * Represents a phase where intent installation has been requested.
  */
-public final class InstallRequest implements IntentProcessPhase {
+final class InstallRequest implements IntentProcessPhase {
 
     private final IntentProcessor intentManager;
     private final IntentData pending;
     private final Optional<IntentData> current;
 
-    public InstallRequest(IntentProcessor processor, IntentData intentData, Optional<IntentData> current) {
+    InstallRequest(IntentProcessor processor, IntentData intentData, Optional<IntentData> current) {
         this.intentManager = checkNotNull(processor);
         this.pending = checkNotNull(intentData);
         this.current = checkNotNull(current);
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/IntentWorker.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/IntentWorker.java
new file mode 100644
index 0000000..aa7ef6a
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/IntentWorker.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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;
+
+    /**
+     * 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
+     */
+    public IntentWorker(IntentProcessor processor, IntentData data, IntentData current) {
+        this.processor = checkNotNull(processor);
+        this.data = checkNotNull(data);
+        this.current = current;
+    }
+
+    @Override
+    public FinalIntentProcessPhase call() throws Exception {
+        IntentProcessPhase update = createInitialPhase();
+        Optional<IntentProcessPhase> currentPhase = Optional.of(update);
+        IntentProcessPhase previousPhase = update;
+
+        while (currentPhase.isPresent()) {
+            previousPhase = currentPhase.get();
+            currentPhase = previousPhase.execute();
+        }
+        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);
+        }
+    }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/WithdrawRequest.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/WithdrawRequest.java
index f2e120f..7940cf7 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/WithdrawRequest.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/WithdrawRequest.java
@@ -25,13 +25,13 @@
 /**
  * Represents a phase of requesting a withdraw of an intent.
  */
-public final class WithdrawRequest implements IntentProcessPhase {
+final class WithdrawRequest implements IntentProcessPhase {
 
     private final IntentProcessor processor;
     private final IntentData pending;
     private final IntentData current;
 
-    public WithdrawRequest(IntentProcessor processor, IntentData intentData, IntentData current) {
+    WithdrawRequest(IntentProcessor processor, IntentData intentData, IntentData current) {
         this.processor = checkNotNull(processor);
         this.pending = checkNotNull(intentData);
         this.current = checkNotNull(current);
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Withdrawn.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Withdrawn.java
index 069a989..b8e724b 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Withdrawn.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Withdrawn.java
@@ -24,15 +24,15 @@
 /**
  * Represents a phase where an intent has been withdrawn.
  */
-public final class Withdrawn extends FinalIntentProcessPhase {
+final class Withdrawn extends FinalIntentProcessPhase {
 
     private final IntentData intentData;
 
-    public Withdrawn(IntentData intentData) {
+    Withdrawn(IntentData intentData) {
         this(intentData, WITHDRAWING);
     }
 
-    public Withdrawn(IntentData intentData, IntentState newState) {
+    Withdrawn(IntentData intentData, IntentState newState) {
         this.intentData = checkNotNull(intentData);
         this.intentData.setState(newState);
     }