+
+def get_dhcp_controller():
+ """
+ Create an host_thread object for manage openvim controller and not create a thread for itself
+ :return: dhcp_host openvim controller object
+ """
+
+ if 'openvim_controller' in config_dic['host_threads']:
+ return config_dic['host_threads']['openvim_controller']
+
+ bridge_ifaces = []
+ controller_ip = config_dic['ovs_controller_ip']
+ ovs_controller_user = config_dic['ovs_controller_user']
+
+ host_test_mode = True if config_dic['mode'] == 'test' or config_dic['mode'] == "OF only" else False
+ host_develop_mode = True if config_dic['mode'] == 'development' else False
+
+ dhcp_host = ht.host_thread(name='openvim_controller', user=ovs_controller_user, host=controller_ip, db=config_dic['db'],
+ db_lock=config_dic['db_lock'], test=host_test_mode,
+ image_path=config_dic['image_path'], version=config_dic['version'],
+ host_id='openvim_controller', develop_mode=host_develop_mode,
+ develop_bridge_iface=bridge_ifaces)
+
+ config_dic['host_threads']['openvim_controller'] = dhcp_host
+ dhcp_host.ssh_connect()
+ return dhcp_host
+
+
+def delete_dhcp_ovs_bridge(vlan, net_uuid):
+ """
+ Delete bridges and port created during dhcp launching at openvim controller
+ :param vlan: net vlan id
+ :param net_uuid: network identifier
+ :return:
+ """
+ dhcp_path = config_dic['ovs_controller_file_path']
+
+ controller_host = get_dhcp_controller()
+ controller_host.delete_dhcp_port(vlan, net_uuid)
+ controller_host.delete_dhcp_server(vlan, net_uuid, dhcp_path)
+
+
+def create_dhcp_ovs_bridge():
+ """
+ Initialize bridge to allocate the dhcp server at openvim controller
+ :return:
+ """
+ controller_host = get_dhcp_controller()
+ controller_host.create_ovs_bridge()
+
+
+def set_mac_dhcp(vm_ip, vlan, first_ip, last_ip, cidr, mac):
+ """"
+ Launch a dhcpserver base on dnsmasq attached to the net base on vlan id across the the openvim computes
+ :param vm_ip: IP address asigned to a VM
+ :param vlan: Segmentation id
+ :param first_ip: First dhcp range ip
+ :param last_ip: Last dhcp range ip
+ :param cidr: net cidr
+ :param mac: VM vnic mac to be macthed with the IP received
+ """
+ if not vm_ip:
+ return
+ ip_tools = IPNetwork(cidr)
+ cidr_len = ip_tools.prefixlen
+ dhcp_netmask = str(ip_tools.netmask)
+ dhcp_path = config_dic['ovs_controller_file_path']
+
+ new_cidr = [first_ip + '/' + str(cidr_len)]
+ if not len(all_matching_cidrs(vm_ip, new_cidr)):
+ vm_ip = None
+
+ controller_host = get_dhcp_controller()
+ controller_host.set_mac_dhcp_server(vm_ip, mac, vlan, dhcp_netmask, dhcp_path)
+
+
+def delete_mac_dhcp(vm_ip, vlan, mac):
+ """
+ Delete into dhcp conf file the ip assigned to a specific MAC address
+ :param vm_ip: IP address asigned to a VM
+ :param vlan: Segmentation id
+ :param mac: VM vnic mac to be macthed with the IP received
+ :return:
+ """
+
+ dhcp_path = config_dic['ovs_controller_file_path']
+
+ controller_host = get_dhcp_controller()
+ controller_host.delete_mac_dhcp_server(vm_ip, mac, vlan, dhcp_path)
+
+
+def launch_dhcp_server(vlan, first_ip, last_ip, cidr):
+ """
+ Launch a dhcpserver base on dnsmasq attached to the net base on vlan id across the the openvim computes
+ :param vlan: vlan identifier
+ :param first_ip: First dhcp range ip
+ :param last_ip: Last dhcp range ip
+ :param cidr: net cidr
+ :return:
+ """
+ ip_tools = IPNetwork(cidr)
+ dhcp_netmask = str(ip_tools.netmask)
+ ip_range = [first_ip, last_ip]
+ dhcp_path = config_dic['ovs_controller_file_path']
+
+ controller_host = get_dhcp_controller()
+ controller_host.create_linux_bridge(vlan)
+ controller_host.create_dhcp_interfaces(vlan, first_ip, dhcp_netmask)
+ controller_host.launch_dhcp_server(vlan, ip_range, dhcp_netmask, dhcp_path)
+
+
+def create_vxlan_mesh(host_id):
+ """
+ Create vxlan mesh across all openvimc controller and computes.
+ :param host_id: host identifier
+ :param host_id: host identifier
+ :return:
+ """
+ dhcp_compute_name = get_vxlan_interface("dhcp")
+ existing_hosts = get_hosts()
+ if len(existing_hosts['hosts']) > 0:
+ # vlxan mesh creation between openvim controller and computes
+ computes_available = existing_hosts['hosts']
+ controller_host = get_dhcp_controller()
+ for compute in computes_available:
+ vxlan_interface_name = get_vxlan_interface(compute['id'][:8])
+ config_dic['host_threads'][compute['id']].insert_task("new-vxlan", dhcp_compute_name, controller_host.host)
+ controller_host.create_ovs_vxlan_tunnel(vxlan_interface_name, compute['ip_name'])
+
+ # vlxan mesh creation between openvim computes
+ for count, compute_owner in enumerate(computes_available):
+ for compute in computes_available:
+ if compute_owner['id'] == compute['id']:
+ pass
+ else:
+ vxlan_interface_name = get_vxlan_interface(compute_owner['id'][:8])
+ controller_host.create_ovs_vxlan_tunnel(vxlan_interface_name, compute_owner['ip_name'])
+ config_dic['host_threads'][compute['id']].insert_task("new-vxlan",
+ vxlan_interface_name,
+ compute_owner['ip_name'])
+
+
+def delete_vxlan_mesh(host_id):
+ """
+ Create a task for remove a specific compute of the vlxan mesh
+ :param host_id: host id to be deleted.
+ """
+ existing_hosts = get_hosts()
+ computes_available = existing_hosts['hosts']
+ #
+ vxlan_interface_name = get_vxlan_interface(host_id[:8])
+ controller_host = get_dhcp_controller()
+ controller_host.delete_ovs_vxlan_tunnel(vxlan_interface_name)
+ # remove bridge from openvim controller if no more computes exist
+ if len(existing_hosts):
+ controller_host.delete_ovs_bridge()
+ # Remove vxlan mesh
+ for compute in computes_available:
+ if host_id == compute['id']:
+ pass
+ else:
+ controller_host.delete_ovs_vxlan_tunnel(vxlan_interface_name)
+ config_dic['host_threads'][compute['id']].insert_task("del-vxlan", vxlan_interface_name)
+
+
+def get_vxlan_interface(local_uuid):
+ """
+ Genearte a vxlan interface name
+ :param local_uuid: host id
+ :return: vlxan-8digits
+ """
+ return 'vxlan-' + local_uuid[:8]
+
+