Policy Module first commit
[osm/MON.git] / policy_module / osm_policy_module / core / database.py
diff --git a/policy_module/osm_policy_module/core/database.py b/policy_module/osm_policy_module/core/database.py
new file mode 100644 (file)
index 0000000..4fb95e7
--- /dev/null
@@ -0,0 +1,38 @@
+import logging
+
+from peewee import *
+from playhouse.sqlite_ext import SqliteExtDatabase
+
+from osm_policy_module.core.config import Config
+
+log = logging.getLogger(__name__)
+cfg = Config.instance()
+
+db = SqliteExtDatabase('mon.db')
+
+
+class BaseModel(Model):
+    class Meta:
+        database = db
+
+
+class ScalingRecord(BaseModel):
+    nsr_id = CharField()
+    name = CharField()
+    content = TextField()
+
+
+class ScalingAlarm(BaseModel):
+    alarm_id = CharField()
+    action = CharField()
+    scaling_record = ForeignKeyField(ScalingRecord, related_name='scaling_alarms')
+
+
+class DatabaseManager:
+    def create_tables(self):
+        try:
+            db.connect()
+            db.create_tables([ScalingRecord, ScalingAlarm])
+            db.close()
+        except Exception as e:
+            log.exception("Error creating tables: ")