Refactor scaling group config and adds partial support for vnf metrics 09/6909/2
authorBenjamin Diaz <bdiaz@whitestack.com>
Fri, 16 Nov 2018 22:46:50 +0000 (19:46 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Fri, 16 Nov 2018 23:04:56 +0000 (20:04 -0300)
Adds support for scaling groups based on vdu-metric corresponding to a juju
metric. It also includes modifications to support MON modifications regarding
vnf-monitoring-params.

Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Change-Id: I1a1604f965c077fa14a19ad7a26da0facc4818ba

osm_policy_module/__init__.py
osm_policy_module/cmd/__init__.py
osm_policy_module/common/__init__.py
osm_policy_module/core/__init__.py
osm_policy_module/core/agent.py
osm_policy_module/core/database.py
osm_policy_module/tests/__init__.py
osm_policy_module/tests/integration/__init__.py
osm_policy_module/tests/integration/test_policy_agent.py
osm_policy_module/tests/unit/__init__.py
osm_policy_module/tests/unit/test_policy_agent.py

index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index 24f1004..8a1a31c 100644 (file)
@@ -35,7 +35,7 @@ from osm_policy_module.common.lcm_client import LcmClient
 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
+from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingPolicy, ScalingCriteria, DatabaseManager
 
 log = logging.getLogger(__name__)
 
@@ -53,6 +53,7 @@ class PolicyModuleAgent:
         self.lcm_client = LcmClient(loop=self.loop)
         self.kafka_server = '{}:{}'.format(cfg.OSMPOL_MESSAGE_HOST,
                                            cfg.OSMPOL_MESSAGE_PORT)
+        self.database_manager = DatabaseManager()
 
     def run(self):
         self.loop.run_until_complete(self.start())
@@ -112,7 +113,7 @@ class PolicyModuleAgent:
             ns_id %s ",
             alarm_id, metric_name, operation, threshold, vdu_name, vnf_member_index, ns_id)
         try:
-            alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
+            alarm = self.database_manager.get_alarm(alarm_id)
             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())
@@ -121,7 +122,7 @@ class PolicyModuleAgent:
                 log.info("Time between last scale and now is less than cooldown time. Skipping.")
                 return
             log.info("Sending scaling action message for ns: %s", alarm_id)
-            await self.lcm_client.scale(alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+            await self.lcm_client.scale(ns_id,
                                         alarm.scaling_criteria.scaling_policy.scaling_group.name,
                                         alarm.vnf_member_index,
                                         alarm.action)
@@ -148,158 +149,164 @@ class PolicyModuleAgent:
         log.debug("_configure_scaling_groups: %s", nsr_id)
         # TODO: Add support for non-nfvi metrics
         alarms_created = []
-        with database.db.atomic():
+        with database.db.atomic() as tx:
             try:
-                with database.db.atomic():
-                    vnfrs = self.db_client.get_vnfrs(nsr_id)
-                    log.info("Found %s vnfrs", len(vnfrs))
-                    for vnfr in vnfrs:
-                        vnfd = self.db_client.get_vnfd(vnfr['vnfd-id'])
-                        log.info("Looking for vnfd %s", vnfr['vnfd-id'])
-                        scaling_groups = vnfd['scaling-group-descriptor']
-                        vnf_monitoring_params = vnfd['monitoring-param']
-                        for scaling_group in scaling_groups:
+                vnfrs = self.db_client.get_vnfrs(nsr_id)
+                for vnfr in vnfrs:
+                    log.info("Processing vnfr: %s", vnfr)
+                    vnfd = self.db_client.get_vnfd(vnfr['vnfd-id'])
+                    log.info("Looking for vnfd %s", vnfr['vnfd-id'])
+                    if 'scaling-group-descriptor' not in vnfd:
+                        continue
+                    scaling_groups = vnfd['scaling-group-descriptor']
+                    vnf_monitoring_params = vnfd['monitoring-param']
+                    for scaling_group in scaling_groups:
+                        try:
+                            scaling_group_record = ScalingGroup.select().where(
+                                ScalingGroup.nsr_id == nsr_id,
+                                ScalingGroup.vnf_member_index == int(vnfr['member-vnf-index-ref']),
+                                ScalingGroup.name == scaling_group['name']
+                            ).get()
+                            log.info("Found existing scaling group record in DB...")
+                        except ScalingGroup.DoesNotExist:
+                            log.info("Creating scaling group record in DB...")
+                            scaling_group_record = ScalingGroup.create(
+                                nsr_id=nsr_id,
+                                vnf_member_index=vnfr['member-vnf-index-ref'],
+                                name=scaling_group['name'],
+                                content=json.dumps(scaling_group)
+                            )
+                            log.info(
+                                "Created scaling group record in DB : nsr_id=%s, vnf_member_index=%s, name=%s",
+                                scaling_group_record.nsr_id,
+                                scaling_group_record.vnf_member_index,
+                                scaling_group_record.name)
+                        for scaling_policy in scaling_group['scaling-policy']:
+                            if scaling_policy['scaling-type'] != 'automatic':
+                                continue
                             try:
-                                scaling_group_record = ScalingGroup.select().where(
-                                    ScalingGroup.nsr_id == nsr_id,
-                                    ScalingGroup.vnf_member_index == int(vnfr['member-vnf-index-ref']),
-                                    ScalingGroup.name == scaling_group['name']
+                                scaling_policy_record = ScalingPolicy.select().join(ScalingGroup).where(
+                                    ScalingPolicy.name == scaling_policy['name'],
+                                    ScalingGroup.id == scaling_group_record.id
                                 ).get()
-                                log.info("Found existing scaling group record in DB...")
-                            except ScalingGroup.DoesNotExist:
-                                log.info("Creating scaling group record in DB...")
-                                scaling_group_record = ScalingGroup.create(
+                                log.info("Found existing scaling policy record in DB...")
+                            except ScalingPolicy.DoesNotExist:
+                                log.info("Creating scaling policy record in DB...")
+                                scaling_policy_record = ScalingPolicy.create(
                                     nsr_id=nsr_id,
-                                    vnf_member_index=vnfr['member-vnf-index-ref'],
-                                    name=scaling_group['name'],
-                                    content=json.dumps(scaling_group)
+                                    name=scaling_policy['name'],
+                                    cooldown_time=scaling_policy['cooldown-time'],
+                                    scaling_group=scaling_group_record
                                 )
-                                log.info(
-                                    "Created scaling group record in DB : nsr_id=%s, vnf_member_index=%s, name=%s",
-                                    scaling_group_record.nsr_id,
-                                    scaling_group_record.vnf_member_index,
-                                    scaling_group_record.name)
-                            for scaling_policy in scaling_group['scaling-policy']:
-                                if scaling_policy['scaling-type'] != 'automatic':
-                                    continue
+                                log.info("Created scaling policy record in DB : name=%s, scaling_group.name=%s",
+                                         scaling_policy_record.name,
+                                         scaling_policy_record.scaling_group.name)
+
+                            for scaling_criteria in scaling_policy['scaling-criteria']:
                                 try:
-                                    scaling_policy_record = ScalingPolicy.select().join(ScalingGroup).where(
-                                        ScalingPolicy.name == scaling_policy['name'],
-                                        ScalingGroup.id == scaling_group_record.id
+                                    scaling_criteria_record = ScalingCriteria.select().join(ScalingPolicy).where(
+                                        ScalingPolicy.id == scaling_policy_record.id,
+                                        ScalingCriteria.name == scaling_criteria['name']
                                     ).get()
-                                    log.info("Found existing scaling policy record in DB...")
-                                except ScalingPolicy.DoesNotExist:
-                                    log.info("Creating scaling policy record in DB...")
-                                    scaling_policy_record = ScalingPolicy.create(
+                                    log.info("Found existing scaling criteria record in DB...")
+                                except ScalingCriteria.DoesNotExist:
+                                    log.info("Creating scaling criteria record in DB...")
+                                    scaling_criteria_record = ScalingCriteria.create(
                                         nsr_id=nsr_id,
-                                        name=scaling_policy['name'],
-                                        cooldown_time=scaling_policy['cooldown-time'],
-                                        scaling_group=scaling_group_record
+                                        name=scaling_criteria['name'],
+                                        scaling_policy=scaling_policy_record
                                     )
-                                    log.info("Created scaling policy record in DB : name=%s, scaling_group.name=%s",
-                                             scaling_policy_record.name,
-                                             scaling_policy_record.scaling_group.name)
-
-                                for scaling_criteria in scaling_policy['scaling-criteria']:
-                                    try:
-                                        scaling_criteria_record = ScalingCriteria.select().join(ScalingPolicy).where(
-                                            ScalingPolicy.id == scaling_policy_record.id,
-                                            ScalingCriteria.name == scaling_criteria['name']
-                                        ).get()
-                                        log.info("Found existing scaling criteria record in DB...")
-                                    except ScalingCriteria.DoesNotExist:
-                                        log.info("Creating scaling criteria record in DB...")
-                                        scaling_criteria_record = ScalingCriteria.create(
-                                            nsr_id=nsr_id,
-                                            name=scaling_criteria['name'],
-                                            scaling_policy=scaling_policy_record
-                                        )
-                                        log.info(
-                                            "Created scaling criteria record in DB : name=%s, scaling_policy.name=%s",
-                                            scaling_criteria_record.name,
-                                            scaling_criteria_record.scaling_policy.name)
+                                    log.info(
+                                        "Created scaling criteria record in DB : name=%s, scaling_policy.name=%s",
+                                        scaling_criteria_record.name,
+                                        scaling_criteria_record.scaling_policy.name)
 
-                                    for vdu_ref in scaling_group['vdu']:
-                                        vnf_monitoring_param = next(
-                                            filter(
-                                                lambda param: param['id'] == scaling_criteria[
-                                                    'vnf-monitoring-param-ref'
-                                                ],
-                                                vnf_monitoring_params)
+                                vnf_monitoring_param = next(
+                                    filter(
+                                        lambda param: param['id'] == scaling_criteria[
+                                            'vnf-monitoring-param-ref'
+                                        ],
+                                        vnf_monitoring_params)
+                                )
+                                if 'vdu-monitoring-param' in vnf_monitoring_param:
+                                    vdurs = list(
+                                        filter(
+                                            lambda vdur: vdur['vdu-id-ref'] == vnf_monitoring_param
+                                            ['vdu-monitoring-param']
+                                            ['vdu-ref'],
+                                            vnfr['vdur']
                                         )
-                                        if vdu_ref['vdu-id-ref'] != vnf_monitoring_param['vdu-monitoring-param'][
-                                            'vdu-ref'
-                                        ]:
-                                            continue
-                                        vdu = next(
-                                            filter(lambda vdu: vdu['id'] == vdu_ref['vdu-id-ref'], vnfd['vdu'])
+                                    )
+                                elif 'vdu-metric' in vnf_monitoring_param:
+                                    vdurs = list(
+                                        filter(
+                                            lambda vdur: vdur['vdu-id-ref'] == vnf_monitoring_param
+                                            ['vdu-metric']
+                                            ['vdu-ref'],
+                                            vnfr['vdur']
                                         )
-                                        vdu_monitoring_params = vdu['monitoring-param']
-                                        vdu_monitoring_param = next(
-                                            filter(
-                                                lambda param: param['id'] == vnf_monitoring_param[
-                                                    'vdu-monitoring-param'
-                                                ][
-                                                    'vdu-monitoring-param-ref'
-                                                ],
-                                                vdu_monitoring_params))
-                                        vdurs = list(
-                                            filter(lambda vdur: vdur['vdu-id-ref'] == vnf_monitoring_param[
-                                                'vdu-monitoring-param'
-                                            ][
-                                                'vdu-ref'
-                                            ], vnfr['vdur']))
-                                        for vdur in vdurs:
-                                            try:
-                                                (ScalingAlarm.select()
-                                                 .join(ScalingCriteria)
-                                                 .join(ScalingPolicy)
-                                                 .join(ScalingGroup)
-                                                 .where(
-                                                    ScalingAlarm.vdu_name == vdur['name'],
-                                                    ScalingCriteria.name == scaling_criteria['name'],
-                                                    ScalingPolicy.name == scaling_policy['name'],
-                                                    ScalingGroup.nsr_id == nsr_id
-                                                ).get())
-                                                log.debug("vdu %s already has an alarm configured", vdur['name'])
-                                                continue
-                                            except ScalingAlarm.DoesNotExist:
-                                                pass
-                                            alarm_uuid = await self.mon_client.create_alarm(
-                                                metric_name=vdu_monitoring_param['nfvi-metric'],
-                                                ns_id=nsr_id,
-                                                vdu_name=vdur['name'],
-                                                vnf_member_index=vnfr['member-vnf-index-ref'],
-                                                threshold=scaling_criteria['scale-in-threshold'],
-                                                operation=scaling_criteria['scale-in-relational-operation'],
-                                                statistic=vnf_monitoring_param['aggregation-type']
-                                            )
-                                            ScalingAlarm.create(
-                                                alarm_id=alarm_uuid,
-                                                action='scale_in',
-                                                vnf_member_index=int(vnfr['member-vnf-index-ref']),
-                                                vdu_name=vdur['name'],
-                                                scaling_criteria=scaling_criteria_record
-                                            )
-                                            alarm_uuid = await self.mon_client.create_alarm(
-                                                metric_name=vdu_monitoring_param['nfvi-metric'],
-                                                ns_id=nsr_id,
-                                                vdu_name=vdur['name'],
-                                                vnf_member_index=vnfr['member-vnf-index-ref'],
-                                                threshold=scaling_criteria['scale-out-threshold'],
-                                                operation=scaling_criteria['scale-out-relational-operation'],
-                                                statistic=vnf_monitoring_param['aggregation-type']
-                                            )
-                                            ScalingAlarm.create(
-                                                alarm_id=alarm_uuid,
-                                                action='scale_out',
-                                                vnf_member_index=int(vnfr['member-vnf-index-ref']),
-                                                vdu_name=vdur['name'],
-                                                scaling_criteria=scaling_criteria_record
-                                            )
+                                    )
+                                elif 'vnf-metric' in vnf_monitoring_param:
+                                    log.warning("vnf-metric is not currently supported.")
+                                    continue
+                                else:
+                                    log.warning(
+                                        "Scaling criteria is referring to a vnf-monitoring-param that does not "
+                                        "contain a reference to a vdu or vnf metric.")
+                                    continue
+                                for vdur in vdurs:
+                                    log.info("Creating alarm for vdur %s ", vdur)
+                                    try:
+                                        (ScalingAlarm.select()
+                                         .join(ScalingCriteria)
+                                         .join(ScalingPolicy)
+                                         .join(ScalingGroup)
+                                         .where(
+                                            ScalingAlarm.vdu_name == vdur['name'],
+                                            ScalingCriteria.name == scaling_criteria['name'],
+                                            ScalingPolicy.name == scaling_policy['name'],
+                                            ScalingGroup.nsr_id == nsr_id
+                                        ).get())
+                                        log.debug("vdu %s already has an alarm configured", vdur['name'])
+                                        continue
+                                    except ScalingAlarm.DoesNotExist:
+                                        pass
+                                    alarm_uuid = await self.mon_client.create_alarm(
+                                        metric_name=vnf_monitoring_param['id'],
+                                        ns_id=nsr_id,
+                                        vdu_name=vdur['name'],
+                                        vnf_member_index=vnfr['member-vnf-index-ref'],
+                                        threshold=scaling_criteria['scale-in-threshold'],
+                                        operation=scaling_criteria['scale-in-relational-operation'],
+                                        statistic=vnf_monitoring_param['aggregation-type']
+                                    )
+                                    ScalingAlarm.create(
+                                        alarm_id=alarm_uuid,
+                                        action='scale_in',
+                                        vnf_member_index=int(vnfr['member-vnf-index-ref']),
+                                        vdu_name=vdur['name'],
+                                        scaling_criteria=scaling_criteria_record
+                                    )
+                                    alarm_uuid = await self.mon_client.create_alarm(
+                                        metric_name=vnf_monitoring_param['id'],
+                                        ns_id=nsr_id,
+                                        vdu_name=vdur['name'],
+                                        vnf_member_index=vnfr['member-vnf-index-ref'],
+                                        threshold=scaling_criteria['scale-out-threshold'],
+                                        operation=scaling_criteria['scale-out-relational-operation'],
+                                        statistic=vnf_monitoring_param['aggregation-type']
+                                    )
+                                    ScalingAlarm.create(
+                                        alarm_id=alarm_uuid,
+                                        action='scale_out',
+                                        vnf_member_index=int(vnfr['member-vnf-index-ref']),
+                                        vdu_name=vdur['name'],
+                                        scaling_criteria=scaling_criteria_record
+                                    )
 
             except Exception as e:
                 log.exception("Error configuring scaling groups:")
+                tx.rollback()
                 if len(alarms_created) > 0:
                     log.info("Cleaning alarm resources in MON")
                     for alarm in alarms_created:
index 7702cfd..9bd8ac8 100644 (file)
@@ -77,3 +77,6 @@ class DatabaseManager:
             db.close()
         except Exception:
             log.exception("Error creating tables: ")
+
+    def get_alarm(self, alarm_id: str):
+        return ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index 3126c0f..c175697 100644 (file)
@@ -461,28 +461,28 @@ class PolicyModuleAgentTest(unittest.TestCase):
         create_alarm.side_effect = _test_configure_scaling_groups_create_alarm
         agent = PolicyModuleAgent(self.loop)
         self.loop.run_until_complete(agent._configure_scaling_groups("test_nsr_id"))
-        create_alarm.assert_any_call(metric_name='average_memory_utilization',
+        create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
                                      ns_id='test_nsr_id',
                                      operation='GT',
                                      statistic='AVERAGE',
                                      threshold=80,
                                      vdu_name='cirros_ns-1-cirros_vnfd-VM-1',
                                      vnf_member_index='1')
-        create_alarm.assert_any_call(metric_name='average_memory_utilization',
+        create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
                                      ns_id='test_nsr_id',
                                      operation='LT',
                                      statistic='AVERAGE',
                                      threshold=20,
                                      vdu_name='cirros_ns-1-cirros_vnfd-VM-1',
                                      vnf_member_index='1')
-        create_alarm.assert_any_call(metric_name='average_memory_utilization',
+        create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
                                      ns_id='test_nsr_id',
                                      operation='GT',
                                      statistic='AVERAGE',
                                      threshold=80,
                                      vdu_name='cirros_ns-2-cirros_vnfd-VM-1',
                                      vnf_member_index='2')
-        create_alarm.assert_any_call(metric_name='average_memory_utilization',
+        create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
                                      ns_id='test_nsr_id',
                                      operation='LT',
                                      statistic='AVERAGE',
index e69de29..d81308a 100644 (file)
@@ -0,0 +1,23 @@
+# -*- 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
+##
index 5e59901..932adf4 100644 (file)
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
+import asyncio
+import datetime
 import unittest
+from unittest import mock
+from unittest.mock import Mock
 
 from osm_policy_module.core.agent import PolicyModuleAgent
+from osm_policy_module.core.database import DatabaseManager
 
 
 class PolicyAgentTest(unittest.TestCase):
     def setUp(self):
-        self.agent = PolicyModuleAgent()
+        self.loop = asyncio.new_event_loop()
+        asyncio.set_event_loop(None)
+
+    @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')
+    def test_handle_instantiated_or_scaled(self, configure_scaling_groups, lcm_client, mon_client, db_client):
+        async def mock_configure_scaling_groups(nsr_id):
+            pass
+
+        agent = PolicyModuleAgent(self.loop)
+        assert lcm_client.called
+        assert mon_client.called
+        assert db_client.called
+        content = {
+            'nslcmop_id': 'test_id',
+        }
+        nslcmop_completed = {
+            'operationState': 'COMPLETED',
+            'nsInstanceId': 'test_nsr_id'
+        }
+        nslcmop_failed = {
+            'operationState': 'FAILED',
+            'nsInstanceId': 'test_nsr_id'
+        }
+        configure_scaling_groups.side_effect = mock_configure_scaling_groups
+
+        db_client.return_value.get_nslcmop.return_value = nslcmop_completed
+        self.loop.run_until_complete(agent._handle_instantiated_or_scaled(content))
+        configure_scaling_groups.assert_called_with('test_nsr_id')
+        configure_scaling_groups.reset_mock()
+
+        db_client.return_value.get_nslcmop.return_value = nslcmop_failed
+        self.loop.run_until_complete(agent._handle_instantiated_or_scaled(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):
+        async def mock_scale(nsr_id, scaling_group_name, vnf_member_index, action):
+            pass
+
+        agent = PolicyModuleAgent(self.loop)
+        assert lcm_client.called
+        assert mon_client.called
+        assert db_client.called
+        content = {
+            'notify_details': {
+                'alarm_uuid': 'test_alarm_uuid',
+                'metric_name': 'test_metric_name',
+                'operation': 'test_operation',
+                'threshold_value': 'test_threshold_value',
+                'vdu_name': 'test_vdu_name',
+                'vnf_member_index': 'test_vnf_member_index',
+                'ns_id': 'test_nsr_id'
+            }
+        }
+        mock_alarm = Mock()
+        mock_alarm.vnf_member_index = 1
+        mock_alarm.action = 'scale_out'
+        mock_scaling_criteria = Mock()
+        mock_scaling_policy = Mock()
+        mock_scaling_group = Mock()
+        mock_scaling_group.nsr_id = 'test_nsr_id'
+        mock_scaling_group.name = 'test_name'
+        mock_scaling_policy.cooldown_time = 60
+        mock_scaling_policy.scaling_group = mock_scaling_group
+        mock_scaling_criteria.scaling_policy = mock_scaling_policy
+        mock_alarm.scaling_criteria = mock_scaling_criteria
+        get_alarm.return_value = mock_alarm
+        lcm_client.return_value.scale.side_effect = mock_scale
+
+        mock_scaling_policy.last_scale = datetime.datetime.now() - datetime.timedelta(minutes=90)
+
+        self.loop.run_until_complete(agent._handle_alarm_notification(content))
+        lcm_client.return_value.scale.assert_called_with('test_nsr_id', 'test_name', 1, 'scale_out')
+        lcm_client.return_value.scale.reset_mock()
+
+        mock_scaling_policy.last_scale = datetime.datetime.now()
+        self.loop.run_until_complete(agent._handle_alarm_notification(content))
+        lcm_client.return_value.scale.assert_not_called()
 
 
 if __name__ == '__main__':