diff --git a/services/TS29222_CAPIF_Events_API/capif_events/app.py b/services/TS29222_CAPIF_Events_API/capif_events/app.py index a4eb7477c05bf2327a43a523732ecf665d7f511f..d21ee573a3cbbf0f63948e8686652ca56e32d3e6 100644 --- a/services/TS29222_CAPIF_Events_API/capif_events/app.py +++ b/services/TS29222_CAPIF_Events_API/capif_events/app.py @@ -17,6 +17,7 @@ from multiprocessing import Process from threading import Thread from flask_executor import Executor from flask_apscheduler import APScheduler +from datetime import datetime from logging.handlers import RotatingFileHandler from config import Config import os @@ -144,18 +145,11 @@ if monitoring_value == "true": config = Config() executor = Executor(app.app) subscriber = Subscriber() -# scheduler = APScheduler() -# scheduler.init_app(app.app) - -# @app.app.before_first_request -# def create_listener_message(): -# executor.submit(subscriber.listen) - -first = True - -@app.app.before_request -def create_listener_message(): - global first - if first: - executor.submit(subscriber.listen) - first = False +scheduler = APScheduler() +scheduler.init_app(app.app) +scheduler.start() + +@scheduler.task('date', id='listener', next_run_time=datetime.now()) +def up_listener(): + with scheduler.app.app_context(): + executor.submit(subscriber.listen()) diff --git a/services/TS29222_CAPIF_Events_API/capif_events/core/events_apis.py b/services/TS29222_CAPIF_Events_API/capif_events/core/events_apis.py index 62604e4e3bf4b3723ab0917332f0236ee7a4ace0..f0226d0fa230277081026b98d547ce7742c684c6 100644 --- a/services/TS29222_CAPIF_Events_API/capif_events/core/events_apis.py +++ b/services/TS29222_CAPIF_Events_API/capif_events/core/events_apis.py @@ -14,6 +14,7 @@ from .responses import internal_server_error, not_found_error, make_response, ba from ..db.db import MongoDatabse from ..util import serialize_clean_camel_case, clean_empty, dict_to_camel_case from .auth_manager import AuthManager +import os class EventSubscriptionsOperations(Resource): @@ -53,6 +54,13 @@ class EventSubscriptionsOperations(Resource): current_app.logger.error("Bad url format") return bad_request_error(detail="Bad Param", cause = "Detected Bad formar of param", invalid_params=[{"param": "notificationDestination", "reason": "Not valid URL format"}]) + if event_subscription.supported_features is None: + return bad_request_error( + detail="supportedFeatures not present in request", + cause="supportedFeatures not present", + invalid_params=[{"param": "supportedFeatures", "reason": "not defined"}] + ) + ## Verify that this subscriberID exist in publishers or invokers result = self.__check_subscriber_id(subscriber_id) @@ -75,8 +83,7 @@ class EventSubscriptionsOperations(Resource): self.auth_manager.add_auth_event(subscription_id, subscriber_id) res = make_response(object=serialize_clean_camel_case(event_subscription), status=201) - res.headers['Location'] = "http://localhost:8080/capif-events/v1/" + \ - str(subscriber_id) + "/subscriptions/" + str(subscription_id) + res.headers['Location'] = f"http://{os.getenv("CAPIF_HOSTNAME")}/capif-events/v1/{str(subscriber_id)}/subscriptions/{str(subscription_id)}" return res @@ -124,6 +131,13 @@ class EventSubscriptionsOperations(Resource): mycol = self.db.get_col_by_name(self.db.event_collection) current_app.logger.debug("Updating event subscription") + + if event_subscription.supported_features is None: + return bad_request_error( + detail="supportedFeatures not present in request", + cause="supportedFeatures not present", + invalid_params=[{"param": "supportedFeatures", "reason": "not defined"}] + ) result = self.__check_subscriber_id(subscriber_id) diff --git a/services/TS29222_CAPIF_Events_API/capif_events/core/notifications.py b/services/TS29222_CAPIF_Events_API/capif_events/core/notifications.py index c1c7db19717d20ef4bce042a17268854e1474ea3..f53791726a997e353ed72cdc61450d48866c45c6 100644 --- a/services/TS29222_CAPIF_Events_API/capif_events/core/notifications.py +++ b/services/TS29222_CAPIF_Events_API/capif_events/core/notifications.py @@ -33,18 +33,27 @@ class Notifications(): current_app.logger.debug(url) data = EventNotification(sub["subscription_id"], events=redis_event.get('event')) 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)): + if EventSubscription.return_supp_feat_dict(sub["supported_features"])["EnhancedEventReport"]: + event_detail={} + for pos, key in enumerate(redis_event.get('key', None)): + current_app.logger.debug(f"information: {redis_event.get('information', None)[pos]}") + if redis_event.get('event', None) in ["SERVICE_API_AVAILABLE", "SERVICE_API_UNAVAILABLE"] and key == "apiIds": + event_detail["apiIds"]=[redis_event.get('information', None)[pos]] + elif EventSubscription.return_supp_feat_dict(sub["supported_features"])["ApiStatusMonitoring"] and key == "serviceAPIDescriptions": + event_detail["serviceAPIDescriptions"]=redis_event.get('information', None)[pos] + elif redis_event.get('event', None) in ["SERVICE_API_UPDATE"]: + event_detail["serviceAPIDescriptions"]=redis_event.get('information', None)[pos] + elif redis_event.get('event', None) in ["API_INVOKER_ONBOARDED", "API_INVOKER_OFFBOARDED", "API_INVOKER_UPDATED"]: + event_detail["apiInvokerIds"]=redis_event.get('information', None)[pos] + elif redis_event.get('event', None) in ["ACCESS_CONTROL_POLICY_UPDATE"]: + event_detail["accCtrlPolList"]=redis_event.get('information', None)[pos] + elif redis_event.get('event', None) in ["SERVICE_API_INVOCATION_SUCCESS", "SERVICE_API_INVOCATION_FAILURE"]: + event_detail["invocationLogs"]=redis_event.get('information', None)[pos] + elif redis_event.get('event', None) in ["API_TOPOLOGY_HIDING_CREATED", "API_TOPOLOGY_HIDING_REVOKED"]: + event_detail["apiTopoHide"]=redis_event.get('information', None)[pos] - 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 + current_app.logger.debug(event_detail) + data.event_detail=event_detail current_app.logger.debug(json.dumps(data.to_dict(),cls=CustomJSONEncoder)) diff --git a/services/TS29222_CAPIF_Publish_Service_API/published_apis/core/serviceapidescriptions.py b/services/TS29222_CAPIF_Publish_Service_API/published_apis/core/serviceapidescriptions.py index c9714caa0cb3851ce812341c90b5d6a003869a30..4a05a00747df3584a465018b03fc3b245bc9c4ef 100644 --- a/services/TS29222_CAPIF_Publish_Service_API/published_apis/core/serviceapidescriptions.py +++ b/services/TS29222_CAPIF_Publish_Service_API/published_apis/core/serviceapidescriptions.py @@ -132,39 +132,27 @@ class PublishServiceOperations(Resource): if res.status_code == 201: current_app.logger.info("Service published") if serviceapidescription.api_status is None or len(serviceapidescription.api_status.aef_ids) > 0: - if serviceapidescription.supported_features is not None: - if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: - current_app.logger.info(f"Service available") - RedisEvent("SERVICE_API_AVAILABLE", - ["serviceAPIDescriptions", "apiIds"], - [[clean_n_camel_case(serviceapidescription.to_dict())], [str(api_id)]]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_AVAILABLE", - ["apiIds"], - [str(api_id)]).send_event() + if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: + current_app.logger.info(f"Service available") + RedisEvent("SERVICE_API_AVAILABLE", + ["serviceAPIDescriptions", "apiIds"], + [[clean_n_camel_case(serviceapidescription.to_dict())], [str(api_id)]]).send_event() else: current_app.logger.info("Service available") RedisEvent("SERVICE_API_AVAILABLE", - ["apiIds"], - [str(api_id)]).send_event() + ["apiIds"], + [str(api_id)]).send_event() else: - if serviceapidescription.supported_features is not None: - if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: - current_app.logger.info(f"Service unavailable") - RedisEvent("SERVICE_API_UNAVAILABLE", - ["serviceAPIDescriptions", "apiIds"], - [[clean_n_camel_case(serviceapidescription.to_dict())], [str(api_id)]]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_UNAVAILABLE", - ["apiIds"], - [str(api_id)]).send_event() + if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: + current_app.logger.info(f"Service unavailable") + RedisEvent("SERVICE_API_UNAVAILABLE", + ["serviceAPIDescriptions", "apiIds"], + [[clean_n_camel_case(serviceapidescription.to_dict())], [str(api_id)]]).send_event() else: current_app.logger.info("Service available") RedisEvent("SERVICE_API_UNAVAILABLE", - ["apiIds"], - [str(api_id)]).send_event() + ["apiIds"], + [str(api_id)]).send_event() return res