Adding ability to add simulated hosts to the null provider.

Change-Id: Iaf96caf904e2e8a724064be39211087bf681dad0
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/CustomTopologySimulator.java b/providers/null/src/main/java/org/onosproject/provider/nil/CustomTopologySimulator.java
index 016b187..a075199 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/CustomTopologySimulator.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/CustomTopologySimulator.java
@@ -17,11 +17,14 @@
 package org.onosproject.provider.nil;
 
 import com.google.common.collect.Maps;
+import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.host.DefaultHostDescription;
 
 import java.util.Map;
 
@@ -80,6 +83,19 @@
         nameToId.put(name, id);
     }
 
+    /**
+     * Creates a simulated host.
+     *
+     * @param hostId   host identifier
+     * @param location host location
+     * @param hostIp   host IP address
+     */
+    public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) {
+        DefaultHostDescription description =
+                new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp);
+        hostProviderService.hostDetected(hostId, description, false);
+    }
+
     @Override
     protected void createDevices() {
     }
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java
new file mode 100644
index 0000000..245b110
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2016 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.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.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.BasicHostConfig;
+import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.provider.nil.CustomTopologySimulator;
+import org.onosproject.provider.nil.NullProviders;
+import org.onosproject.provider.nil.TopologySimulator;
+
+import java.util.Iterator;
+
+/**
+ * Adds a simulated end-station host to the custom topology simulation.
+ */
+@Command(scope = "onos", name = "null-create-host",
+        description = "Adds a simulated end-station host to the custom topology simulation")
+public class CreateNullHost extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
+            required = true, multiValued = false)
+    String deviceName = null;
+
+    @Argument(index = 1, name = "hostId", description = "Host identifier",
+            required = true, multiValued = false)
+    String hostId = null;
+
+    @Argument(index = 2, name = "hostIp", description = "Host IP address",
+            required = true, multiValued = false)
+    String hostIp = null;
+
+    @Argument(index = 3, name = "latitude", description = "Geo latitude",
+            required = true, multiValued = false)
+    Double latitude = null;
+
+    @Argument(index = 4, name = "longitude", description = "Geo longitude",
+            required = true, multiValued = false)
+    Double longitude = null;
+
+    @Override
+    protected void execute() {
+        NullProviders service = get(NullProviders.class);
+        NetworkConfigService cfgService = get(NetworkConfigService.class);
+
+        TopologySimulator simulator = service.currentSimulator();
+        if (!(simulator instanceof CustomTopologySimulator)) {
+            error("Custom topology simulator is not active.");
+            return;
+        }
+
+        CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
+        DeviceId deviceId = sim.deviceId(deviceName);
+        HostLocation location = findAvailablePort(deviceId);
+        HostId id = HostId.hostId(hostId);
+        BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
+        cfg.latitude(latitude);
+        cfg.longitude(longitude);
+        cfg.apply();
+
+        sim.createHost(id, location, IpAddress.valueOf(hostIp));
+    }
+
+    // Finds an available connect point among edge ports of the specified device
+    private HostLocation findAvailablePort(DeviceId deviceId) {
+        EdgePortService eps = get(EdgePortService.class);
+        HostService hs = get(HostService.class);
+        Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
+
+        while (points.hasNext()) {
+            ConnectPoint point = points.next();
+            if (hs.getConnectedHosts(point).isEmpty()) {
+                return new HostLocation(point, System.currentTimeMillis());
+            }
+        }
+        return null;
+    }
+
+}
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
index 575319d..5b44a76 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
@@ -24,11 +24,13 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
 import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.host.HostService;
 import org.onosproject.provider.nil.CustomTopologySimulator;
 import org.onosproject.provider.nil.NullProviders;
 import org.onosproject.provider.nil.TopologySimulator;
 
 import java.util.Iterator;
+import java.util.Objects;
 
 /**
  * Adds a simulated link to the custom topology simulation.
@@ -64,15 +66,24 @@
         }
 
         CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
-        ConnectPoint one = findAvailablePort(sim.deviceId(src));
-        ConnectPoint two = findAvailablePort(sim.deviceId(dst));
+        ConnectPoint one = findAvailablePort(sim.deviceId(src), null);
+        ConnectPoint two = findAvailablePort(sim.deviceId(dst), one);
         sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
     }
 
-    private ConnectPoint findAvailablePort(DeviceId deviceId) {
+    // Finds an available connect point among edge ports of the specified device
+    private ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
         EdgePortService eps = get(EdgePortService.class);
+        HostService hs = get(HostService.class);
         Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
-        return points.hasNext() ? points.next() : null;
+
+        while (points.hasNext()) {
+            ConnectPoint point = points.next();
+            if (!Objects.equals(point, otherPoint) && hs.getConnectedHosts(point).isEmpty()) {
+                return point;
+            }
+        }
+        return null;
     }
 
 }
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 0ac6e09..67c4314 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
@@ -47,6 +47,9 @@
         <command>
             <action class="org.onosproject.provider.nil.cli.CreateNullLink"/>
         </command>
+        <command>
+            <action class="org.onosproject.provider.nil.cli.CreateNullHost"/>
+        </command>
     </command-bundle>
 
     <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/>