From eb5aa76ae65a4e4a5431e78a525f3bbfaae505d0 Mon Sep 17 00:00:00 2001 From: Wachtel Date: Tue, 1 Oct 2024 12:08:12 +0200 Subject: [PATCH] updated existing files to work with the MBCS app --- app/utils.py | 42 ++++++ config_EDC_consumer_light.ipynb | 232 ++++++++++++++++++++++++++++---- requirements.txt | 2 + 3 files changed, 252 insertions(+), 24 deletions(-) diff --git a/app/utils.py b/app/utils.py index 177c21c..be43acd 100644 --- a/app/utils.py +++ b/app/utils.py @@ -6,6 +6,7 @@ + # --- EDC Configuration --- # extract the correct data offer from the received catalog def get_data_offer(catalog_agreements): @@ -59,6 +60,47 @@ def offer2et(offer, edc_ccb, header_cpc): return 0, et_dict + +# --- Processing --- +def get_data_er(assetId, header_control_plane, url_edc_consumer_control_plane_base): # get data endpoint reference (+token) + """ + Oututs: + status + error info XOR et_dict + + """ + agreement_body = { + "@context": { + "@vocab": "https://w3id.org/edc/v0.0.1/ns/" + }, + "@type": "QuerySpec", + "filterExpression": [ + { + "operandLeft": "assetId", + "operator": "=", + "operandRight": assetId, + } + ] + } + # query the edrs: + res_catalog_agreement = requests.post(url=url_edc_consumer_control_plane_base + '/management/v2/edrs/request', headers=header_control_plane, json=agreement_body) + + res_offer, offer = get_data_offer(res_catalog_agreement.json()) + str_offer_status = "Status Offer: "+ str(res_offer) + + if res_offer != 0: + return res_offer, str_offer_status + + # obtain the necessary data from the dictionary + res_et, et_dict = offer2et(offer, url_edc_consumer_control_plane_base, header_control_plane) + if res_et == -2: + return res_et, "Token Request Failed:" + str(et_dict) + elif res_et != 0: + return res_et, "Unknown error" + + return res_et, et_dict + + # --- EDR Negotiations --- # catalog requests: def create_generic_catalog_request_body(asset_type, edc_provider_bpn, url_edc_provider_control_plane_base): diff --git a/config_EDC_consumer_light.ipynb b/config_EDC_consumer_light.ipynb index 6b59721..7cfd052 100644 --- a/config_EDC_consumer_light.ipynb +++ b/config_EDC_consumer_light.ipynb @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -48,18 +48,29 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "object_of_agreement = 'simple_test' # we 'magically' know this due to the push notification" + "object_of_agreement = 'MB-DSCS'#'simple_test' # we 'magically' know this due to the push notification" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# see if there are some edrs which have beennegotiated for\n", "agreement_body = {\n", @@ -82,9 +93,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Status Offer: 0\n", + "Status Endpoint and Token: 0\n" + ] + } + ], "source": [ "res_offer, offer = get_data_offer(res_catalog_agreement.json())\n", "print(\"Status Offer: \"+ str(res_offer))\n", @@ -100,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -109,9 +129,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# obtain data:\n", "res_data_info = requests.get(url=et_dict['endpoint'], headers={'Authorization': et_dict['token']})\n", @@ -120,14 +151,97 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "res_data = requests.get(url=et_dict['endpoint'] + '/$value?extent=WithBlobValue', headers={'Authorization': et_dict['token']})\n", "res_data" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "element_id = res_data_info.json()['submodelElements'][0]['idShort']\n", + "value_enc = res_data.json()[element_id]['value']" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5654100" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(value_enc)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "from app.mbcs_business_logic import csvblob2dataframe\n", + "df_gen = csvblob2dataframe(value_enc)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "from app.mbcs_business_logic import evaluate_usecase" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Component_1': {'Status': False, 'CS-Serivce': np.True_},\n", + " 'Component_2': {'Status': False, 'CS-Serivce': np.False_},\n", + " 'M': False}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "evaluate_usecase(df_gen)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -147,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -177,9 +291,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- Catalog Offer - dcat:Catalog 980b980b-a1b5-419a-8915-aa2a733e40fe\n", + "Index id Action Obligation \n", + "-------- ------------------------------------------ ------------------------------------------ ------------------------------------------\n", + "0: 1d38a8d8-c676-4d9b-b151-277953d3b3a5 http://www.w3.org/ns/odrl/2/use [] \n", + "1: 03175311-7430-4096-a60b-eb98a2d84b46 http://www.w3.org/ns/odrl/2/use [] \n", + "2: simple_test http://www.w3.org/ns/odrl/2/use [] \n", + "3: 8a960fe2-e325-4d4c-b567-6ea1af95c695 http://www.w3.org/ns/odrl/2/use [] \n", + "4: MB-DS http://www.w3.org/ns/odrl/2/use [] \n", + "5: MB-DSCS http://www.w3.org/ns/odrl/2/use [] \n", + "\n", + "dspace:connector @https://dataprovider-controlplane.factory-x.catena-x.net/api/v1/dsp\n", + "\n" + ] + } + ], "source": [ "# note: we query against our own EDC (the consumer EDC, who then will negotiate with the target EDC)\n", "res_catalog = requests.post(url=url_edc_consumer_control_plane_base + '/management/v2/catalog/request', headers=header_control_plane, json=catalog_request_body)\n", @@ -188,7 +321,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -212,9 +345,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# create request body for the EDR negotiate\n", "edr_negotiation_body = create_poc_ContractRequest_body(dct_endpointUrl, offer_id, edc_provider_bpn, object_of_agreement)\n", @@ -224,9 +368,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'7482df38-644b-4dfd-98a0-6c77121a7897'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "edr_negotation_id = res_edr_negotiation.json()['@id']\n", "edr_negotation_id # <- necessary to get the state " @@ -234,9 +389,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# get the negotiation state:\n", "res_get_edr_negotiation_state = requests.get(url=url_edc_consumer_control_plane_base + '/management/v2/contractnegotiations/' + edr_negotation_id + '/state', headers=header_control_plane)\n", @@ -245,9 +411,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'@type': 'NegotiationState',\n", + " 'state': 'FINALIZED',\n", + " '@context': {'@vocab': 'https://w3id.org/edc/v0.0.1/ns/',\n", + " 'edc': 'https://w3id.org/edc/v0.0.1/ns/',\n", + " 'tx': 'https://w3id.org/tractusx/v0.0.1/ns/',\n", + " 'tx-auth': 'https://w3id.org/tractusx/auth/',\n", + " 'cx-policy': 'https://w3id.org/catenax/policy/',\n", + " 'odrl': 'http://www.w3.org/ns/odrl/2/'}}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "res_get_edr_negotiation_state.json() # <- this should have finalized" ] diff --git a/requirements.txt b/requirements.txt index c6c5d3b..34c9f6d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ PyYAML==6.0.2 requests==2.32.3 +numpy==2.1.1 +pandas==2.2.3 fastapi[standard]>=0.115.0 \ No newline at end of file