Commit d4ce7132 authored by Pelayo Torres's avatar Pelayo Torres
Browse files

events logic and apiStatus

parent c033343a
Loading
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ from models.event_notification import EventNotification
from models.access_control_policy_list_ext import AccessControlPolicyListExt
from models.capif_event_detail import CAPIFEventDetail
from models.event_subscription import EventSubscription
from models.service_api_description import ServiceAPIDescription
from encoder import CustomJSONEncoder
import sys
import json
@@ -34,15 +35,14 @@ class Notifications():
                if redis_event.get('key', None) != None and redis_event.get('information', None) != None:
                    event_detail={}
                    for pos, key in enumerate(redis_event.get('key', None)):
                        current_app.logger.debug(sub["supported_features"])
                        current_app.logger.debug("AQUI")
                        if sub["supported_features"] is not None:
                            if redis_event.get('event', None) in ["SERVICE_API_AVAILABLE", "SERVICE_API_UNAVAILABLE"] and key == "serviceAPIDescriptions" and not EventSubscription.return_supp_feat_dict(sub["supported_features"])["ApiStatusMonitoring"]:
                                current_app.logger.debug("ApiStatusMonitoring not supported.")
                            else:

                        if sub["supported_features"] is None:
                            if not (key == "serviceAPIDescriptions" and redis_event.get('event', None) in ["SERVICE_API_AVAILABLE", "SERVICE_API_UNAVAILABLE"]):
                                event_detail[key]=redis_event.get('information', None)[pos]
                        else:
                            if not (redis_event.get('event', None) in ["SERVICE_API_AVAILABLE", "SERVICE_API_UNAVAILABLE"] and key == "serviceAPIDescriptions" and not EventSubscription.return_supp_feat_dict(sub["supported_features"])["ApiStatusMonitoring"]):
                                event_detail[key]=redis_event.get('information', None)[pos]

                    current_app.logger.debug(event_detail)
                    data.event_detail=event_detail

+15 −1
Original line number Diff line number Diff line
@@ -24,25 +24,39 @@ def clean_empty(d):


def dict_to_camel_case(my_dict):


        result = {}

        for attr, value in my_dict.items():

            if len(attr.split('_')) != 1:
                my_key = ''.join(word.title() for word in attr.split('_'))
                my_key = ''.join([my_key[0].lower(), my_key[1:]])
            else:
                my_key = attr

            if my_key == "serviceApiCategory":
                my_key = "serviceAPICategory"
            elif my_key == "serviceApiDescriptions":
                my_key = "serviceAPIDescriptions"

            if isinstance(value, list):
                result[my_key] = list(map(
                    lambda x: dict_to_camel_case(x) if isinstance(x, dict) else x, value ))

            elif hasattr(value, "to_dict"):
                result[my_key] = dict_to_camel_case(value)

            elif isinstance(value, dict):
                value = dict_to_camel_case(value)
                result[my_key] = value
            else:
                result[my_key] = value

        return result


def _deserialize(data, klass):
    """Deserializes dict, list, str into an object.

+26 −14
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ class PublishServiceOperations(Resource):
                        if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]:
                            current_app.logger.info(f"Service available")
                            RedisEvent("SERVICE_API_AVAILABLE", ["serviceAPIDescriptions", "apiIds"],
                                    [[serviceapidescription.to_dict()],[str(api_id)]]).send_event()
                                    [[clean_n_camel_case(serviceapidescription.to_dict())],[str(api_id)]]).send_event()
                        else:
                            current_app.logger.info("Service available")
                            RedisEvent("SERVICE_API_AVAILABLE", ["apiIds"],
@@ -143,7 +143,7 @@ class PublishServiceOperations(Resource):
                        if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]:
                            current_app.logger.info(f"Service unavailable")
                            RedisEvent("SERVICE_API_UNAVAILABLE", ["serviceAPIDescriptions", "apiIds"],
                                    [[serviceapidescription.to_dict()],[str(api_id)]]).send_event()
                                    [[clean_n_camel_case(serviceapidescription.to_dict())],[str(api_id)]]).send_event()
                        else:
                            current_app.logger.info("Service available")
                            RedisEvent("SERVICE_API_UNAVAILABLE", ["apiIds"],
@@ -205,9 +205,9 @@ class PublishServiceOperations(Resource):
                return result

            my_query = {'apf_id': apf_id, 'api_id': service_api_id}
            serviceapidescription = mycol.find_one(my_query, {"_id": 0})
            serviceapidescription_dict = mycol.find_one(my_query, {"_id": 0, "onboarding_date": 0, "apf_id": 0})

            if serviceapidescription is None:
            if serviceapidescription_dict is None:
                current_app.logger.error(service_api_not_found_message)
                return not_found_error(detail="Service API not existing", cause="Service API id not found")

@@ -218,16 +218,28 @@ class PublishServiceOperations(Resource):
            current_app.logger.debug("Removed service from database")
            out = "The service matching api_id " + service_api_id + " was deleted."
            res = make_response(out, status=204)
            serviceapidescription= clean_empty(dict_to_camel_case(serviceapidescription_dict))
            if res.status_code == 204:
                current_app.logger.info("Removed service published")
                if ServiceAPIDescription.return_supp_feat_dict(serviceapidescription["supported_features"])["ApiStatusMonitoring"]:
                    current_app.logger.info(f"Service unavailable")
                    RedisEvent("SERVICE_API_UNAVAILABLE", ["serviceAPIDescriptions", "apiIds"],
                            [[serviceapidescription],[str(service_api_id)]]).send_event()
                is_supported = serviceapidescription.get("supportedFeatures") and \
               ServiceAPIDescription.return_supp_feat_dict(serviceapidescription["supportedFeatures"]).get("ApiStatusMonitoring")

                if is_supported:
                    current_app.logger.info("Service unavailable")
                    RedisEvent(
                        "SERVICE_API_UNAVAILABLE",
                        ["serviceAPIDescriptions", "apiIds"],
                        [[serviceapidescription], [str(service_api_id)]]
                    ).send_event()

                else:
                    current_app.logger.info("Service available")
                    RedisEvent("SERVICE_API_UNAVAILABLE", ["apiIds"],
                            [str(service_api_id)]).send_event()
                    status_message = "Service available" if serviceapidescription.get("supportedFeatures") is None else "Service unavailable"
                    current_app.logger.info(status_message)
                    RedisEvent(
                        "SERVICE_API_UNAVAILABLE",
                        ["apiIds"],
                        [str(service_api_id)]
                    ).send_event()

            return res

        except Exception as e:
@@ -280,7 +292,7 @@ class PublishServiceOperations(Resource):
                        if service_api_description.return_supp_feat_dict(service_api_description.supported_features)["ApiStatusMonitoring"]:
                            current_app.logger.info(f"Service available")
                            RedisEvent("SERVICE_API_AVAILABLE", ["serviceAPIDescriptions", "apiIds"],
                                    [[service_api_description_updated],[str(service_api_id)]]).send_event()
                                    [[service_api_description],[str(service_api_id)]]).send_event()
                        else:
                            current_app.logger.info("Service available")
                            RedisEvent("SERVICE_API_AVAILABLE", ["apiIds"],
@@ -294,7 +306,7 @@ class PublishServiceOperations(Resource):
                        if service_api_description.return_supp_feat_dict(service_api_description.supported_features)["ApiStatusMonitoring"]:
                            current_app.logger.info(f"Service unavailable")
                            RedisEvent("SERVICE_API_UNAVAILABLE", ["serviceAPIDescriptions", "apiIds"],
                                    [[service_api_description_updated],[str(service_api_id)]]).send_event()
                                    [[service_api_description],[str(service_api_id)]]).send_event()
                        else:
                            current_app.logger.info("Service available")
                            RedisEvent("SERVICE_API_UNAVAILABLE", ["apiIds"],