Skip to content

Commit

Permalink
updated existing files to work with the MBCS app
Browse files Browse the repository at this point in the history
  • Loading branch information
Wachtel authored and Wachtel committed Oct 1, 2024
1 parent 2842576 commit eb5aa76
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 24 deletions.
42 changes: 42 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@




# --- EDC Configuration ---
# extract the correct data offer from the received catalog
def get_data_offer(catalog_agreements):
Expand Down Expand Up @@ -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):
Expand Down
232 changes: 208 additions & 24 deletions config_EDC_consumer_light.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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": [
"<Response [200]>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# see if there are some edrs which have beennegotiated for\n",
"agreement_body = {\n",
Expand All @@ -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",
Expand All @@ -100,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -109,9 +129,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"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",
Expand All @@ -120,14 +151,97 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"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,
Expand All @@ -147,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -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",
Expand All @@ -188,7 +321,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -212,9 +345,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"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",
Expand All @@ -224,19 +368,41 @@
},
{
"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 "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"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",
Expand All @@ -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"
]
Expand Down
Loading

0 comments on commit eb5aa76

Please sign in to comment.