Improved error message when device has no more free ports

Change-Id: Ic8a0122621f772408628e924420855b349a41d5c
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 2eb3fe3..1ea55a4 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
@@ -78,7 +78,13 @@
 
         CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
         HostId id = sim.nextHostId();
-        Set<HostLocation> locations = getLocations(sim, deviceNames);
+        Set<HostLocation> locations;
+        try {
+            locations = getLocations(sim, deviceNames);
+        } catch (NoLocationException e) {
+            error("\u001B[1;31mHost not created - no location (free port) available on %s\u001B[0m", e.getMessage());
+            return;
+        }
         Set<IpAddress> ips = getIps(hostIps);
 
         BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
@@ -97,10 +103,15 @@
         return ips.build();
     }
 
-    private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
+    private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames)
+            throws NoLocationException {
         ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
         String[] csv = deviceNames.split(",");
         for (String s : csv) {
+            HostLocation loc = findAvailablePort(sim.deviceId(s));
+            if (loc == null) {
+                throw new NoLocationException(deviceNames);
+            }
             locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
         }
         return locations.build();
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 5063e12..8ffca1a 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
@@ -62,6 +62,13 @@
         CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
         ConnectPoint one = findAvailablePort(sim.deviceId(src), null);
         ConnectPoint two = findAvailablePort(sim.deviceId(dst), one);
+        if (one == null) {
+            error("\u001B[1;31mLink not created - no location (free port) available on src %s\u001B[0m", src);
+            return;
+        } else if (two == null) {
+            error("\u001B[1;31mLink not created - no location (free port) available on dst %s\u001B[0m", dst);
+            return;
+        }
         sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
     }
 
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/NoLocationException.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/NoLocationException.java
new file mode 100644
index 0000000..821a514
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/NoLocationException.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2019-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;
+
+/**
+ * Represents that there is no location (free port) available on the device to add a host or link to.
+ */
+public class NoLocationException extends RuntimeException {
+
+    /**
+     * Constructs an exception with no message and no underlying cause.
+     */
+    public NoLocationException() {
+    }
+
+    /**
+     * Constructs an exception with the specified message.
+     *
+     * @param message the message describing the specific nature of the error
+     */
+    public NoLocationException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructs an exception with the specified message and the underlying cause.
+     *
+     * @param message the message describing the specific nature of the error
+     * @param cause the underlying cause of this error
+     */
+    public NoLocationException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}