[ONOS-6348] Intent installer redesign

Change-Id: I9ae2e8158dc1c686eaf848f330566f9dbb78405f
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentExtensionService.java b/core/api/src/main/java/org/onosproject/net/intent/IntentExtensionService.java
index b4d40c9..f36b356 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentExtensionService.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentExtensionService.java
@@ -48,4 +48,37 @@
      * @return the set of compiler bindings
      */
     Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers();
+
+    /**
+     * Registers the specific installer for the given intent class.
+     *
+     * @param cls intent class
+     * @param installer intent installer
+     * @param <T> the type of intent
+     */
+     <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer);
+
+    /**
+     * Unregisters the installer for the specific intent class.
+     *
+     * @param cls intent class
+     * @param <T> the type of intent
+     */
+    <T extends Intent> void unregisterInstaller(Class<T> cls);
+
+    /**
+     * Returns immutable set of binding of currently registered intent installers.
+     *
+     * @return the set of installer bindings
+     */
+    Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers();
+
+    /**
+     * Returns the installer for specific installable intent.
+     *
+     * @param cls the type of intent
+     * @param <T> the type of intent
+     * @return the installer for specific installable intent
+     */
+    <T extends Intent> IntentInstaller<T> getInstaller(Class<T> cls);
 }
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentInstallCoordinator.java b/core/api/src/main/java/org/onosproject/net/intent/IntentInstallCoordinator.java
new file mode 100644
index 0000000..6ba0935
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentInstallCoordinator.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2017-present 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;
+
+/**
+ * Contact point for installers to report progress of intent installation and to
+ * update intent-specific status and context appropriately.
+ */
+public interface IntentInstallCoordinator {
+    /**
+     * Handles success state for an Intent operation context.
+     *
+     * @param context the Intent operation context
+     */
+    void intentInstallSuccess(IntentOperationContext context);
+
+    /**
+     * Handles failed state for an Intent operation context.
+     *
+     * @param context the Intent operation context
+     */
+    void intentInstallFailed(IntentOperationContext context);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentInstallationContext.java b/core/api/src/main/java/org/onosproject/net/intent/IntentInstallationContext.java
new file mode 100644
index 0000000..3656c16
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentInstallationContext.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2017-present 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.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Installation context for a high level Intent.
+ * Records pending and error operation contexts of installable Intents for the
+ * high level Intent.
+ */
+public class IntentInstallationContext {
+    private IntentData toUninstall;
+    private IntentData toInstall;
+    private Set<IntentOperationContext> pendingContexts = Sets.newConcurrentHashSet();
+    private Set<IntentOperationContext> errorContexts = Sets.newConcurrentHashSet();
+
+    /**
+     * Creates an Intent installation context by given information.
+     *
+     * @param toUninstall the Intent to uninstall
+     * @param toInstall the Intent to install
+     */
+    public IntentInstallationContext(IntentData toUninstall, IntentData toInstall) {
+        this.toUninstall = toUninstall;
+        this.toInstall = toInstall;
+    }
+
+    /**
+     * Removes a pending operation context.
+     *
+     * @param context the operation context to be added
+     */
+    public void removePendingContext(IntentOperationContext context) {
+        this.pendingContexts.remove(context);
+    }
+
+    /**
+     * Adds a pending context.
+     *
+     * @param context the operation context to be added
+     */
+    public void addPendingContext(IntentOperationContext context) {
+        this.pendingContexts.add(context);
+    }
+
+    /**
+     * Adds an error context.
+     *
+     * @param context the error context to be added.
+     */
+    public void addErrorContext(IntentOperationContext context) {
+        this.errorContexts.add(context);
+    }
+
+    /**
+     * Retrieves the pending contexts.
+     *
+     * @return the pending contexts
+     */
+    public Set<IntentOperationContext> pendingContexts() {
+        return ImmutableSet.copyOf(pendingContexts);
+    }
+
+    /**
+     * Retrieves the error contexts.
+     *
+     * @return the error contexts
+     */
+    public Set<IntentOperationContext> errorContexts() {
+        return ImmutableSet.copyOf(errorContexts);
+    }
+
+    /**
+     * Check if pending context is empty.
+     *
+     * @return true if pending contexts is empty; false otherwise
+     */
+    public synchronized boolean isPendingContextsEmpty() {
+        return pendingContexts.isEmpty();
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("pendingContexts", pendingContexts)
+                .add("errorContexts", errorContexts)
+                .toString();
+    }
+
+    /**
+     * Retrieves the Intent data which to be uninstalled.
+     *
+     * @return the Intent data; empty value if not exists
+     */
+    public Optional<IntentData> toUninstall() {
+        return Optional.ofNullable(toUninstall);
+    }
+
+    /**
+     * Retrieves the Intent data which to be installed.
+     *
+     * @return the Intent data; empty value if not exists
+     */
+    public Optional<IntentData> toInstall() {
+        return Optional.ofNullable(toInstall);
+    }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentInstaller.java b/core/api/src/main/java/org/onosproject/net/intent/IntentInstaller.java
new file mode 100644
index 0000000..ac41865
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentInstaller.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017-present 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;
+
+/**
+ * Manage installation process for specific installable Intents.
+ */
+public interface IntentInstaller<T extends Intent> {
+
+    /**
+     * The installation direction.
+     */
+    enum Direction {
+        /**
+         * Direction for adding any installable objects(flows, configs...).
+         */
+        ADD,
+
+        /**
+         * Direction for removing any installable objects(flows, configs...).
+         */
+        REMOVE
+    }
+
+    /**
+     * Applies an Intent operation context.
+     *
+     * @param context the Intent operation context
+     */
+    void apply(IntentOperationContext<T> context);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentOperationContext.java b/core/api/src/main/java/org/onosproject/net/intent/IntentOperationContext.java
new file mode 100644
index 0000000..e36d7ec
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentOperationContext.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2017-present 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.Lists;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Operation context for installable Intent.
+ *
+ * @param <T> the type of installable Intent
+ */
+public class IntentOperationContext<T extends Intent> {
+    private IntentInstallationContext intentInstallationContext;
+    private List<T> intentsToUninstall;
+    private List<T> intentsToInstall;
+
+    /**
+     * Creates an operation context.
+     *
+     * @param intentsToUninstall the Intents to uninstall
+     * @param intentsToInstall the Intents to install
+     * @param intentInstallationContext the high level Intent installation information
+     */
+    public IntentOperationContext(List<T> intentsToUninstall, List<T> intentsToInstall,
+                                  IntentInstallationContext intentInstallationContext) {
+        this.intentsToUninstall = Lists.newArrayList(intentsToUninstall);
+        this.intentsToInstall = Lists.newArrayList(intentsToInstall);
+        this.intentInstallationContext = intentInstallationContext;
+    }
+
+    /**
+     * Retrieves installable Intents to uninstall.
+     *
+     * @return the Intents to uninstall
+     */
+    public List<T> intentsToUninstall() {
+        return intentsToUninstall;
+    }
+
+    /**
+     * Retrieves installable Intents to install.
+     *
+     * @return the Intents to install
+     */
+    public List<T> intentsToInstall() {
+        return intentsToInstall;
+    }
+
+    /**
+     * Retrieves high level Intent installation information.
+     *
+     * @return the high level Intent installation information
+     */
+    public IntentInstallationContext intentInstallationContext() {
+        return intentInstallationContext;
+    }
+
+    /**
+     * Retrieves high level Intent data to uninstall.
+     *
+     * @return high level Intent data to uninstall
+     */
+    public Optional<IntentData> toUninstall() {
+        return intentInstallationContext.toUninstall();
+    }
+
+    /**
+     * Retrieves high level Intent data to install.
+     *
+     * @return high level Intent data to install
+     */
+    public Optional<IntentData> toInstall() {
+        return intentInstallationContext.toInstall();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof IntentOperationContext)) {
+            return false;
+        }
+
+        IntentOperationContext that = (IntentOperationContext) obj;
+        return Objects.equals(intentsToInstall, that.intentsToInstall) &&
+                Objects.equals(intentsToUninstall, that.intentsToUninstall) &&
+                Objects.equals(intentInstallationContext, intentInstallationContext);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(intentsToInstall,
+                            intentsToUninstall,
+                            intentInstallationContext);
+    }
+}