Adding command to add routes and to generate flows from them.

Enhanced FlowRuleStore and FlowRuleService with a new method.

Change-Id: I011371c1931294448e361fc1ceb120d89c14489d
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/NullFlowRuleProvider.java b/providers/null/src/main/java/org/onosproject/provider/nil/NullFlowRuleProvider.java
index 2cbb4d0..beaeb6a 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/NullFlowRuleProvider.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/NullFlowRuleProvider.java
@@ -74,12 +74,12 @@
 
     @Override
     public void applyFlowRule(FlowRule... flowRules) {
-        // FIXME: invoke executeBatch
+        throw new UnsupportedOperationException("Cannot apply individual flow rules");
     }
 
     @Override
     public void removeFlowRule(FlowRule... flowRules) {
-        // FIXME: invoke executeBatch
+        throw new UnsupportedOperationException("Cannot remove individual flow rules");
     }
 
     @Override
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
index ed68dfc..a9efa78 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
@@ -17,6 +17,7 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.primitives.Longs;
+import org.onlab.util.Tools;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
@@ -33,9 +34,12 @@
  * Base command for adding simulated entities to the custom topology simulation.
  */
 public abstract class CreateNullEntity extends AbstractShellCommand {
+
     protected static final String GEO = "geo";
     protected static final String GRID = "grid";
 
+    protected static final int MAX_EDGE_PORT_TRIES = 5;
+
     /**
      * Validates that the simulator is custom.
      *
@@ -93,14 +97,32 @@
      * @return connect point available for link or host attachment
      */
     protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
-        EdgePortService eps = get(EdgePortService.class);
         HostService hs = get(HostService.class);
-
-        List<ConnectPoint> points = ImmutableList
-                .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
-                              eps.getEdgePoints(deviceId));
-        return points.stream()
+        return findAvailablePorts(deviceId).stream()
                 .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
                 .findFirst().orElse(null);
     }
+
+    /**
+     * Finds an available connect points among edge ports of the specified device.
+     *
+     * @param deviceId device identifier
+     * @return list of connect points available for link or host attachments
+     */
+    protected List<ConnectPoint> findAvailablePorts(DeviceId deviceId) {
+        EdgePortService eps = get(EdgePortService.class);
+
+        // As there may be a slight delay in edge service getting updated, retry a few times
+        for (int i = 0; i < MAX_EDGE_PORT_TRIES; i++) {
+            List<ConnectPoint> points = ImmutableList
+                    .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
+                                  eps.getEdgePoints(deviceId));
+            if (!points.isEmpty()) {
+                return points;
+            }
+            Tools.delay(100);
+        }
+        return ImmutableList.of();
+    }
+
 }
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHosts.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHosts.java
new file mode 100644
index 0000000..0495038
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHosts.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.provider.nil.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.provider.nil.CustomTopologySimulator;
+import org.onosproject.provider.nil.NullProviders;
+import org.onosproject.provider.nil.TopologySimulator;
+
+import java.util.List;
+
+/**
+ * Adds a simulated end-station host to the custom topology simulation.
+ */
+@Command(scope = "onos", name = "null-create-hosts",
+        description = "Adds a simulated end-station host to the custom topology simulation")
+public class CreateNullHosts extends CreateNullEntity {
+
+    @Argument(index = 0, name = "deviceName", description = "Name of device where hosts are attached",
+            required = true)
+    String deviceName = null;
+
+    @Argument(index = 1, name = "hostIpPattern", description = "Host IP pattern",
+            required = true)
+    String hostIpPattern = null;
+
+    @Argument(index = 2, name = "hostCount", description = "Number of hosts to create",
+            required = true)
+    int hostCount = 0;
+
+    @Override
+    protected void execute() {
+        NullProviders service = get(NullProviders.class);
+
+        TopologySimulator simulator = service.currentSimulator();
+        if (!validateSimulator(simulator)) {
+            return;
+        }
+
+        CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
+
+        List<ConnectPoint> points = findAvailablePorts(sim.deviceId(deviceName));
+        String pattern = hostIpPattern.replace("*", "%d");
+        for (int h = 0; h < hostCount; h++) {
+            HostLocation location = new HostLocation(points.get(h), System.currentTimeMillis());
+            IpAddress ip = IpAddress.valueOf(String.format(pattern, h));
+            HostId id = sim.nextHostId();
+            sim.createHost(id, location, ip);
+        }
+    }
+
+}
diff --git a/providers/null/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/providers/null/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index e1abc40..7be09e8 100644
--- a/providers/null/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/providers/null/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -50,6 +50,9 @@
         <command>
             <action class="org.onosproject.provider.nil.cli.CreateNullHost"/>
         </command>
+        <command>
+            <action class="org.onosproject.provider.nil.cli.CreateNullHosts"/>
+        </command>
     </command-bundle>
 
     <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/>