Fix black, flake8 and cover issues 96/13096/2
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 24 Mar 2023 13:51:38 +0000 (14:51 +0100)
committerGulsum Atici <gulsum.atici@canonical.com>
Tue, 28 Mar 2023 09:07:27 +0000 (12:07 +0300)
Change-Id: I6baef962d0e272c415d9d19436ac4c6c08fc3d92
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
17 files changed:
devops-stages/stage-test.sh
osm_policy_module/alarming/service.py
osm_policy_module/autoscaling/service.py
osm_policy_module/common/lcm_client.py
osm_policy_module/common/mon_client.py
osm_policy_module/core/agent.py
osm_policy_module/core/config.py
osm_policy_module/core/database.py
osm_policy_module/healing/service.py
osm_policy_module/migrations/002_add_vnf_alarm.py
osm_policy_module/migrations/006_add_heal_alarm.py
osm_policy_module/tests/integration/test_policy_agent.py
osm_policy_module/tests/unit/alarming/test_alarming_service.py
osm_policy_module/tests/unit/core/test_policy_agent.py
osm_policy_module/tests/unit/healing/test_healing_service.py
requirements-dev.txt
tox.ini

index 75a3c61..2802151 100755 (executable)
@@ -1,20 +1,20 @@
-# Copyright 2017 Intel Research and Development Ireland Limited
-# *************************************************************
-
-# This file is part of OSM Monitoring module
-# All Rights Reserved to Intel Corporation
-
-# 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
+#!/bin/bash
 
-#         http://www.apache.org/licenses/LICENSE-2.0
+# Copyright 2017 Intel Research and Development Ireland Limited
 
+# 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.
+# 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.
+
+echo "Launching tox"
+TOX_PARALLEL_NO_SPINNER=1 tox --parallel=auto
 
-OUTPUT=$(TOX_PARALLEL_NO_SPINNER=1 tox --parallel=auto)
-printf "$OUTPUT"
index 0e355d8..1651b27 100644 (file)
@@ -111,10 +111,17 @@ class AlarmingService:
                                 )
                                 alarm_action = dict()
                                 for action_type in ["ok", "insufficient-data", "alarm"]:
-                                    if "actions" in alarm_descriptor and action_type in alarm_descriptor["actions"]:
-                                        for url in alarm_descriptor["actions"][action_type]:
+                                    if (
+                                        "actions" in alarm_descriptor
+                                        and action_type in alarm_descriptor["actions"]
+                                    ):
+                                        for url in alarm_descriptor["actions"][
+                                            action_type
+                                        ]:
                                             if "webhook" in alarm_action:
-                                                alarm_action["webhook"].append(url["url"])
+                                                alarm_action["webhook"].append(
+                                                    url["url"]
+                                                )
                                             else:
                                                 alarm_action["webhook"] = [url["url"]]
                                 alarm_uuid = await self.mon_client.create_alarm(
@@ -132,10 +139,10 @@ class AlarmingService:
                                     nsr_id=nsr_id,
                                     vnf_member_index=vnfr["member-vnf-index-ref"],
                                     vdu_name=vdur["name"],
-                                    last_action='insufficient-data',
+                                    last_action="insufficient-data",
                                     id_suffix=0,
                                     ok_ack=False,
-                                    alarm_ack=False
+                                    alarm_ack=False,
                                 )
                                 for action_type in ["ok", "insufficient-data", "alarm"]:
                                     if (
@@ -211,8 +218,10 @@ class AlarmingService:
                 if vnf_member_index is None:
                     alarm_conditions = VnfAlarm.nsr_id == nsr_id
                 else:
-                    query_list = [VnfAlarm.nsr_id == nsr_id,
-                                  VnfAlarm.vnf_member_index == vnf_member_index]
+                    query_list = [
+                        VnfAlarm.nsr_id == nsr_id,
+                        VnfAlarm.vnf_member_index == vnf_member_index,
+                    ]
                     alarm_conditions = functools.reduce(operator.and_, query_list)
                 for alarm in VnfAlarmRepository.list(alarm_conditions):
                     log.debug("Deleting vnf alarm %s", alarm.alarm_uuid)
@@ -236,7 +245,7 @@ class AlarmingService:
             database.db.close()
 
     async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict):
-        alert_timeout = int(self.conf.get('alert', 'timeout'))
+        alert_timeout = int(self.conf.get("alert", "timeout"))
         database.db.connect()
         try:
             with database.db.atomic():
@@ -251,14 +260,19 @@ class AlarmingService:
                     If both the status are 'ok', it avoid sending repetitive ok notification.
                     """
                     if action.type == status:
-                        if bool(self.conf.get('alert', 'enhanced_alarms')):
-                            if ((status != "ok" or (status == "ok" and alarm.ok_ack is False)) and
-                               (status != "alarm" or (status == "alarm" and alarm.alarm_ack is False))):
+                        if bool(self.conf.get("alert", "enhanced_alarms")):
+                            if (
+                                status != "ok"
+                                or (status == "ok" and alarm.ok_ack is False)
+                            ) and (
+                                status != "alarm"
+                                or (status == "alarm" and alarm.alarm_ack is False)
+                            ):
                                 log.info(
                                     "Executing request to url %s for vnf alarm %s with status %s",
                                     action.url,
                                     alarm.alarm_id,
-                                    status
+                                    status,
                                 )
                                 try:
                                     if status == "alarm" and alarm.last_action == "ok":
@@ -271,11 +285,18 @@ class AlarmingService:
                                 except Exception as e:
                                     log.exception(e)
 
-                                payload["notify_details"]["alarm_number"] = alarm.id_suffix
+                                payload["notify_details"][
+                                    "alarm_number"
+                                ] = alarm.id_suffix
                                 headers = {"content-type": "application/json"}
                                 try:
-                                    resp = requests.post(url=action.url, data=json.dumps(payload),
-                                                         headers=headers, verify=False, timeout=alert_timeout)
+                                    resp = requests.post(
+                                        url=action.url,
+                                        data=json.dumps(payload),
+                                        headers=headers,
+                                        verify=False,
+                                        timeout=alert_timeout,
+                                    )
                                     log.info("Response %s", resp)
                                     if resp.status_code == 200:
                                         if status == "ok":
@@ -289,22 +310,34 @@ class AlarmingService:
                                             alarm.ok_ack = False
                                             alarm.save()
                                 except RequestException as e:
-                                    log.info("Error: RequestException while connecting to url %s", action.url)
+                                    log.info(
+                                        "Error: RequestException while connecting to url %s",
+                                        action.url,
+                                    )
                                     log.debug("RequestException %s", e)
 
                                 except ConnectionError:
-                                    log.exception("Error connecting to url %s", action.url)
+                                    log.exception(
+                                        "Error connecting to url %s", action.url
+                                    )
                         else:
                             log.info(
                                 "Executing request to url %s for vnf alarm %s with status %s",
                                 action.url,
                                 alarm.alarm_id,
-                                status
+                                status,
                             )
                             try:
-                                requests.post(url=action.url, json=json.dumps(payload), timeout=alert_timeout)
+                                requests.post(
+                                    url=action.url,
+                                    json=json.dumps(payload),
+                                    timeout=alert_timeout,
+                                )
                             except RequestException as e:
-                                log.info("Error: RequestException while connecting to url %s", action.url)
+                                log.info(
+                                    "Error: RequestException while connecting to url %s",
+                                    action.url,
+                                )
                                 log.debug("RequestException %s", e)
                             except ConnectionError:
                                 log.exception("Error connecting to url %s", action.url)
index d195bb7..33472fd 100644 (file)
@@ -198,7 +198,7 @@ class AutoscalingService:
                                                     operation=scaling_criteria[
                                                         "scale-in-relational-operation"
                                                     ],
-                                                    action="scale_in"
+                                                    action="scale_in",
                                                 )
                                             )
                                             alarm = ScalingAlarmRepository.create(
@@ -262,8 +262,10 @@ class AutoscalingService:
                     if vnf_member_index is None:
                         scale_conditions = ScalingGroup.nsr_id == nsr_id
                     else:
-                        query_list = [ScalingGroup.nsr_id == nsr_id,
-                                      ScalingGroup.vnf_member_index == vnf_member_index]
+                        query_list = [
+                            ScalingGroup.nsr_id == nsr_id,
+                            ScalingGroup.vnf_member_index == vnf_member_index,
+                        ]
                         scale_conditions = functools.reduce(operator.and_, query_list)
                     for scaling_group in ScalingGroupRepository.list(scale_conditions):
                         for scaling_policy in scaling_group.scaling_policies:
index 102b0dc..c94a424 100644 (file)
@@ -133,8 +133,16 @@ class LcmClient:
         return nslcmop
 
     async def heal(
-            self, nsr_id: str, vnfinstance_id: str, vdur_name: str, vdu_id: str,
-            vnf_member_index: str, heal_type: str, day1: bool, count_index: int):
+        self,
+        nsr_id: str,
+        vnfinstance_id: str,
+        vdur_name: str,
+        vdu_id: str,
+        vnf_member_index: str,
+        heal_type: str,
+        day1: bool,
+        count_index: int,
+    ):
         """
         Sends healing action to LCM through the message bus.
 
@@ -159,8 +167,15 @@ class LcmClient:
         )
         nsr = self.db_client.get_nsr(nsr_id)
         nslcmop = self._generate_nslcmop_heal(
-            nsr_id, vnfinstance_id, vdur_name, vdu_id, vnf_member_index, heal_type, day1,
-            count_index, nsr['_admin']
+            nsr_id,
+            vnfinstance_id,
+            vdur_name,
+            vdu_id,
+            vnf_member_index,
+            heal_type,
+            day1,
+            count_index,
+            nsr["_admin"],
         )
         self.db_client.create_nslcmop(nslcmop)
         log.debug("Sending heal action message: %s", json.dumps(nslcmop))
@@ -213,12 +228,12 @@ class LcmClient:
                             {
                                 "run-day1": day1,
                                 "count-index": count_index,
-                                "vdu-id": vdu_id
+                                "vdu-id": vdu_id,
                             }
-                        ]
-                    }
+                        ],
+                    },
                 }
-            ]
+            ],
         }
 
         nslcmop = {
@@ -239,8 +254,8 @@ class LcmClient:
                 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
             },
             "_admin": {
-                "projects_read": admin['projects_read'],
-                "projects_write": admin['projects_write']
-            }
+                "projects_read": admin["projects_read"],
+                "projects_write": admin["projects_write"],
+            },
         }
         return nslcmop
index b8c3779..f9a51cd 100644 (file)
@@ -53,7 +53,7 @@ class MonClient:
         threshold: int,
         operation: str,
         statistic: str = "AVERAGE",
-        action: str = '',
+        action: str = "",
     ):
         cor_id = random.randint(1, 10e7)
         msg = self._build_create_alarm_payload(
@@ -174,7 +174,6 @@ class MonClient:
         operation: str,
         action: str,
     ):
-
         alarm_create_request = {
             "correlation_id": cor_id,
             "alarm_name": "osm_alarm_{}_{}_{}_{}".format(
index 9867c0c..2daebde 100644 (file)
@@ -37,7 +37,14 @@ from osm_policy_module.core.config import Config
 
 log = logging.getLogger(__name__)
 
-ALLOWED_KAFKA_KEYS = ["instantiated", "scaled", "terminated", "notify_alarm", "policy_updated", "vnf_terminated"]
+ALLOWED_KAFKA_KEYS = [
+    "instantiated",
+    "scaled",
+    "terminated",
+    "notify_alarm",
+    "policy_updated",
+    "vnf_terminated",
+]
 
 
 class PolicyModuleAgent:
@@ -68,7 +75,6 @@ class PolicyModuleAgent:
         log.debug("_process_msg topic=%s key=%s msg=%s", topic, key, msg)
         try:
             if key in ALLOWED_KAFKA_KEYS:
-
                 if key == "instantiated":
                     await self._handle_instantiated(msg)
 
@@ -167,39 +173,49 @@ class PolicyModuleAgent:
 
     async def _handle_policy_update(self, content):
         log.info("_handle_policy_update: %s", content)
-        nsr_id = content['nsr_id']
-        vnf_member_index = content['vnf_member_index']
+        nsr_id = content["nsr_id"]
+        vnf_member_index = content["vnf_member_index"]
         if (
             content["operationState"] == "COMPLETED"
             or content["operationState"] == "PARTIALLY_COMPLETED"
         ):
             log.info(
                 "Updating policies of VNF with nsr_id: %s and vnf-member-index: %s"
-                % (nsr_id, vnf_member_index))
-            await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index)
+                % (nsr_id, vnf_member_index)
+            )
+            await self.autoscaling_service.delete_scaling_groups(
+                nsr_id, vnf_member_index
+            )
             await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index)
-            await self.autoscaling_service.configure_scaling_groups(nsr_id, vnf_member_index)
+            await self.autoscaling_service.configure_scaling_groups(
+                nsr_id, vnf_member_index
+            )
             await self.alarming_service.configure_vnf_alarms(nsr_id, vnf_member_index)
         else:
             log.info(
                 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
                 "Current state is %s. Skipping...",
-                content['operationState'])
+                content["operationState"],
+            )
 
     async def _handle_vnf_terminated(self, content):
-        nsr_id = content['nsr_id']
-        vnf_member_index = content['vnf_member_index']
+        nsr_id = content["nsr_id"]
+        vnf_member_index = content["vnf_member_index"]
         if (
             content["operationState"] == "COMPLETED"
             or content["operationState"] == "PARTIALLY_COMPLETED"
         ):
             log.info(
                 "Deleting policies of VNF with nsr_id: %s and vnf-member-index: %s"
-                % (nsr_id, vnf_member_index))
-            await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index)
+                % (nsr_id, vnf_member_index)
+            )
+            await self.autoscaling_service.delete_scaling_groups(
+                nsr_id, vnf_member_index
+            )
             await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index)
         else:
             log.info(
                 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
                 "Current state is %s. Skipping...",
-                content['operationState'])
+                content["operationState"],
+            )
index 289894a..1bc9b2b 100644 (file)
@@ -43,7 +43,7 @@ class Config:
             path = "pol.yaml"
             config_file = pkg_resources.resource_filename(__name__, path)
         with open(config_file) as f:
-            self.conf = yaml.load(f)
+            self.conf = yaml.safe_load(f)
 
     def get(self, section, field=None):
         if not field:
index e596d74..b6c98d3 100644 (file)
@@ -98,7 +98,7 @@ class VnfAlarm(BaseModel):
     nsr_id = CharField()
     vnf_member_index = CharField()
     vdu_name = CharField()
-    last_action = CharField(default='insufficient-data')
+    last_action = CharField(default="insufficient-data")
     id_suffix = IntegerField()
     ok_ack = BooleanField(default=False)
     alarm_ack = BooleanField(default=False)
@@ -122,7 +122,7 @@ class HealingAction(BaseModel):
     cooldown_time = IntegerField()
     count_index = IntegerField()
     last_heal = DateTimeField(default=datetime.datetime.now)
-    last_status = CharField(default='insufficient-data')
+    last_status = CharField(default="insufficient-data")
     day1 = BooleanField(default=False)
 
 
@@ -245,7 +245,11 @@ class AlarmActionRepository:
 class HealingActionRepository:
     @staticmethod
     def list(*expressions) -> Iterable[HealingAction]:
-        log.info("### Printing healing action db alarm {}".format(HealingAction.select().where(*expressions)))
+        log.info(
+            "### Printing healing action db alarm {}".format(
+                HealingAction.select().where(*expressions)
+            )
+        )
         return HealingAction.select().where(*expressions)
 
     @staticmethod
index 8434c66..1885d05 100644 (file)
@@ -43,7 +43,7 @@ log = logging.getLogger(__name__)
 class HealingService:
     def __init__(self, config: Config, loop=None):
         """
-          Initializing the HealingService
+        Initializing the HealingService
         """
         log.info("HealingService Initialized")
         self.conf = config
@@ -57,8 +57,8 @@ class HealingService:
 
     async def configure_healing_alarms(self, nsr_id: str):
         """
-           Configuring the Healing alarms
-           :param nsr_id: Network service record id
+        Configuring the Healing alarms
+        :param nsr_id: Network service record id
         """
         log.info("Configuring Healing alarm for NS %s", nsr_id)
         alarms_created = []
@@ -67,32 +67,38 @@ class HealingService:
             with database.db.atomic():
                 vnfrs = self.db_client.get_vnfrs(nsr_id)
                 for vnfr in vnfrs:
-                    vnfd = self.db_client.get_vnfd(vnfr['vnfd-id'])
+                    vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"])
                     df = vnfd.get("df", [{}])[0]
                     if "healing-aspect" not in df:
                         log.info("No healing configuration present in vnfd")
                         continue
                     healing_aspects = df["healing-aspect"]
                     for healing_aspect in healing_aspects:
-                        for healing_policy in healing_aspect.get(
-                            "healing-policy", ()
-                        ):
-                            vdu_id = healing_policy['vdu-id']
+                        for healing_policy in healing_aspect.get("healing-policy", ()):
+                            vdu_id = healing_policy["vdu-id"]
                             for vdur in vnfr["vdur"]:
                                 if vdu_id == vdur["vdu-id-ref"]:
                                     try:
                                         HealingActionRepository.get(
-                                            HealingAction.alarm_id == healing_policy['event-name'],
-                                            HealingAction.vdur_name == vdur['name'],
+                                            HealingAction.alarm_id
+                                            == healing_policy["event-name"],
+                                            HealingAction.vdur_name == vdur["name"],
                                             HealingAction.nsr_id == nsr_id,
-                                            HealingAction.cooldown_time == healing_policy['cooldown-time'],
-                                            HealingAction.recovery_action == healing_policy['action-on-recovery'],
-                                            HealingAction.vnfinstance_id == vnfr['id'],
-                                            HealingAction.vdu_id == healing_policy['vdu-id'],
-                                            HealingAction.count_index == vdur['count-index']
+                                            HealingAction.cooldown_time
+                                            == healing_policy["cooldown-time"],
+                                            HealingAction.recovery_action
+                                            == healing_policy["action-on-recovery"],
+                                            HealingAction.vnfinstance_id == vnfr["id"],
+                                            HealingAction.vdu_id
+                                            == healing_policy["vdu-id"],
+                                            HealingAction.count_index
+                                            == vdur["count-index"],
+                                        )
+                                        log.debug(
+                                            "vdu %s already has an alarm configured with same id %s",
+                                            healing_policy["vdu-id"],
+                                            healing_policy["event-name"],
                                         )
-                                        log.debug("vdu %s already has an alarm configured with same id %s",
-                                                  healing_policy['vdu-id'], healing_policy['event-name'])
                                         continue
                                     except HealingAction.DoesNotExist:
                                         pass
@@ -101,28 +107,26 @@ class HealingService:
                                     alarm_uuid = await self.mon_client.create_alarm(
                                         metric_name=metric_name,
                                         ns_id=nsr_id,
-                                        vdu_name=vdur['name'],
-                                        vnf_member_index=vnfr[
-                                            'member-vnf-index-ref'
-                                        ],
+                                        vdu_name=vdur["name"],
+                                        vnf_member_index=vnfr["member-vnf-index-ref"],
                                         threshold=1,
                                         operation="LT",
-                                        statistic="AVERAGE"
+                                        statistic="AVERAGE",
                                     )
                                     alarm = HealingActionRepository.create(
-                                        alarm_id=healing_policy['event-name'],
+                                        alarm_id=healing_policy["event-name"],
                                         alarm_uuid=alarm_uuid,
                                         nsr_id=nsr_id,
-                                        vnf_member_index=vnfr[
-                                            'member-vnf-index-ref'
+                                        vnf_member_index=vnfr["member-vnf-index-ref"],
+                                        vdur_name=vdur["name"],
+                                        recovery_action=healing_policy[
+                                            "action-on-recovery"
                                         ],
-                                        vdur_name=vdur['name'],
-                                        recovery_action=healing_policy['action-on-recovery'],
-                                        cooldown_time=healing_policy['cooldown-time'],
-                                        day1=healing_policy['day1'],
-                                        vdu_id=healing_policy['vdu-id'],
-                                        vnfinstance_id=vnfr['id'],
-                                        count_index=vdur['count-index']
+                                        cooldown_time=healing_policy["cooldown-time"],
+                                        day1=healing_policy["day1"],
+                                        vdu_id=healing_policy["vdu-id"],
+                                        vnfinstance_id=vnfr["id"],
+                                        count_index=vdur["count-index"],
                                     )
                                     alarms_created.append(alarm)
 
@@ -131,12 +135,16 @@ class HealingService:
             if len(alarms_created) > 0:
                 for alarm in alarms_created:
                     try:
-                        await self.mon_client.delete_alarm(alarm.nsr_id,
-                                                           alarm.vnf_member_index,
-                                                           alarm.vdu_name,
-                                                           alarm.alarm_uuid)
+                        await self.mon_client.delete_alarm(
+                            alarm.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)
+                        log.exception(
+                            "Error deleting alarm in MON %s", alarm.alarm_uuid
+                        )
             raise e
         finally:
             database.db.close()
@@ -151,26 +159,20 @@ class HealingService:
                 ):
                     try:
                         self.db_client.get_vdur(
-                            nsr_id,
-                            alarm.vnf_member_index,
-                            alarm.vdur_name
+                            nsr_id, alarm.vnf_member_index, alarm.vdur_name
                         )
                     except VdurNotFound:
-                        log.info(
-                            "Deleting orphaned alarm %s",
-                            alarm.alarm_uuid
-                        )
+                        log.info("Deleting orphaned alarm %s", alarm.alarm_uuid)
                         try:
                             await self.mon_client.delete_alarm(
                                 alarm.nsr_id,
                                 alarm.vnf_member_index,
                                 alarm.vdur_name,
-                                alarm.alarm_uuid
+                                alarm.alarm_uuid,
                             )
                         except ValueError:
                             log.exception(
-                                "Error deleting alarm in MON %s",
-                                alarm.alarm_uuid
+                                "Error deleting alarm in MON %s", alarm.alarm_uuid
                             )
                         alarm.delete_instance()
 
@@ -182,8 +184,8 @@ class HealingService:
 
     async def delete_healing_alarms(self, nsr_id):
         """
-            Deleting the healing alarms
-            :param nsr_id: Network service record id
+        Deleting the healing alarms
+        :param nsr_id: Network service record id
         """
         log.info("Deleting healing vnf alarms for network service %s", nsr_id)
         database.db.connect()
@@ -197,12 +199,11 @@ class HealingService:
                             alarm.nsr_id,
                             alarm.vnf_member_index,
                             alarm.vdur_name,
-                            alarm.alarm_uuid
+                            alarm.alarm_uuid,
                         )
                     except ValueError:
                         log.exception(
-                            "Error deleting alarm in MON %s",
-                            alarm.alarm_uuid
+                            "Error deleting alarm in MON %s", alarm.alarm_uuid
                         )
                     alarm.delete_instance()
 
@@ -214,9 +215,9 @@ class HealingService:
 
     async def update_alarm_status(self, alarm_uuid: str, status: str):
         """
-          For updating the alarm status
-          :param alarm_uuid: vdu uuid
-          :param status: Status of an alarm
+        For updating the alarm status
+        :param alarm_uuid: vdu uuid
+        :param status: Status of an alarm
         """
         database.db.connect()
         try:
@@ -227,17 +228,15 @@ class HealingService:
                 alarm.last_status = status
                 alarm.save()
         except HealingAction.DoesNotExist:
-            log.debug(
-                "There is no healing action configured for alarm %s.", alarm_uuid
-            )
+            log.debug("There is no healing action configured for alarm %s.", alarm_uuid)
         finally:
             database.db.close()
 
     async def handle_alarm(self, alarm_uuid: str, status: str):
         """
-          For Handling the healing alarms
-          :param alarm_uuid: vdu uuid
-          :param status: Status of an alarm
+        For Handling the healing alarms
+        :param alarm_uuid: vdu uuid
+        :param status: Status of an alarm
         """
         await self.update_alarm_status(alarm_uuid, status)
         database.db.connect()
@@ -259,13 +258,13 @@ class HealingService:
                     vnfinstance_id = alarm.vnfinstance_id
                     alarms = HealingActionRepository.list(
                         HealingAction.vnf_member_index == vnf_member_index,
-                        HealingAction.vdur_name == vdur_name
+                        HealingAction.vdur_name == vdur_name,
                     )
                     statuses = []
 
                     for alarm in alarms:
                         statuses.append(alarm.last_status)
-                    if ((set(statuses) == {'alarm'}) or ('alarm' in statuses)):
+                    if (set(statuses) == {"alarm"}) or ("alarm" in statuses):
                         delta = datetime.datetime.now() - last_heal
                         if delta.total_seconds() > cooldown_time:
                             await self.lcm_client.heal(
@@ -276,7 +275,7 @@ class HealingService:
                                 vnf_member_index,
                                 heal_type,
                                 day1,
-                                count_index
+                                count_index,
                             )
                             last_heal = datetime.datetime.now()
                             log.info("datetime.datetime.now %s", datetime.datetime.now)
@@ -284,9 +283,6 @@ class HealingService:
                             alarm.save()
 
         except HealingAction.DoesNotExist:
-            log.info(
-                "There is no healing action configured for alarm %s.",
-                alarm_uuid
-            )
+            log.info("There is no healing action configured for alarm %s.", alarm_uuid)
         finally:
             database.db.close()
index 65bf26b..ab31d5a 100644 (file)
@@ -60,7 +60,7 @@ def migrate(migrator, database, fake=False, **kwargs):
         nsr_id = pw.CharField(max_length=255)
         vnf_member_index = pw.IntegerField()
         vdu_name = pw.CharField(max_length=255)
-        last_action = pw.CharField(max_length=255, default='insufficient-data')
+        last_action = pw.CharField(max_length=255, default="insufficient-data")
         id_suffix = pw.IntegerField()
         ok_ack = pw.BooleanField(default=False)
         alarm_ack = pw.BooleanField(default=False)
index b44f42d..a2d2a69 100644 (file)
@@ -66,7 +66,7 @@ def migrate(migrator, database, fake=False, **kwargs):
         cooldown_time = pw.IntegerField()
         count_index = pw.IntegerField()
         last_heal = pw.DateTimeField()
-        last_status = pw.CharField(max_length=255, default='insufficient-data')
+        last_status = pw.CharField(max_length=255, default="insufficient-data")
         day1 = pw.BooleanField(default=False)
 
         class Meta:
@@ -76,4 +76,4 @@ def migrate(migrator, database, fake=False, **kwargs):
 def rollback(migrator, database, fake=False, **kwargs):
     """Write your rollback migrations here."""
 
-    migrator.remove_model('healingaction')
+    migrator.remove_model("healingaction")
index 0329b7c..73db304 100644 (file)
@@ -375,7 +375,7 @@ vnfd_record_mock = {
                             "recovery-type": "automatic",
                             "action-on-recovery": "REDEPLOY_ONLY",
                             "cooldown-time": 180,
-                            "day1": False
+                            "day1": False,
                         }
                     ],
                 }
@@ -592,7 +592,7 @@ class PolicyModuleAgentTest(unittest.TestCase):
             vnf_member_index="1",
             threshold=20.0,
             operation="LT",
-            action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}"
+            action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}",
         )
         create_alarm.assert_any_call(
             metric_name="average_memory_utilization",
@@ -601,7 +601,7 @@ class PolicyModuleAgentTest(unittest.TestCase):
             vnf_member_index="2",
             threshold=20.0,
             operation="LT",
-            action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}"
+            action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}",
         )
 
     @patch.object(DbMongo, "db_connect", Mock())
index 563768f..af672e6 100644 (file)
@@ -39,71 +39,84 @@ class TestAlarmingService(TestCase):
     def setUp(self):
         self.config = Config()
         self.loop = asyncio.new_event_loop()
-        self.payload = {
-            "notify_details": {
-                "alarm_number": 0
-            }
-        }
+        self.payload = {"notify_details": {"alarm_number": 0}}
         self.headers = {"content-type": "application/json"}
 
     @mock.patch.object(VnfAlarmRepository, "get")
     @mock.patch("requests.post")
     @mock.patch("osm_policy_module.core.database.db")
     def test_handle_alarm_suppression(self, database, requests_post, get_alarm):
-        alert_timeout = int(self.config.get('alert', 'timeout'))
+        alert_timeout = int(self.config.get("alert", "timeout"))
         mock_alarm = self._build_mock_alarm("test_id", last_action="ok")
         get_alarm.return_value = mock_alarm
         service = AlarmingService(self.config)
-        if bool(self.config.get('alert', 'enhanced_alarms')):
-            self.loop.run_until_complete(service.handle_alarm("test_id", "alarm", self.payload))
+        if bool(self.config.get("alert", "enhanced_alarms")):
+            self.loop.run_until_complete(
+                service.handle_alarm("test_id", "alarm", self.payload)
+            )
             requests_post.assert_called_once_with(
-                url='http://alarm-url/', data='{"notify_details": {"alarm_number": 1}}',
-                headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout
+                url="http://alarm-url/",
+                data='{"notify_details": {"alarm_number": 1}}',
+                headers={"content-type": "application/json"},
+                verify=False,
+                timeout=alert_timeout,
             )
         else:
             self.loop.run_until_complete(service.handle_alarm("test_id", "alarm", {}))
-            requests_post.assert_called_once_with(json="{}", url="http://alarm-url/", timeout=alert_timeout)
+            requests_post.assert_called_once_with(
+                json="{}", url="http://alarm-url/", timeout=alert_timeout
+            )
 
     @mock.patch.object(VnfAlarmRepository, "get")
     @mock.patch("requests.post")
     @mock.patch("osm_policy_module.core.database.db")
     def test_handle_ok_suppression(self, database, requests_post, get_alarm):
-        alert_timeout = int(self.config.get('alert', 'timeout'))
+        alert_timeout = int(self.config.get("alert", "timeout"))
         mock_alarm = self._build_mock_alarm("test_id", last_action="alarm")
         get_alarm.return_value = mock_alarm
         service = AlarmingService(self.config)
-        if bool(self.config.get('alert', 'enhanced_alarms')):
-            self.loop.run_until_complete(service.handle_alarm("test_id", "ok", self.payload))
-            requests_post.assert_called_once_with( 
-                url='http://ok-url/', data='{"notify_details": {"alarm_number": 0}}',
-                headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout
+        if bool(self.config.get("alert", "enhanced_alarms")):
+            self.loop.run_until_complete(
+                service.handle_alarm("test_id", "ok", self.payload)
+            )
+            requests_post.assert_called_once_with(
+                url="http://ok-url/",
+                data='{"notify_details": {"alarm_number": 0}}',
+                headers={"content-type": "application/json"},
+                verify=False,
+                timeout=alert_timeout,
             )
         else:
             self.loop.run_until_complete(service.handle_alarm("test_id", "ok", {}))
-            requests_post.assert_called_once_with(json="{}", url="http://ok-url/", timeout=alert_timeout)
+            requests_post.assert_called_once_with(
+                json="{}", url="http://ok-url/", timeout=alert_timeout
+            )
 
     @mock.patch.object(VnfAlarmRepository, "get")
     @mock.patch("requests.post")
     @mock.patch("osm_policy_module.core.database.db")
     def test_handle_insufficientalarm(self, database, requests_post, get_alarm):
-        alert_timeout = int(self.config.get('alert', 'timeout'))
+        alert_timeout = int(self.config.get("alert", "timeout"))
         mock_alarm = self._build_mock_alarm("test_id")
         get_alarm.return_value = mock_alarm
         service = AlarmingService(self.config)
-        if bool(self.config.get('alert', 'enhanced_alarms')):
+        if bool(self.config.get("alert", "enhanced_alarms")):
             self.loop.run_until_complete(
                 service.handle_alarm("test_id", "insufficient-data", self.payload)
             )
             requests_post.assert_called_once_with(
-                url='http://insufficient-data-url/', data='{"notify_details": {"alarm_number": 0}}', 
-                headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout
+                url="http://insufficient-data-url/",
+                data='{"notify_details": {"alarm_number": 0}}',
+                headers={"content-type": "application/json"},
+                verify=False,
+                timeout=alert_timeout,
             )
         else:
             self.loop.run_until_complete(
                 service.handle_alarm("test_id", "insufficient-data", {})
             )
             requests_post.assert_called_once_with(
-                json="{}", url='http://insufficient-data-url/', timeout=alert_timeout
+                json="{}", url="http://insufficient-data-url/", timeout=alert_timeout
             )
 
     @mock.patch.object(VnfAlarmRepository, "get")
@@ -123,7 +136,7 @@ class TestAlarmingService(TestCase):
         insufficient_data_url="http://insufficient-data-url/",
         ok_url="http://ok-url/",
         id_suffix=0,
-        last_action="insufficient-data"
+        last_action="insufficient-data",
     ):
         mock_alarm = mock.Mock()
         mock_alarm.alarm_id = alarm_id
index cc17891..43bc508 100644 (file)
@@ -146,12 +146,12 @@ class PolicyAgentTest(unittest.TestCase):
         content = {
             "nsr_id": "test_nsr_id",
             "vnf_member_index": "1",
-            "operationState": "COMPLETED"
+            "operationState": "COMPLETED",
         }
         failed_content = {
             "nsr_id": "test_nsr_id",
             "vnf_member_index": "1",
-            "operationState": "FAILED"
+            "operationState": "FAILED",
         }
         configure_scaling_groups.side_effect = mock_configure_scaling_groups
         configure_vnf_alarms.side_effect = mock_configure_vnf_alarms
@@ -250,12 +250,12 @@ class PolicyAgentTest(unittest.TestCase):
         content = {
             "nsr_id": "test_nsr_id",
             "vnf_member_index": "1",
-            "operationState": "COMPLETED"
+            "operationState": "COMPLETED",
         }
         failed_content = {
             "nsr_id": "test_nsr_id",
             "vnf_member_index": "1",
-            "operationState": "FAILED"
+            "operationState": "FAILED",
         }
         delete_scaling_groups.side_effect = mock_delete_scaling_groups
         delete_vnf_alarms.side_effect = mock_delete_vnf_alarms
index e99f654..7f48f83 100644 (file)
@@ -82,7 +82,7 @@ class TestHealscalingService(TestCase):
             "test_vnf_member_index",
             "test_heal_type",
             "test_day1",
-            "test_count_index"
+            "test_count_index",
         )
 
     def _build_mock_alarm(
index 21f6ba6..268bdd8 100644 (file)
 # limitations under the License.
 #######################################################################################
 aiokafka==0.7.2
-    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
 dataclasses==0.6
-    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
 kafka-python==2.0.2
     # via
-    #   -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    #   -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
     #   aiokafka
-osm-common @ git+https://osm.etsi.org/gerrit/osm/common.git@master
+osm-common @ git+https://osm.etsi.org/gerrit/osm/common.git@v12.0
     # via -r requirements-dev.in
 pycrypto==2.6.1
-    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
 pymongo==3.12.3
-    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
 pyyaml==5.4.1
-    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=master
+    # via -r https://osm.etsi.org/gitweb/?p=osm/common.git;a=blob_plain;f=requirements.txt;hb=v12.0
diff --git a/tox.ini b/tox.ini
index ee801f6..ad27514 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -27,14 +27,15 @@ basepython = python3.8
 setenv = VIRTUAL_ENV={envdir}
          PYTHONDONTWRITEBYTECODE = 1
 deps =  -r{toxinidir}/requirements.txt
+parallel_show_output = true
 
 #######################################################################################
 [testenv:black]
 deps = black
 skip_install = true
 commands =
-        black --check --diff osm_policy_module/
-        black --check --diff setup.py
+        black --check --diff osm_policy_module/
+        black --check --diff setup.py
 
 
 #######################################################################################
@@ -49,7 +50,7 @@ commands =
         coverage report --omit='*tests*'
         coverage html -d ./cover --omit='*tests*'
         coverage xml -o coverage.xml --omit=*tests*
-whitelist_externals = sh
+allowlist_externals = sh
 
 
 #######################################################################################
@@ -66,7 +67,7 @@ deps =  {[testenv]deps}
         -r{toxinidir}/requirements-test.txt
         pylint
 commands =
-    pylint -E osm_policy_module
+        - pylint -E osm_policy_module
 
 
 #######################################################################################
@@ -84,7 +85,7 @@ commands =
 [testenv:pip-compile]
 deps =  pip-tools==6.6.2
 skip_install = true
-whitelist_externals = bash
+allowlist_externals = bash
         [
 commands =
         - bash -c "for file in requirements*.in ; do \
@@ -108,13 +109,14 @@ commands =
         python3 setup.py --command-packages=stdeb.command sdist_dsc
         sh -c 'cd deb_dist/osm-policy-module*/ && dpkg-buildpackage -rfakeroot -uc -us'
         sh -c 'rm osm_policy_module/requirements.txt'
-whitelist_externals = sh
+allowlist_externals = sh
 
 #######################################################################################
 [flake8]
 ignore =
         W291,
         W293,
+        W503,
         E123,
         E125,
         E226,