Fixing null provider custom topo simulator

- properly deal with device mastership
- allow creation of multi-homed hosts
- made UI location parameters optional
- added a simulated fabric script

Change-Id: I8558cc06aa4c323fab898b02fba9659b202c5392
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 cf1e22d..52883a2 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
@@ -27,6 +27,7 @@
 import org.onosproject.net.host.DefaultHostDescription;
 
 import java.util.Map;
+import java.util.Set;
 
 import static org.onlab.util.Tools.toHex;
 import static org.onosproject.provider.nil.NullProviders.SCHEME;
@@ -96,6 +97,19 @@
         hostProviderService.hostDetected(hostId, description, false);
     }
 
+    /**
+     * Creates a simulated multi-homed host.
+     *
+     * @param hostId   host identifier
+     * @param locations host locations
+     * @param hostIps   host IP addresses
+     */
+    public void createHost(HostId hostId, Set<HostLocation> locations, Set<IpAddress> hostIps) {
+        DefaultHostDescription description =
+                new DefaultHostDescription(hostId.mac(), hostId.vlanId(), locations, hostIps, false);
+        hostProviderService.hostDetected(hostId, description, false);
+    }
+
     @Override
     protected void createDevices() {
     }
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java b/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
index 1250877..7313f12 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
@@ -439,8 +439,9 @@
 
         @Override
         public boolean isReachable(DeviceId deviceId) {
-            return topoShape.equals("custom") ||
-                    (simulator != null && simulator.contains(deviceId) &&
+            return simulator != null &&
+                    (simulator.contains(deviceId) || !deviceService.getPorts(deviceId).isEmpty()) &&
+                    (simulator instanceof CustomTopologySimulator ||
                             topologyMutationDriver.isReachable(deviceId));
         }
 
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/TopologySimulator.java b/providers/null/src/main/java/org/onosproject/provider/nil/TopologySimulator.java
index 8152da3..f531ae6 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/TopologySimulator.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/TopologySimulator.java
@@ -177,9 +177,7 @@
      * @param i index of the device id in the list.
      */
     protected void createDevice(int i) {
-        DeviceId id = DeviceId.deviceId(SCHEME + ":" + toHex(i));
-        deviceIds.add(id);
-        createDevice(id, i);
+        createDevice(DeviceId.deviceId(SCHEME + ":" + toHex(i)), i);
     }
 
     /**
@@ -205,6 +203,7 @@
                 new DefaultDeviceDescription(id.uri(), type,
                                              "ON.Lab", "0.1", "0.1", "1234",
                                              new ChassisId(chassisId));
+        deviceIds.add(id);
         deviceProviderService.deviceConnected(id, desc);
         deviceProviderService.updatePorts(id, buildPorts(portCount));
     }
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
index b6c668f..4d081f8 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
@@ -17,7 +17,6 @@
 
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.config.NetworkConfigService;
@@ -31,34 +30,32 @@
  */
 @Command(scope = "onos", name = "null-create-device",
         description = "Adds a simulated device to the custom topology simulation")
-public class CreateNullDevice extends AbstractShellCommand {
-    private static final String GEO = "geo";
-    private static final String GRID = "grid";
+public class CreateNullDevice extends CreateNullEntity {
 
     @Argument(index = 0, name = "type", description = "Device type, e.g. switch, roadm",
-            required = true, multiValued = false)
+            required = true)
     String type = null;
 
     @Argument(index = 1, name = "name", description = "Device name",
-            required = true, multiValued = false)
+            required = true)
     String name = null;
 
     @Argument(index = 2, name = "portCount", description = "Port count",
-            required = true, multiValued = false)
+            required = true)
     Integer portCount = null;
 
     @Argument(index = 3, name = "latOrY",
             description = "Geo latitude / Grid y-coord",
-            required = true, multiValued = false)
+            required = false)
     Double latOrY = null;
 
     @Argument(index = 4, name = "longOrX",
             description = "Geo longitude / Grid x-coord",
-            required = true, multiValued = false)
+            required = false)
     Double longOrX = null;
 
     @Argument(index = 5, name = "locType", description = "Location type {geo|grid}",
-            required = false, multiValued = false)
+            required = false)
     String locType = GEO;
 
     @Override
@@ -67,28 +64,15 @@
         NetworkConfigService cfgService = get(NetworkConfigService.class);
 
         TopologySimulator simulator = service.currentSimulator();
-        if (!(simulator instanceof CustomTopologySimulator)) {
-            error("Custom topology simulator is not active.");
-            return;
-        }
-
-        if (!(GEO.equals(locType) || GRID.equals(locType))) {
-            error("locType must be 'geo' or 'grid'.");
+        if (!validateSimulator(simulator) || !validateLocType(locType)) {
             return;
         }
 
         CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
         DeviceId deviceId = sim.nextDeviceId();
         BasicDeviceConfig cfg = cfgService.addConfig(deviceId, BasicDeviceConfig.class);
-        cfg.name(name)
-                .locType(locType);
-
-        if (GEO.equals(locType)) {
-            cfg.latitude(latOrY).longitude(longOrX);
-        } else {
-            cfg.gridX(longOrX).gridY(latOrY);
-        }
-        cfg.apply();
+        cfg.name(name);
+        setUiCoordinates(cfg, locType, latOrY, longOrX);
 
         sim.createDevice(deviceId, name, Device.Type.valueOf(type.toUpperCase()), portCount);
     }
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
new file mode 100644
index 0000000..ed68dfc
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
@@ -0,0 +1,106 @@
+/*
+ * 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 com.google.common.collect.ImmutableList;
+import com.google.common.primitives.Longs;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.basics.BasicElementConfig;
+import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.provider.nil.CustomTopologySimulator;
+import org.onosproject.provider.nil.TopologySimulator;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * 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";
+
+    /**
+     * Validates that the simulator is custom.
+     *
+     * @param simulator topology simulator
+     * @return true if valid
+     */
+    protected boolean validateSimulator(TopologySimulator simulator) {
+        if (!(simulator instanceof CustomTopologySimulator)) {
+            error("Custom topology simulator is not active.");
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Validates that the location type is valid.
+     *
+     * @param locType location type
+     * @return true if valid
+     */
+    protected boolean validateLocType(String locType) {
+        if (!(GEO.equals(locType) || GRID.equals(locType))) {
+            error("locType must be 'geo' or 'grid'.");
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Sets the UI location coordinates appropriately.
+     *
+     * @param cfg     element config
+     * @param locType location type
+     * @param latOrY  latitude or Y grid
+     * @param longOrX longitude or X grid
+     */
+    protected void setUiCoordinates(BasicElementConfig cfg,
+                                    String locType, Double latOrY, Double longOrX) {
+        if (latOrY != null && longOrX != null) {
+            cfg.locType(locType);
+            if (GEO.equals(locType)) {
+                cfg.latitude(latOrY).longitude(longOrX);
+            } else {
+                cfg.gridX(longOrX).gridY(latOrY);
+            }
+        }
+        cfg.apply();
+    }
+
+    /**
+     * Finds an available connect point among edge ports of the specified device.
+     *
+     * @param deviceId   device identifier
+     * @param otherPoint optional other point to be excluded from search
+     * @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()
+                .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
+                .findFirst().orElse(null);
+    }
+}
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
index 5197e79..e1eb5dd 100644
--- 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
@@ -16,54 +16,51 @@
 
 package org.onosproject.provider.nil.cli;
 
+import com.google.common.collect.ImmutableSet;
 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.HashSet;
-import java.util.Iterator;
+import java.util.Set;
+
+import static java.util.Objects.requireNonNull;
 
 /**
  * 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 {
-    private static final String GEO = "geo";
-    private static final String GRID = "grid";
+public class CreateNullHost extends CreateNullEntity {
 
-    @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
-            required = true, multiValued = false)
-    String deviceName = null;
+    @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated",
+            required = true)
+    String deviceNames = null;
 
-    @Argument(index = 1, name = "hostIp", description = "Host IP address",
-            required = true, multiValued = false)
-    String hostIp = null;
+    @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated",
+            required = true)
+    String hostIps = null;
 
     @Argument(index = 2, name = "latOrY",
             description = "Geo latitude / Grid y-coord",
-            required = true, multiValued = false)
+            required = false)
     Double latOrY = null;
 
     @Argument(index = 3, name = "longOrX",
             description = "Geo longitude / Grid x-coord",
-            required = true, multiValued = false)
+            required = false)
     Double longOrX = null;
 
     @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
-            required = false, multiValued = false)
+            required = false)
     String locType = GEO;
 
     @Override
@@ -72,48 +69,43 @@
         NetworkConfigService cfgService = get(NetworkConfigService.class);
 
         TopologySimulator simulator = service.currentSimulator();
-        if (!(simulator instanceof CustomTopologySimulator)) {
-            error("Custom topology simulator is not active.");
-            return;
-        }
-
-        if (!(GEO.equals(locType) || GRID.equals(locType))) {
-            error("locType must be 'geo' or 'grid'.");
+        if (!validateSimulator(simulator) || !validateLocType(locType)) {
             return;
         }
 
         CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
-        DeviceId deviceId = sim.deviceId(deviceName);
         HostId id = sim.nextHostId();
-        HostLocation location = findAvailablePort(deviceId);
+        Set<HostLocation> locations = getLocations(sim, deviceNames);
+        Set<IpAddress> ips = getIps(hostIps);
+
         BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
+        setUiCoordinates(cfg, locType, latOrY, longOrX);
 
-        cfg.locType(locType);
-        cfg.setLocations(new HashSet<HostLocation>() {{ add(location); }});
+        sim.createHost(id, locations, ips);
+    }
 
-        if (GEO.equals(locType)) {
-            cfg.latitude(latOrY).longitude(longOrX);
-        } else {
-            cfg.gridX(longOrX).gridY(latOrY);
+    private Set<IpAddress> getIps(String hostIps) {
+        ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
+        String[] csv = hostIps.split(",");
+        for (String s : csv) {
+            ips.add(IpAddress.valueOf(s));
         }
-        cfg.apply();
+        return ips.build();
+    }
 
-        sim.createHost(id, location, IpAddress.valueOf(hostIp));
+    private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
+        ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
+        String[] csv = deviceNames.split(",");
+        for (String s : csv) {
+            locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
+        }
+        return locations.build();
     }
 
     // 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;
+        ConnectPoint point = findAvailablePort(deviceId, null);
+        return point == null ? null : new HostLocation(point, System.currentTimeMillis());
     }
 
 }
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 1b609cf..72e1781 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
@@ -19,40 +19,32 @@
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
-import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.ConnectPoint;
-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.
  */
 @Command(scope = "onos", name = "null-create-link",
         description = "Adds a simulated link to the custom topology simulation")
-public class CreateNullLink extends AbstractShellCommand {
+public class CreateNullLink extends CreateNullEntity {
 
     @Argument(index = 0, name = "type", description = "Link type, e.g. direct, indirect, optical",
-            required = true, multiValued = false)
+            required = true)
     String type = null;
 
     @Argument(index = 1, name = "src", description = "Source device name",
-            required = true, multiValued = false)
+            required = true)
     String src = null;
 
     @Argument(index = 2, name = "dst", description = "Destination device name",
-            required = true, multiValued = false)
+            required = true)
     String dst = null;
 
-    @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only",
-            required = false, multiValued = false)
+    @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only")
     private boolean unidirectional = false;
 
     @Override
@@ -71,19 +63,4 @@
         sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
     }
 
-    // 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();
-
-        while (points.hasNext()) {
-            ConnectPoint point = points.next();
-            if (!Objects.equals(point, otherPoint) && hs.getConnectedHosts(point).isEmpty()) {
-                return point;
-            }
-        }
-        return null;
-    }
-
 }
diff --git a/tools/test/topos/cfab-null b/tools/test/topos/cfab-null
new file mode 100755
index 0000000..8993d2e
--- /dev/null
+++ b/tools/test/topos/cfab-null
@@ -0,0 +1,85 @@
+#!/bin/bash
+# -----------------------------------------------------------------------------
+# Creates a spine-leaf fabric with large number of hosts using null providers
+# -----------------------------------------------------------------------------
+
+# config
+node=${1:-$OCI}
+
+# Create the script of ONOS commands first and then execute it all at once.
+export CMDS="/tmp/fab-onos.cmds"
+rm $CMDS
+
+function sim {
+    echo "$@" >> $CMDS
+}
+
+sim "wipe-out please"
+
+spinePorts=48
+leafPorts=64
+accessPorts=1024
+
+# Create spines
+for spine in {1..2}; do
+    sim "null-create-device switch Spine-${spine} ${spinePorts}"
+done
+
+# Create 2 leaf pairs with dual links to the spines and a link between the pair
+for pair in A B; do
+    sim "null-create-device switch Leaf-${pair}1 ${leafPorts}"
+    sim "null-create-device switch Leaf-${pair}2 ${leafPorts}"
+    sim "null-create-link direct Leaf-${pair}1 Leaf-${pair}2"
+
+    for spine in {1..2}; do
+        for link in {1..2}; do
+            sim "null-create-link direct Spine-${spine} Leaf-${pair}1"
+            sim "null-create-link direct Spine-${spine} Leaf-${pair}2"
+        done
+    done
+
+    # Create hosts for each leaf group; multi-homed to each leaf in the pair
+    [ $pair = A ] && pn=1 || pn=2
+    for host in {1..10}; do
+        sim "null-create-host Leaf-${pair}1,Leaf-${pair}2 10.${pn}.1.${host}"
+    done
+done
+
+# Create 8 single leafs with dual links to the spines
+for access in {1..8}; do
+    sim "null-create-device switch Access-${access} ${accessPorts}"
+
+    for spine in {1..2}; do
+        for link in {1..2}; do
+            sim "null-create-link direct Spine-${spine} Access-${access}"
+        done
+    done
+
+    # Create hosts for each single leaf
+    for host in {1..25}; do
+        sim "null-create-host Access-${access} 10.0.${access}.${host}"
+    done
+done
+
+
+
+# make sure null providers are activated
+onos ${node} app activate org.onosproject.null
+
+sleep 2
+
+# start custom simulation..
+onos ${node} null-simulation start custom
+
+# Generate the recipe using the following:
+# 2 spines, can potentially be few more
+# 12 leaves in total
+#     2 leaf pair
+#     8 non-paired
+# Host per leaf up to 1K
+
+sleep 5
+
+# Add devices, links, and hosts
+cat $CMDS | onos ${node}
+