Feature 1413 resiliency to single component failure
[osm/RO.git] / osm_ro / nfvo_db.py
index ac392c6..cb345e5 100644 (file)
@@ -36,7 +36,9 @@ import time
 
 tables_with_createdat_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
                            "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
-                           "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets"]
+                           "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs", "datacenter_nets",
+                           "instance_actions", "vim_actions"]
+
 
 class nfvo_db(db_base.db_base):
     def __init__(self, host=None, user=None, passwd=None, database=None, log_name='openmano.db', log_level=None):
@@ -552,10 +554,10 @@ class nfvo_db(db_base.db_base):
 #             print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
 #             return self._format_error(e)
 
-    def get_scenario(self, scenario_id, tenant_id=None, datacenter_id=None):
+    def get_scenario(self, scenario_id, tenant_id=None, datacenter_vim_id=None, datacenter_id=None):
         '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
         scenario_id is the uuid or the name if it is not a valid uuid format
-        if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid 
+        if datacenter_vim_id,d datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid
         Only one scenario must mutch the filtering or an error is returned
         ''' 
         tries = 2
@@ -579,21 +581,26 @@ class nfvo_db(db_base.db_base):
                         scenario_dict["cloud-config"] = yaml.load(scenario_dict["cloud_config"])
                     del scenario_dict["cloud_config"]
                     #sce_vnfs
-                    cmd = "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' ORDER BY created_at".format(scenario_dict['uuid'])
+                    cmd = "SELECT uuid,name,member_vnf_index,vnf_id,description FROM sce_vnfs WHERE scenario_id='{}' "\
+                          "ORDER BY created_at".format(scenario_dict['uuid'])
                     self.logger.debug(cmd)
                     self.cur.execute(cmd)
                     scenario_dict['vnfs'] = self.cur.fetchall()
                     for vnf in scenario_dict['vnfs']:
                         #sce_interfaces
-                        cmd = "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf['uuid'])
+                        cmd = "SELECT scei.uuid,scei.sce_net_id,scei.interface_id,i.external_name,scei.ip_address"\
+                              " FROM sce_interfaces as scei join interfaces as i on scei.interface_id=i.uuid"\
+                              " WHERE scei.sce_vnf_id='{}' ORDER BY scei.created_at".format(vnf['uuid'])
                         self.logger.debug(cmd)
                         self.cur.execute(cmd)
                         vnf['interfaces'] = self.cur.fetchall()
                         #vms
-                        cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description, vms.boot_data as boot_data " \
-                                " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \
-                                " WHERE vnfs.uuid='" + vnf['vnf_id'] +"'"  \
-                                " ORDER BY vms.created_at"
+                        cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name," \
+                              " vms.description as description, vms.boot_data as boot_data, count," \
+                              " vms.availability_zone as availability_zone" \
+                              " FROM vnfs join vms on vnfs.uuid=vms.vnf_id" \
+                              " WHERE vnfs.uuid='" + vnf['vnf_id'] + "'"  \
+                              " ORDER BY vms.created_at"
                         self.logger.debug(cmd)
                         self.cur.execute(cmd)
                         vnf['vms'] = self.cur.fetchall()
@@ -602,14 +609,14 @@ class nfvo_db(db_base.db_base):
                                 vm["boot_data"] = yaml.safe_load(vm["boot_data"])
                             else:
                                 del vm["boot_data"]
-                            if datacenter_id!=None:
-                                cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_id='{}'".format(vm['image_id'],datacenter_id)
+                            if datacenter_vim_id!=None:
+                                cmd = "SELECT vim_id FROM datacenters_images WHERE image_id='{}' AND datacenter_vim_id='{}'".format(vm['image_id'],datacenter_vim_id)
                                 self.logger.debug(cmd)
                                 self.cur.execute(cmd) 
                                 if self.cur.rowcount==1:
                                     vim_image_dict = self.cur.fetchone()
                                     vm['vim_image_id']=vim_image_dict['vim_id']
-                                cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_id='{}'".format(vm['flavor_id'],datacenter_id)
+                                cmd = "SELECT vim_id FROM datacenters_flavors WHERE flavor_id='{}' AND datacenter_vim_id='{}'".format(vm['flavor_id'],datacenter_vim_id)
                                 self.logger.debug(cmd)
                                 self.cur.execute(cmd) 
                                 if self.cur.rowcount==1:
@@ -671,7 +678,7 @@ class nfvo_db(db_base.db_base):
                         self.logger.debug(cmd)
                         self.cur.execute(cmd) 
                         d_net = self.cur.fetchone()
-                        if d_net==None or datacenter_id==None:
+                        if d_net==None or datacenter_vim_id==None:
                             #print "nfvo_db.get_scenario() WARNING external net %s not found"  % net['name']
                             net['vim_id']=None
                         else:
@@ -720,6 +727,49 @@ class nfvo_db(db_base.db_base):
                 self._format_error(e, tries, "delete", "instances running")
             tries -= 1
 
+    def new_rows(self, tables, uuid_list=None):
+        """
+        Make a transactional insertion of rows at several tables
+        :param tables: list with dictionary where the keys are the table names and the values are a row or row list
+            with the values to be inserted at the table. Each row is a dictionary with the key values. E.g.:
+            tables = [
+                {"table1": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
+                {"table2": [ {"column1": value, "column2: value, ... }, {"column1": value, "column2: value, ... }, ...],
+                {"table3": {"column1": value, "column2: value, ... }
+            }
+            If tables does not contain the 'created_at', it is generated incrementally with the order of tables. You can
+            provide a integer value, that it is an index multiply by 0.00001 to add to the created time to manually set
+            up and order
+        :param uuid_list: list of created uuids, first one is the root (#TODO to store at uuid table)
+        :return: None if success,  raise exception otherwise
+        """
+        tries = 2
+        while tries:
+            created_time = time.time()
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    for table in tables:
+                        for table_name, row_list in table.items():
+                            index = 0
+                            if isinstance(row_list, dict):
+                                row_list = (row_list, )  #create a list with the single value
+                            for row in row_list:
+                                if table_name in self.tables_with_created_field:
+                                    if "created_at" in row:
+                                        created_time_param = created_time + row.pop("created_at")*0.00001
+                                    else:
+                                        created_time_param = created_time + index*0.00001
+                                        index += 1
+                                else:
+                                    created_time_param = 0
+                                self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None,
+                                                               created_time=created_time_param)
+                    return
+            except (mdb.Error, AttributeError) as e:
+                self._format_error(e, tries)
+            tries -= 1
+
     def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
         tries = 2
         while tries:
@@ -846,7 +896,7 @@ class nfvo_db(db_base.db_base):
             try:
                 with self.con:
                     self.cur = self.con.cursor(mdb.cursors.DictCursor)
-                    #instance table
+                    # instance table
                     where_list=[]
                     if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" )
                     if db_base._check_valid_uuid(instance_id):
@@ -873,7 +923,7 @@ class nfvo_db(db_base.db_base):
                         instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
                     del instance_dict["cloud_config"]
                     
-                    #instance_vnfs
+                    # instance_vnfs
                     cmd = "SELECT iv.uuid as uuid,sv.vnf_id as vnf_id,sv.name as vnf_name, sce_vnf_id, datacenter_id, datacenter_tenant_id"\
                             " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \
                             " WHERE iv.instance_scenario_id='{}'" \