X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=vim_db.py;h=c34160dddb8316dda68c99d908c842b55c713f8c;hb=refs%2Fchanges%2F17%2F1317%2F3;hp=cdc3dcce3bfca31050c256d0be524a7e29d554d4;hpb=e9317ff999987c09369b9eab6bcd044ced8f1048;p=osm%2Fopenvim.git diff --git a/vim_db.py b/vim_db.py index cdc3dcc..c34160d 100644 --- a/vim_db.py +++ b/vim_db.py @@ -50,7 +50,7 @@ HTTP_Internal_Server_Error = 500 class vim_db(): - def __init__(self, vlan_range, debug="ERROR"): + def __init__(self, vlan_range, logger_name= None, debug=None): '''vlan_range must be a tuple (vlan_ini, vlan_end) with available vlan values for networks every dataplane network contain a unique value, regardless of it is used or not ''' @@ -59,8 +59,13 @@ class vim_db(): self.net_vlan_usedlist = None self.net_vlan_lastused = self.net_vlan_range[0] -1 self.debug=debug - self.logger = logging.getLogger('vim.db') - self.logger.setLevel( getattr(logging, debug) ) + if logger_name: + self.logger_name = logger_name + else: + self.logger_name = 'openvim.db' + self.logger = logging.getLogger(self.logger_name) + if debug: + self.logger.setLevel( getattr(logging, debug) ) def connect(self, host=None, user=None, passwd=None, database=None): @@ -153,7 +158,7 @@ class vim_db(): return -HTTP_Conflict, "Value %s already in use for %s" % (e.args[1][de+15:fk], e.args[1][fk+7:]) if uk>=0: if wc>=0: - return -HTTP_Bad_Request, "Field %s can not be used for filtering" % e.args[1][uk+14:wc] + return -HTTP_Bad_Request, "Field %s cannot be used for filtering" % e.args[1][uk+14:wc] if fl>=0: return -HTTP_Bad_Request, "Field %s does not exist" % e.args[1][uk+14:wc] return -HTTP_Internal_Server_Error, "Database internal Error %d: %s" % (e.args[0], e.args[1]) @@ -217,33 +222,41 @@ class vim_db(): 'WHERE': dict of key:values, translated to key=value AND ... (Optional) 'WHERE_NOT': dict of key:values, translated to key!=value AND ... (Optional) 'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional) + 'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional) 'LIMIT': limit of number of rows (Optional) + 'DISTINCT': make a select distinct to remove repeated elements Return: a list with dictionarys at each row ''' #print sql_dict - select_= "SELECT " + ("*" if 'SELECT' not in sql_dict else ",".join(map(str,sql_dict['SELECT'])) ) + select_ = "SELECT " + if sql_dict.get("DISTINCT"): + select_ += "DISTINCT " + select_ += ("*" if not sql_dict.get('SELECT') else ",".join(map(str,sql_dict['SELECT'])) ) #print 'select_', select_ from_ = "FROM " + str(sql_dict['FROM']) #print 'from_', from_ where_and = None where_or = None - if 'WHERE' in sql_dict and len(sql_dict['WHERE']) > 0: - w=sql_dict['WHERE'] - where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) - if 'WHERE_NOT' in sql_dict and len(sql_dict['WHERE_NOT']) > 0: - w=sql_dict['WHERE_NOT'] - where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE') + if w: + where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE_NOT') + if w: + where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) if where_and: where_and += " AND " + where_and_not else: where_and = where_and_not - if 'WHERE_OR' in sql_dict and len(sql_dict['WHERE_OR']) > 0: - w=sql_dict['WHERE_OR'] + w = sql_dict.get('WHERE_OR') + if w: where_or = " OR ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) if where_and!=None and where_or!=None: - where_ = "WHERE (" + where_and + ") OR " + where_or + if sql_dict.get("WHERE_AND_OR") == "AND": + where_ = "WHERE " + where_and + " AND (" + where_or + ")" + else: + where_ = "WHERE (" + where_and + ") OR " + where_or elif where_and!=None and where_or==None: where_ = "WHERE " + where_and elif where_and==None and where_or!=None: @@ -251,7 +264,7 @@ class vim_db(): else: where_ = "" #print 'where_', where_ - limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else "" + limit_ = "LIMIT " + str(sql_dict['LIMIT']) if sql_dict.get("LIMIT") else "" #print 'limit_', limit_ cmd = " ".join( (select_, from_, where_, limit_) ) for retry_ in range(0,2): @@ -1168,7 +1181,7 @@ class vim_db(): self.cur = self.con.cursor() match_found = False if len(valid_hosts)<=0: - error_text = 'No room at data center. Can not find a host with %s MB memory and %s cpus available' % (str(requirements['ram']), str(requirements['vcpus'])) + error_text = 'No room at data center. Cannot find a host with %s MB memory and %s cpus available' % (str(requirements['ram']), str(requirements['vcpus'])) #self.logger.debug(error_text) return -1, error_text @@ -1182,7 +1195,7 @@ class vim_db(): self.cur.close() self.cur = self.con.cursor() if len(valid_for_memory)<=0: - error_text = 'No room at data center. Can not find a host with %s GB Hugepages memory available' % str(requirements['numa']['memory']) + error_text = 'No room at data center. Cannot find a host with %s GB Hugepages memory available' % str(requirements['numa']['memory']) #self.logger.debug(error_text) return -1, error_text @@ -1202,7 +1215,7 @@ class vim_db(): self.cur.close() self.cur = self.con.cursor() if len(valid_for_processor)<=0: - error_text = 'No room at data center. Can not find a host with %s %s available' % (str(requirements['numa']['proc_req_nb']),cpu_requirement_text) + error_text = 'No room at data center. Cannot find a host with %s %s available' % (str(requirements['numa']['proc_req_nb']),cpu_requirement_text) #self.logger.debug(error_text) return -1, error_text @@ -1229,7 +1242,7 @@ class vim_db(): else: valid_numas.append(m_numa['numa_id']) if len(valid_numas)<=0: - error_text = 'No room at data center. Can not find a host with %s MB hugepages memory and %s %s available in the same numa' %\ + error_text = 'No room at data center. Cannot find a host with %s MB hugepages memory and %s %s available in the same numa' %\ (requirements['numa']['memory'], str(requirements['numa']['proc_req_nb']),cpu_requirement_text) #self.logger.debug(error_text) return -1, error_text @@ -1342,7 +1355,7 @@ class vim_db(): break if not match_found: - error_text = 'No room at data center. Can not find a host with the required hugepages, vcpus and interfaces' + error_text = 'No room at data center. Cannot find a host with the required hugepages, vcpus and interfaces' #self.logger.debug(error_text) return -1, error_text @@ -1701,9 +1714,9 @@ class vim_db(): if net['tenant_id']==tenant_id and net['shared']=='false': return -1, "needed admin privileges to attach to the net %s" % net_id #check types - if (net['type'] in ('p2p','data') and port_type != 'instance:data') or \ + if (net['type'] in ('ptp','data') and port_type not in ('instance:data','external')) or \ (net['type'] in ('bridge_data','bridge_man') and port_type not in ('instance:bridge', 'instance:ovs')): - return -1, "can not attach a port of type %s into a net of type %s" % (port_type, net['type']) + return -1, "Cannot attach a port of type %s into a net of type %s" % (port_type, net['type']) if net['type'] == 'ptp': #look how many nb_ports, data = self.get_ports( {'net_id':net_id} )