Changes in database for allowing IP parameters, changes in DB migration script, new...
[osm/RO.git] / nfvo.py
diff --git a/nfvo.py b/nfvo.py
index 13d5c9e..c33a44b 100644 (file)
--- a/nfvo.py
+++ b/nfvo.py
@@ -768,19 +768,24 @@ def new_scenario(mydb, tenant_id, topo):
 
 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
     for name,vnf in vnfs.items():
-        WHERE_={}
+        where={}
+        where_or={"tenant_id": tenant_id, 'public': "true"}
         error_text = ""
         error_pos = "'topology':'nodes':'" + name + "'"
         if 'vnf_id' in vnf:
             error_text += " 'vnf_id' " +  vnf['vnf_id']
-            WHERE_['uuid'] = vnf['vnf_id']
+            where['uuid'] = vnf['vnf_id']
         if 'VNF model' in vnf:
             error_text += " 'VNF model' " +  vnf['VNF model']
-            WHERE_['name'] = vnf['VNF model']
-        if len(WHERE_) == 0:
+            where['name'] = vnf['VNF model']
+        if len(where) == 0:
             raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
         
-        vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), FROM='vnfs', WHERE=WHERE_)
+        vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
+                               FROM='vnfs',
+                               WHERE=where, 
+                               WHERE_OR=where_or,
+                               WHERE_AND_OR="AND")
         if len(vnf_db)==0:
             raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
         elif len(vnf_db)>1:
@@ -1024,18 +1029,23 @@ def new_scenario_v02(mydb, tenant_id, scenario_dict):
 
 #1: Check that VNF are present at database table vnfs and update content into scenario dict
     for name,vnf in scenario["vnfs"].iteritems():
-        WHERE_={}
+        where={}
+        where_or={"tenant_id": tenant_id, 'public': "true"}
         error_text = ""
         error_pos = "'topology':'nodes':'" + name + "'"
         if 'vnf_id' in vnf:
             error_text += " 'vnf_id' " +  vnf['vnf_id']
-            WHERE_['uuid'] = vnf['vnf_id']
+            where['uuid'] = vnf['vnf_id']
         if 'vnf_name' in vnf:
             error_text += " 'vnf_name' " +  vnf['vnf_name']
-            WHERE_['name'] = vnf['vnf_name']
-        if len(WHERE_) == 0:
+            where['name'] = vnf['vnf_name']
+        if len(where) == 0:
             raise NfvoException("Needed a 'vnf_id' or 'VNF model' at " + error_pos, HTTP_Bad_Request)
-        vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), FROM='vnfs', WHERE=WHERE_)
+        vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
+                               FROM='vnfs',
+                               WHERE=where,
+                               WHERE_OR=where_or,
+                               WHERE_AND_OR="AND")
         if len(vnf_db)==0:
             raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
         elif len(vnf_db)>1:
@@ -1308,6 +1318,7 @@ def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instanc
 
 def create_instance(mydb, tenant_id, instance_dict):
     #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+    logger.debug("Creating instance...")
     scenario = instance_dict["scenario"]
     datacenter_id = None
     datacenter_name=None
@@ -1761,23 +1772,39 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
             try:
                 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
                 if "console" in action_dict:
-                    if data["server"]=="127.0.0.1" or data["server"]=="localhost":
+                    if not global_config["http_console_proxy"]:
+                        vm_result[ vm['uuid'] ] = {"vim_result": 200,
+                                                   "description": "{protocol}//{ip}:{port}/{suffix}".format(
+                                                                                protocol=data["protocol"],
+                                                                                ip = data["server"],
+                                                                                port = data["port"],
+                                                                                suffix = data["suffix"]),
+                                                   "name":vm['name']
+                                                }
+                        vm_ok +=1
+                    elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
                         vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
                                                    "description": "this console is only reachable by local interface",
                                                    "name":vm['name']
                                                 }
                         vm_error+=1
-                        continue
+                    else:
                     #print "console data", data
-                    try: 
-                        console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
-                        vm_result[ vm['uuid'] ] = {"vim_result": 200,
-                                                   "description": "%s//%s:%d/%s" %(data["protocol"], console_thread.host, console_thread.port, data["suffix"]),
-                                                   "name":vm['name']
-                                                }
-                        vm_ok +=1
-                    except NfvoException as e:
-                        vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
+                        try: 
+                            console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
+                            vm_result[ vm['uuid'] ] = {"vim_result": 200,
+                                                       "description": "{protocol}//{ip}:{port}/{suffix}".format(
+                                                                                    protocol=data["protocol"],
+                                                                                    ip = global_config["http_console_host"],
+                                                                                    port = console_thread.port,
+                                                                                    suffix = data["suffix"]),
+                                                       "name":vm['name']
+                                                    }
+                            vm_ok +=1
+                        except NfvoException as e:
+                            vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
+                            vm_error+=1
+
                 else:
                     vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
                     vm_ok +=1
@@ -1786,9 +1813,9 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
                 vm_error+=1
 
     if vm_ok==0: #all goes wrong
-        return 1, vm_result
+        return vm_result
     else:
-        return 1, vm_result
+        return vm_result
     
 def create_or_use_console_proxy_thread(console_server, console_port):
     #look for a non-used port
@@ -1798,7 +1825,7 @@ def create_or_use_console_proxy_thread(console_server, console_port):
         return global_config["console_thread"][console_thread_key]
     
     for port in  global_config["console_port_iterator"]():
-        print "create_or_use_console_proxy_thread() port:", port
+        #print "create_or_use_console_proxy_thread() port:", port
         if port in global_config["console_ports"]:
             continue
         try: