IntentStore interface changes

Change-Id: Ifa8728fb4c7edcc6d58a5e071f7252493ab2a578
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java b/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
index e3a0522..b1571ad 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
@@ -72,6 +72,10 @@
         super(type, intent);
     }
 
+    public static IntentEvent getEvent(IntentData data) {
+        return getEvent(data.state(), data.intent());
+    }
+
     public static IntentEvent getEvent(IntentState state, Intent intent) {
         Type type;
         switch (state) {
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentService.java b/core/api/src/main/java/org/onosproject/net/intent/IntentService.java
index 3c1c91a..1ba1b29 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentService.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentService.java
@@ -52,18 +52,6 @@
     void replace(IntentId oldIntentId, Intent newIntent);
 
     /**
-     * Submits a batch of submit & withdraw operations. Such a batch is
-     * assumed to be processed together.
-     * <p>
-     * This is an asynchronous request meaning that the environment may be
-     * affected at later time.
-     * </p>
-     * @param operations batch of intent operations
-     */
-    @Deprecated
-    void execute(IntentOperations operations);
-
-    /**
      * Returns an iterable of intents currently in the system.
      *
      * @return set of intents
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 dcb2259..00f638d 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
@@ -46,7 +46,9 @@
      * @return intent or null if not found
      */
     @Deprecated
-    Intent getIntent(IntentId intentId);
+    default Intent getIntent(IntentId intentId) {
+        throw new UnsupportedOperationException("deprecated");
+    };
 
     /**
      * Returns the state of the specified intent.
@@ -55,7 +57,9 @@
      * @return current intent state
      */
     @Deprecated
-    IntentState getIntentState(IntentId intentId);
+    default IntentState getIntentState(IntentId intentId) {
+        throw new UnsupportedOperationException("deprecated");
+    }
 
     /**
      * Returns the list of the installable events associated with the specified
@@ -65,7 +69,9 @@
      * @return compiled installable intents
      */
     @Deprecated
-    List<Intent> getInstallableIntents(IntentId intentId);
+    default List<Intent> getInstallableIntents(IntentId intentId) {
+        throw new UnsupportedOperationException("deprecated");
+    }
 
     /**
      * Execute writes in a batch.
@@ -74,7 +80,10 @@
      * @param batch BatchWrite to execute
      * @return failed operations
      */
+    @Deprecated
     List<Operation> batchWrite(BatchWrite batch);
+    default void write(IntentData newData) {}
+    default void batchWrite(Iterable<IntentData> updates) {}
 
     /**
      * Returns the intent with the specified identifier.
diff --git a/core/api/src/test/java/org/onosproject/net/intent/FakeIntentManager.java b/core/api/src/test/java/org/onosproject/net/intent/FakeIntentManager.java
index ae9bcf5..a9a331f 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/FakeIntentManager.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/FakeIntentManager.java
@@ -191,11 +191,6 @@
     }
 
     @Override
-    public void execute(IntentOperations operations) {
-        // TODO: implement later
-    }
-
-    @Override
     public Set<Intent> getIntents() {
         return Collections.unmodifiableSet(new HashSet<>(intents.values()));
     }
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
index 335d9f1..e80883b 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
@@ -38,11 +38,6 @@
     }
 
     @Override
-    public void execute(IntentOperations operations) {
-
-    }
-
-    @Override
     public Iterable<Intent> getIntents() {
         return null;
     }
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java b/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
index a91fa0a..628e848 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
@@ -15,17 +15,29 @@
  */
 package org.onosproject.net.intent.impl;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Collectors;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
-import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.core.IdGenerator;
 import org.onosproject.event.AbstractListenerRegistry;
@@ -44,36 +56,26 @@
 import org.onosproject.net.intent.IntentId;
 import org.onosproject.net.intent.IntentInstaller;
 import org.onosproject.net.intent.IntentListener;
-import org.onosproject.net.intent.IntentOperation;
-import org.onosproject.net.intent.IntentOperations;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.intent.IntentState;
 import org.onosproject.net.intent.IntentStore;
 import org.onosproject.net.intent.IntentStoreDelegate;
 import org.slf4j.Logger;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.EnumSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.stream.Collectors;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.concurrent.Executors.newFixedThreadPool;
 import static org.onlab.util.Tools.namedThreads;
-import static org.onosproject.net.intent.IntentState.*;
+import static org.onosproject.net.intent.IntentState.FAILED;
+import static org.onosproject.net.intent.IntentState.INSTALLED;
+import static org.onosproject.net.intent.IntentState.INSTALLING;
+import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
+import static org.onosproject.net.intent.IntentState.WITHDRAWN;
+import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -171,25 +173,6 @@
     }
 
     @Override
-    public void execute(IntentOperations operations) {
-        for (IntentOperation op : operations.operations()) {
-            switch (op.type()) {
-                case SUBMIT:
-                case UPDATE:
-                    submit(op.intent());
-                    break;
-                case WITHDRAW:
-                    withdraw(op.intent());
-                    break;
-                //fallthrough
-                case REPLACE:
-                default:
-                    throw new UnsupportedOperationException("replace not supported");
-            }
-        }
-    }
-
-    @Override
     public Iterable<Intent> getIntents() {
         return store.getIntents();
     }
@@ -396,19 +379,13 @@
 
     private void buildAndSubmitBatches(Iterable<IntentId> intentIds,
                                        boolean compileAllFailed) {
-        Map<ApplicationId, IntentOperations.Builder> batches = Maps.newHashMap();
         // Attempt recompilation of the specified intents first.
         for (IntentId id : intentIds) {
             Intent intent = store.getIntent(id);
             if (intent == null) {
                 continue;
             }
-            IntentOperations.Builder builder = batches.get(intent.appId());
-            if (builder == null) {
-                builder = IntentOperations.builder(intent.appId());
-                batches.put(intent.appId(), builder);
-            }
-            builder.addUpdateOperation(id);
+            submit(intent);
         }
 
         if (compileAllFailed) {
@@ -416,15 +393,10 @@
             for (Intent intent : getIntents()) {
                 IntentState state = getIntentState(intent.id());
                 if (RECOMPILE.contains(state)) {
-                    IntentOperations.Builder builder = batches.get(intent.appId());
-                    if (builder == null) {
-                        builder = IntentOperations.builder(intent.appId());
-                        batches.put(intent.appId(), builder);
-                    }
                     if (state == WITHDRAW_REQ) {
-                        builder.addWithdrawOperation(intent.id());
+                        withdraw(intent);
                     } else {
-                        builder.addUpdateOperation(intent.id());
+                        submit(intent);
                     }
                 }
             }
diff --git a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleIntentStore.java b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleIntentStore.java
index dee2351..4b67421 100644
--- a/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleIntentStore.java
+++ b/core/store/trivial/src/main/java/org/onosproject/store/trivial/impl/SimpleIntentStore.java
@@ -15,7 +15,6 @@
  */
 package org.onosproject.store.trivial.impl;
 
-import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -26,19 +25,15 @@
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.IntentData;
 import org.onosproject.net.intent.IntentEvent;
-import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.intent.IntentState;
 import org.onosproject.net.intent.IntentStore;
 import org.onosproject.net.intent.IntentStoreDelegate;
 import org.onosproject.store.AbstractStore;
 import org.slf4j.Logger;
 
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -77,66 +72,11 @@
     }
 
     @Override
-    public Intent getIntent(IntentId intentId) {
-        throw new UnsupportedOperationException("deprecated");
-    }
-
-    @Override
-    public IntentState getIntentState(IntentId id) {
-        throw new UnsupportedOperationException("deprecated");
-    }
-
-    private void setState(Intent intent, IntentState state) {
-        //FIXME
-        IntentId id = intent.id();
-//        states.put(id, state);
-        IntentEvent.Type type = null;
-
-        switch (state) {
-        case INSTALL_REQ:
-            type = IntentEvent.Type.INSTALL_REQ;
-            break;
-        case INSTALLED:
-            type = IntentEvent.Type.INSTALLED;
-            break;
-        case FAILED:
-            type = IntentEvent.Type.FAILED;
-            break;
-        case WITHDRAW_REQ:
-            type = IntentEvent.Type.WITHDRAW_REQ;
-            break;
-        case WITHDRAWN:
-            type = IntentEvent.Type.WITHDRAWN;
-            break;
-        default:
-            break;
-        }
-        if (type != null) {
-            notifyDelegate(new IntentEvent(type, intent));
-        }
-    }
-
-    private void setInstallableIntents(IntentId intentId, List<Intent> result) {
-        //FIXME
-//        installable.put(intentId, result);
-    }
-
-    @Override
-    public List<Intent> getInstallableIntents(IntentId intentId) {
-        throw new UnsupportedOperationException("deprecated");
-    }
-
-    @Override
     public IntentData getIntentData(String key) {
         return current.get(key);
     }
 
-    private void removeInstalledIntents(IntentId intentId) {
-        //FIXME
-//        installable.remove(intentId);
-    }
-
-    /**
+    /*
      * Execute writes in a batch.
      *
      * @param batch BatchWrite to execute
@@ -144,6 +84,8 @@
      */
     @Override
     public List<Operation> batchWrite(BatchWrite batch) {
+        throw new UnsupportedOperationException("deprecated");
+        /*
         if (batch.isEmpty()) {
             return Collections.emptyList();
         }
@@ -195,16 +137,41 @@
             }
         }
         return failed;
+        */
     }
 
     @Override
+    public void write(IntentData newData) {
+        //FIXME need to compare the versions
+        current.put(newData.key(), newData);
+        try {
+            notifyDelegate(IntentEvent.getEvent(newData));
+        } catch (IllegalArgumentException e) {
+            //no-op
+            log.trace("ignore this exception: {}", e);
+        }
+        IntentData old = pending.get(newData.key());
+        if (old != null /* && FIXME version check */) {
+            pending.remove(newData.key());
+        }
+    }
+
+    @Override
+    public void batchWrite(Iterable<IntentData> updates) {
+        for (IntentData data : updates) {
+            write(data);
+        }
+    }
+
+
+    @Override
     public void addPending(IntentData data) {
         //FIXME need to compare versions
         pending.put(data.key(), data);
         checkNotNull(delegate, "Store delegate is not set")
                 .process(data);
+        notifyDelegate(IntentEvent.getEvent(data));
     }
-    // FIXME!!! pending.remove(intent.key()); // TODO check version
 
 
     @Override