Merge "added env vars for mongo ha"
diff --git a/osm_policy_module/common/mon_client.py b/osm_policy_module/common/mon_client.py
index 911e655..2f2dc12 100644
--- a/osm_policy_module/common/mon_client.py
+++ b/osm_policy_module/common/mon_client.py
@@ -74,7 +74,7 @@
                 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 @@
                 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 @@
             '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
diff --git a/osm_policy_module/core/agent.py b/osm_policy_module/core/agent.py
index 8a1a31c..3e64652 100644
--- a/osm_policy_module/core/agent.py
+++ b/osm_policy_module/core/agent.py
@@ -36,10 +36,11 @@
 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 @@
         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 @@
                 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 @@
 
     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 @@
             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 @@
             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 @@
                 "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 @@
                                         )
                                     )
                                 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 @@
                                         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 @@
                                         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 @@
                 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
diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py
index 9bd8ac8..a39f982 100644
--- a/osm_policy_module/core/database.py
+++ b/osm_policy_module/core/database.py
@@ -53,20 +53,20 @@
     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 @@
         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
index 0000000..906bb03
--- /dev/null
+++ b/osm_policy_module/core/exceptions.py
@@ -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/unit/core/__init__.py b/osm_policy_module/tests/unit/core/__init__.py
new file mode 100644
index 0000000..d81308a
--- /dev/null
+++ b/osm_policy_module/tests/unit/core/__init__.py
@@ -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/test_policy_agent.py b/osm_policy_module/tests/unit/core/test_policy_agent.py
similarity index 100%
rename from osm_policy_module/tests/unit/test_policy_agent.py
rename to osm_policy_module/tests/unit/core/test_policy_agent.py
diff --git a/osm_policy_module/tests/unit/utils/__init__.py b/osm_policy_module/tests/unit/utils/__init__.py
new file mode 100644
index 0000000..d81308a
--- /dev/null
+++ b/osm_policy_module/tests/unit/utils/__init__.py
@@ -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/examples/cirros_vdu_scaling_nsd.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_nsd.yaml
similarity index 100%
rename from osm_policy_module/tests/examples/cirros_vdu_scaling_nsd.yaml
rename to osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_nsd.yaml
diff --git a/osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml
similarity index 80%
rename from osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml
rename to osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml
index dc599f5..a626674 100644
--- a/osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml
+++ b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_1.yaml
@@ -38,8 +38,8 @@
                     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
+            -   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
@@ -49,17 +49,17 @@
             min-instance-count: 1
             max-instance-count: 10
             scaling-policy:
-            -   name: "auto_memory_util_above_threshold"
+            -   name: "auto_cpu_util_above_threshold"
                 scaling-type: "automatic"
                 threshold-time: 10
                 cooldown-time: 60
                 scaling-criteria:
-                -   name: "group1_memory_util_above_threshold"
+                -   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_memory_util"
+                    vnf-monitoring-param-ref: "cirros_vnf_cpu_util"
             vdu:
             -   vdu-id-ref: cirros_vnfd-VM
                 count: 1
@@ -67,8 +67,9 @@
             # -   trigger: post-scale-out
             #     vnf-config-primitive-name-ref:
         monitoring-param:
-        -   id: "cirros_vnf_memory_util"
-            name: "cirros_vnf_memory_util"
+        -   id: "cirros_vnf_cpu_util"
+            name: "cirros_vnf_cpu_util"
             aggregation-type: AVERAGE
-            vdu-ref: "cirros_vnfd-VM"
-            vdu-monitoring-param-ref: "cirros_vnfd-VM_memory_util"
\ No newline at end of file
+            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/examples/cirros_vdu_scaling_vnfd.yaml b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml
similarity index 78%
copy from osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml
copy to osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml
index dc599f5..8e0c603 100644
--- a/osm_policy_module/tests/examples/cirros_vdu_scaling_vnfd.yaml
+++ b/osm_policy_module/tests/unit/utils/examples/cirros_vdu_scaling_vnfd_2.yaml
@@ -8,9 +8,9 @@
         version: '1.0'
         # Place the logo as png in icons directory and provide the name here
         logo: cirros-64.png
-        # Management interface
+        # Management vdu id
         mgmt-interface:
-            cp: eth0
+            vdu-id: cirros_vnfd-VM
         # Atleast one VDU need to be specified
         vdu:
         -   id: cirros_vnfd-VM
@@ -38,8 +38,8 @@
                     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
+            -   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
@@ -49,17 +49,17 @@
             min-instance-count: 1
             max-instance-count: 10
             scaling-policy:
-            -   name: "auto_memory_util_above_threshold"
+            -   name: "auto_cpu_util_above_threshold"
                 scaling-type: "automatic"
                 threshold-time: 10
                 cooldown-time: 60
                 scaling-criteria:
-                -   name: "group1_memory_util_above_threshold"
+                -   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_memory_util"
+                    vnf-monitoring-param-ref: "cirros_vnf_cpu_util"
             vdu:
             -   vdu-id-ref: cirros_vnfd-VM
                 count: 1
@@ -67,8 +67,9 @@
             # -   trigger: post-scale-out
             #     vnf-config-primitive-name-ref:
         monitoring-param:
-        -   id: "cirros_vnf_memory_util"
-            name: "cirros_vnf_memory_util"
+        -   id: "cirros_vnf_cpu_util"
+            name: "cirros_vnf_cpu_util"
             aggregation-type: AVERAGE
-            vdu-ref: "cirros_vnfd-VM"
-            vdu-monitoring-param-ref: "cirros_vnfd-VM_memory_util"
\ No newline at end of file
+            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
index 0000000..5b770dd
--- /dev/null
+++ b/osm_policy_module/tests/unit/utils/test_vnfd_utils.py
@@ -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
index 0000000..d81308a
--- /dev/null
+++ b/osm_policy_module/utils/__init__.py
@@ -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
index 0000000..234d864
--- /dev/null
+++ b/osm_policy_module/utils/vnfd.py
@@ -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'])