Refactors autoscaling module to separate concerns and allow better control of db connections

Change-Id: I57034d34ae2fbb3c4aeda784f9542e643d348460
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
diff --git a/osm_policy_module/autoscaling/__init__.py b/osm_policy_module/autoscaling/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/osm_policy_module/autoscaling/__init__.py
diff --git a/osm_policy_module/autoscaling/agent.py b/osm_policy_module/autoscaling/agent.py
new file mode 100644
index 0000000..bc12dc7
--- /dev/null
+++ b/osm_policy_module/autoscaling/agent.py
@@ -0,0 +1,148 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# 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.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
+import asyncio
+import logging
+
+import peewee
+
+from osm_policy_module.autoscaling.service import Service
+from osm_policy_module.common.message_bus_client import MessageBusClient
+from osm_policy_module.core.config import Config
+from osm_policy_module.core.database import ScalingAlarm
+
+log = logging.getLogger(__name__)
+
+ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'terminated', 'notify_alarm']
+
+
+class PolicyModuleAgent:
+    def __init__(self, config: Config, loop=None):
+        self.conf = config
+        if not loop:
+            loop = asyncio.get_event_loop()
+        self.loop = loop
+        self.msg_bus = MessageBusClient(config)
+        self.service = Service(config, loop)
+
+    def run(self):
+        self.loop.run_until_complete(self.start())
+
+    async def start(self):
+        topics = [
+            "ns",
+            "alarm_response"
+        ]
+        await self.msg_bus.aioread(topics, self._process_msg)
+        log.critical("Exiting...")
+
+    async def _process_msg(self, topic, key, msg):
+        log.debug("_process_msg topic=%s key=%s msg=%s", topic, key, msg)
+        log.info("Message arrived: %s", msg)
+        try:
+            if key in ALLOWED_KAFKA_KEYS:
+
+                if key == 'instantiated':
+                    await self._handle_instantiated(msg)
+
+                if key == 'scaled':
+                    await self._handle_scaled(msg)
+
+                if key == 'terminated':
+                    await self._handle_terminated(msg)
+
+                if key == 'notify_alarm':
+                    await self._handle_alarm_notification(msg)
+            else:
+                log.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key)
+        except peewee.PeeweeException:
+            log.exception("Database error consuming message: ")
+            raise
+        except Exception:
+            log.exception("Error consuming message: ")
+
+    async def _handle_alarm_notification(self, content):
+        log.debug("_handle_alarm_notification: %s", content)
+        alarm_uuid = content['notify_details']['alarm_uuid']
+        metric_name = content['notify_details']['metric_name']
+        operation = content['notify_details']['operation']
+        threshold = content['notify_details']['threshold_value']
+        vdu_name = content['notify_details']['vdu_name']
+        vnf_member_index = content['notify_details']['vnf_member_index']
+        nsr_id = content['notify_details']['ns_id']
+        log.info(
+            "Received alarm notification for alarm %s, \
+            metric %s, \
+            operation %s, \
+            threshold %s, \
+            vdu_name %s, \
+            vnf_member_index %s, \
+            ns_id %s ",
+            alarm_uuid, metric_name, operation, threshold, vdu_name, vnf_member_index, nsr_id)
+        try:
+            alarm = self.service.get_alarm(alarm_uuid)
+            await self.service.scale(alarm)
+        except ScalingAlarm.DoesNotExist:
+            log.info("There is no action configured for alarm %s.", alarm_uuid)
+
+    async def _handle_instantiated(self, content):
+        log.debug("_handle_instantiated: %s", content)
+        nslcmop_id = content['nslcmop_id']
+        nslcmop = self.service.get_nslcmop(nslcmop_id)
+        if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
+            nsr_id = nslcmop['nsInstanceId']
+            log.info("Configuring scaling groups for network service with nsr_id: %s", nsr_id)
+            await self.service.configure_scaling_groups(nsr_id)
+        else:
+            log.info(
+                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
+                "Current state is %s. Skipping...",
+                nslcmop['operationState'])
+
+    async def _handle_scaled(self, content):
+        log.debug("_handle_scaled: %s", content)
+        nslcmop_id = content['nslcmop_id']
+        nslcmop = self.service.get_nslcmop(nslcmop_id)
+        if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
+            nsr_id = nslcmop['nsInstanceId']
+            log.info("Configuring scaling groups for network service with nsr_id: %s", nsr_id)
+            await self.service.configure_scaling_groups(nsr_id)
+            log.info("Checking for orphaned alarms to be deleted for network service with nsr_id: %s", nsr_id)
+            await self.service.delete_orphaned_alarms(nsr_id)
+        else:
+            log.info(
+                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
+                "Current state is %s. Skipping...",
+                nslcmop['operationState'])
+
+    async def _handle_terminated(self, content):
+        log.debug("_handle_deleted: %s", content)
+        nsr_id = content['nsr_id']
+        if content['operationState'] == 'COMPLETED' or content['operationState'] == 'PARTIALLY_COMPLETED':
+            log.info("Deleting scaling groups and alarms for network service with nsr_id: %s", nsr_id)
+            await self.service.delete_scaling_groups(nsr_id)
+        else:
+            log.info(
+                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
+                "Current state is %s. Skipping...",
+                content['operationState'])
diff --git a/osm_policy_module/core/agent.py b/osm_policy_module/autoscaling/service.py
similarity index 73%
rename from osm_policy_module/core/agent.py
rename to osm_policy_module/autoscaling/service.py
index 65c741f..e1f0704 100644
--- a/osm_policy_module/core/agent.py
+++ b/osm_policy_module/autoscaling/service.py
@@ -26,24 +26,20 @@
 import json
 import logging
 
-import peewee
-
 from osm_policy_module.common.common_db_client import CommonDbClient
 from osm_policy_module.common.lcm_client import LcmClient
-from osm_policy_module.common.message_bus_client import MessageBusClient
 from osm_policy_module.common.mon_client import MonClient
 from osm_policy_module.core import database
 from osm_policy_module.core.config import Config
-from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingPolicy, ScalingCriteria, DatabaseManager
+from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingPolicy, ScalingCriteria
 from osm_policy_module.core.exceptions import VdurNotFound
 from osm_policy_module.utils.vnfd import VnfdUtils
 
 log = logging.getLogger(__name__)
 
-ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'terminated', 'notify_alarm']
 
+class Service:
 
-class PolicyModuleAgent:
     def __init__(self, config: Config, loop=None):
         self.conf = config
         if not loop:
@@ -52,127 +48,11 @@
         self.db_client = CommonDbClient(config)
         self.mon_client = MonClient(config, loop=self.loop)
         self.lcm_client = LcmClient(config, loop=self.loop)
-        self.database_manager = DatabaseManager(config)
-        self.msg_bus = MessageBusClient(config)
 
-    def run(self):
-        self.loop.run_until_complete(self.start())
-
-    async def start(self):
-        topics = [
-            "ns",
-            "alarm_response"
-        ]
-        await self.msg_bus.aioread(topics, self._process_msg)
-        log.critical("Exiting...")
-
-    async def _process_msg(self, topic, key, msg):
-        log.debug("_process_msg topic=%s key=%s msg=%s", topic, key, msg)
-        log.info("Message arrived: %s", msg)
-        try:
-            if key in ALLOWED_KAFKA_KEYS:
-
-                if key == 'instantiated':
-                    await self._handle_instantiated(msg)
-
-                if key == 'scaled':
-                    await self._handle_scaled(msg)
-
-                if key == 'terminated':
-                    await self._handle_terminated(msg)
-
-                if key == 'notify_alarm':
-                    await self._handle_alarm_notification(msg)
-            else:
-                log.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key)
-        except peewee.PeeweeException:
-            log.exception("Database error consuming message: ")
-            raise
-        except Exception:
-            log.exception("Error consuming message: ")
-
-    async def _handle_alarm_notification(self, content):
-        log.debug("_handle_alarm_notification: %s", content)
-        alarm_uuid = content['notify_details']['alarm_uuid']
-        metric_name = content['notify_details']['metric_name']
-        operation = content['notify_details']['operation']
-        threshold = content['notify_details']['threshold_value']
-        vdu_name = content['notify_details']['vdu_name']
-        vnf_member_index = content['notify_details']['vnf_member_index']
-        nsr_id = content['notify_details']['ns_id']
-        log.info(
-            "Received alarm notification for alarm %s, \
-            metric %s, \
-            operation %s, \
-            threshold %s, \
-            vdu_name %s, \
-            vnf_member_index %s, \
-            ns_id %s ",
-            alarm_uuid, metric_name, operation, threshold, vdu_name, vnf_member_index, nsr_id)
-        try:
-            alarm = self.database_manager.get_alarm(alarm_uuid)
-            delta = datetime.datetime.now() - alarm.scaling_criteria.scaling_policy.last_scale
-            log.debug("last_scale: %s", alarm.scaling_criteria.scaling_policy.last_scale)
-            log.debug("now: %s", datetime.datetime.now())
-            log.debug("delta: %s", delta)
-            if delta.total_seconds() < alarm.scaling_criteria.scaling_policy.cooldown_time:
-                log.info("Time between last scale and now is less than cooldown time. Skipping.")
-                return
-            log.info("Sending scaling action message for ns: %s", nsr_id)
-            await self.lcm_client.scale(nsr_id,
-                                        alarm.scaling_criteria.scaling_policy.scaling_group.name,
-                                        alarm.vnf_member_index,
-                                        alarm.action)
-            alarm.scaling_criteria.scaling_policy.last_scale = datetime.datetime.now()
-            alarm.scaling_criteria.scaling_policy.save()
-        except ScalingAlarm.DoesNotExist:
-            log.info("There is no action configured for alarm %s.", alarm_uuid)
-
-    async def _handle_instantiated(self, content):
-        log.debug("_handle_instantiated: %s", content)
-        nslcmop_id = content['nslcmop_id']
-        nslcmop = self.db_client.get_nslcmop(nslcmop_id)
-        if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
-            nsr_id = nslcmop['nsInstanceId']
-            log.info("Configuring scaling groups for network service with nsr_id: %s", nsr_id)
-            await self._configure_scaling_groups(nsr_id)
-        else:
-            log.info(
-                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
-                "Current state is %s. Skipping...",
-                nslcmop['operationState'])
-
-    async def _handle_scaled(self, content):
-        log.debug("_handle_scaled: %s", content)
-        nslcmop_id = content['nslcmop_id']
-        nslcmop = self.db_client.get_nslcmop(nslcmop_id)
-        if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
-            nsr_id = nslcmop['nsInstanceId']
-            log.info("Configuring scaling groups for network service with nsr_id: %s", nsr_id)
-            await self._configure_scaling_groups(nsr_id)
-            log.info("Checking for orphaned alarms to be deleted for network service with nsr_id: %s", nsr_id)
-            await self._delete_orphaned_alarms(nsr_id)
-        else:
-            log.info(
-                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
-                "Current state is %s. Skipping...",
-                nslcmop['operationState'])
-
-    async def _handle_terminated(self, content):
-        log.debug("_handle_deleted: %s", content)
-        nsr_id = content['nsr_id']
-        if content['operationState'] == 'COMPLETED' or content['operationState'] == 'PARTIALLY_COMPLETED':
-            log.info("Deleting scaling groups and alarms for network service with nsr_id: %s", nsr_id)
-            await self._delete_scaling_groups(nsr_id)
-        else:
-            log.info(
-                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
-                "Current state is %s. Skipping...",
-                content['operationState'])
-
-    async def _configure_scaling_groups(self, nsr_id: str):
+    async def configure_scaling_groups(self, nsr_id: str):
         log.debug("_configure_scaling_groups: %s", nsr_id)
         alarms_created = []
+        database.db.connect()
         with database.db.atomic() as tx:
             try:
                 vnfrs = self.db_client.get_vnfrs(nsr_id)
@@ -346,8 +226,10 @@
                                                            alarm.vdu_name,
                                                            alarm.alarm_uuid)
                 raise e
+        database.db.close()
 
-    async def _delete_scaling_groups(self, nsr_id: str):
+    async def delete_scaling_groups(self, nsr_id: str):
+        database.db.connect()
         with database.db.atomic() as tx:
             try:
                 for scaling_group in ScalingGroup.select().where(ScalingGroup.nsr_id == nsr_id):
@@ -371,8 +253,10 @@
                 log.exception("Error deleting scaling groups and alarms:")
                 tx.rollback()
                 raise e
+        database.db.close()
 
-    async def _delete_orphaned_alarms(self, nsr_id):
+    async def delete_orphaned_alarms(self, nsr_id):
+        database.db.connect()
         with database.db.atomic() as tx:
             try:
                 for scaling_group in ScalingGroup.select().where(ScalingGroup.nsr_id == nsr_id):
@@ -397,3 +281,31 @@
                 log.exception("Error deleting orphaned alarms:")
                 tx.rollback()
                 raise e
+        database.db.close()
+
+    async def scale(self, alarm):
+        database.db.connect()
+        with database.db.atomic():
+            delta = datetime.datetime.now() - alarm.scaling_criteria.scaling_policy.last_scale
+            if delta.total_seconds() > alarm.scaling_criteria.scaling_policy.cooldown_time:
+                log.info("Sending scaling action message for ns: %s",
+                         alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id)
+                await self.lcm_client.scale(alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+                                            alarm.scaling_criteria.scaling_policy.scaling_group.name,
+                                            alarm.vnf_member_index,
+                                            alarm.action)
+                alarm.scaling_criteria.scaling_policy.last_scale = datetime.datetime.now()
+                alarm.scaling_criteria.scaling_policy.save()
+            else:
+                log.info("Time between last scale and now is less than cooldown time. Skipping.")
+        database.db.close()
+
+    def get_alarm(self, alarm_uuid: str):
+        database.db.connect()
+        with database.db.atomic():
+            alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()
+        database.db.close()
+        return alarm
+
+    def get_nslcmop(self, nslcmop_id):
+        return self.db_client.get_nslcmop(nslcmop_id)
diff --git a/osm_policy_module/cmd/policy_module_agent.py b/osm_policy_module/cmd/policy_module_agent.py
index af2f602..19e8054 100644
--- a/osm_policy_module/cmd/policy_module_agent.py
+++ b/osm_policy_module/cmd/policy_module_agent.py
@@ -26,7 +26,7 @@
 import logging
 import sys
 
-from osm_policy_module.core.agent import PolicyModuleAgent
+from osm_policy_module.autoscaling.agent import PolicyModuleAgent
 from osm_policy_module.core.config import Config
 from osm_policy_module.core.database import DatabaseManager
 
diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py
index 3ca2e33..ecaa315 100644
--- a/osm_policy_module/core/database.py
+++ b/osm_policy_module/core/database.py
@@ -76,10 +76,8 @@
         db.initialize(connect(config.get('sql', 'database_uri')))
 
     def create_tables(self) -> None:
+        db.connect()
         with db.atomic():
             router = Router(db, os.path.dirname(migrations.__file__))
             router.run()
-
-    def get_alarm(self, alarm_uuid: str):
-        with db.atomic():
-            return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()
+        db.close()
diff --git a/osm_policy_module/tests/integration/test_policy_agent.py b/osm_policy_module/tests/integration/test_policy_agent.py
index 7ad9944..4a7efbb 100644
--- a/osm_policy_module/tests/integration/test_policy_agent.py
+++ b/osm_policy_module/tests/integration/test_policy_agent.py
@@ -23,6 +23,7 @@
 ##
 import asyncio
 import logging
+import os
 import sys
 import unittest
 import uuid
@@ -35,7 +36,7 @@
 from osm_policy_module.common.common_db_client import CommonDbClient
 from osm_policy_module.common.mon_client import MonClient
 from osm_policy_module.core import database
-from osm_policy_module.core.agent import PolicyModuleAgent
+from osm_policy_module.autoscaling.agent import PolicyModuleAgent
 from osm_policy_module.core.config import Config
 from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingPolicy, ScalingCriteria
 
@@ -426,15 +427,17 @@
 class PolicyModuleAgentTest(unittest.TestCase):
     def setUp(self):
         super()
-        database.db.initialize(connect('sqlite://'))
+        database.db.initialize(connect('sqlite:///test_db.sqlite'))
         database.db.bind(MODELS)
         database.db.connect()
         database.db.drop_tables(MODELS)
         database.db.create_tables(MODELS)
+        database.db.close()
         self.loop = asyncio.new_event_loop()
 
     def tearDown(self):
         super()
+        os.remove('test_db.sqlite')
 
     @patch.object(DbMongo, 'db_connect', Mock())
     @patch.object(KafkaProducer, '__init__')
@@ -459,7 +462,7 @@
         create_alarm.side_effect = _test_configure_scaling_groups_create_alarm
         config = Config()
         agent = PolicyModuleAgent(config, self.loop)
-        self.loop.run_until_complete(agent._configure_scaling_groups("test_nsr_id"))
+        self.loop.run_until_complete(agent.service.configure_scaling_groups("test_nsr_id"))
         create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
                                      ns_id='test_nsr_id',
                                      operation='GT',
diff --git a/osm_policy_module/tests/unit/core/test_policy_agent.py b/osm_policy_module/tests/unit/core/test_policy_agent.py
index 7fc2dc9..57b0db1 100644
--- a/osm_policy_module/tests/unit/core/test_policy_agent.py
+++ b/osm_policy_module/tests/unit/core/test_policy_agent.py
@@ -27,20 +27,20 @@
 from unittest import mock
 from unittest.mock import Mock
 
-from osm_policy_module.core.agent import PolicyModuleAgent
+from osm_policy_module.autoscaling.agent import PolicyModuleAgent
+from osm_policy_module.autoscaling.service import Service
 from osm_policy_module.core.config import Config
-from osm_policy_module.core.database import DatabaseManager
 
 
 class PolicyAgentTest(unittest.TestCase):
     def setUp(self):
         self.loop = asyncio.new_event_loop()
 
-    @mock.patch('osm_policy_module.core.agent.CommonDbClient')
-    @mock.patch('osm_policy_module.core.agent.MonClient')
-    @mock.patch('osm_policy_module.core.agent.LcmClient')
-    @mock.patch.object(PolicyModuleAgent, '_configure_scaling_groups')
-    @mock.patch.object(PolicyModuleAgent, '_delete_orphaned_alarms')
+    @mock.patch('osm_policy_module.autoscaling.service.CommonDbClient')
+    @mock.patch('osm_policy_module.autoscaling.service.MonClient')
+    @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
+    @mock.patch.object(Service, 'configure_scaling_groups')
+    @mock.patch.object(Service, 'delete_orphaned_alarms')
     def test_handle_instantiated(self, delete_orphaned_alarms, configure_scaling_groups, lcm_client,
                                  mon_client, db_client):
         async def mock_configure_scaling_groups(nsr_id):
@@ -77,11 +77,12 @@
         self.loop.run_until_complete(agent._handle_instantiated(content))
         configure_scaling_groups.assert_not_called()
 
-    @mock.patch('osm_policy_module.core.agent.CommonDbClient')
-    @mock.patch('osm_policy_module.core.agent.MonClient')
-    @mock.patch('osm_policy_module.core.agent.LcmClient')
-    @mock.patch.object(DatabaseManager, 'get_alarm')
-    def test_handle_alarm_notification(self, get_alarm, lcm_client, mon_client, db_client):
+    @mock.patch('osm_policy_module.autoscaling.service.CommonDbClient')
+    @mock.patch('osm_policy_module.autoscaling.service.MonClient')
+    @mock.patch('osm_policy_module.autoscaling.service.LcmClient')
+    @mock.patch('osm_policy_module.core.database.db')
+    @mock.patch.object(Service, 'get_alarm')
+    def test_handle_alarm_notification(self, get_alarm, db, lcm_client, mon_client, db_client):
         async def mock_scale(nsr_id, scaling_group_name, vnf_member_index, action):
             pass