Adds support for autoscaling based on vnf-metric

Change-Id: I179395ce1390f9031b15d141ff3b37839943368e
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
diff --git a/osm_policy_module/core/agent.py b/osm_policy_module/core/agent.py
index ab8f857..3e64652 100644
--- a/osm_policy_module/core/agent.py
+++ b/osm_policy_module/core/agent.py
@@ -36,6 +36,7 @@
 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__)
 
@@ -262,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 "
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'])