+def print_to_stderr(msg, end='\n'):
+ """Writes any text to stderr.
+
+ :param msg: Message to print.
+ :param end: By default print new line at the end.
+ :type msg: str
+ :type end: str
+ """
+ try:
+ sys.stderr.write(str(msg) + end)
+ except ValueError:
+ pass
+
+def get_assigned_interfaces(args, network="flat"):
+ """Retrieve assigned interfaces in openstack network.
+
+ :param args: Command line params.
+ :param network: Openstack network.
+ :type args: ArgumentParser
+ :type network: str
+ :returns: Assigned interfaces.
+ :rtype: list
+ :raises RuntimeError: If response is not 200.
+ """
+ req = requests.get('http://{}/openstack/rest/ports/{}'
+ .format(args.virl_ip, network),
+ auth=(args.username, args.password))
+ if req.status_code == 200:
+ return req.json()
+ else:
+ raise RuntimeError("ERROR: Retrieving ports in use - "
+ "Status other than 200 HTTP OK:\n{}"
+ .format(req.content))
+
+def get_assigned_interfaces_count(args, network="flat"):
+ """Count assigned interfaces in openstack network.
+
+ :param args: Command line params.
+ :param network: Openstack network.
+ :type args: ArgumentParser
+ :type network: str
+ :returns: Assigned interfaces count.
+ :rtype: int
+ """
+ return len(get_assigned_interfaces(args, network=network))
+
+def check_ip_addresses(args):
+ """Check IP address availability.
+
+ :param args: Command line params.
+ :type args: ArgumentParser
+ :raises RuntimeError: If not enough free addresses available.
+ """
+ for i in range(args.wait_count):
+ if (args.quota - \
+ get_assigned_interfaces_count(args) >= IPS_PER_SIMULATION):
+ break
+ if args.verbosity >= 2:
+ print_to_stderr("DEBUG: - Attempt {} out of {}, waiting for free "
+ "IP addresses".format(i, args.wait_count))
+ # Wait random amount of time within range 1-3 minutes
+ time.sleep(random.randint(60,180))
+ else:
+ raise RuntimeError("ERROR: Not enough IP addresses to run simulation")
+
+def check_virl_resources(args):
+ """Check virl resources availability.
+
+ :param args: Command line params.
+ :type args: ArgumentParser
+ """
+ check_ip_addresses(args)
+