diff --git a/services/TS29222_CAPIF_Publish_Service_API/published_apis/controllers/default_controller.py b/services/TS29222_CAPIF_Publish_Service_API/published_apis/controllers/default_controller.py index 3e2ddb97608cc3473c1c2c6728d27e1bc8852c87..b5bed44324c851bd2abeebaa5826f4a8635db94e 100644 --- a/services/TS29222_CAPIF_Publish_Service_API/published_apis/controllers/default_controller.py +++ b/services/TS29222_CAPIF_Publish_Service_API/published_apis/controllers/default_controller.py @@ -173,6 +173,13 @@ def apf_id_service_apis_service_api_id_put(service_api_id, apf_id, body): # noq current_app.logger.info( "Updating service api id with id: " + service_api_id) + if 'supportedFeatures' not in body: + return bad_request_error( + detail="supportedFeatures not present in request", + cause="supportedFeatures not present", + invalid_params=[{"param": "supportedFeatures", "reason": "not defined"}] + ) + if request.is_json: body = ServiceAPIDescription.from_dict(request.get_json()) # noqa: E501 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 83d5f7bea82e175fe1796de590d7f5d4cf13d7c5..ffc31e59a6df7b2d6b136e445b6d70ae89fc946c 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,28 +132,13 @@ 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.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: - current_app.logger.info(f"Service available") - RedisEvent("SERVICE_API_AVAILABLE", - service_api_descriptions=[clean_n_camel_case( - serviceapidescription.to_dict())], - api_ids=[str(api_id)]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_AVAILABLE", - api_ids=[str(api_id)]).send_event() - else: - if serviceapidescription.return_supp_feat_dict(serviceapidescription.supported_features)["ApiStatusMonitoring"]: - current_app.logger.info(f"Service unavailable") - RedisEvent("SERVICE_API_UNAVAILABLE", - service_api_descriptions=[clean_n_camel_case( - serviceapidescription.to_dict())], - api_ids=[str(api_id)]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_UNAVAILABLE", - api_ids=[str(api_id)]).send_event() + event_to_send = self.service_api_availability_event( + clean_n_camel_case( + serviceapidescription_dict)) + RedisEvent(event_to_send, + service_api_descriptions=[clean_n_camel_case( + serviceapidescription.to_dict())], + api_ids=[str(api_id)]).send_event() return res @@ -229,25 +214,12 @@ class PublishServiceOperations(Resource): serviceapidescription = clean_empty( dict_to_camel_case(serviceapidescription_dict)) if res.status_code == 204: - 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", - service_api_descriptions=[serviceapidescription], - api_ids=[str(service_api_id)] - ).send_event() - - else: - status_message = "Service available" if serviceapidescription.get( - "supportedFeatures") is None else "Service unavailable" - current_app.logger.info(status_message) - RedisEvent( - "SERVICE_API_UNAVAILABLE", api_ids=[str(service_api_id)] - ).send_event() + current_app.logger.info("Service unavailable") + RedisEvent( + "SERVICE_API_UNAVAILABLE", + service_api_descriptions=[serviceapidescription], + api_ids=[str(service_api_id)] + ).send_event() return res @@ -268,9 +240,18 @@ class PublishServiceOperations(Resource): "Updating service api with id: " + service_api_id) my_query = {'apf_id': apf_id, 'api_id': service_api_id} - serviceapidescription = mycol.find_one(my_query) - - if serviceapidescription is None: + serviceapidescription_old = mycol.find_one(my_query, {"_id": 0, + "api_name": 1, + "api_id": 1, + "aef_profiles": 1, + "description": 1, + "supported_features": 1, + "shareable_info": 1, + "service_api_category": 1, + "api_supp_feats": 1, + "pub_api_path": 1, + "ccf_id": 1}) + if serviceapidescription_old 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") @@ -278,7 +259,7 @@ class PublishServiceOperations(Resource): service_api_description = clean_empty(service_api_description) result = mycol.find_one_and_update( - serviceapidescription, + serviceapidescription_old, {"$set": service_api_description}, projection={"_id": 0, "api_name": 1, @@ -307,39 +288,12 @@ class PublishServiceOperations(Resource): if response.status_code == 200: RedisEvent("SERVICE_API_UPDATE", service_api_descriptions=[service_api_description_updated]).send_event() - if service_api_description.api_status is None or len(service_api_description.api_status.aef_ids) > 0: - if service_api_description.supported_features is not None: - 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", - service_api_descriptions=[ - service_api_description], - api_ids=[str(service_api_id)] - ).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_AVAILABLE", - api_ids=[str(service_api_id)]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_AVAILABLE", - api_ids=[str(service_api_id)]).send_event() - else: - if service_api_description.supported_features is not None: - 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", - service_api_descriptions=[ - service_api_description], - api_ids=[str(service_api_id)]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_UNAVAILABLE", - api_ids=[str(service_api_id)]).send_event() - else: - current_app.logger.info("Service available") - RedisEvent("SERVICE_API_UNAVAILABLE", - api_ids=[str(service_api_id)]).send_event() + + my_service_api = clean_n_camel_case(serviceapidescription_old) + self.send_events_on_update( + service_api_id, + my_service_api, + service_api_description_updated) return response @@ -347,3 +301,46 @@ class PublishServiceOperations(Resource): exception = "An exception occurred in update service" current_app.logger.error(exception + "::" + str(e)) return internal_server_error(detail=exception, cause=str(e)) + + def send_events_on_update(self, + service_api_id, + service_api_description_old, + service_api_description_new): + current_app.logger.debug("send Events if needed") + service_api_status_event_old = self.service_api_availability_event( + service_api_description_old) + service_api_status_event_new = self.service_api_availability_event( + service_api_description_old) + + if service_api_status_event_old == service_api_status_event_new: + current_app.logger.info( + "service_api_status not changed, it remains " + + service_api_status_event_new + + " Then no event will be sent") + else: + current_app.logger.info("service_api_status changed, event " + + service_api_status_event_new + + " Event will be sent") + RedisEvent(service_api_status_event_new, + service_api_descriptions=[ + service_api_description_new], + api_ids=[str(service_api_id)]).send_event() + + def service_api_availability_event(self, service_api_description): + service_api_status = "" + if ServiceAPIDescription.return_supp_feat_dict(service_api_description.get("supportedFeatures"))["ApiStatusMonitoring"]: + current_app.logger.info( + "ApiStatusMonitoring active") + if service_api_description.get("apiStatus") is None or len(service_api_description.get("apiStatus").get("aefIds")) > 0: + current_app.logger.info( + "Service available, at least one AEF is available") + service_api_status = "SERVICE_API_AVAILABLE" + else: + current_app.logger.info( + "Service unavailable, all AEFs are unavailable") + service_api_status = "SERVICE_API_UNAVAILABLE" + else: + current_app.logger.info("ApiStatusMonitoring") + current_app.logger.info("Service available") + service_api_status = "SERVICE_API_AVAILABLE" + return service_api_status