avoid openstack default gateway creation at VLD ip-profile.
[osm/RO.git] / osm_ro / nfvo_db.py
index 071d03a..4376278 100644 (file)
@@ -36,7 +36,10 @@ 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", "sce_vnffgs", "sce_rsps", "sce_rsp_hops",
+                           "sce_classifiers", "sce_classifier_matches", "instance_sfis", "instance_sfs",
+                           "instance_classifications", "instance_sfps"]
 
 
 class nfvo_db(db_base.db_base):
@@ -212,7 +215,7 @@ class nfvo_db(db_base.db_base):
                     myVNFDict["description"] = vnf_descriptor['vnf']['description']
                     myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
                     myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
-                    
+
                     vnf_id = self._new_row_internal('vnfs', myVNFDict, add_uuid=True, root_uuid=None, created_time=created_time)
                     #print "Adding new vms to the NFVO database"
                     #For each vm, we must create the appropriate vm in the NFVO database.
@@ -553,10 +556,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 +582,31 @@ class nfvo_db(db_base.db_base):
                     if scenario_dict["cloud_config"]:
                         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'])
+                    # sce_vnfs
+                    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 mgmt_access FROM vnfs WHERE uuid='{}'".format(scenario_dict['vnfs'][0]['vnf_id'])
+                        self.logger.debug(cmd)
+                        self.cur.execute(cmd)
+                        mgmt_access_dict = self.cur.fetchall()
+                        if mgmt_access_dict[0].get('mgmt_access'):
+                            vnf['mgmt_access'] = yaml.load(mgmt_access_dict[0]['mgmt_access'])
+                        else:
+                            vnf['mgmt_access'] = None
+                        # 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'])
                         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
+                        cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, image_list, 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" \
@@ -607,14 +620,18 @@ 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 vm["image_list"]:
+                                vm["image_list"] = yaml.safe_load(vm["image_list"])
+                            else:
+                                del vm["image_list"]
+                            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:
@@ -630,9 +647,14 @@ class nfvo_db(db_base.db_base):
                             self.logger.debug(cmd)
                             self.cur.execute(cmd)
                             vm['interfaces'] = self.cur.fetchall()
-                            for index in range(0,len(vm['interfaces'])):
-                                vm['interfaces'][index]['port-security'] = vm['interfaces'][index].pop("port_security")
-                                vm['interfaces'][index]['floating-ip'] = vm['interfaces'][index].pop("floating_ip")
+                            for iface in vm['interfaces']:
+                                iface['port-security'] = iface.pop("port_security")
+                                iface['floating-ip'] = iface.pop("floating_ip")
+                                for sce_interface in vnf["interfaces"]:
+                                    if sce_interface["interface_id"] == iface["uuid"]:
+                                        if sce_interface["ip_address"]:
+                                            iface["ip_address"] = sce_interface["ip_address"]
+                                        break
                         #nets    every net of a vms
                         cmd = "SELECT uuid,name,type,description FROM nets WHERE vnf_id='{}'".format(vnf['vnf_id'])  
                         self.logger.debug(cmd)
@@ -650,7 +672,7 @@ class nfvo_db(db_base.db_base):
                                 raise db_base.db_base_Exception("More than one ip-profile found with this criteria: net_id='{}'".format(vnf_net['uuid']), db_base.HTTP_Bad_Request)
                             
                     #sce_nets
-                    cmd = "SELECT uuid,name,type,external,description" \
+                    cmd = "SELECT uuid,name,type,external,description,vim_network_name" \
                           " FROM sce_nets  WHERE scenario_id='{}'" \
                           " ORDER BY created_at ".format(scenario_dict['uuid'])
                     self.logger.debug(cmd)
@@ -676,7 +698,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:
@@ -684,6 +706,36 @@ class nfvo_db(db_base.db_base):
                     
                     db_base._convert_datetime2str(scenario_dict)
                     db_base._convert_str2boolean(scenario_dict, ('public','shared','external','port-security','floating-ip') )
+
+                    #forwarding graphs
+                    cmd = "SELECT uuid,name,description,vendor FROM sce_vnffgs WHERE scenario_id='{}' "\
+                          "ORDER BY created_at".format(scenario_dict['uuid'])
+                    self.logger.debug(cmd)
+                    self.cur.execute(cmd)
+                    scenario_dict['vnffgs'] = self.cur.fetchall()
+                    for vnffg in scenario_dict['vnffgs']:
+                        cmd = "SELECT uuid,name FROM sce_rsps WHERE sce_vnffg_id='{}' "\
+                              "ORDER BY created_at".format(vnffg['uuid'])
+                        self.logger.debug(cmd)
+                        self.cur.execute(cmd)
+                        vnffg['rsps'] = self.cur.fetchall()
+                        for rsp in vnffg['rsps']:
+                            cmd = "SELECT uuid,if_order,interface_id,sce_vnf_id FROM sce_rsp_hops WHERE sce_rsp_id='{}' "\
+                                  "ORDER BY created_at".format(rsp['uuid'])
+                            self.logger.debug(cmd)
+                            self.cur.execute(cmd)
+                            rsp['connection_points'] = self.cur.fetchall();
+                            cmd = "SELECT uuid,name,sce_vnf_id,interface_id FROM sce_classifiers WHERE sce_vnffg_id='{}' "\
+                                  "AND sce_rsp_id='{}' ORDER BY created_at".format(vnffg['uuid'], rsp['uuid'])
+                            self.logger.debug(cmd)
+                            self.cur.execute(cmd)
+                            rsp['classifier'] = self.cur.fetchone();
+                            cmd = "SELECT uuid,ip_proto,source_ip,destination_ip,source_port,destination_port FROM sce_classifier_matches "\
+                                  "WHERE sce_classifier_id='{}' ORDER BY created_at".format(rsp['classifier']['uuid'])
+                            self.logger.debug(cmd)
+                            self.cur.execute(cmd)
+                            rsp['classifier']['matches'] = self.cur.fetchall()
+
                     return scenario_dict
             except (mdb.Error, AttributeError) as e:
                 self._format_error(e, tries)
@@ -709,9 +761,9 @@ class nfvo_db(db_base.db_base):
                     self.cur.execute(cmd)
                     rows = self.cur.fetchall()
                     if self.cur.rowcount==0:
-                        raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Bad_Request)
+                        raise db_base.db_base_Exception("No scenario found where " + where_text, db_base.HTTP_Not_Found)
                     elif self.cur.rowcount>1:
-                        raise db_base.db_base_Exception("More than one scenario found where " + where_text, db_base.HTTP_Bad_Request)
+                        raise db_base.db_base_Exception("More than one scenario found where " + where_text, db_base.HTTP_Conflict)
                     scenario_uuid = rows[0]["uuid"]
                     scenario_name = rows[0]["name"]
                     
@@ -719,7 +771,7 @@ class nfvo_db(db_base.db_base):
                     cmd = "DELETE FROM scenarios WHERE uuid='{}'".format(scenario_uuid)
                     self.logger.debug(cmd)
                     self.cur.execute(cmd)
-    
+
                     return scenario_uuid + " " + scenario_name
             except (mdb.Error, AttributeError) as e:
                 self._format_error(e, tries, "delete", "instances running")
@@ -727,7 +779,7 @@ class nfvo_db(db_base.db_base):
 
     def new_rows(self, tables, uuid_list=None):
         """
-        Make a transactional insertion of rows at several tables
+        Make a transactional insertion of rows at several tables. Can be also a deletion
         :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 = [
@@ -735,6 +787,10 @@ class nfvo_db(db_base.db_base):
                 {"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
+            If dict contains {"TO-DELETE": uuid} the entry is deleted if exist instead of inserted
         :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
         """
@@ -750,13 +806,20 @@ class nfvo_db(db_base.db_base):
                             if isinstance(row_list, dict):
                                 row_list = (row_list, )  #create a list with the single value
                             for row in row_list:
+                                if "TO-DELETE" in row:
+                                    self._delete_row_by_id_internal(table_name, row["TO-DELETE"])
+                                    continue
+
                                 if table_name in self.tables_with_created_field:
-                                    created_time_param = created_time + index*0.00001
+                                    if "created_at" in row:
+                                        created_time_param = created_time + (index + row.pop("created_at"))*0.00001
+                                    else:
+                                        created_time_param = created_time + index*0.00001
+                                    index += 1
                                 else:
-                                    created_time_param=0
+                                    created_time_param = 0
                                 self._new_row_internal(table_name, row, add_uuid=False, root_uuid=None,
                                                                created_time=created_time_param)
-                                index += 1
                     return
             except (mdb.Error, AttributeError) as e:
                 self._format_error(e, tries)
@@ -888,36 +951,40 @@ class nfvo_db(db_base.db_base):
             try:
                 with self.con:
                     self.cur = self.con.cursor(mdb.cursors.DictCursor)
-                    #instance table
-                    where_list=[]
-                    if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" )
+                    # instance table
+                    where_list = []
+                    if tenant_id:
+                        where_list.append("inst.tenant_id='{}'".format(tenant_id))
                     if db_base._check_valid_uuid(instance_id):
-                        where_list.append( "inst.uuid='" + instance_id +"'" )
+                        where_list.append("inst.uuid='{}'".format(instance_id))
                     else:
-                        where_list.append( "inst.name='" + instance_id +"'" )
+                        where_list.append("inst.name='{}'".format(instance_id))
                     where_text = " AND ".join(where_list)
-                    cmd = "SELECT inst.uuid as uuid,inst.name as name,inst.scenario_id as scenario_id, datacenter_id" +\
-                                " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
-                                " ,inst.description as description,inst.created_at as created_at" +\
-                                " ,inst.cloud_config as 'cloud_config'" +\
-                            " FROM instance_scenarios as inst join scenarios as s on inst.scenario_id=s.uuid"+\
+                    cmd = "SELECT inst.uuid as uuid, inst.name as name, inst.scenario_id as scenario_id, datacenter_id"\
+                                " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" \
+                                " ,inst.description as description, inst.created_at as created_at" \
+                                " ,inst.cloud_config as cloud_config, s.osm_id as nsd_osm_id" \
+                            " FROM instance_scenarios as inst left join scenarios as s on inst.scenario_id=s.uuid" \
                             " WHERE " + where_text
                     self.logger.debug(cmd)
                     self.cur.execute(cmd)
                     rows = self.cur.fetchall()
                     
-                    if self.cur.rowcount==0:
+                    if self.cur.rowcount == 0:
                         raise db_base.db_base_Exception("No instance found where " + where_text, db_base.HTTP_Not_Found)
-                    elif self.cur.rowcount>1:
-                        raise db_base.db_base_Exception("More than one instance found where " + where_text, db_base.HTTP_Bad_Request)
+                    elif self.cur.rowcount > 1:
+                        raise db_base.db_base_Exception("More than one instance found where " + where_text,
+                                                        db_base.HTTP_Bad_Request)
                     instance_dict = rows[0]
                     if instance_dict["cloud_config"]:
                         instance_dict["cloud-config"] = yaml.load(instance_dict["cloud_config"])
                     del instance_dict["cloud_config"]
                     
-                    #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" \
+                    # instance_vnfs
+                    cmd = "SELECT iv.uuid as uuid, iv.vnf_id as vnf_id, sv.name as vnf_name, sce_vnf_id, datacenter_id"\
+                                " ,datacenter_tenant_id, v.mgmt_access, sv.member_vnf_index, v.osm_id as vnfd_osm_id "\
+                            " FROM instance_vnfs as iv left join sce_vnfs as sv "\
+                                "on iv.sce_vnf_id=sv.uuid join vnfs as v on iv.vnf_id=v.uuid" \
                             " WHERE iv.instance_scenario_id='{}'" \
                             " ORDER BY iv.created_at ".format(instance_dict['uuid'])
                     self.logger.debug(cmd)
@@ -926,7 +993,8 @@ class nfvo_db(db_base.db_base):
                     for vnf in instance_dict['vnfs']:
                         vnf_manage_iface_list=[]
                         #instance vms
-                        cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\
+                        cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as "\
+                               "created_at, name, vms.osm_id as vdu_osm_id, vim_name"\
                                 " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
                                 " WHERE instance_vnf_id='{}' ORDER BY iv.created_at".format(vnf['uuid'])
                         self.logger.debug(cmd)
@@ -934,7 +1002,7 @@ class nfvo_db(db_base.db_base):
                         vnf['vms'] = self.cur.fetchall()
                         for vm in vnf['vms']:
                             vm_manage_iface_list=[]
-                            #instance_interfaces
+                            # instance_interfaces
                             cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
                                   " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id"\
                                   " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
@@ -962,7 +1030,47 @@ class nfvo_db(db_base.db_base):
                     self.logger.debug(cmd)
                     self.cur.execute(cmd)
                     instance_dict['nets'] = self.cur.fetchall()
-                    
+
+                    #instance_sfps
+                    cmd = "SELECT uuid,vim_sfp_id,sce_rsp_id,datacenter_id,"\
+                          "datacenter_tenant_id,status,error_msg,vim_info"\
+                            " FROM instance_sfps" \
+                            " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
+                    self.logger.debug(cmd)
+                    self.cur.execute(cmd)
+                    instance_dict['sfps'] = self.cur.fetchall()
+
+                    # for sfp in instance_dict['sfps']:
+                    #instance_sfs
+                    cmd = "SELECT uuid,vim_sf_id,sce_rsp_hop_id,datacenter_id,"\
+                          "datacenter_tenant_id,status,error_msg,vim_info"\
+                            " FROM instance_sfs" \
+                            " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) # TODO: replace instance_scenario_id with instance_sfp_id
+                    self.logger.debug(cmd)
+                    self.cur.execute(cmd)
+                    instance_dict['sfs'] = self.cur.fetchall()
+
+                    #for sf in instance_dict['sfs']:
+                    #instance_sfis
+                    cmd = "SELECT uuid,vim_sfi_id,sce_rsp_hop_id,datacenter_id,"\
+                          "datacenter_tenant_id,status,error_msg,vim_info"\
+                            " FROM instance_sfis" \
+                            " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid']) # TODO: replace instance_scenario_id with instance_sf_id
+                    self.logger.debug(cmd)
+                    self.cur.execute(cmd)
+                    instance_dict['sfis'] = self.cur.fetchall()
+#                            for sfi in instance_dict['sfi']:
+
+                    #instance_classifications
+                    cmd = "SELECT uuid,vim_classification_id,sce_classifier_match_id,datacenter_id,"\
+                          "datacenter_tenant_id,status,error_msg,vim_info"\
+                            " FROM instance_classifications" \
+                            " WHERE instance_scenario_id='{}' ORDER BY created_at".format(instance_dict['uuid'])
+                    self.logger.debug(cmd)
+                    self.cur.execute(cmd)
+                    instance_dict['classifications'] = self.cur.fetchall()
+#                    for classification in instance_dict['classifications']
+
                     db_base._convert_datetime2str(instance_dict)
                     db_base._convert_str2boolean(instance_dict, ('public','shared','created') )
                     return instance_dict