Upcoming maintenance: Thursday 21 August @ 12:00-14:00 CEST.

Skip to content
Snippets Groups Projects
Commit 8bd84c7c authored by Waleed Akbar's avatar Waleed Akbar
Browse files

Exception added

parent 58fd2c5a
No related branches found
No related tags found
2 merge requests!294Release TeraFlowSDN 4.0,!207Resolve "(CTTC) Separation of Monitoring"
...@@ -24,5 +24,5 @@ cd $PROJECTDIR/src ...@@ -24,5 +24,5 @@ cd $PROJECTDIR/src
# python3 kpi_manager/tests/test_unitary.py # python3 kpi_manager/tests/test_unitary.py
RCFILE=$PROJECTDIR/coverage/.coveragerc RCFILE=$PROJECTDIR/coverage/.coveragerc
python3 -m pytest --log-level=INFO --log-cli-level=INFO --verbose \ python3 -m pytest --log-level=DEBUG --log-cli-level=DEBUG --verbose \
kpi_manager/database/tests/KpiDBtests.py kpi_manager/tests/test_kpi_db.py
\ No newline at end of file \ No newline at end of file
...@@ -14,10 +14,8 @@ ...@@ -14,10 +14,8 @@
import logging import logging
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, Integer, String, Float, Text, ForeignKey from sqlalchemy import Column, Integer, String, Float, Text
# from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import registry from sqlalchemy.orm import registry
from sqlalchemy.orm import sessionmaker, relationship
from common.proto.kpi_management_pb2 import KpiDescriptor from common.proto.kpi_management_pb2 import KpiDescriptor
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
......
...@@ -12,14 +12,13 @@ ...@@ -12,14 +12,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging, time import logging
from typing import List, Tuple
from sqlalchemy import select, and_
import sqlalchemy_utils import sqlalchemy_utils
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from kpi_manager.database.KpiEngine import KpiEngine from kpi_manager.database.KpiEngine import KpiEngine
from kpi_manager.database.KpiModel import Kpi as KpiModel from kpi_manager.database.KpiModel import Kpi as KpiModel
from common.method_wrappers.ServiceExceptions import (
AlreadyExistsException, OperationFailedException)
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
DB_NAME = "kpi" DB_NAME = "kpi"
...@@ -31,8 +30,6 @@ class KpiDB: ...@@ -31,8 +30,6 @@ class KpiDB:
LOGGER.error('Unable to get SQLAlchemy DB Engine...') LOGGER.error('Unable to get SQLAlchemy DB Engine...')
return False return False
self.db_name = DB_NAME self.db_name = DB_NAME
# self.drop_database(self.db_engine) # added to test
# self.create_database(self.db_engine) # to add database
self.Session = sessionmaker(bind=self.db_engine) self.Session = sessionmaker(bind=self.db_engine)
def create_database(self) -> None: def create_database(self) -> None:
...@@ -50,6 +47,7 @@ class KpiDB: ...@@ -50,6 +47,7 @@ class KpiDB:
LOGGER.debug("Tables created in the DB Name: {:}".format(self.db_name)) LOGGER.debug("Tables created in the DB Name: {:}".format(self.db_name))
except Exception as e: except Exception as e:
LOGGER.debug("Tables cannot be created in the kpi database. {:s}".format(str(e))) LOGGER.debug("Tables cannot be created in the kpi database. {:s}".format(str(e)))
raise OperationFailedException ("Tables can't be created", extra_details=["unable to create table {:}".format(e)])
def verify_tables(self): def verify_tables(self):
try: try:
...@@ -70,10 +68,12 @@ class KpiDB: ...@@ -70,10 +68,12 @@ class KpiDB:
except Exception as e: except Exception as e:
session.rollback() session.rollback()
if "psycopg2.errors.UniqueViolation" in str(e): if "psycopg2.errors.UniqueViolation" in str(e):
LOGGER.debug(f"Unique key voilation: {row.__class__.__name__} table. {str(e)}") LOGGER.error(f"Unique key voilation: {row.__class__.__name__} table. {str(e)}")
raise AlreadyExistsException(row.__class__.__name__, row,
extra_details=["Unique key voilation: {:}".format(e)] )
else: else:
LOGGER.error(f"Failed to insert new row into {row.__class__.__name__} table. {str(e)}") LOGGER.error(f"Failed to insert new row into {row.__class__.__name__} table. {str(e)}")
return False raise OperationFailedException ("Deletion by column id", extra_details=["unable to delete row {:}".format(e)])
finally: finally:
session.close() session.close()
...@@ -90,7 +90,7 @@ class KpiDB: ...@@ -90,7 +90,7 @@ class KpiDB:
except Exception as e: except Exception as e:
session.rollback() session.rollback()
LOGGER.debug(f"Failed to retrieve {model.__name__} ID. {str(e)}") LOGGER.debug(f"Failed to retrieve {model.__name__} ID. {str(e)}")
raise raise OperationFailedException ("search by column id", extra_details=["unable to search row {:}".format(e)])
finally: finally:
session.close() session.close()
...@@ -108,6 +108,7 @@ class KpiDB: ...@@ -108,6 +108,7 @@ class KpiDB:
except Exception as e: except Exception as e:
session.rollback() session.rollback()
LOGGER.error("Error deleting %s with %s %s: %s", model.__name__, col_name, id_to_search, e) LOGGER.error("Error deleting %s with %s %s: %s", model.__name__, col_name, id_to_search, e)
raise OperationFailedException ("Deletion by column id", extra_details=["unable to delete row {:}".format(e)])
finally: finally:
session.close() session.close()
...@@ -148,6 +149,6 @@ class KpiDB: ...@@ -148,6 +149,6 @@ class KpiDB:
return result return result
except Exception as e: except Exception as e:
LOGGER.error(f"Error fetching filtered rows from {model.__name__} table with filters {filter_object} ::: {e}") LOGGER.error(f"Error fetching filtered rows from {model.__name__} table with filters {filter_object} ::: {e}")
return [] raise OperationFailedException ("Select by filter", extra_details=["unable to apply the filter {:}".format(e)])
finally: finally:
session.close() session.close()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment