Minor fixes at sdn-port-mapping
[osm/RO.git] / osm_ro / nfvo.py
index 19af225..282b7f3 100644 (file)
@@ -372,7 +372,10 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da
                                 config=extra, persistent_info=persistent_info
                         )
             except Exception as e:
-                raise NfvoException("Error at VIM  {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
+                http_code = HTTP_Internal_Server_Error
+                if isinstance(e, vimconn.vimconnException):
+                    http_code = e.http_code
+                raise NfvoException("Error at VIM  {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
         return vim_dict
     except db_base_Exception as e:
         raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
@@ -2240,7 +2243,8 @@ def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
                 db_sce_vnf = {
                     "uuid": sce_vnf_uuid,
                     "scenario_id": scenario_uuid,
-                    "name": get_str(vnf, "member-vnf-index", 255),
+                    # "name": get_str(vnf, "member-vnf-index", 255),
+                    "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
                     "vnf_id": existing_vnf[0]["uuid"],
                     "member_vnf_index": str(vnf["member-vnf-index"]),
                     # TODO 'start-by-default': True
@@ -2991,11 +2995,11 @@ def create_instance(mydb, tenant_id, instance_dict):
         for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
             found = False
             for scenario_vnf in scenarioDict['vnfs']:
-                if vnf_name == scenario_vnf['name']:
+                if vnf_name == scenario_vnf['name'] or vnf_name == scenario_vnf['member_vnf_index']:
                     found = True
                     break
             if not found:
-                raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
+                raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), HTTP_Bad_Request)
             if "datacenter" in vnf_instance_desc:
                 # Add this datacenter to myvims
                 vnf_instance_desc["datacenter"] = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
@@ -4199,16 +4203,16 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
 
     input_vnfs = action_dict.pop("vnfs", [])
     input_vms = action_dict.pop("vms", [])
-    action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
+    action_over_all = True if not input_vnfs and not input_vms else False
     vm_result = {}
     vm_error = 0
     vm_ok = 0
     for sce_vnf in instanceDict['vnfs']:
         for vm in sce_vnf['vms']:
-            if not action_over_all:
-                if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
-                                vm['uuid'] not in input_vms and vm['name'] not in input_vms:
-                    continue
+            if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
+                    sce_vnf['member_vnf_index'] not in input_vnfs and \
+                    vm['uuid'] not in input_vms and vm['name'] not in input_vms:
+                continue
             try:
                 if "add_public_key" in action_dict:
                     mgmt_access = {}
@@ -4353,22 +4357,33 @@ def delete_tenant(mydb, tenant):
 
 
 def new_datacenter(mydb, datacenter_descriptor):
+    sdn_port_mapping = None
     if "config" in datacenter_descriptor:
-        datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
-    #Check that datacenter-type is correct
+        sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
+        datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
+                                                         width=256)
+    # Check that datacenter-type is correct
     datacenter_type = datacenter_descriptor.get("type", "openvim");
-    module_info = None
+    module_info = None
     try:
         module = "vimconn_" + datacenter_type
         pkg = __import__("osm_ro." + module)
-        vim_conn = getattr(pkg, module)
+        vim_conn = getattr(pkg, module)
         # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
     except (IOError, ImportError):
         # if module_info and module_info[0]:
         #    file.close(module_info[0])
-        raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type, module), HTTP_Bad_Request)
+        raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
+                                                                                                  module),
+                            HTTP_Bad_Request)
 
     datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
+    if sdn_port_mapping:
+        try:
+            datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
+        except Exception as e:
+            mydb.delete_row_by_id("datacenters", datacenter_id)   # Rollback
+            raise e
     return datacenter_id
 
 
@@ -4380,10 +4395,14 @@ def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
     datacenter_id = datacenter['uuid']
     where={'uuid': datacenter['uuid']}
     remove_port_mapping = False
+    new_sdn_port_mapping = None
     if "config" in datacenter_descriptor:
         if datacenter_descriptor['config'] != None:
             try:
                 new_config_dict = datacenter_descriptor["config"]
+                if "sdn-port-mapping" in new_config_dict:
+                    remove_port_mapping = True
+                    new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
                 #delete null fields
                 to_delete=[]
                 for k in new_config_dict:
@@ -4413,6 +4432,11 @@ def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
                 logger.error("Error deleting datacenter-port-mapping " + str(e))
 
     mydb.update_rows('datacenters', datacenter_descriptor, where)
+    if new_sdn_port_mapping:
+        try:
+            datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
+        except ovimException as e:
+            logger.error("Error adding datacenter-port-mapping " + str(e))
     return datacenter_id
 
 
@@ -4580,9 +4604,10 @@ def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=
             logger.error("Cannot delete datacenter_tenants " + str(e))
             pass  # the error will be caused because dependencies, vim_tenant can not be deleted
         thread_id = tenant_datacenter_item["datacenter_tenant_id"]
-        thread = vim_threads["running"][thread_id]
-        thread.insert_task("exit")
-        vim_threads["deleting"][thread_id] = thread
+        thread = vim_threads["running"].get(thread_id)
+        if thread:
+            thread.insert_task("exit")
+            vim_threads["deleting"][thread_id] = thread
     return "datacenter {} detached. {}".format(datacenter_id, warning)