From 7985696fc5f7a8fd0cad0e8f474eb451b9241241 Mon Sep 17 00:00:00 2001
From: Attila Farkas <394097@mail.muni.cz>
Date: Wed, 7 Oct 2020 15:41:05 +0200
Subject: [PATCH] Find available ip for the controller

---
 modules/controller.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/modules/controller.py b/modules/controller.py
index f7ca0b0..3af3946 100644
--- a/modules/controller.py
+++ b/modules/controller.py
@@ -1,6 +1,9 @@
 """Contains functions for controller creation."""
 
 
+from netaddr import *
+from itertools import chain
+
 CONTROLLER_NAME = "controller"
 
 
@@ -14,6 +17,28 @@ def _are_controller_parameters_free(definitions):
     return True
 
 
+def _is_ip_available(ip, definitions):
+    """Check if the given ip is used somewhere"""
+    for mapping in chain(definitions["router_mappings"],
+                         definitions["net_mappings"]):
+        if mapping["ip"] == ip:
+            return False
+    return True
+
+
+def _find_available_ip(definitions):
+    """Return an available ip for the controller"""
+    network = IPNetwork(definitions["networks"][0]["cidr"])
+    for ip in network:
+        if _is_ip_available(ip, definitions):
+            return str(ip)
+
+
+def _add_controller(definitions):
+    """Add controller to definitions"""
+    ip = _find_available_ip(definitions)
+
+
 def create_controller(definitions):
     """Add the definition of controller to definitions.
 
@@ -25,4 +50,6 @@ def create_controller(definitions):
         raise ValueError("A device with the same name as border router "
                          "already exists.")
 
+    _add_controller(definitions)
+
     return
-- 
GitLab