Commit c076157e authored by Javier Velázquez's avatar Javier Velázquez
Browse files

Fix the use of async functions

parent fce0e374
Loading
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -799,7 +799,7 @@ class Api:

    # --- SUBSCRIPTIONS ---

    async def get_subscriptions(self, client_id, slice_id = None):
    def get_subscriptions(self, client_id, slice_id = None):
        try:
            if slice_id is not None:
                try:
@@ -809,7 +809,7 @@ class Api:
                if not subscription:
                    raise ValueError(f"Client '{client_id}' has no subscription for slice '{slice_id}'")
                
                telemetry = await self.get_telemetry(slice_id)
                telemetry = self.get_telemetry(slice_id)

                return {
                    **subscription,
@@ -823,7 +823,7 @@ class Api:
            for sub in subscriptions:
                slice_id = sub["slice_id"]

                telemetry = await self.get_telemetry(slice_id)
                telemetry = self.get_telemetry(slice_id)

                result.append({
                    **sub,
@@ -840,7 +840,7 @@ class Api:
        except Exception as e:
            return send_response(False, code=500, message=str(e))

    async def add_subscription(self, client_id, slice_id, frequency):
    def add_subscription(self, client_id, slice_id, frequency):
        try:
            try:
                subscription = get_subscription(client_id, slice_id)
@@ -869,7 +869,7 @@ class Api:
        except Exception as e:
            return send_response(False, code=500, message=str(e))

    async def update_subscription(self, client_id, slice_id, frequency):
    def update_subscription(self, client_id, slice_id, frequency):
        try:
            try:
                subscription = get_subscription(client_id, slice_id)
@@ -918,7 +918,7 @@ class Api:

    # --- TELEMETRY ---

    async def get_telemetry(self, slice_id = None):
    def get_telemetry(self, slice_id = None):
        logging.debug(f"Getting telemetry for slice_id: {slice_id}")
        try:         
            if slice_id is not None:
@@ -938,7 +938,7 @@ class Api:
                slo_sle_template = next(iter(slo_sle_template), None)
                if not slo_sle_template:
                    raise ValueError(f"SLO/SLE template '{template_id}' not found for slice '{slice_id}'")
                metrics = await self.slice_service.monitoring(slice_id, slo_sle_template, slice_sdps)
                metrics = self.slice_service.monitoring(slice_id, slo_sle_template, slice_sdps)
                return metrics, 200
            
            telemetry_data = {}
@@ -960,7 +960,7 @@ class Api:
                    raise ValueError("No SDPs found")
                if len(slice_sdps) > 2:
                    raise Exception(f"Monitoring for more than 2 SDPs is not supported. Found {len(slice_sdps)} SDPs.")
                telemetry_data[slice_id] = await self.slice_service.monitoring(slice_id, slo_sle_template, slice_sdps)
                telemetry_data[slice_id] = self.slice_service.monitoring(slice_id, slo_sle_template, slice_sdps)
            return telemetry_data, 200

        except ValueError as e:
@@ -981,7 +981,7 @@ class Api:
    async def stream_client_subscriptions(self, client_id):
        while True:
            try:
                data, code = await self.get_subscriptions(client_id)
                data, code = self.get_subscriptions(client_id)
                if code == 200:
                    yield f"data: {json.dumps(data)}\n\n"
                    subs = data.get("subscriptions", [])
@@ -997,7 +997,7 @@ class Api:
    async def stream_slice_subscription(self, client_id, slice_id):
        while True:
            try:
                data, code = await self.get_subscriptions(client_id, slice_id)
                data, code = self.get_subscriptions(client_id, slice_id)
                if code == 200:
                    yield f"data: {json.dumps(data)}\n\n"
                    freq = data.get("frequency", 5)
+2 −2
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class NSController:
            "setup_time": setup_time
        }
    
    async def monitoring(self, slice_id, slo_sle_template, sdps):
    def monitoring(self, slice_id, slo_sle_template, sdps):
        """
        Monitor the status of a specific network slice.

@@ -159,7 +159,7 @@ class NSController:
        }

        # Request the realizer to retrieve the metrics for the links of the specified slice
        await realizer(payload, action="MONITOR", controller_type=self.controller_type)
        realizer(payload, action="MONITOR", controller_type=self.controller_type)

        # Request the mapper to aggregate the metrics and store them in the database
        metrics = mapper(payload, action="MONITOR")
+2 −2
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import asyncio
from .restconf.connectors.tfs_connector import tfs_connector
from flask import current_app

async def get_metrics(path, slice_id, controller_type):
def get_metrics(path, slice_id, controller_type):
    if controller_type == "RESTCONF":
        links = []
        if slice_id not in current_app.config["TELEMETRY_CACHE"]: 
@@ -30,6 +30,6 @@ async def get_metrics(path, slice_id, controller_type):
                current_app.config["TELEMETRY_CACHE"]
            )
            future = asyncio.run_coroutine_threadsafe(coro, bg_loop)
            await asyncio.wrap_future(future)
            future.result()
        else:
            logging.debug(f"Telemetry for slice '{slice_id}' already in cache. Skipping.")
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ from src.planner.shortest_path import get_shortest_path
from src.realizer.restconf.connectors.tfs_connector import tfs_connector
from flask import current_app

async def realizer(payload, need_nrp=False, order=None, nrp=None, controller_type=None, response=None, rules = None, action="CREATE"):
def realizer(payload, need_nrp=False, order=None, nrp=None, controller_type=None, response=None, rules = None, action="CREATE"):
    """
    Manage the slice creation workflow.

@@ -102,7 +102,7 @@ async def realizer(payload, need_nrp=False, order=None, nrp=None, controller_typ
            path, response = get_shortest_path(topology, sdp_ids[0], sdp_ids[1])
            if response == 200:
                logging.debug(f"Retrieved shortest path for slice '{slice_id}': {path}")
                await get_metrics(path, slice_id, controller_type)
                get_metrics(path, slice_id, controller_type)
            else:
                raise Exception("Error: Shortest path not retrieved ")
        else: