Add openflow-port-mapping CLI command
[osm/openvim.git] / httpserver.py
index 954c1d5..edf1e8d 100644 (file)
@@ -47,7 +47,7 @@ from vim_schema import host_new_schema, host_edit_schema, tenant_new_schema, \
     flavor_new_schema, flavor_update_schema, \
     image_new_schema, image_update_schema, \
     server_new_schema, server_action_schema, network_new_schema, network_update_schema, \
-    port_new_schema, port_update_schema
+    port_new_schema, port_update_schema, openflow_controller_schema, of_port_map_new_schema
 import ovim
 import logging
 
@@ -147,12 +147,14 @@ def check_extended(extended, allow_net_attach=False):
 #
 # dictionaries that change from HTTP API to database naming
 #
+http2db_id={'id':'uuid'}
 http2db_host={'id':'uuid'}
 http2db_tenant={'id':'uuid'}
 http2db_flavor={'id':'uuid','imageRef':'image_id'}
 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_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'}
 
 def remove_extra_items(data, schema):
@@ -860,114 +862,140 @@ def http_delete_host_id(host_id):
         print "http_delete_host_id error",result, content
         bottle.abort(-result, content)
         return
-
-
-
 #
 # TENANTS
 #
 
+
 @bottle.route(url_base + '/tenants', method='GET')
 def http_get_tenants():
-    my = config_dic['http_threads'][ threading.current_thread().name ]
-    select_,where_,limit_ = filter_query_string(bottle.request.query, http2db_tenant,
-            ('id','name','description','enabled') )
-    result, content = my.db.get_table(FROM='tenants', SELECT=select_,WHERE=where_,LIMIT=limit_)
-    if result < 0:
-        print "http_get_tenants Error", content
-        bottle.abort(-result, content)
-    else:
-        change_keys_http2db(content, http2db_tenant, reverse=True)
-        convert_boolean(content, ('enabled',))
-        data={'tenants' : content}
-        #data['tenants_links'] = dict([('tenant', row['id']) for row in content])
+    """
+    Retreive tenant list from DB
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_tenant,
+                                                      ('id', 'name', 'description', 'enabled'))
+        tenants = my.ovim.get_tenants(select_, where_)
+        delete_nulls(tenants)
+        change_keys_http2db(tenants, http2db_tenant, reverse=True)
+        data = {'tenants': tenants}
         return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
 
 @bottle.route(url_base + '/tenants/<tenant_id>', method='GET')
 def http_get_tenant_id(tenant_id):
-    my = config_dic['http_threads'][ threading.current_thread().name ]
-    result, content = my.db.get_table(FROM='tenants', SELECT=('uuid','name','description', 'enabled'),WHERE={'uuid': tenant_id} )
-    if result < 0:
-        print "http_get_tenant_id error %d %s" % (result, content)
-        bottle.abort(-result, content)
-    elif result==0:
-        print "http_get_tenant_id tenant '%s' not found" % tenant_id
-        bottle.abort(HTTP_Not_Found, "tenant %s not found" % tenant_id)
-    else:
-        change_keys_http2db(content, http2db_tenant, reverse=True)
-        convert_boolean(content, ('enabled',))
-        data={'tenant' : content[0]}
-        #data['tenants_links'] = dict([('tenant', row['id']) for row in content])
+    """
+    Get tenant from DB by id
+    :param tenant_id: tenant id
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        tenant = my.ovim.show_tenant_id(tenant_id)
+        delete_nulls(tenant)
+        change_keys_http2db(tenant, http2db_tenant, reverse=True)
+        data = {'tenant': tenant}
         return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
 
 
 @bottle.route(url_base + '/tenants', method='POST')
 def http_post_tenants():
-    '''insert a tenant into the database.'''
-    my = config_dic['http_threads'][ threading.current_thread().name ]
-    #parse input data
-    http_content = format_in( tenant_new_schema )
-    r = remove_extra_items(http_content, tenant_new_schema)
-    if r is not None: print "http_post_tenants: Warning: remove extra items ", r
-    change_keys_http2db(http_content['tenant'], http2db_tenant)
+    """
+    Insert a tenant into the database.
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        http_content = format_in(tenant_new_schema)
+        r = remove_extra_items(http_content, tenant_new_schema)
+        if r is not None:
+            my.logger.error("http_post_tenants: Warning: remove extra items " + str(r), exc_info=True)
+        # insert in data base
+        tenant_id = my.ovim.new_tentant(http_content['tenant'])
+        tenant = my.ovim.show_tenant_id(tenant_id)
+        change_keys_http2db(tenant, http2db_tenant, reverse=True)
+        delete_nulls(tenant)
+        data = {'tenant': tenant}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
 
-    #insert in data base
-    result, content = my.db.new_tenant(http_content['tenant'])
-            
-    if result >= 0:
-        return http_get_tenant_id(content)
-    else:
-        bottle.abort(-result, content)
-        return
     
 @bottle.route(url_base + '/tenants/<tenant_id>', method='PUT')
 def http_put_tenant_id(tenant_id):
-    '''update a tenant into the database.'''
-    my = config_dic['http_threads'][ threading.current_thread().name ]
-    #parse input data
-    http_content = format_in( tenant_edit_schema )
-    r = remove_extra_items(http_content, tenant_edit_schema)
-    if r is not None: print "http_put_tenant_id: Warning: remove extra items ", r
-    change_keys_http2db(http_content['tenant'], http2db_tenant)
+    """
+    Update a tenantinto DB.
+    :param tenant_id: tentant id
+    :return:
+    """
+
+    my = config_dic['http_threads'][threading.current_thread().name]
+    try:
+        # parse input data
+        http_content = format_in(tenant_edit_schema)
+        r = remove_extra_items(http_content, tenant_edit_schema)
+        if r is not None:
+            print "http_put_tenant_id: Warning: remove extra items ", r
+        change_keys_http2db(http_content['tenant'], http2db_tenant)
+        # insert in data base
+        my.ovim.edit_tenant(tenant_id, http_content['tenant'])
+        tenant = my.ovim.show_tenant_id(tenant_id)
+        change_keys_http2db(tenant, http2db_tenant, reverse=True)
+        data = {'tenant': tenant}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
 
-    #insert in data base
-    result, content = my.db.update_rows('tenants', http_content['tenant'], WHERE={'uuid': tenant_id}, log=True )
-    if result >= 0:
-        return http_get_tenant_id(tenant_id)
-    else:
-        bottle.abort(-result, content)
-        return
 
 @bottle.route(url_base + '/tenants/<tenant_id>', method='DELETE')
 def http_delete_tenant_id(tenant_id):
-    my = config_dic['http_threads'][ threading.current_thread().name ]
-    #check permissions
-    r, tenants_flavors = my.db.get_table(FROM='tenants_flavors', SELECT=('flavor_id','tenant_id'), WHERE={'tenant_id': tenant_id})
-    if r<=0:
-        tenants_flavors=()
-    r, tenants_images  = my.db.get_table(FROM='tenants_images',  SELECT=('image_id','tenant_id'),  WHERE={'tenant_id': tenant_id})
-    if r<=0:
-        tenants_images=()
-    result, content = my.db.delete_row('tenants', tenant_id)
-    if result == 0:
-        bottle.abort(HTTP_Not_Found, content)
-    elif result >0:
-        print "alf", tenants_flavors, tenants_images
-        for flavor in tenants_flavors:
-            my.db.delete_row_by_key("flavors", "uuid",  flavor['flavor_id'])
-        for image in tenants_images:
-            my.db.delete_row_by_key("images", "uuid",   image['image_id'])
-        data={'result' : content}
-        return format_out(data)
-    else:
-        print "http_delete_tenant_id error",result, content
-        bottle.abort(-result, content)
-        return
+    """
+    Delete a tenant from the database.
+    :param tenant_id: tenant id
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
 
+    try:
+        content = my.ovim.delete_tentant(tenant_id)
+        data = {'result': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
 #
 # FLAVORS
 #
 
+
 @bottle.route(url_base + '/<tenant_id>/flavors', method='GET')
 def http_get_flavors(tenant_id):
     my = config_dic['http_threads'][ threading.current_thread().name ]
@@ -1591,14 +1619,14 @@ def http_post_server_id(tenant_id):
             r,c = config_dic['host_threads'][ server['host_id'] ].insert_task( 'restore-iface',*port )
             if r < 0:
                 print ' http_post_servers ERROR RESTORE IFACE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' +  c
-        #updata nets
-        for net in nets:
-            r,c = config_dic['of_thread'].insert_task("update-net", net)
-            if r < 0:
-                print ':http_post_servers ERROR UPDATING NETS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' +  c
+        # update nets
+        for net_id in nets:
+            try:
+                my.ovim.net_update_ofc_thread(net_id)
+            except ovim.ovimException as e:
+                my.logger.error("http_post_servers, Error updating network with id '{}', '{}'".format(net_id, str(e)))
 
-            
-        #look for dhcp ip address
+        # look for dhcp ip address
         r2, c2 = my.db.get_table(FROM="ports", SELECT=["mac", "ip_address", "net_id"], WHERE={"instance_id": new_instance})
         if r2 >0:
             for iface in c2:
@@ -1764,7 +1792,8 @@ def http_server_action(server_id, tenant_id, action):
         return http_get_image_id(tenant_id, image_uuid)
     
     #Update DB only for CREATING or DELETING status
-    data={'result' : 'in process'}
+    data={'result' : 'deleting in process'}
+    warn_text=""
     if new_status != None and new_status == 'DELETING':
         nets=[]
         ports_to_free=[]
@@ -1776,14 +1805,16 @@ def http_server_action(server_id, tenant_id, action):
         for port in ports_to_free:
             r1,c1 = config_dic['host_threads'][ server['host_id'] ].insert_task( 'restore-iface',*port )
             if r1 < 0:
-                print ' http_post_server_action error at server deletion ERROR resore-iface !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' +  c1
-                data={'result' : 'deleting in process, but ifaces cannot be restored!!!!!'}
-        for net in nets:
-            r1,c1 = config_dic['of_thread'].insert_task("update-net", net)
-            if r1 < 0:
-                print ' http_post_server_action error at server deletion ERROR UPDATING NETS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' +  c1
-                data={'result' : 'deleting in process, but openflow rules cannot be deleted!!!!!'}
-        #look for dhcp ip address 
+                my.logger.error("http_post_server_action server deletion ERROR at resore-iface!!!! " + c1)
+                warn_text += "; Error iface '{}' cannot be restored '{}'".format(str(port), str(e))
+        for net_id in nets:
+            try:
+                my.ovim.net_update_ofc_thread(net_id)
+            except ovim.ovimException as e:
+                my.logger.error("http_server_action, Error updating network with id '{}', '{}'".format(net_id, str(e)))
+                warn_text += "; Error openflow rules of network '{}' cannot be restore '{}'".format(net_id, str (e))
+
+        # look for dhcp ip address
         if r2 >0 and config_dic.get("dhcp_server"):
             for iface in c2:
                 if iface["net_id"] in config_dic["dhcp_nets"]:
@@ -1800,7 +1831,7 @@ def http_server_action(server_id, tenant_id, action):
             delete_dhcp_ovs_bridge(vlan, net_id)
             delete_mac_dhcp(vm_ip, vlan, mac)
             config_dic['host_threads'][server['host_id']].insert_task('del-ovs-port', vlan, net_id)
-    return format_out(data)
+    return format_out(data + warn_text)
 
 
 
@@ -1945,7 +1976,7 @@ def http_put_network_id(network_id):
         http_content = format_in(network_update_schema)
         change_keys_http2db(http_content['network'], http2db_network)
         network = http_content['network']
-        return my.ovim.edit_network(network_id, network)
+        return format_out(my.ovim.edit_network(network_id, network))
 
     except ovim.ovimException as e:
         my.logger.error(str(e), exc_info=True)
@@ -1982,6 +2013,127 @@ def http_delete_network_id(network_id):
 #
 
 
+@bottle.route(url_base + '/openflow/controller', method='GET')
+def http_get_openflow_controller():
+    """
+    Retrieve a openflow controllers list from DB.
+    :return:
+    """
+    # TODO check if show a proper list
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_ofc,
+                                                      ('id', 'name', 'dpid', 'ip', 'port', 'type',
+                                                       'version', 'user', 'password'))
+
+        content = my.ovim.get_of_controllers(select_, where_)
+        delete_nulls(content)
+        change_keys_http2db(content, http2db_ofc, reverse=True)
+        data = {'ofcs': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/controller/<uuid>', method='GET')
+def http_get_openflow_controller_id(uuid):
+    """
+    Get an openflow controller by dpid from DB.get_of_controllers
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+
+        content = my.ovim.show_of_controller(uuid)
+        delete_nulls(content)
+        change_keys_http2db(content, http2db_ofc, reverse=True)
+        data = {'ofc': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/controller/', method='POST')
+def http_post_openflow_controller():
+    """
+    Create a new openflow controller into DB
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        http_content = format_in(openflow_controller_schema)
+        of_c = http_content['ofc']
+        uuid = my.ovim.new_of_controller(of_c)
+        content = my.ovim.show_of_controller(uuid)
+        delete_nulls(content)
+        change_keys_http2db(content, http2db_ofc, reverse=True)
+        data = {'ofc': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/controller/<of_controller_id>', method='PUT')
+def http_put_openflow_controller_by_id(of_controller_id):
+    """
+    Create an openflow controller into DB
+    :param of_controller_id: openflow controller dpid
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        http_content = format_in(openflow_controller_schema)
+        of_c = http_content['ofc']
+
+        content = my.ovim.edit_of_controller(of_controller_id, of_c)
+        delete_nulls(content)
+        change_keys_http2db(content, http2db_ofc, reverse=True)
+        data = {'ofc': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/controller/<of_controller_id>', method='DELETE')
+def http_delete_openflow_controller(of_controller_id):
+    """
+    Delete  an openflow controller from DB.
+    :param of_controller_id: openflow controller dpid
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        content = my.ovim.delete_of_controller(of_controller_id)
+        data = {'result': content}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
 @bottle.route(url_base + '/networks/<network_id>/openflow', method='GET')
 def http_get_openflow_id(network_id):
     """
@@ -2035,9 +2187,9 @@ def http_put_openflow_id(network_id):
     data = {'result': str(result) + " nets updates"}
     return format_out(data)
 
-
+@bottle.route(url_base + '/networks/clear/openflow/<ofc_id>', method='DELETE')
 @bottle.route(url_base + '/networks/clear/openflow', method='DELETE')
-def http_clear_openflow_rules():
+def http_clear_openflow_rules(ofc_id=None):
     """
     To make actions over the net. The action is to delete ALL openflow rules
     :return:
@@ -2047,7 +2199,7 @@ def http_clear_openflow_rules():
     if not my.admin:
         bottle.abort(HTTP_Unauthorized, "Needed admin privileges")
     try:
-        my.ovim.delete_openflow_rules()
+        my.ovim.delete_openflow_rules(ofc_id)
     except ovim.ovimException as e:
         my.logger.error(str(e), exc_info=True)
         bottle.abort(e.http_code, str(e))
@@ -2058,16 +2210,25 @@ def http_clear_openflow_rules():
     data = {'result': " Clearing openflow rules in process"}
     return format_out(data)
 
-
+@bottle.route(url_base + '/networks/openflow/ports/<ofc_id>', method='GET')
 @bottle.route(url_base + '/networks/openflow/ports', method='GET')
-def http_get_openflow_ports():
+def http_get_openflow_ports(ofc_id=None):
     """
     Obtain switch ports names of openflow controller
     :return:
     """
     my = config_dic['http_threads'][threading.current_thread().name]
-    ports = my.ovim.get_openflow_ports()
-    data = {'ports': ports}
+
+    try:
+        ports = my.ovim.get_openflow_ports(ofc_id)
+        data = {'ports': ports}
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
     return format_out(data)
 #
 # PORTS
@@ -2192,3 +2353,78 @@ def http_delete_port_id(port_id):
         bottle.abort(HTTP_Bad_Request, str(e))
 
 
+@bottle.route(url_base + '/openflow/mapping', method='POST')
+def http_of_port_mapping():
+    """
+    Create new compute port mapping entry
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        http_content = format_in(of_port_map_new_schema)
+        r = remove_extra_items(http_content, of_port_map_new_schema)
+        if r is not None:
+            my.logger.error("http_of_port_mapping: Warning: remove extra items " + str(r), exc_info=True)
+
+        # insert in data base
+        port_mapping = my.ovim.set_of_port_mapping(http_content['of_port_mapings'])
+        change_keys_http2db(port_mapping, http2db_id, reverse=True)
+        delete_nulls(port_mapping)
+        data = {'of_port_mappings': port_mapping}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/mapping', method='GET')
+def get_of_port_mapping():
+    """
+    Get compute port mapping
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_id,
+                                                      ('id', 'ofc_id', 'region', 'compute_node', 'pci',
+                                                       'switch_dpid', 'switch_port', 'switch_mac'))
+        # insert in data base
+        port_mapping = my.ovim.get_of_port_mappings(select_, where_)
+        change_keys_http2db(port_mapping, http2db_id, reverse=True)
+        delete_nulls(port_mapping)
+        data = {'of_port_mappings': port_mapping}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+
+
+@bottle.route(url_base + '/openflow/mapping/<region>', method='DELETE')
+def delete_of_port_mapping(region):
+    """
+    Insert a tenant into the database.
+    :return:
+    """
+    my = config_dic['http_threads'][threading.current_thread().name]
+
+    try:
+        # insert in data base
+        db_filter = {'region': region}
+        result = my.ovim.clear_of_port_mapping(db_filter)
+        data = {'result': result}
+        return format_out(data)
+    except ovim.ovimException as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(e.http_code, str(e))
+    except Exception as e:
+        my.logger.error(str(e), exc_info=True)
+        bottle.abort(HTTP_Bad_Request, str(e))
+