Started refactoring Intent Manager

Introduced IntentData and reworked APIs

Change-Id: I1fa437ceb1b72c4017ac2da1573bfbeb64c0632a
diff --git a/core/api/src/main/java/org/onosproject/net/intent/Intent.java b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
index 19170ae..1146013 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/Intent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
@@ -36,7 +36,7 @@
     private final IntentId id;
 
     private final ApplicationId appId;
-    private final String key;
+    private final String key; // TODO make this a class
 
     private final Collection<NetworkResource> resources;
 
@@ -156,4 +156,8 @@
             idGenerator = null;
         }
     }
+
+    public String key() {
+        return key;
+    }
 }
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentData.java b/core/api/src/main/java/org/onosproject/net/intent/IntentData.java
new file mode 100644
index 0000000..0f2bd84
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentData.java
@@ -0,0 +1,88 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.store.Timestamp;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * A wrapper class that contains an intents, its state, and other metadata for
+ * internal use.
+ */
+public class IntentData { //FIXME need to make this "immutable"
+                          // manager should be able to mutate a local copy while processing
+    private Intent intent;
+
+    private IntentState state;
+    private Timestamp version;
+
+    private List<Intent> installables;
+
+    public IntentData(Intent intent, IntentState state, Timestamp version) {
+        this.intent = intent;
+        this.state = state;
+        this.version = version;
+    }
+
+    // kryo constructor
+    protected IntentData() {
+    }
+
+    public Intent intent() {
+        return intent;
+    }
+
+    public IntentState state() {
+        return state;
+    }
+
+    public String key() {
+        return intent.key();
+    }
+
+    public void setState(IntentState newState) {
+        this.state = newState;
+    }
+
+    public void setInstallables(List<Intent> installables) {
+        this.installables = ImmutableList.copyOf(installables);
+    }
+
+    public List<Intent> installables() {
+        return installables;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(intent, version);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final IntentData other = (IntentData) obj;
+        return Objects.equals(this.intent, other.intent)
+                && Objects.equals(this.version, other.version);
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentOperation.java b/core/api/src/main/java/org/onosproject/net/intent/IntentOperation.java
index afb0010..c25c28f 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentOperation.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentOperation.java
@@ -27,9 +27,7 @@
 public final class IntentOperation {
 
     private final Type type;
-    private final IntentId intentId;
     private final Intent intent;
-    //FIXME consider pulling the key out (we will hash based on key)
 
     /**
      * Operation type.
@@ -62,12 +60,10 @@
      * Creates an intent operation.
      *
      * @param type     operation type
-     * @param intentId identifier of the intent subject to the operation
      * @param intent   intent subject
      */
-    IntentOperation(Type type, IntentId intentId, Intent intent) {
+    public IntentOperation(Type type, Intent intent) {
         this.type = checkNotNull(type);
-        this.intentId = checkNotNull(intentId);
         this.intent = intent;
     }
 
@@ -86,7 +82,11 @@
      * @return intent identifier
      */
     public IntentId intentId() {
-        return intentId;
+        return intent.id();
+    }
+
+    public String key() {
+        return intent.key();
     }
 
     /**
@@ -101,7 +101,7 @@
 
     @Override
     public int hashCode() {
-        return Objects.hash(type, intentId, intent);
+        return Objects.hash(type, intent);
     }
 
     @Override
@@ -114,7 +114,6 @@
         }
         final IntentOperation other = (IntentOperation) obj;
         return Objects.equals(this.type, other.type) &&
-                Objects.equals(this.intentId, other.intentId) &&
                 Objects.equals(this.intent, other.intent);
     }
 
@@ -123,7 +122,6 @@
     public String toString() {
         return toStringHelper(this)
                 .add("type", type)
-                .add("intentId", intentId)
                 .add("intent", intent)
                 .toString();
     }
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentOperations.java b/core/api/src/main/java/org/onosproject/net/intent/IntentOperations.java
index 2f30fa4..d1d4c3d 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentOperations.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentOperations.java
@@ -31,7 +31,7 @@
 /**
  * Batch of intent submit/withdraw/replace operations.
  */
-@Deprecated
+@Deprecated //DELETEME
 public final class IntentOperations {
 
     private final List<IntentOperation> operations;
@@ -120,7 +120,7 @@
          */
         public Builder addSubmitOperation(Intent intent) {
             checkNotNull(intent, "Intent cannot be null");
-            builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
+            builder.add(new IntentOperation(SUBMIT, intent));
             return this;
         }
 
@@ -134,7 +134,7 @@
         public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
             checkNotNull(oldIntentId, "Intent ID cannot be null");
             checkNotNull(newIntent, "Intent cannot be null");
-            builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
+            builder.add(new IntentOperation(REPLACE, newIntent)); //FIXME
             return this;
         }
 
@@ -146,7 +146,7 @@
          */
         public Builder addWithdrawOperation(IntentId intentId) {
             checkNotNull(intentId, "Intent ID cannot be null");
-            builder.add(new IntentOperation(WITHDRAW, intentId, null));
+            builder.add(new IntentOperation(WITHDRAW, null)); //FIXME
             return this;
         }
 
@@ -158,7 +158,7 @@
          */
         public Builder addUpdateOperation(IntentId intentId) {
             checkNotNull(intentId, "Intent ID cannot be null");
-            builder.add(new IntentOperation(UPDATE, intentId, null));
+            builder.add(new IntentOperation(UPDATE, null)); //FIXME
             return this;
         }
 
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentState.java b/core/api/src/main/java/org/onosproject/net/intent/IntentState.java
index e4d7423..73b337c 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentState.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentState.java
@@ -30,7 +30,7 @@
      * Intents will also pass through this state when they are updated.
      * </p>
      */
-    INSTALL_REQ,
+    INSTALL_REQ, // TODO submit_REQ?
 
     /**
      * Signifies that the intent is being compiled into installable intents.
@@ -66,7 +66,7 @@
      * previously failed to be installed.
      * </p>
      */
-    RECOMPILING,
+    RECOMPILING, // TODO perhaps repurpose as BROKEN.
 
     /**
      * Indicates that an application has requested that an intent be withdrawn.
@@ -92,5 +92,5 @@
      * Signifies that the intent has failed compiling, installing or
      * recompiling states.
      */
-    FAILED
+    FAILED //TODO consider renaming to UNSAT.
 }
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentStore.java b/core/api/src/main/java/org/onosproject/net/intent/IntentStore.java
index 534279b..e951389 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentStore.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentStore.java
@@ -76,9 +76,9 @@
     /**
      * Adds a new operation, which should be persisted and delegated.
      *
-     * @param op operation
+     * @param intent operation
      */
-    default void add(IntentOperation op) {} //FIXME remove when impl.
+    default void addPending(IntentData intent) {} //FIXME remove when impl.
 
     /**
      * Checks to see whether the calling instance is the master for processing
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentStoreDelegate.java b/core/api/src/main/java/org/onosproject/net/intent/IntentStoreDelegate.java
index ab56c99..6d0f87e 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentStoreDelegate.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentStoreDelegate.java
@@ -23,10 +23,10 @@
 public interface IntentStoreDelegate extends StoreDelegate<IntentEvent> {
 
     /**
-     * Provides an intent operation that should be processed (compiled and
+     * Provides an intent data object that should be processed (compiled and
      * installed) by this manager.
      *
-     * @param op    intent operation
+     * @param intentData    intent data object
      */
-    void process(IntentOperation op);
+    void process(IntentData intentData);
 }