IntentManager: use IntentStore batch APIs

Change-Id: Ie60f3e53f48fa6acbcaf5cf6837bdef12b36a98d
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/IntentPushTestCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/IntentPushTestCommand.java
index 0decb08..9326221 100644
--- a/cli/src/main/java/org/onlab/onos/cli/net/IntentPushTestCommand.java
+++ b/cli/src/main/java/org/onlab/onos/cli/net/IntentPushTestCommand.java
@@ -15,10 +15,13 @@
  */
 package org.onlab.onos.cli.net;
 
+import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Lists;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.core.ApplicationId;
+import org.onlab.onos.core.CoreService;
 import org.onlab.onos.net.ConnectPoint;
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.PortNumber;
@@ -61,17 +64,27 @@
               required = true, multiValued = false)
     String egressDeviceString = null;
 
-    @Argument(index = 2, name = "count",
-              description = "Number of intents to push",
-              required = true, multiValued = false)
-    String countString = null;
+
+    @Argument(index = 2, name = "Intents per appId",
+            description = "Number of intents per appId",
+            required = true, multiValued = false)
+    String intentsPerAppId = null;
+
+    @Argument(index = 3, name = "apps",
+            description = "Number of appIds",
+            required = false, multiValued = false)
+    String appIds = null;
+
 
     private IntentService service;
     private CountDownLatch latch;
     private long start, end;
+    private int apps;
+    private int intentsPerApp;
     private int count;
     private boolean add;
 
+
     @Override
     protected void execute() {
         service = get(IntentService.class);
@@ -85,13 +98,18 @@
         PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
         ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
 
-        count = Integer.parseInt(countString);
+        apps = appIds != null ? Integer.parseInt(appIds) : 1;
+        intentsPerApp = Integer.parseInt(intentsPerAppId);
+
+        count = intentsPerApp * apps;
+
 
         service.addListener(this);
 
+        ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
+
         add = true;
         latch = new CountDownLatch(count);
-        List<Intent> operations = generateIntents(ingress, egress);
         submitIntents(operations);
 
         add = false;
@@ -101,36 +119,41 @@
         service.removeListener(this);
     }
 
-    private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
+    private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
         TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
                 .matchEthType(Ethernet.TYPE_IPV4);
         TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
 
-        List<Intent> intents = Lists.newArrayList();
-        for (int i = 1; i <= count; i++) {
-            TrafficSelector s = selector
-                    .matchEthSrc(MacAddress.valueOf(i))
-                    .build();
-            intents.add(new PointToPointIntent(appId(), s, treatment,
-                                               ingress, egress));
+        ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
+        for (int app = 1; app <= apps; app++) {
+            for (int i = 1; i <= intentsPerApp; i++) {
+                TrafficSelector s = selector
+                        .matchEthSrc(MacAddress.valueOf(i))
+                        .build();
+                intents.put(app, new PointToPointIntent(appId(), s, treatment,
+                                                        ingress, egress));
 
+            }
         }
         return intents;
     }
 
-    private void submitIntents(List<Intent> intents) {
-        IntentOperations.Builder builder = IntentOperations.builder(appId());
-        for (Intent intent : intents) {
-            if (add) {
-                builder.addSubmitOperation(intent);
-            } else {
-                builder.addWithdrawOperation(intent.id());
+    private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
+        List<IntentOperations> opList = Lists.newArrayList();
+        for (Integer app : intents.keySet()) {
+            IntentOperations.Builder builder = IntentOperations.builder(appId(app));
+            for (Intent intent : intents.get(app)) {
+                if (add) {
+                    builder.addSubmitOperation(intent);
+                } else {
+                    builder.addWithdrawOperation(intent.id());
+                }
             }
+            opList.add(builder.build());
         }
-        IntentOperations ops = builder.build();
 
         start = System.currentTimeMillis();
-        service.execute(ops);
+        opList.forEach(ops -> service.execute(ops));
         try {
             if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
                 printResults(count);
@@ -148,6 +171,16 @@
         print("Time to %s %d intents: %d ms", text, count, delta);
     }
 
+
+    /**
+     * Returns application ID for the CLI.
+     *
+     * @return command-line application identifier
+     */
+    protected ApplicationId appId(Integer id) {
+        return get(CoreService.class).registerApplication("org.onlab.onos.cli-" + id);
+    }
+
     /**
      * Extracts the port number portion of the ConnectPoint.
      *
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/LeaderCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/LeaderCommand.java
new file mode 100644
index 0000000..cd5ee09
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/LeaderCommand.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2014 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.onlab.onos.cli.net;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.cluster.Leadership;
+import org.onlab.onos.cluster.LeadershipService;
+
+import java.util.Map;
+
+/**
+ * Prints the leader for every topic.
+ */
+@Command(scope = "onos", name = "leaders",
+        description = "Finds the leader for particular topic.")
+public class LeaderCommand extends AbstractShellCommand {
+
+    @Override
+    protected void execute() {
+        LeadershipService leaderService = get(LeadershipService.class);
+        Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard();
+        print("Topic:\t\tLeader");
+        for (String topic : leaderBoard.keySet()) {
+            print("%s:\t%s", topic, leaderBoard.get(topic).leader().id());
+        }
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index b725bcb..9cc21dd 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -258,6 +258,9 @@
         <command>
             <action class="org.onlab.onos.cli.net.AddFlowsCommand"/>
         </command>
+        <command>
+            <action class="org.onlab.onos.cli.net.LeaderCommand"/>
+        </command>
 
         <command>
             <action class="org.onlab.onos.cli.net.WipeOutCommand"/>