diff --git a/services/TS29222_CAPIF_Discover_Service_API/service_apis/controllers/default_controller.py b/services/TS29222_CAPIF_Discover_Service_API/service_apis/controllers/default_controller.py index 13fd27e34627e5419aeb0910fe9c6a662e5663a5..05c6d6a440ab84fdeb7df321e913fcc3aa74ce52 100644 --- a/services/TS29222_CAPIF_Discover_Service_API/service_apis/controllers/default_controller.py +++ b/services/TS29222_CAPIF_Discover_Service_API/service_apis/controllers/default_controller.py @@ -68,12 +68,15 @@ def all_service_apis_get(api_invoker_id, api_name=None, api_version=None, comm_t if request.is_json: service_kpis = ServiceKpis.from_dict(request.get_json()()) # noqa: E501 current_app.logger.info("Discovering service apis") - query_params = {"api_name":api_name, "api_version":api_version, "comm_type":comm_type, - "protocol":protocol, "aef_id":aef_id, "data_format":data_format, - "api_cat":api_cat, "supported_features":supported_features, "api_supported_features":api_supported_features} + + query_params = {"api_name": api_name, "api_version": api_version, "comm_type": comm_type, + "protocol": protocol, "aef_id": aef_id, "data_format": data_format, + "api_cat": api_cat, "api_supported_features": api_supported_features, + "supported_features": supported_features} if supported_features is not None: supp_feat_dict = DiscoveredAPIs.return_supp_feat_dict(supported_features) + current_app.logger.info(supp_feat_dict) if supp_feat_dict['VendSpecQueryParams']: for q_params in request.args: if "vend-spec" in q_params: diff --git a/services/TS29222_CAPIF_Discover_Service_API/service_apis/core/discoveredapis.py b/services/TS29222_CAPIF_Discover_Service_API/service_apis/core/discoveredapis.py index e07e4a1fab8a746c729eb7c04f9e49622471e99b..8ac986ab3e7b6bdd0f9bb5e320ba371437d3311e 100644 --- a/services/TS29222_CAPIF_Discover_Service_API/service_apis/core/discoveredapis.py +++ b/services/TS29222_CAPIF_Discover_Service_API/service_apis/core/discoveredapis.py @@ -5,6 +5,20 @@ from ..models.discovered_apis import DiscoveredAPIs from ..util import serialize_clean_camel_case from ..core.resources import Resource import json +from ..vendor_specific import find_attribute_in_body, filter_apis_with_vendor_specific_params, remove_vendor_specific_fields + + +def filter_fields(filtered_apis): + key_filter = [ + "api_name", "api_id", "aef_profiles", "description", + "supported_features", "shareable_info", "service_api_category", + "api_supp_feats", "pub_api_path", "ccf_id" + ] + field_filtered_api = {} + for key in filtered_apis.keys(): + if key in key_filter or 'vendorSpecific' in key: + field_filtered_api[key] = filtered_apis[key] + return field_filtered_api class DiscoverApisOperations(Resource): @@ -37,17 +51,17 @@ class DiscoverApisOperations(Resource): "api_supported_features": "api_supp_feats" } + vend_spec_query_params_n_values = {} + supp_feat = query_params["supported_features"] + del query_params["supported_features"] + for param in query_params: - current_app.logger.info(query_params[param]) if query_params[param] is not None: if "vend-spec" in param: - current_app.logger.info("vend-spec in param") + vend_param = param.split("vend-spec-")[1] attribute_path = query_params[param]["target"].split('/') - attribute = "aef_profiles.0." + attribute_path[1] + "." + attribute_path[2] - my_params.append({attribute: query_params[param]["value"]}) + vend_spec_query_params_n_values[attribute_path[1] + "." + vend_param] = query_params[param]["value"] elif param in ["api_version", "comm_type", "protocol", "aef_id", "data_format"]: - current_app.logger.info(query_params_name[param]) - current_app.logger.info(query_params_name[param].replace("QPV", query_params[param])) my_params.append(json.loads(query_params_name[param].replace("QPV", query_params[param]))) else: my_params.append({query_params_name[param]: query_params[param]}) @@ -55,10 +69,29 @@ class DiscoverApisOperations(Resource): if my_params: my_query = {"$and": my_params} - discoved_apis = services.find(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}) + discoved_apis = services.find(my_query, {"_id":0}) json_docs = [] - for discoved_api in discoved_apis: - json_docs.append(discoved_api) + if supp_feat is None: + for discoved_api in discoved_apis: + vendor_specific_fields_path = find_attribute_in_body(discoved_api, '') + json_docs.append(filter_fields(remove_vendor_specific_fields(discoved_api, vendor_specific_fields_path))) + else: + supported_features = DiscoveredAPIs.return_supp_feat_dict(supp_feat) + if supported_features['VendSpecQueryParams']: + for discoved_api in discoved_apis: + vendor_specific_fields_path = find_attribute_in_body(discoved_api, '') + if vendor_specific_fields_path: + if vend_spec_query_params_n_values: + vs_filtered_apis = filter_apis_with_vendor_specific_params(discoved_api, vend_spec_query_params_n_values, vendor_specific_fields_path) + if vs_filtered_apis: + json_docs.append(filter_fields(vs_filtered_apis)) + else: + json_docs.append(filter_fields(discoved_api)) + else: + for discoved_api in discoved_apis: + vendor_specific_fields_path = find_attribute_in_body(discoved_api, '') + if not vendor_specific_fields_path: + json_docs.append(filter_fields(discoved_api)) if len(json_docs) == 0: return not_found_error(detail="API Invoker " + api_invoker_id + " has no API Published that accomplish filter conditions", cause="No API Published accomplish filter conditions") diff --git a/services/TS29222_CAPIF_Discover_Service_API/service_apis/models/discovered_apis.py b/services/TS29222_CAPIF_Discover_Service_API/service_apis/models/discovered_apis.py index a3d5e36006dfd66181253a58d56cb77744a5fcbd..cce0ff8b29b46d1d9d46af2f735ae215a2570931 100644 --- a/services/TS29222_CAPIF_Discover_Service_API/service_apis/models/discovered_apis.py +++ b/services/TS29222_CAPIF_Discover_Service_API/service_apis/models/discovered_apis.py @@ -32,13 +32,14 @@ class DiscoveredAPIs(Model): @classmethod def return_supp_feat_dict(cls, supp_feat): + TOTAL_FEATURES = 3 supp_feat_in_hex = int(supp_feat, 16) - supp_feat_in_bin = bin(supp_feat_in_hex)[2:] + supp_feat_in_bin = bin(supp_feat_in_hex)[2:].zfill(TOTAL_FEATURES)[::-1] return { - "RNAA": True if supp_feat_in_bin[0] == "1" else False, + "ApiSupportedFeatureQuery": True if supp_feat_in_bin[0] == "1" else False, "VendSpecQueryParams": True if supp_feat_in_bin[1] == "1" else False, - "ApiSupportedFeatureQuery": True if supp_feat_in_bin[2] == "1" else False, + "RNAA": True if supp_feat_in_bin[2] == "1" else False } @classmethod diff --git a/services/TS29222_CAPIF_Discover_Service_API/service_apis/vendor_specific.py b/services/TS29222_CAPIF_Discover_Service_API/service_apis/vendor_specific.py new file mode 100644 index 0000000000000000000000000000000000000000..f7a71ebb339d1e0b874b1824724d05de12a114c6 --- /dev/null +++ b/services/TS29222_CAPIF_Discover_Service_API/service_apis/vendor_specific.py @@ -0,0 +1,91 @@ +import re + + +def find_attribute_in_body(test, path): + f_key = [] + if type(test) == dict: + for k, v in test.items(): + if 'vendorSpecific' in k: + if path == '': + temp_path = k + else: + temp_path = path + "." + k + f_key.append(temp_path) + elif type(v) == dict: + if path == '': + temp_path = k + else: + temp_path = path + "." + k + f_key += find_attribute_in_body(v, temp_path) + elif type(v) == list: + if path == '': + temp_path = k + else: + temp_path = path + "." + k + for i, val in enumerate(v): + f_key += find_attribute_in_body(val, temp_path + "." + str(i)) + return f_key + + +def vendor_specific_key_n_value(vendor_specific_fields, body): + vendor_specific = {} + for field in vendor_specific_fields: + parts = field.split('.') + tmp_body = body + for part in parts: + if part.isnumeric(): + part = int(part) + v = tmp_body[part] + tmp_body = v + vendor_specific[field] = v + return vendor_specific + + +def add_vend_spec_fields(vendor_specific, serviceapidescription_dict): + pattern = re.compile(r'(?