Updates for Python 3.10 and Ubuntu 22.04
[osm/POL.git] / osm_policy_module / autoscaling / service.py
index 41731b4..fd96da3 100644 (file)
@@ -22,7 +22,6 @@
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
-import asyncio
 import datetime
 import json
 import logging
@@ -50,16 +49,13 @@ log = logging.getLogger(__name__)
 
 
 class AutoscalingService:
-    def __init__(self, config: Config, loop=None):
+    def __init__(self, config: Config):
         self.conf = config
-        if not loop:
-            loop = asyncio.get_event_loop()
-        self.loop = loop
         self.db_client = CommonDbClient(config)
-        self.mon_client = MonClient(config, loop=self.loop)
-        self.lcm_client = LcmClient(config, loop=self.loop)
+        self.mon_client = MonClient(config)
+        self.lcm_client = LcmClient(config)
 
-    async def configure_scaling_groups(self, nsr_id: str):
+    async def configure_scaling_groups(self, nsr_id: str, vnf_member_index=None):
         """
         Configures scaling groups for a network service. Creates records in DB. Creates alarms in MON.
         :param nsr_id: Network service record id
@@ -73,7 +69,13 @@ class AutoscalingService:
         try:
             with database.db.atomic() as tx:
                 try:
-                    vnfrs = self.db_client.get_vnfrs(nsr_id)
+                    if vnf_member_index is None:
+                        vnfrs = self.db_client.get_vnfrs(nsr_id)
+                    else:
+                        vnfrs = []
+                        vnfr = self.db_client.get_vnfr(nsr_id, vnf_member_index)
+                        vnfrs.append(vnfr)
+                    # vnfrs = self.db_client.get_vnfrs(nsr_id)
                     for vnfr in vnfrs:
                         log.debug("Processing vnfr: %s", vnfr)
                         vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"])
@@ -192,7 +194,7 @@ class AutoscalingService:
                                                     operation=scaling_criteria[
                                                         "scale-in-relational-operation"
                                                     ],
-                                                    action="scale_in"
+                                                    action="scale_in",
                                                 )
                                             )
                                             alarm = ScalingAlarmRepository.create(
@@ -256,8 +258,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: