Fix bug 350 - Sensitive information recorded in openmano.log
[osm/RO.git] / osm_ro / db_base.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 '''
25 Base class for openmano database manipulation
26 '''
27 __author__="Alfonso Tierno"
28 __date__ ="$4-Apr-2016 10:05:01$"
29
30 import MySQLdb as mdb
31 import uuid as myUuid
32 import utils as af
33 import json
34 #import yaml
35 import time
36 import logging
37 import datetime
38 from jsonschema import validate as js_v, exceptions as js_e
39
40 HTTP_Bad_Request = 400
41 HTTP_Unauthorized = 401
42 HTTP_Not_Found = 404
43 HTTP_Method_Not_Allowed = 405
44 HTTP_Request_Timeout = 408
45 HTTP_Conflict = 409
46 HTTP_Service_Unavailable = 503
47 HTTP_Internal_Server_Error = 500
48
49 def _check_valid_uuid(uuid):
50 id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
51 id_schema2 = {"type" : "string", "pattern": "^[a-fA-F0-9]{32}$"}
52 try:
53 js_v(uuid, id_schema)
54 return True
55 except js_e.ValidationError:
56 try:
57 js_v(uuid, id_schema2)
58 return True
59 except js_e.ValidationError:
60 return False
61 return False
62
63 def _convert_datetime2str(var):
64 '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s'
65 It enters recursively in the dict var finding this kind of variables
66 '''
67 if type(var) is dict:
68 for k,v in var.items():
69 if type(v) is datetime.datetime:
70 var[k]= v.strftime('%Y-%m-%dT%H:%M:%S')
71 elif type(v) is dict or type(v) is list or type(v) is tuple:
72 _convert_datetime2str(v)
73 if len(var) == 0: return True
74 elif type(var) is list or type(var) is tuple:
75 for v in var:
76 _convert_datetime2str(v)
77
78 def _convert_bandwidth(data, reverse=False, logger=None):
79 '''Check the field bandwidth recursivelly and when found, it removes units and convert to number
80 It assumes that bandwidth is well formed
81 Attributes:
82 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid
83 'reverse': by default convert form str to int (Mbps), if True it convert from number to units
84 Return:
85 None
86 '''
87 if type(data) is dict:
88 for k in data.keys():
89 if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list:
90 _convert_bandwidth(data[k], reverse, logger)
91 if "bandwidth" in data:
92 try:
93 value=str(data["bandwidth"])
94 if not reverse:
95 pos = value.find("bps")
96 if pos>0:
97 if value[pos-1]=="G": data["bandwidth"] = int(data["bandwidth"][:pos-1]) * 1000
98 elif value[pos-1]=="k": data["bandwidth"]= int(data["bandwidth"][:pos-1]) / 1000
99 else: data["bandwidth"]= int(data["bandwidth"][:pos-1])
100 else:
101 value = int(data["bandwidth"])
102 if value % 1000 == 0: data["bandwidth"]=str(value/1000) + " Gbps"
103 else: data["bandwidth"]=str(value) + " Mbps"
104 except:
105 if logger:
106 logger.error("convert_bandwidth exception for type '%s' data '%s'", type(data["bandwidth"]), data["bandwidth"])
107 return
108 if type(data) is tuple or type(data) is list:
109 for k in data:
110 if type(k) is dict or type(k) is tuple or type(k) is list:
111 _convert_bandwidth(k, reverse, logger)
112
113 def _convert_str2boolean(data, items):
114 '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean
115 Done recursively
116 Attributes:
117 'data': dictionary variable to be checked. None or empty is considered valid
118 'items': tuple of keys to convert
119 Return:
120 None
121 '''
122 if type(data) is dict:
123 for k in data.keys():
124 if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list:
125 _convert_str2boolean(data[k], items)
126 if k in items:
127 if type(data[k]) is str:
128 if data[k]=="false" or data[k]=="False" or data[k]=="0": data[k]=False
129 elif data[k]=="true" or data[k]=="True" or data[k]=="1": data[k]=True
130 elif type(data[k]) is int:
131 if data[k]==0: data[k]=False
132 elif data[k]==1: data[k]=True
133 if type(data) is tuple or type(data) is list:
134 for k in data:
135 if type(k) is dict or type(k) is tuple or type(k) is list:
136 _convert_str2boolean(k, items)
137
138 class db_base_Exception(Exception):
139 '''Common Exception for all database exceptions'''
140
141 def __init__(self, message, http_code=HTTP_Bad_Request):
142 Exception.__init__(self, message)
143 self.http_code = http_code
144
145 class db_base():
146 tables_with_created_field=()
147
148 def __init__(self, host=None, user=None, passwd=None, database=None, log_name='db', log_level=None):
149 self.host = host
150 self.user = user
151 self.passwd = passwd
152 self.database = database
153 self.con = None
154 self.log_level=log_level
155 self.logger = logging.getLogger(log_name)
156 if self.log_level:
157 self.logger.setLevel( getattr(logging, log_level) )
158
159 def connect(self, host=None, user=None, passwd=None, database=None):
160 '''Connect to specific data base.
161 The first time a valid host, user, passwd and database must be provided,
162 Following calls can skip this parameters
163 '''
164 try:
165 if host: self.host = host
166 if user: self.user = user
167 if passwd: self.passwd = passwd
168 if database: self.database = database
169
170 self.con = mdb.connect(self.host, self.user, self.passwd, self.database)
171 self.logger.debug("DB: connected to '%s' at '%s@%s'", self.database, self.user, self.host)
172 except mdb.Error as e:
173 raise db_base_Exception("Cannot connect to DataBase '{}' at '{}@{}' Error {}: {}".format(
174 self.database, self.user, self.host, e.args[0], e.args[1]),
175 http_code = HTTP_Unauthorized )
176
177 def get_db_version(self):
178 ''' Obtain the database schema version.
179 Return: (negative, text) if error or version 0.0 where schema_version table is missing
180 (version_int, version_text) if ok
181 '''
182 cmd = "SELECT version_int,version FROM schema_version"
183 tries = 2
184 while tries:
185 try:
186 with self.con:
187 self.cur = self.con.cursor()
188 self.logger.debug(cmd)
189 self.cur.execute(cmd)
190 rows = self.cur.fetchall()
191 highest_version_int=0
192 highest_version=""
193 for row in rows: #look for the latest version
194 if row[0]>highest_version_int:
195 highest_version_int, highest_version = row[0:2]
196 return highest_version_int, highest_version
197 except (mdb.Error, AttributeError) as e:
198 #self.logger.error("get_db_version DB Exception %d: %s. Command %s",e.args[0], e.args[1], cmd)
199 self._format_error(e, tries)
200 tries -= 1
201
202 def disconnect(self):
203 '''disconnect from specific data base'''
204 try:
205 self.con.close()
206 self.con = None
207 except mdb.Error as e:
208 self.logger.error("while disconnecting from DB: Error %d: %s",e.args[0], e.args[1])
209 return
210 except AttributeError as e: #self.con not defined
211 if e[0][-5:] == "'con'":
212 self.logger.warn("while disconnecting from DB: Error %d: %s",e.args[0], e.args[1])
213 return
214 else:
215 raise
216
217 def _format_error(self, e, tries=1, command=None, extra=None):
218 '''Creates a text error base on the produced exception
219 Params:
220 e: mdb exception
221 retry: in case of timeout, if reconnecting to database and retry, or raise and exception
222 cmd: database command that produce the exception
223 command: if the intention is update or delete
224 extra: extra information to add to some commands
225 Return
226 HTTP error in negative, formatted error text
227 '''
228 if isinstance(e,AttributeError ):
229 raise db_base_Exception("DB Exception " + str(e), HTTP_Internal_Server_Error)
230 if e.args[0]==2006 or e.args[0]==2013 : #MySQL server has gone away (((or))) Exception 2013: Lost connection to MySQL server during query
231 if tries>1:
232 self.logger.warn("DB Exception '%s'. Retry", str(e))
233 #reconnect
234 self.connect()
235 return
236 else:
237 raise db_base_Exception("Database connection timeout Try Again", HTTP_Request_Timeout)
238
239 fk=e.args[1].find("foreign key constraint fails")
240 if fk>=0:
241 if command=="update":
242 raise db_base_Exception("tenant_id '{}' not found.".format(extra), HTTP_Not_Found)
243 elif command=="delete":
244 raise db_base_Exception("Resource is not free. There are {} that prevent deleting it.".format(extra), HTTP_Conflict)
245 de = e.args[1].find("Duplicate entry")
246 fk = e.args[1].find("for key")
247 uk = e.args[1].find("Unknown column")
248 wc = e.args[1].find("in 'where clause'")
249 fl = e.args[1].find("in 'field list'")
250 #print de, fk, uk, wc,fl
251 if de>=0:
252 if fk>=0: #error 1062
253 raise db_base_Exception("Value {} already in use for {}".format(e.args[1][de+15:fk], e.args[1][fk+7:]), HTTP_Conflict)
254 if uk>=0:
255 if wc>=0:
256 raise db_base_Exception("Field {} can not be used for filtering".format(e.args[1][uk+14:wc]), HTTP_Bad_Request)
257 if fl>=0:
258 raise db_base_Exception("Field {} does not exist".format(e.args[1][uk+14:wc]), HTTP_Bad_Request)
259 raise db_base_Exception("Database internal Error {}: {}".format(e.args[0], e.args[1]), HTTP_Internal_Server_Error)
260
261 def __str2db_format(self, data):
262 '''Convert string data to database format.
263 If data is None it returns the 'Null' text,
264 otherwise it returns the text surrounded by quotes ensuring internal quotes are escaped.
265 '''
266 if data==None:
267 return 'Null'
268 elif isinstance(data[1], str):
269 return json.dumps(data)
270 else:
271 return json.dumps(str(data))
272
273 def __tuple2db_format_set(self, data):
274 """Compose the needed text for a SQL SET, parameter 'data' is a pair tuple (A,B),
275 and it returns the text 'A="B"', where A is a field of a table and B is the value
276 If B is None it returns the 'A=Null' text, without surrounding Null by quotes
277 If B is not None it returns the text "A='B'" or 'A="B"' where B is surrounded by quotes,
278 and it ensures internal quotes of B are escaped.
279 B can be also a dict with special keys:
280 {"INCREMENT": NUMBER}, then it produce "A=A+NUMBER"
281 """
282 if data[1] == None:
283 return str(data[0]) + "=Null"
284 elif isinstance(data[1], str):
285 return str(data[0]) + '=' + json.dumps(data[1])
286 elif isinstance(data[1], dict):
287 if "INCREMENT" in data[1]:
288 return "{A}={A}{N:+d}".format(A=data[0], N=data[1]["INCREMENT"])
289 raise db_base_Exception("Format error for UPDATE field")
290 else:
291 return str(data[0]) + '=' + json.dumps(str(data[1]))
292
293 def __tuple2db_format_where(self, data):
294 '''Compose the needed text for a SQL WHERE, parameter 'data' is a pair tuple (A,B),
295 and it returns the text 'A="B"', where A is a field of a table and B is the value
296 If B is None it returns the 'A is Null' text, without surrounding Null by quotes
297 If B is not None it returns the text "A='B'" or 'A="B"' where B is surrounded by quotes,
298 and it ensures internal quotes of B are escaped.
299 '''
300 if data[1]==None:
301 return str(data[0]) + " is Null"
302 elif isinstance(data[1], str):
303 return str(data[0]) + '=' + json.dumps(data[1])
304 else:
305 return str(data[0]) + '=' + json.dumps(str(data[1]))
306
307 def __tuple2db_format_where_not(self, data):
308 '''Compose the needed text for a SQL WHERE(not). parameter 'data' is a pair tuple (A,B),
309 and it returns the text 'A<>"B"', where A is a field of a table and B is the value
310 If B is None it returns the 'A is not Null' text, without surrounding Null by quotes
311 If B is not None it returns the text "A<>'B'" or 'A<>"B"' where B is surrounded by quotes,
312 and it ensures internal quotes of B are escaped.
313 '''
314 if data[1]==None:
315 return str(data[0]) + " is not Null"
316 elif isinstance(data[1], str):
317 return str(data[0]) + '<>' + json.dumps(data[1])
318 else:
319 return str(data[0]) + '<>' + json.dumps(str(data[1]))
320
321 def __remove_quotes(self, data):
322 '''remove single quotes ' of any string content of data dictionary'''
323 for k,v in data.items():
324 if type(v) == str:
325 if "'" in v:
326 data[k] = data[k].replace("'","_")
327
328 def _update_rows(self, table, UPDATE, WHERE, modified_time=0):
329 ''' Update one or several rows into a table.
330 Atributes
331 UPDATE: dictionary with the key: value to change
332 table: table where to update
333 WHERE: dictionary of elements to update
334 Return: the number of updated rows, exception if error
335 '''
336 #gettting uuid
337 values = ",".join(map(self.__tuple2db_format_set, UPDATE.iteritems() ))
338 if modified_time:
339 values += ",modified_at={:f}".format(modified_time)
340 cmd= "UPDATE " + table +" SET " + values +\
341 " WHERE " + " and ".join(map(self.__tuple2db_format_where, WHERE.iteritems() ))
342 self.logger.debug(cmd)
343 self.cur.execute(cmd)
344 return self.cur.rowcount
345
346 def _new_uuid(self, root_uuid=None, used_table=None, created_time=0):
347 """
348 Generate a new uuid. It DOES NOT begin or end the transaction, so self.con.cursor must be created
349 :param root_uuid: master uuid of the transaction
350 :param used_table: the table this uuid is intended for
351 :param created_time: time of creation
352 :return: the created uuid
353 """
354
355 uuid = str(myUuid.uuid1())
356 # defining root_uuid if not provided
357 if root_uuid is None:
358 root_uuid = uuid
359 if created_time:
360 created_at = created_time
361 else:
362 created_at = time.time()
363 # inserting new uuid
364 cmd = "INSERT INTO uuids (uuid, root_uuid, used_at, created_at) VALUES ('{:s}','{:s}','{:s}', {:f})".format(
365 uuid, root_uuid, used_table, created_at)
366 self.logger.debug(cmd)
367 self.cur.execute(cmd)
368 return uuid
369
370 def _new_row_internal(self, table, INSERT, add_uuid=False, root_uuid=None, created_time=0, confidential_data=False):
371 ''' Add one row into a table. It DOES NOT begin or end the transaction, so self.con.cursor must be created
372 Attribute
373 INSERT: dictionary with the key:value to insert
374 table: table where to insert
375 add_uuid: if True, it will create an uuid key entry at INSERT if not provided
376 created_time: time to add to the created_time column
377 It checks presence of uuid and add one automatically otherwise
378 Return: uuid
379 '''
380
381 if add_uuid:
382 #create uuid if not provided
383 if 'uuid' not in INSERT:
384 uuid = INSERT['uuid'] = str(myUuid.uuid1()) # create_uuid
385 else:
386 uuid = str(INSERT['uuid'])
387 else:
388 uuid=None
389 if add_uuid:
390 #defining root_uuid if not provided
391 if root_uuid is None:
392 root_uuid = uuid
393 if created_time:
394 created_at = created_time
395 else:
396 created_at=time.time()
397 #inserting new uuid
398 cmd = "INSERT INTO uuids (uuid, root_uuid, used_at, created_at) VALUES ('{:s}','{:s}','{:s}', {:f})".format(uuid, root_uuid, table, created_at)
399 self.logger.debug(cmd)
400 self.cur.execute(cmd)
401 #insertion
402 cmd= "INSERT INTO " + table +" SET " + \
403 ",".join(map(self.__tuple2db_format_set, INSERT.iteritems() ))
404 if created_time:
405 cmd += ",created_at=%f" % created_time
406 if confidential_data:
407 index = cmd.find("SET")
408 subcmd = cmd[:index] + 'SET...'
409 self.logger.debug(subcmd)
410 else:
411 self.logger.debug(cmd)
412 self.cur.execute(cmd)
413 self.cur.rowcount
414 return uuid
415
416 def _get_rows(self,table,uuid):
417 cmd = "SELECT * FROM {} WHERE uuid='{}'".format(str(table), str(uuid))
418 self.logger.debug(cmd)
419 self.cur.execute(cmd)
420 rows = self.cur.fetchall()
421 return rows
422
423 def new_row(self, table, INSERT, add_uuid=False, created_time=0, confidential_data=False):
424 ''' Add one row into a table.
425 Attribute
426 INSERT: dictionary with the key: value to insert
427 table: table where to insert
428 tenant_id: only useful for logs. If provided, logs will use this tenant_id
429 add_uuid: if True, it will create an uuid key entry at INSERT if not provided
430 It checks presence of uuid and add one automatically otherwise
431 Return: (result, uuid) where result can be 0 if error, or 1 if ok
432 '''
433 if table in self.tables_with_created_field and created_time==0:
434 created_time=time.time()
435 tries = 2
436 while tries:
437 try:
438 with self.con:
439 self.cur = self.con.cursor()
440 return self._new_row_internal(table, INSERT, add_uuid, None, created_time, confidential_data)
441
442 except (mdb.Error, AttributeError) as e:
443 self._format_error(e, tries)
444 tries -= 1
445
446 def update_rows(self, table, UPDATE, WHERE, modified_time=0):
447 ''' Update one or several rows into a table.
448 Atributes
449 UPDATE: dictionary with the key: value to change
450 table: table where to update
451 WHERE: dictionary of elements to update
452 Return: (result, descriptive text) where result indicates the number of updated files
453 '''
454 if table in self.tables_with_created_field and modified_time==0:
455 modified_time=time.time()
456 tries = 2
457 while tries:
458 try:
459 with self.con:
460 self.cur = self.con.cursor()
461 return self._update_rows(table, UPDATE, WHERE)
462
463 except (mdb.Error, AttributeError) as e:
464 self._format_error(e, tries)
465 tries -= 1
466
467 def delete_row_by_id(self, table, uuid):
468 tries = 2
469 while tries:
470 try:
471 with self.con:
472 #delete host
473 self.cur = self.con.cursor()
474 cmd = "DELETE FROM {} WHERE uuid = '{}'".format(table, uuid)
475 self.logger.debug(cmd)
476 self.cur.execute(cmd)
477 deleted = self.cur.rowcount
478 if deleted:
479 #delete uuid
480 self.cur = self.con.cursor()
481 cmd = "DELETE FROM uuids WHERE root_uuid = '{}'".format(uuid)
482 self.logger.debug(cmd)
483 self.cur.execute(cmd)
484 return deleted
485 except (mdb.Error, AttributeError) as e:
486 self._format_error(e, tries, "delete", "dependencies")
487 tries -= 1
488
489 def delete_row(self, **sql_dict):
490 ''' Deletes rows from a table.
491 Attribute sql_dir: dictionary with the following key: value
492 'FROM': string of table name (Mandatory)
493 'WHERE': dict of key:values, translated to key=value AND ... (Optional)
494 'WHERE_NOT': dict of key:values, translated to key<>value AND ... (Optional)
495 if value is None, it is translated to key is not null
496 'LIMIT': limit of number of rows (Optional)
497 Return: the number of deleted or exception if error
498 '''
499 #print sql_dict
500 from_ = "FROM " + str(sql_dict['FROM'])
501 #print 'from_', from_
502 if 'WHERE' in sql_dict and len(sql_dict['WHERE']) > 0:
503 w=sql_dict['WHERE']
504 where_ = "WHERE " + " AND ".join(map(self.__tuple2db_format_where, w.iteritems()))
505 else: where_ = ""
506 if 'WHERE_NOT' in sql_dict and len(sql_dict['WHERE_NOT']) > 0:
507 w=sql_dict['WHERE_NOT']
508 where_2 = " AND ".join(map(self.__tuple2db_format_where_not, w.iteritems()))
509 if len(where_)==0: where_ = "WHERE " + where_2
510 else: where_ = where_ + " AND " + where_2
511 #print 'where_', where_
512 limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else ""
513 #print 'limit_', limit_
514 cmd = " ".join( ("DELETE", from_, where_, limit_) )
515 tries = 2
516 while tries:
517 try:
518 with self.con:
519 self.cur = self.con.cursor()
520 self.logger.debug(cmd)
521 self.cur.execute(cmd)
522 deleted = self.cur.rowcount
523 return deleted
524 except (mdb.Error, AttributeError) as e:
525 self._format_error(e, tries)
526 tries -= 1
527
528 def get_rows_by_id(self, table, uuid):
529 '''get row from a table based on uuid'''
530 tries = 2
531 while tries:
532 try:
533 with self.con:
534 self.cur = self.con.cursor(mdb.cursors.DictCursor)
535 cmd="SELECT * FROM {} where uuid='{}'".format(str(table), str(uuid))
536 self.logger.debug(cmd)
537 self.cur.execute(cmd)
538 rows = self.cur.fetchall()
539 return rows
540 except (mdb.Error, AttributeError) as e:
541 self._format_error(e, tries)
542 tries -= 1
543
544 def get_rows(self, **sql_dict):
545 ''' Obtain rows from a table.
546 Attribute sql_dir: dictionary with the following key: value
547 'SELECT': list or tuple of fields to retrieve) (by default all)
548 'FROM': string of table name (Mandatory)
549 'WHERE': dict of key:values, translated to key=value (key is null) AND ... (Optional)
550 'WHERE_NOT': dict of key:values, translated to key<>value (key is not null) AND ... (Optional)
551 'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional)
552 'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional)
553 'LIMIT': limit of number of rows (Optional)
554 'ORDER_BY': list or tuple of fields to order, add ' DESC' to each item if inverse order is required
555 Return: a list with dictionaries at each row
556 '''
557 #print sql_dict
558 select_= "SELECT " + ("*" if 'SELECT' not in sql_dict else ",".join(map(str,sql_dict['SELECT'])) )
559 #print 'select_', select_
560 from_ = "FROM " + str(sql_dict['FROM'])
561 #print 'from_', from_
562 where_and = ""
563 where_or = ""
564 w=sql_dict.get('WHERE')
565 if w:
566 where_and = " AND ".join(map(self.__tuple2db_format_where, w.iteritems() ))
567 w=sql_dict.get('WHERE_NOT')
568 if w:
569 if where_and: where_and += " AND "
570 where_and += " AND ".join(map(self.__tuple2db_format_where_not, w.iteritems() ) )
571 w=sql_dict.get('WHERE_OR')
572 if w:
573 where_or = " OR ".join(map(self.__tuple2db_format_where, w.iteritems() ))
574 if where_and and where_or:
575 if sql_dict.get("WHERE_AND_OR") == "AND":
576 where_ = "WHERE " + where_and + " AND (" + where_or + ")"
577 else:
578 where_ = "WHERE (" + where_and + ") OR " + where_or
579 elif where_and and not where_or:
580 where_ = "WHERE " + where_and
581 elif not where_and and where_or:
582 where_ = "WHERE " + where_or
583 else:
584 where_ = ""
585 #print 'where_', where_
586 limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else ""
587 order_ = "ORDER BY " + ",".join(map(str,sql_dict['ORDER_BY'])) if 'ORDER_BY' in sql_dict else ""
588
589 #print 'limit_', limit_
590 cmd = " ".join( (select_, from_, where_, limit_, order_) )
591 tries = 2
592 while tries:
593 try:
594 with self.con:
595 self.cur = self.con.cursor(mdb.cursors.DictCursor)
596 self.logger.debug(cmd)
597 self.cur.execute(cmd)
598 rows = self.cur.fetchall()
599 return rows
600 except (mdb.Error, AttributeError) as e:
601 self._format_error(e, tries)
602 tries -= 1
603
604 def get_table_by_uuid_name(self, table, uuid_name, error_item_text=None, allow_serveral=False, WHERE_OR={}, WHERE_AND_OR="OR"):
605 ''' Obtain One row from a table based on name or uuid.
606 Attribute:
607 table: string of table name
608 uuid_name: name or uuid. If not uuid format is found, it is considered a name
609 allow_severeral: if False return ERROR if more than one row are founded
610 error_item_text: in case of error it identifies the 'item' name for a proper output text
611 'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional)
612 'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional
613 Return: if allow_several==False, a dictionary with this row, or error if no item is found or more than one is found
614 if allow_several==True, a list of dictionaries with the row or rows, error if no item is found
615 '''
616
617 if error_item_text==None:
618 error_item_text = table
619 what = 'uuid' if af.check_valid_uuid(uuid_name) else 'name'
620 cmd = " SELECT * FROM {} WHERE {}='{}'".format(table, what, uuid_name)
621 if WHERE_OR:
622 where_or = " OR ".join(map(self.__tuple2db_format_where, WHERE_OR.iteritems() ))
623 if WHERE_AND_OR == "AND":
624 cmd += " AND (" + where_or + ")"
625 else:
626 cmd += " OR " + where_or
627
628
629 tries = 2
630 while tries:
631 try:
632 with self.con:
633 self.cur = self.con.cursor(mdb.cursors.DictCursor)
634 self.logger.debug(cmd)
635 self.cur.execute(cmd)
636 number = self.cur.rowcount
637 if number==0:
638 return -HTTP_Not_Found, "No %s found with %s '%s'" %(error_item_text, what, uuid_name)
639 elif number>1 and not allow_serveral:
640 return -HTTP_Bad_Request, "More than one %s found with %s '%s'" %(error_item_text, what, uuid_name)
641 if allow_serveral:
642 rows = self.cur.fetchall()
643 else:
644 rows = self.cur.fetchone()
645 return rows
646 except (mdb.Error, AttributeError) as e:
647 self._format_error(e, tries)
648 tries -= 1
649
650 def get_uuid(self, uuid):
651 '''check in the database if this uuid is already present'''
652 for retry_ in range(0,2):
653 try:
654 with self.con:
655 self.cur = self.con.cursor(mdb.cursors.DictCursor)
656 self.cur.execute("SELECT * FROM uuids where uuid='" + str(uuid) + "'")
657 rows = self.cur.fetchall()
658 return self.cur.rowcount, rows
659 except (mdb.Error, AttributeError) as e:
660 print "nfvo_db.get_uuid DB Exception %d: %s" % (e.args[0], e.args[1])
661 r,c = self._format_error(e)
662 if r!=-HTTP_Request_Timeout or retry_==1: return r,c
663
664 def get_uuid_from_name(self, table, name):
665 '''Searchs in table the name and returns the uuid
666 '''
667 tries = 2
668 while tries:
669 try:
670 with self.con:
671 self.cur = self.con.cursor(mdb.cursors.DictCursor)
672 where_text = "name='" + name +"'"
673 self.cur.execute("SELECT * FROM " + table + " WHERE "+ where_text)
674 rows = self.cur.fetchall()
675 if self.cur.rowcount==0:
676 return 0, "Name %s not found in table %s" %(name, table)
677 elif self.cur.rowcount>1:
678 return self.cur.rowcount, "More than one VNF with name %s found in table %s" %(name, table)
679 return self.cur.rowcount, rows[0]["uuid"]
680 except (mdb.Error, AttributeError) as e:
681 self._format_error(e, tries)
682 tries -= 1
683