Added LICENSE file to root folder
[osm/openvim.git] / osm_openvim / httpserver.py
index e95c820..0e5bf39 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 ##
-# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
+# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
 # This file is part of openvim
 # All Rights Reserved.
 #
@@ -38,6 +38,7 @@ import datetime
 import hashlib
 import os
 import imp
+import socket
 from netaddr import IPNetwork, IPAddress, all_matching_cidrs
 #import only if needed because not needed in test mode. To allow an easier installation   import RADclass
 from jsonschema import validate as js_v, exceptions as js_e
@@ -152,7 +153,7 @@ http2db_host={'id':'uuid'}
 http2db_tenant={'id':'uuid'}
 http2db_flavor={'id':'uuid','imageRef':'image_id', 'size': 'image_size'}
 http2db_image={'id':'uuid', 'created':'created_at', 'updated':'modified_at', 'public': 'public'}
-http2db_server={'id':'uuid','hostId':'host_id','flavorRef':'flavor_id','imageRef':'image_id','created':'created_at'}
+http2db_server={'id':'uuid','hostId':'host_id','flavorRef':'flavor_id','osImageType':'os_image_type','imageRef':'image_id','created':'created_at'}  #Unikernels extension
 http2db_network={'id':'uuid','provider:vlan':'vlan', 'provider:physical': 'provider'}
 http2db_ofc = {'id': 'uuid'}
 http2db_port={'id':'uuid', 'network_id':'net_id', 'mac_address':'mac', 'device_owner':'type','device_id':'instance_id','binding:switch_port':'switch_port','binding:vlan':'vlan', 'bandwidth':'Mbps'}
@@ -512,7 +513,7 @@ def http_get_hosts():
 
 def get_hosts():
     select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_host,
-                                                  ('id', 'name', 'description', 'status', 'admin_state_up', 'ip_name'))
+                                                  ('id', 'name', 'description', 'status', 'admin_state_up', 'ip_name', 'hypervisors'))  #Unikernels extension
     
     myself = config_dic['http_threads'][ threading.current_thread().name ]
     result, content = myself.db.get_table(FROM='hosts', SELECT=select_, WHERE=where_, LIMIT=limit_)
@@ -637,6 +638,11 @@ def http_post_hosts():
     for numa in host.get("numas", ()):
         if "hugepages_consumed" in numa:
             del numa["hugepages_consumed"]
+        for core in numa.get("cores", ()):
+            if "instance_id" in core:
+                del core["instance_id"]
+            if "v_thread_id" in core:
+                del core["v_thread_id"]
     result, content = my.db.new_host(host)
     if result >= 0:
         if content['admin_state_up']:
@@ -647,10 +653,11 @@ def http_post_hosts():
             thread = ht.host_thread(name=host.get('name',ip_name), user=user, host=ip_name,
                                     password=host.get('password'),
                                     keyfile=host.get('keyfile', config_dic["host_ssh_keyfile"]),
-                                    db=config_dic['db'], db_lock=config_dic['db_lock'],
+                                    db=config_dic['db'],
                                     test=host_test_mode, image_path=config_dic['host_image_path'],
                                     version=config_dic['version'], host_id=content['uuid'],
-                                    develop_mode=host_develop_mode, develop_bridge_iface=host_develop_bridge_iface)
+                                    develop_mode=host_develop_mode, develop_bridge_iface=host_develop_bridge_iface,
+                                    hypervisors=host.get('hypervisors', None))  #Unikernels extension
 
             thread.start()
             config_dic['host_threads'][content['uuid']] = thread
@@ -660,7 +667,7 @@ def http_post_hosts():
                 create_dhcp_ovs_bridge()
                 config_dic['host_threads'][content['uuid']].insert_task("new-ovsbridge")
                 # create vlxan bwt OVS controller and computes
-                create_vxlan_mesh(content['uuid'])
+                create_vxlan_mesh(content['uuid'], my.logger)
 
         # return host data
         change_keys_http2db(content, http2db_host, reverse=True)
@@ -686,7 +693,7 @@ def delete_dhcp_ovs_bridge(vlan, net_uuid):
     dhcp_controller = http_controller.ovim.get_dhcp_controller()
 
     dhcp_controller.delete_dhcp_server(vlan, net_uuid, dhcp_path)
-    dhcp_controller.delete_dhcp_port(vlan, net_uuid)
+    dhcp_controller.delete_dhcp_port(vlan, net_uuid, dhcp_path)
 
 
 def create_dhcp_ovs_bridge():
@@ -744,12 +751,12 @@ def delete_mac_dhcp(vm_ip, vlan, mac):
     dhcp_controller.delete_mac_dhcp_server(vm_ip, mac, vlan, dhcp_path)
 
 
-def create_vxlan_mesh(host_id):
+def create_vxlan_mesh(host_id, logger=None):
     """
     Create vxlan mesh across all openvimc controller and computes.
-    :param host_id: host identifier
-    :param host_id: host identifier
-    :return:
+    :param host_id: Added compute node id. Anyway vlan is created by all compute nodes
+    :param logger: To log errors
+    :return: None
     """
     dhcp_compute_name = get_vxlan_interface("dhcp")
     existing_hosts = get_hosts()
@@ -761,22 +768,27 @@ def create_vxlan_mesh(host_id):
         dhcp_controller = http_controller.ovim.get_dhcp_controller()
 
         for compute in computes_available:
+            try:
+                if compute['ip_name'] != 'localhost':
+                    remote_ip = socket.gethostbyname(compute['ip_name'])
+                else:
+                    remote_ip = 'localhost'
+            except socket.error as e:
+                if logger:
+                    logger.error("Cannot get compute node remote ip from '{}'. Skipping: {}".format(
+                        compute['ip_name'], e))
+                continue
+            # vxlan ovs_controller <=> compute node
             vxlan_interface_name = get_vxlan_interface(compute['id'][:8])
             config_dic['host_threads'][compute['id']].insert_task("new-vxlan", dhcp_compute_name, dhcp_controller.host)
-            dhcp_controller.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])
-                    dhcp_controller.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'])
-
+            dhcp_controller.create_ovs_vxlan_tunnel(vxlan_interface_name, remote_ip)
+            # vxlan from others compute node to cthis ompute node
+            for compute_src in computes_available:
+                if compute_src['id'] == compute['id']:
+                    continue
+                config_dic['host_threads'][compute_src['id']].insert_task("new-vxlan",
+                                                              vxlan_interface_name,
+                                                              remote_ip)
 
 def delete_vxlan_mesh(host_id):
     """
@@ -847,7 +859,7 @@ def http_put_host_id(host_id):
         if config_dic['network_type'] == 'ovs':
             # create mesh with new host data
             config_dic['host_threads'][host_id].insert_task("new-ovsbridge")
-            create_vxlan_mesh(host_id)
+            create_vxlan_mesh(host_id, my.logger)
 
         #print data
         return format_out(data)
@@ -1610,7 +1622,7 @@ def http_post_server_id(tenant_id):
             return
     #print json.dumps(server, indent=4)
      
-    result, content = ht.create_server(server, config_dic['db'], config_dic['db_lock'], config_dic['mode']=='normal')
+    result, content = ht.create_server(server, config_dic['db'], config_dic['mode']=='normal')
 
     if result >= 0:
     #Insert instance to database
@@ -1665,10 +1677,15 @@ def http_post_server_id(tenant_id):
                         dhcp_enable = bool(server_net['network']['enable_dhcp'])
                         vm_dhcp_ip = c2[0]["ip_address"]
                         config_dic['host_threads'][server['host_id']].insert_task("create-ovs-bridge-port", vlan)
-
-                        dns = yaml.safe_load(server_net['network'].get("dns"))
-                        routes = yaml.safe_load(server_net['network'].get("routes"))
-                        links = yaml.safe_load(server_net['network'].get("links"))
+                        dns = server_net['network'].get("dns")
+                        if dns:
+                            dns = yaml.safe_load(server_net['network'].get("dns"))
+                        routes = server_net['network'].get("routes")
+                        if routes:
+                            routes = yaml.safe_load(server_net['network'].get("routes"))
+                        links = server_net['network'].get("links")
+                        if links:
+                            links = yaml.safe_load(server_net['network'].get("links"))
                         if dhcp_enable:
                             dhcp_firt_ip = str(server_net['network']['dhcp_first_ip'])
                             dhcp_last_ip = str(server_net['network']['dhcp_last_ip'])