fault/fm-rest-api/fm/fm/db/sqlalchemy/models.py

151 lines
5.0 KiB
Python
Executable File

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- encoding: utf-8 -*-
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Copyright (c) 2018 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import json
from six.moves.urllib.parse import urlparse
from oslo_config import cfg
from sqlalchemy import Column, ForeignKey, Integer, Boolean
from sqlalchemy import String
from sqlalchemy import DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import TypeDecorator, VARCHAR
from oslo_db.sqlalchemy import models
from sqlalchemy.dialects.mysql import DATETIME
from oslo_log import log
CONF = cfg.CONF
LOG = log.getLogger(__name__)
def table_args():
engine_name = urlparse(cfg.CONF.database_connection).scheme
if engine_name == 'mysql':
return {'mysql_engine': 'InnoDB',
'mysql_charset': "utf8"}
return None
def get_dialect_name():
db_driver_name = 'mysql'
if CONF.database.connection is None:
LOG.error("engine_name is None")
return db_driver_name
engine_name = urlparse(CONF.database.connection).scheme
if engine_name is not None:
db_driver_name = engine_name.split("+", 1)[0]
LOG.info("db_drive_name is %s" % db_driver_name)
return db_driver_name
class JSONEncodedDict(TypeDecorator):
"""Represents an immutable structure as a json-encoded string."""
impl = VARCHAR
def process_bind_param(self, value, dialect):
if value is not None:
value = json.dumps(value)
return value
def process_result_value(self, value, dialect):
if value is not None:
value = json.loads(value)
return value
class FmBase(models.TimestampMixin, models.ModelBase):
metadata = None
def as_dict(self):
d = {}
for c in self.__table__.columns:
d[c.name] = self[c.name]
return d
Base = declarative_base(cls=FmBase)
class Alarm(Base):
__tablename__ = 'alarm'
id = Column(Integer, primary_key=True, nullable=False)
uuid = Column(String(255), unique=True, index=True)
alarm_id = Column('alarm_id', String(255),
ForeignKey('event_suppression.alarm_id'),
nullable=True, index=True)
alarm_state = Column(String(255))
entity_type_id = Column(String(255), index=True)
entity_instance_id = Column(String(255), index=True)
if get_dialect_name() == 'mysql':
timestamp = Column(DATETIME(fsp=6))
else:
timestamp = Column(DateTime(timezone=False))
severity = Column(String(255), index=True)
reason_text = Column(String(255))
alarm_type = Column(String(255), index=True)
probable_cause = Column(String(255))
proposed_repair_action = Column(String(255))
service_affecting = Column(Boolean, default=False)
suppression = Column(Boolean, default=False)
inhibit_alarms = Column(Boolean, default=False)
masked = Column(Boolean, default=False)
class EventLog(Base):
__tablename__ = 'event_log'
id = Column(Integer, primary_key=True, nullable=False)
uuid = Column(String(255), unique=True, index=True)
event_log_id = Column('event_log_id', String(255),
ForeignKey('event_suppression.alarm_id'),
nullable=True, index=True)
state = Column(String(255))
entity_type_id = Column(String(255), index=True)
entity_instance_id = Column(String(255), index=True)
if get_dialect_name() == 'mysql':
timestamp = Column(DATETIME(fsp=6))
else:
timestamp = Column(DateTime(timezone=False))
severity = Column(String(255), index=True)
reason_text = Column(String(255))
event_log_type = Column(String(255), index=True)
probable_cause = Column(String(255))
proposed_repair_action = Column(String(255))
service_affecting = Column(Boolean, default=False)
suppression = Column(Boolean, default=False)
class EventSuppression(Base):
__tablename__ = 'event_suppression'
id = Column('id', Integer, primary_key=True, nullable=False)
uuid = Column('uuid', String(36), unique=True)
alarm_id = Column('alarm_id', String(255), unique=True)
description = Column('description', String(255))
suppression_status = Column('suppression_status', String(255))
set_for_deletion = Column('set_for_deletion', Boolean)
mgmt_affecting = Column('mgmt_affecting', String(255))
degrade_affecting = Column('degrade_affecting', String(255))