Merge "added env vars for mongo ha"
authorlavado <glavado@whitestack.com>
Tue, 27 Nov 2018 14:00:13 +0000 (15:00 +0100)
committerGerrit Code Review <root@osm.etsi.org>
Tue, 27 Nov 2018 14:00:13 +0000 (15:00 +0100)
16 files changed:
osm_policy_module/common/mon_client.py
osm_policy_module/core/agent.py
osm_policy_module/core/database.py
osm_policy_module/core/exceptions.py [new file with mode: 0644]
osm_policy_module/tests/examples/cirros_vdu_scaling_nsd.yaml [deleted file]
osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml [deleted file]
osm_policy_module/tests/unit/core/__init__.py [new file with mode: 0644]
osm_policy_module/tests/unit/core/test_policy_agent.py [new file with mode: 0644]
osm_policy_module/tests/unit/test_policy_agent.py [deleted file]
osm_policy_module/tests/unit/utils/__init__.py [new file with mode: 0644]
osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_nsd.yaml [new file with mode: 0644]
osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml [new file with mode: 0644]
osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml [new file with mode: 0644]
osm_policy_module/tests/unit/utils/test_vnfd_utils.py [new file with mode: 0644]
osm_policy_module/utils/__init__.py [new file with mode: 0644]
osm_policy_module/utils/vnfd.py [new file with mode: 0644]

index 911e655..2f2dc12 100644 (file)
@@ -74,7 +74,7 @@ class MonClient:
                 if message.key == 'create_alarm_response':
                     content = json.loads(message.value)
                     log.info("Received create_alarm_response %s", content)
-                    if self._is_alarm_response_correlation_id_eq(cor_id, content):
+                    if content['alarm_create_response']['correlation_id'] == cor_id:
                         if not content['alarm_create_response']['status']:
                             raise ValueError("Error creating alarm in MON")
                         alarm_uuid = content['alarm_create_response']['alarm_uuid']
@@ -112,7 +112,7 @@ class MonClient:
                 if message.key == 'delete_alarm_response':
                     content = json.loads(message.value)
                     log.info("Received delete_alarm_response %s", content)
-                    if self._is_alarm_response_correlation_id_eq(cor_id, content):
+                    if content['alarm_delete_response']['correlation_id'] == cor_id:
                         if not content['alarm_delete_response']['status']:
                             raise ValueError("Error deleting alarm in MON")
                         alarm_uuid = content['alarm_delete_response']['alarm_uuid']
@@ -155,6 +155,3 @@ class MonClient:
             'alarm_delete_request': alarm_delete_request,
         }
         return msg
-
-    def _is_alarm_response_correlation_id_eq(self, cor_id, message_content):
-        return message_content['alarm_create_response']['correlation_id'] == cor_id
index 8a1a31c..3e64652 100644 (file)
@@ -36,10 +36,11 @@ 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.utils.vnfd import VnfdUtils
 
 log = logging.getLogger(__name__)
 
-ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'notify_alarm']
+ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'terminated', 'notify_alarm']
 
 
 class PolicyModuleAgent:
@@ -71,6 +72,7 @@ class PolicyModuleAgent:
         await consumer.start()
         try:
             async for msg in consumer:
+                log.info("Message arrived: %s", msg)
                 await self._process_msg(msg.topic, msg.key, msg.value)
         finally:
             await consumer.stop()
@@ -87,6 +89,9 @@ class PolicyModuleAgent:
                 if key == 'instantiated' or key == 'scaled':
                     await self._handle_instantiated_or_scaled(content)
 
+                if key == 'terminated':
+                    await self._handle_terminated(content)
+
                 if key == 'notify_alarm':
                     await self._handle_alarm_notification(content)
             else:
@@ -96,13 +101,13 @@ class PolicyModuleAgent:
 
     async def _handle_alarm_notification(self, content):
         log.debug("_handle_alarm_notification: %s", content)
-        alarm_id = content['notify_details']['alarm_uuid']
+        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']
-        ns_id = content['notify_details']['ns_id']
+        nsr_id = content['notify_details']['ns_id']
         log.info(
             "Received alarm notification for alarm %s, \
             metric %s, \
@@ -111,9 +116,9 @@ class PolicyModuleAgent:
             vdu_name %s, \
             vnf_member_index %s, \
             ns_id %s ",
-            alarm_id, metric_name, operation, threshold, vdu_name, vnf_member_index, ns_id)
+            alarm_uuid, metric_name, operation, threshold, vdu_name, vnf_member_index, nsr_id)
         try:
-            alarm = self.database_manager.get_alarm(alarm_id)
+            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())
@@ -121,15 +126,15 @@ class PolicyModuleAgent:
             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", alarm_id)
-            await self.lcm_client.scale(ns_id,
+            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_id)
+            log.info("There is no action configured for alarm %s.", alarm_uuid)
 
     async def _handle_instantiated_or_scaled(self, content):
         log.debug("_handle_instantiated_or_scaled: %s", content)
@@ -145,9 +150,20 @@ class PolicyModuleAgent:
                 "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):
         log.debug("_configure_scaling_groups: %s", nsr_id)
-        # TODO: Add support for non-nfvi metrics
         alarms_created = []
         with database.db.atomic() as tx:
             try:
@@ -247,8 +263,13 @@ class PolicyModuleAgent:
                                         )
                                     )
                                 elif 'vnf-metric' in vnf_monitoring_param:
-                                    log.warning("vnf-metric is not currently supported.")
-                                    continue
+                                    vdu = VnfdUtils.get_mgmt_vdu(vnfd)
+                                    vdurs = list(
+                                        filter(
+                                            lambda vdur: vdur['vdu-id-ref'] == vdu['id'],
+                                            vnfr['vdur']
+                                        )
+                                    )
                                 else:
                                     log.warning(
                                         "Scaling criteria is referring to a vnf-monitoring-param that does not "
@@ -280,13 +301,14 @@ class PolicyModuleAgent:
                                         operation=scaling_criteria['scale-in-relational-operation'],
                                         statistic=vnf_monitoring_param['aggregation-type']
                                     )
-                                    ScalingAlarm.create(
-                                        alarm_id=alarm_uuid,
+                                    alarm = ScalingAlarm.create(
+                                        alarm_uuid=alarm_uuid,
                                         action='scale_in',
                                         vnf_member_index=int(vnfr['member-vnf-index-ref']),
                                         vdu_name=vdur['name'],
                                         scaling_criteria=scaling_criteria_record
                                     )
+                                    alarms_created.append(alarm)
                                     alarm_uuid = await self.mon_client.create_alarm(
                                         metric_name=vnf_monitoring_param['id'],
                                         ns_id=nsr_id,
@@ -296,13 +318,14 @@ class PolicyModuleAgent:
                                         operation=scaling_criteria['scale-out-relational-operation'],
                                         statistic=vnf_monitoring_param['aggregation-type']
                                     )
-                                    ScalingAlarm.create(
-                                        alarm_id=alarm_uuid,
+                                    alarm = ScalingAlarm.create(
+                                        alarm_uuid=alarm_uuid,
                                         action='scale_out',
                                         vnf_member_index=int(vnfr['member-vnf-index-ref']),
                                         vdu_name=vdur['name'],
                                         scaling_criteria=scaling_criteria_record
                                     )
+                                    alarms_created.append(alarm)
 
             except Exception as e:
                 log.exception("Error configuring scaling groups:")
@@ -310,5 +333,33 @@ class PolicyModuleAgent:
                 if len(alarms_created) > 0:
                     log.info("Cleaning alarm resources in MON")
                     for alarm in alarms_created:
-                        await self.mon_client.delete_alarm(*alarm)
+                        await self.mon_client.delete_alarm(alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+                                                           alarm.vnf_member_index,
+                                                           alarm.vdu_name,
+                                                           alarm.alarm_uuid)
+                raise e
+
+    async def _delete_scaling_groups(self, nsr_id: str):
+        with database.db.atomic() as tx:
+            try:
+                for scaling_group in ScalingGroup.select().where(ScalingGroup.nsr_id == nsr_id):
+                    for scaling_policy in scaling_group.scaling_policies:
+                        for scaling_criteria in scaling_policy.scaling_criterias:
+                            for alarm in scaling_criteria.scaling_alarms:
+                                try:
+                                    await self.mon_client.delete_alarm(
+                                        alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+                                        alarm.vnf_member_index,
+                                        alarm.vdu_name,
+                                        alarm.alarm_uuid)
+                                except ValueError:
+                                    log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
+                                alarm.delete_instance()
+                            scaling_criteria.delete_instance()
+                        scaling_policy.delete_instance()
+                    scaling_group.delete_instance()
+
+            except Exception as e:
+                log.exception("Error deleting scaling groups and alarms:")
+                tx.rollback()
                 raise e
index 9bd8ac8..a39f982 100644 (file)
@@ -53,20 +53,20 @@ class ScalingPolicy(BaseModel):
     name = CharField()
     cooldown_time = IntegerField()
     last_scale = DateTimeField(default=datetime.datetime.now)
-    scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies')
+    scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies', on_delete='CASCADE')
 
 
 class ScalingCriteria(BaseModel):
     name = CharField()
-    scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias')
+    scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias', on_delete='CASCADE')
 
 
 class ScalingAlarm(BaseModel):
-    alarm_id = CharField()
+    alarm_uuid = CharField(unique=True)
     action = CharField()
     vnf_member_index = IntegerField()
     vdu_name = CharField()
-    scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms')
+    scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms', on_delete='CASCADE')
 
 
 class DatabaseManager:
@@ -78,5 +78,5 @@ class DatabaseManager:
         except Exception:
             log.exception("Error creating tables: ")
 
-    def get_alarm(self, alarm_id: str):
-        return ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
+    def get_alarm(self, alarm_uuid: str):
+        return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()
diff --git a/osm_policy_module/core/exceptions.py b/osm_policy_module/core/exceptions.py
new file mode 100644 (file)
index 0000000..906bb03
--- /dev/null
@@ -0,0 +1,27 @@
+# -*- 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
+##
+
+
+class ManagementVduNotFound(Exception):
+    pass
diff --git a/osm_policy_module/tests/examples/cirros_vdu_scaling_nsd.yaml b/osm_policy_module/tests/examples/cirros_vdu_scaling_nsd.yaml
deleted file mode 100644 (file)
index a9e0abd..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-nsd:nsd-catalog:
-    nsd:
-    -   id: cirros_vdu_scaling_ns
-        name: cirros_vdu_scaling_ns
-        short-name: cirros_vdu_scaling_ns
-        description: Simple NS example with a cirros_vdu_scaling_vnf
-        vendor: OSM
-        version: '1.0'
-
-        # Place the logo as png in icons directory and provide the name here
-        logo: osm.png
-
-        # Specify the VNFDs that are part of this NSD
-        constituent-vnfd:
-            # The member-vnf-index needs to be unique, starting from 1
-            # vnfd-id-ref is the id of the VNFD
-            # Multiple constituent VNFDs can be specified
-        -   member-vnf-index: '1'
-            vnfd-id-ref: cirros_vdu_scaling_vnf
-        -   member-vnf-index: '2'
-            vnfd-id-ref: cirros_vdu_scaling_vnf
-        vld:
-        # Networks for the VNFs
-            -   id: cirros_nsd_vld1
-                name: cirros_nsd_vld1
-                type: ELAN
-                mgmt-network: 'true'
-                # vim-network-name: <update>
-                # provider-network:
-                #     segmentation_id: <update>
-                vnfd-connection-point-ref:
-                # Specify the constituent VNFs
-                # member-vnf-index-ref - entry from constituent vnf
-                # vnfd-id-ref - VNFD id
-                # vnfd-connection-point-ref - connection point name in the VNFD
-                -   member-vnf-index-ref: 1
-                    vnfd-id-ref: cirros_vdu_scaling_vnf
-                    # NOTE: Validate the entry below
-                    vnfd-connection-point-ref: eth0
-                -   member-vnf-index-ref: 2
-                    vnfd-id-ref: cirros_vdu_scaling_vnf
-                    vnfd-connection-point-ref: eth0
\ No newline at end of file
diff --git a/osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml b/osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml
deleted file mode 100644 (file)
index dc599f5..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-vnfd:vnfd-catalog:
-    vnfd:
-    -   id: cirros_vdu_scaling_vnf
-        name: cirros_vdu_scaling_vnf
-        short-name: cirros_vdu_scaling_vnf
-        description: Simple VNF example with a cirros and a scaling group descriptor
-        vendor: OSM
-        version: '1.0'
-        # Place the logo as png in icons directory and provide the name here
-        logo: cirros-64.png
-        # Management interface
-        mgmt-interface:
-            cp: eth0
-        # Atleast one VDU need to be specified
-        vdu:
-        -   id: cirros_vnfd-VM
-            name: cirros_vnfd-VM
-            description: cirros_vnfd-VM
-            count: 1
-
-            # Flavour of the VM to be instantiated for the VDU
-            # flavor below can fit into m1.micro
-            vm-flavor:
-                vcpu-count: 1
-                memory-mb: 256
-                storage-gb: 2
-            # Image/checksum or image including the full path
-            image: 'cirros034'
-            #checksum:
-            interface:
-            # Specify the external interfaces
-            # There can be multiple interfaces defined
-            -   name: eth0
-                type: EXTERNAL
-                virtual-interface:
-                    type: VIRTIO
-                    bandwidth: '0'
-                    vpci: 0000:00:0a.0
-                external-connection-point-ref: eth0
-            monitoring-param:
-            -   id: "cirros_vnfd-VM_memory_util"
-                nfvi-metric: "average_memory_utilization" # The associated NFVI metric to be monitored. Id of the metric
-                #interface-name-ref: reference to interface name, required for some metrics
-        connection-point:
-        -   name: eth0
-            type: VPORT
-        scaling-group-descriptor:
-        -   name: "scale_cirros_vnfd-VM"
-            min-instance-count: 1
-            max-instance-count: 10
-            scaling-policy:
-            -   name: "auto_memory_util_above_threshold"
-                scaling-type: "automatic"
-                threshold-time: 10
-                cooldown-time: 60
-                scaling-criteria:
-                -   name: "group1_memory_util_above_threshold"
-                    scale-in-threshold: 20
-                    scale-in-relational-operation: "LT"
-                    scale-out-threshold: 80
-                    scale-out-relational-operation: "GT"
-                    vnf-monitoring-param-ref: "cirros_vnf_memory_util"
-            vdu:
-            -   vdu-id-ref: cirros_vnfd-VM
-                count: 1
-            # scaling-config-action:            # Para utilizar charms
-            # -   trigger: post-scale-out
-            #     vnf-config-primitive-name-ref:
-        monitoring-param:
-        -   id: "cirros_vnf_memory_util"
-            name: "cirros_vnf_memory_util"
-            aggregation-type: AVERAGE
-            vdu-ref: "cirros_vnfd-VM"
-            vdu-monitoring-param-ref: "cirros_vnfd-VM_memory_util"
\ No newline at end of file
diff --git a/osm_policy_module/tests/unit/core/__init__.py b/osm_policy_module/tests/unit/core/__init__.py
new file mode 100644 (file)
index 0000000..d81308a
--- /dev/null
@@ -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
+##
diff --git a/osm_policy_module/tests/unit/core/test_policy_agent.py b/osm_policy_module/tests/unit/core/test_policy_agent.py
new file mode 100644 (file)
index 0000000..932adf4
--- /dev/null
@@ -0,0 +1,123 @@
+# -*- 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 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.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__':
+    unittest.main()
diff --git a/osm_policy_module/tests/unit/test_policy_agent.py b/osm_policy_module/tests/unit/test_policy_agent.py
deleted file mode 100644 (file)
index 932adf4..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-# -*- 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 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.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__':
-    unittest.main()
diff --git a/osm_policy_module/tests/unit/utils/__init__.py b/osm_policy_module/tests/unit/utils/__init__.py
new file mode 100644 (file)
index 0000000..d81308a
--- /dev/null
@@ -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
+##
diff --git a/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_nsd.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_nsd.yaml
new file mode 100644 (file)
index 0000000..a9e0abd
--- /dev/null
@@ -0,0 +1,42 @@
+nsd:nsd-catalog:
+    nsd:
+    -   id: cirros_vdu_scaling_ns
+        name: cirros_vdu_scaling_ns
+        short-name: cirros_vdu_scaling_ns
+        description: Simple NS example with a cirros_vdu_scaling_vnf
+        vendor: OSM
+        version: '1.0'
+
+        # Place the logo as png in icons directory and provide the name here
+        logo: osm.png
+
+        # Specify the VNFDs that are part of this NSD
+        constituent-vnfd:
+            # The member-vnf-index needs to be unique, starting from 1
+            # vnfd-id-ref is the id of the VNFD
+            # Multiple constituent VNFDs can be specified
+        -   member-vnf-index: '1'
+            vnfd-id-ref: cirros_vdu_scaling_vnf
+        -   member-vnf-index: '2'
+            vnfd-id-ref: cirros_vdu_scaling_vnf
+        vld:
+        # Networks for the VNFs
+            -   id: cirros_nsd_vld1
+                name: cirros_nsd_vld1
+                type: ELAN
+                mgmt-network: 'true'
+                # vim-network-name: <update>
+                # provider-network:
+                #     segmentation_id: <update>
+                vnfd-connection-point-ref:
+                # Specify the constituent VNFs
+                # member-vnf-index-ref - entry from constituent vnf
+                # vnfd-id-ref - VNFD id
+                # vnfd-connection-point-ref - connection point name in the VNFD
+                -   member-vnf-index-ref: 1
+                    vnfd-id-ref: cirros_vdu_scaling_vnf
+                    # NOTE: Validate the entry below
+                    vnfd-connection-point-ref: eth0
+                -   member-vnf-index-ref: 2
+                    vnfd-id-ref: cirros_vdu_scaling_vnf
+                    vnfd-connection-point-ref: eth0
\ No newline at end of file
diff --git a/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml
new file mode 100644 (file)
index 0000000..a626674
--- /dev/null
@@ -0,0 +1,75 @@
+vnfd:vnfd-catalog:
+    vnfd:
+    -   id: cirros_vdu_scaling_vnf
+        name: cirros_vdu_scaling_vnf
+        short-name: cirros_vdu_scaling_vnf
+        description: Simple VNF example with a cirros and a scaling group descriptor
+        vendor: OSM
+        version: '1.0'
+        # Place the logo as png in icons directory and provide the name here
+        logo: cirros-64.png
+        # Management interface
+        mgmt-interface:
+            cp: eth0
+        # Atleast one VDU need to be specified
+        vdu:
+        -   id: cirros_vnfd-VM
+            name: cirros_vnfd-VM
+            description: cirros_vnfd-VM
+            count: 1
+
+            # Flavour of the VM to be instantiated for the VDU
+            # flavor below can fit into m1.micro
+            vm-flavor:
+                vcpu-count: 1
+                memory-mb: 256
+                storage-gb: 2
+            # Image/checksum or image including the full path
+            image: 'cirros034'
+            #checksum:
+            interface:
+            # Specify the external interfaces
+            # There can be multiple interfaces defined
+            -   name: eth0
+                type: EXTERNAL
+                virtual-interface:
+                    type: VIRTIO
+                    bandwidth: '0'
+                    vpci: 0000:00:0a.0
+                external-connection-point-ref: eth0
+            monitoring-param:
+            -   id: "cirros_vnfd-VM_cpu_util"
+                nfvi-metric: "cpu_utilization" # The associated NFVI metric to be monitored. Id of the metric
+                #interface-name-ref: reference to interface name, required for some metrics
+        connection-point:
+        -   name: eth0
+            type: VPORT
+        scaling-group-descriptor:
+        -   name: "scale_cirros_vnfd-VM"
+            min-instance-count: 1
+            max-instance-count: 10
+            scaling-policy:
+            -   name: "auto_cpu_util_above_threshold"
+                scaling-type: "automatic"
+                threshold-time: 10
+                cooldown-time: 60
+                scaling-criteria:
+                -   name: "group1_cpu_util_above_threshold"
+                    scale-in-threshold: 20
+                    scale-in-relational-operation: "LT"
+                    scale-out-threshold: 80
+                    scale-out-relational-operation: "GT"
+                    vnf-monitoring-param-ref: "cirros_vnf_cpu_util"
+            vdu:
+            -   vdu-id-ref: cirros_vnfd-VM
+                count: 1
+            # scaling-config-action:            # Para utilizar charms
+            # -   trigger: post-scale-out
+            #     vnf-config-primitive-name-ref:
+        monitoring-param:
+        -   id: "cirros_vnf_cpu_util"
+            name: "cirros_vnf_cpu_util"
+            aggregation-type: AVERAGE
+            vdu-monitoring-param:
+              vdu-ref: "cirros_vnfd-VM"
+              vdu-monitoring-param-ref: "cirros_vnfd-VM_cpu_util"
\ No newline at end of file
diff --git a/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml
new file mode 100644 (file)
index 0000000..8e0c603
--- /dev/null
@@ -0,0 +1,75 @@
+vnfd:vnfd-catalog:
+    vnfd:
+    -   id: cirros_vdu_scaling_vnf
+        name: cirros_vdu_scaling_vnf
+        short-name: cirros_vdu_scaling_vnf
+        description: Simple VNF example with a cirros and a scaling group descriptor
+        vendor: OSM
+        version: '1.0'
+        # Place the logo as png in icons directory and provide the name here
+        logo: cirros-64.png
+        # Management vdu id
+        mgmt-interface:
+            vdu-id: cirros_vnfd-VM
+        # Atleast one VDU need to be specified
+        vdu:
+        -   id: cirros_vnfd-VM
+            name: cirros_vnfd-VM
+            description: cirros_vnfd-VM
+            count: 1
+
+            # Flavour of the VM to be instantiated for the VDU
+            # flavor below can fit into m1.micro
+            vm-flavor:
+                vcpu-count: 1
+                memory-mb: 256
+                storage-gb: 2
+            # Image/checksum or image including the full path
+            image: 'cirros034'
+            #checksum:
+            interface:
+            # Specify the external interfaces
+            # There can be multiple interfaces defined
+            -   name: eth0
+                type: EXTERNAL
+                virtual-interface:
+                    type: VIRTIO
+                    bandwidth: '0'
+                    vpci: 0000:00:0a.0
+                external-connection-point-ref: eth0
+            monitoring-param:
+            -   id: "cirros_vnfd-VM_cpu_util"
+                nfvi-metric: "cpu_utilization" # The associated NFVI metric to be monitored. Id of the metric
+                #interface-name-ref: reference to interface name, required for some metrics
+        connection-point:
+        -   name: eth0
+            type: VPORT
+        scaling-group-descriptor:
+        -   name: "scale_cirros_vnfd-VM"
+            min-instance-count: 1
+            max-instance-count: 10
+            scaling-policy:
+            -   name: "auto_cpu_util_above_threshold"
+                scaling-type: "automatic"
+                threshold-time: 10
+                cooldown-time: 60
+                scaling-criteria:
+                -   name: "group1_cpu_util_above_threshold"
+                    scale-in-threshold: 20
+                    scale-in-relational-operation: "LT"
+                    scale-out-threshold: 80
+                    scale-out-relational-operation: "GT"
+                    vnf-monitoring-param-ref: "cirros_vnf_cpu_util"
+            vdu:
+            -   vdu-id-ref: cirros_vnfd-VM
+                count: 1
+            # scaling-config-action:            # Para utilizar charms
+            # -   trigger: post-scale-out
+            #     vnf-config-primitive-name-ref:
+        monitoring-param:
+        -   id: "cirros_vnf_cpu_util"
+            name: "cirros_vnf_cpu_util"
+            aggregation-type: AVERAGE
+            vdu-monitoring-param:
+              vdu-ref: "cirros_vnfd-VM"
+              vdu-monitoring-param-ref: "cirros_vnfd-VM_cpu_util"
\ No newline at end of file
diff --git a/osm_policy_module/tests/unit/utils/test_vnfd_utils.py b/osm_policy_module/tests/unit/utils/test_vnfd_utils.py
new file mode 100644 (file)
index 0000000..5b770dd
--- /dev/null
@@ -0,0 +1,45 @@
+# -*- 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 os
+import unittest
+
+import yaml
+
+from osm_policy_module.utils.vnfd import VnfdUtils
+
+
+class VnfdUtilsTest(unittest.TestCase):
+    def test_get_mgmt_vdu_by_cp(self):
+        with open(
+                os.path.join(os.path.dirname(__file__), 'examples/cirros_vdu_scaling_vnfd_1.yaml'), 'r') as file:
+            vnfd = yaml.safe_load(file)['vnfd:vnfd-catalog']['vnfd'][0]
+            vdu = VnfdUtils.get_mgmt_vdu(vnfd)
+            self.assertEqual(vdu['id'], 'cirros_vnfd-VM')
+
+    def test_get_mgmt_vdu_by_id(self):
+        with open(
+                os.path.join(os.path.dirname(__file__), 'examples/cirros_vdu_scaling_vnfd_2.yaml'), 'r') as file:
+            vnfd = yaml.safe_load(file)['vnfd:vnfd-catalog']['vnfd'][0]
+            vdu = VnfdUtils.get_mgmt_vdu(vnfd)
+            self.assertEqual(vdu['id'], 'cirros_vnfd-VM')
diff --git a/osm_policy_module/utils/__init__.py b/osm_policy_module/utils/__init__.py
new file mode 100644 (file)
index 0000000..d81308a
--- /dev/null
@@ -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
+##
diff --git a/osm_policy_module/utils/vnfd.py b/osm_policy_module/utils/vnfd.py
new file mode 100644 (file)
index 0000000..234d864
--- /dev/null
@@ -0,0 +1,41 @@
+# -*- 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
+##
+from osm_policy_module.core.exceptions import ManagementVduNotFound
+
+
+class VnfdUtils:
+
+    @staticmethod
+    def get_mgmt_vdu(vnfd: dict):
+        if 'cp' in vnfd['mgmt-interface']:
+            for vdu in vnfd['vdu']:
+                for interface in vdu['interface']:
+                    if 'external-connection-point-ref' in interface:
+                        if interface['external-connection-point-ref'] == vnfd['mgmt-interface']['cp']:
+                            return vdu
+        elif 'vdu-id' in vnfd['mgmt-interface']:
+            for vdu in vnfd['vdu']:
+                if vdu['id'] == vnfd['mgmt-interface']['vdu-id']:
+                    return vdu
+        raise ManagementVduNotFound("Management vdu not founr in vnfd %s", vnfd['id'])