forked from eclipse-tractusx/item-relationship-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform-and-upload.py
726 lines (643 loc) · 31.3 KB
/
transform-and-upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/python
# #############################################################################
# Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
# Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
# #############################################################################
import argparse
import json
import math
import time
import uuid
from copy import copy
import requests
from requests.adapters import HTTPAdapter, Retry
def create_submodel_payload(json_payload):
return json.dumps(json_payload)
def create_edc_asset_payload(submodel_url_, asset_id_):
return json.dumps({
"@context": edc_context(),
"@id": f"{asset_id_}",
"edc:properties": {
"edc:description": "IRS EDC Test Asset",
"edc:id": f"{asset_id_}",
},
"edc:dataAddress": {
"@type": "edc:DataAddress",
"edc:type": "HttpData",
"edc:baseUrl": f"{submodel_url_}",
"edc:proxyPath": "true",
"edc:proxyBody": "false",
"edc:proxyMethod": "false",
"edc:proxyQueryParams": "false"
}
})
def create_edc_registry_asset_payload(registry_url_, asset_prop_id_):
return json.dumps({
"@context": edc_context(),
"@id": f"{asset_prop_id_}", # DTR-EDC-instance-unique-ID
"edc:properties": {
"edc:description": "Digital Twin Registry Endpoint of IRS DEV",
"edc:id": f"{asset_prop_id_}", # DTR-EDC-instance-unique-ID
"edc:type": "data.core.digitalTwinRegistry"
},
"edc:dataAddress": {
"@type": "edc:DataAddress",
"edc:type": "HttpData",
"edc:baseUrl": f"{registry_url_}",
"edc:proxyPath": "true",
"edc:proxyMethod": "true",
"edc:proxyQueryParams": "true",
"edc:proxyBody": "true",
"edc:contentType": "application-json"
}
})
def edc_context():
return {
"dct": "https://purl.org/dc/terms/",
"tx": "https://w3id.org/tractusx/v0.0.1/ns/",
"edc": "https://w3id.org/edc/v0.0.1/ns/",
"dcat": "https://www.w3.org/ns/dcat/",
"odrl": "http://www.w3.org/ns/odrl/2/",
"dspace": "https://w3id.org/dspace/v0.8/"
}
def create_edc_notification_asset_payload(ess_url_, asset_id_, notification_type_):
return json.dumps({
"@context": edc_context(),
"@id": f"{asset_id_}",
"edc:properties": {
"edc:description": "ESS notification endpoint",
"edc:id": f"{asset_id_}",
"edc:notificationtype": f"{notification_type_}",
"edc:notificationmethod": "receive"
},
"edc:dataAddress": {
"@type": "edc:DataAddress",
"edc:type": "HttpData",
"edc:baseUrl": f"{ess_url_}",
"edc:proxyBody": "true",
"edc:proxyMethod": "true",
}
})
def create_esr_edc_asset_payload(esr_url_, asset_prop_id_, digital_twin_id_):
return json.dumps({
"@context": edc_context(),
"@id": f"{asset_prop_id_}",
"edc:properties": {
"edc:description": "product description",
"edc:id": f"{asset_prop_id_}",
},
"edc:dataAddress": {
"@type": "edc:DataAddress",
"edc:type": "HttpData",
"edc:baseUrl": f"{esr_url_}/{digital_twin_id_}/asBuilt/ISO14001/submodel",
"edc:proxyBody": "true",
"edc:proxyMethod": "true",
}
})
def create_edc_contract_definition_payload(edc_policy_id_, asset_prop_id_):
return json.dumps({
"@context": edc_context(),
"@type": "ContractDefinition",
"accessPolicyId": f"{edc_policy_id_}",
"contractPolicyId": f"{edc_policy_id_}",
"assetsSelector": {
"@type": "CriterionDto",
"operandLeft": "https://w3id.org/edc/v0.0.1/ns/id",
"operator": "=",
"operandRight": f"{asset_prop_id_}"
}
}
)
def create_aas_shell(global_asset_id_, id_short_, identification_, specific_asset_id_, submodel_descriptors_):
return json.dumps({
"description": [],
"globalAssetId": {
"value": [
global_asset_id_
]
},
"idShort": id_short_,
"identification": identification_,
"specificAssetIds": specific_asset_id_,
"submodelDescriptors": submodel_descriptors_
})
def create_submodel_descriptor(id_short_, identification_, semantic_id_, endpoint_address_):
return json.dumps(
{
"description": [],
"idShort": id_short_,
"identification": identification_,
"semanticId": {
"value": [
semantic_id_
]
},
"endpoints": [
{
"interface": "HTTP",
"protocolInformation": {
"endpointAddress": endpoint_address_,
"endpointProtocol": "AAS/IDS",
"endpointProtocolVersion": "0.1",
"subprotocol": "IDS",
"subprotocolBody": "TDB",
"subprotocolBodyEncoding": "plain"
}
}
]
}
)
def create_aas_shell_3_0(global_asset_id_, id_short_, identification_, specific_asset_id_, submodel_descriptors_):
return json.dumps({
"description": [],
"globalAssetId": global_asset_id_,
"idShort": id_short_,
"id": identification_,
"specificAssetIds": specific_asset_id_,
"submodelDescriptors": submodel_descriptors_
})
def create_submodel_descriptor_3_0(id_short_, identification_, semantic_id_, dataplane_asset_address_, asset_id_,
endpoint_):
return json.dumps(
{
"description": [],
"idShort": id_short_,
"id": identification_,
"semanticId": {
"type": "ExternalReference",
"keys": [
{
"type": "GlobalReference",
"value": semantic_id_
}
]
},
"endpoints": [
{
"interface": "SUBMODEL-3.0",
"protocolInformation": {
"href": dataplane_asset_address_,
"endpointProtocol": "HTTP",
"endpointProtocolVersion": ["1.1"],
"subprotocol": "DSP",
"subprotocolBody": f"id={asset_id_};dspEndpoint={endpoint_}",
"subprotocolBodyEncoding": "plain",
"securityAttributes": [
{
"type": "NONE",
"key": "NONE",
"value": "NONE"
}
]
}
}
]
}
)
def print_response(response_):
print(response_)
if response_.status_code > 205:
print(response_.text)
if response_.status_code in [404, 401] or response_.status_code >= 500:
raise Exception("Failed to call service")
def check_url_args(submodel_server_upload_urls_, submodel_server_urls_, edc_upload_urls_, edc_urls_, dataplane_urls_,
edc_bpns_):
nr_of_submodel_server_upload_urls = len(submodel_server_upload_urls_)
nr_of_submodel_server_urls = len(submodel_server_urls_)
if nr_of_submodel_server_upload_urls != nr_of_submodel_server_urls:
raise ArgumentException(
f"Number and order of submodelserver upload URLs '{submodel_server_upload_urls_}' "
f"has to match number and order Number and order of submodelserver URLs '{submodel_server_urls_}'")
nr_of_edc_upload_urls = len(edc_upload_urls_)
nr_of_edc_urls = len(edc_urls_)
if nr_of_edc_upload_urls != nr_of_edc_urls:
raise ArgumentException(
f"Number and order of edc upload URLs '{edc_upload_urls_}' has to match number and order of edc URLs "
f"'{edc_urls_}'")
nr_of_edc_bpns = len(edc_bpns_)
if nr_of_edc_urls != nr_of_edc_bpns:
raise ArgumentException(
f"Number and order of edc URLs '{nr_of_edc_urls}' has to match number and order of edc BPNs "
f"'{nr_of_edc_bpns}'")
if nr_of_submodel_server_urls != nr_of_edc_urls:
raise ArgumentException(
f"Number and order of edc URLs '{edc_urls_}' has to match number and order of submodelserver URLS "
f"'{submodel_server_urls_}'")
nr_of_dataplane_urls = len(dataplane_urls_)
if nr_of_dataplane_urls != nr_of_edc_urls:
raise ArgumentException(
f"Number and order of edc controlplane URLs '{edc_urls_}' has to match number and order of edc dataplane "
f"URLS'{submodel_server_urls_}'")
class ArgumentException(Exception):
def __init__(self, *args, **kwargs): # real signature unknown
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def create_policy(policy_, edc_upload_url_, edc_policy_path_, headers_, session_):
url_ = edc_upload_url_ + edc_policy_path_
print(f"Create policy {policy_['@id']} on EDC {url_}")
response_ = session_.request(method="GET", url=f"{url_}/{policy_['@id']}", headers=headers_)
if response_.status_code == 200 and response_.json():
print(f"Policy {policy_['@id']} already exists. Skipping creation.")
else:
response_ = session_.request(method="POST", url=url_, headers=headers_, data=json.dumps(policy_))
print(response_)
if response_.status_code > 205:
print(response_.text)
else:
print(f"Successfully created policy {response_.json()['@id']}.")
def create_registry_asset(edc_upload_urls_, edc_bpns_, edc_asset_path_, edc_contract_definition_path_, catalog_path_, header_,
session_, edc_urls_, policy_, registry_asset_id_, aas_url_):
for edc_upload_url_ in edc_upload_urls_:
index = edc_upload_urls_.index(edc_upload_url_)
edc_url_ = edc_urls_[index]
edc_bpn_ = edc_bpns_[index]
print(edc_url_)
print(edc_upload_url_)
catalog_url_ = edc_upload_url_ + catalog_path_
payload_ = {
"@context": edc_context(),
"edc:protocol": "dataspace-protocol-http",
"edc:counterPartyAddress": f"{edc_url_}/api/v1/dsp",
"edc:counterPartyId": f"{edc_bpn_}",
"edc:querySpec": {
"edc:filterExpression": {
"@type": "edc:Criterion",
"edc:operandLeft": "https://w3id.org/edc/v0.0.1/ns/type",
"edc:operator": "=",
"edc:operandRight": "data.core.digitalTwinRegistry"
}
}
}
print(f"Query Catalog for registry asset {catalog_url_}")
response_ = session_.request(method="POST", url=catalog_url_, headers=header_, data=json.dumps(payload_))
print_response(response_)
asset_url_ = edc_upload_url_ + edc_asset_path_
print(response_.status_code)
catalog_response_ = response_.json()
if response_.status_code == 200 and len(catalog_response_['dcat:dataset']) >= 1:
first_offer_ = catalog_response_['dcat:dataset']
print(
f"Offer with type {first_offer_['type']} already exists. Skipping creation.")
else:
payload_ = create_edc_registry_asset_payload(aas_url_, registry_asset_id_)
response_ = session_.request(method="POST", url=asset_url_,
headers=header_,
data=payload_)
print(response_)
if response_.status_code > 205:
print(response_.text)
else:
print(f"Successfully created registry asset {response_.json()['@id']}.")
print("Create registry edc contract definition")
payload_ = create_edc_contract_definition_payload(policy_, registry_asset_id_)
response_ = session_.request(method="POST", url=edc_upload_url_ + edc_contract_definition_path_,
headers=header_,
data=payload_)
print_response(response_)
def create_notification_assets(edc_upload_urls_, edc_asset_path_, edc_contract_definition_path_, edc_catalog_path_,
edc_public_path_, header_, session_, edc_urls_, policy_, ess_base_url_):
for edc_upload_url_ in edc_upload_urls_:
index = edc_upload_urls_.index(edc_upload_url_)
edc_url_ = edc_urls_[index] + edc_public_path_
print(edc_url_)
print(edc_upload_url_)
notifications_ = [
{
"id": "ess-response-asset",
"type": "ess-supplier-response",
"path": "/ess/notification/receive"
},
{
"id": "notify-request-asset",
"type": "ess-supplier-request",
"path": "/ess/mock/notification/receive"
},
{
"id": "notify-request-asset-recursive",
"type": "ess-supplier-request",
"path": "/ess/notification/receive-recursive"
}
]
for notification_ in notifications_:
notification_id = notification_['id']
notification_type = notification_['type']
ess_url = f"{ess_base_url_}{notification_['path']}"
response_ = search_for_asset_in_catalog(edc_catalog_path_, edc_upload_url_, edc_url_, header_, session_,
notification_id)
asset_url_ = edc_upload_url_ + edc_asset_path_
print(response_.status_code)
catalog_response_ = response_.json()
if response_.status_code == 200 and len(catalog_response_['dcat:dataset']) >= 1:
print(
f"Offer with id {notification_id} already exists. Skipping creation.")
else:
payload_ = create_edc_notification_asset_payload(ess_url, notification_id, notification_type)
response_ = session_.request(method="POST", url=asset_url_, headers=header_, data=payload_)
print(response_)
if response_.status_code > 205:
print(response_.text)
else:
print(f"Successfully created notification asset {response_.json()['@id']}.")
print("Create registry edc contract definition")
payload_ = create_edc_contract_definition_payload(policy_, notification_id)
response_ = session_.request(method="POST", url=edc_upload_url_ + edc_contract_definition_path_,
headers=header_,
data=payload_)
print_response(response_)
def search_for_asset_in_catalog(edc_catalog_path_, edc_upload_url_, edc_url_, headers_, session_, asset_id_):
catalog_url_ = edc_upload_url_ + edc_catalog_path_
payload_ = {
"@context": edc_context(),
"edc:protocol": "dataspace-protocol-http",
"edc:providerUrl": f"{edc_url_}",
"edc:querySpec": {
"edc:filterExpression": {
"@type": "edc:Criterion",
"edc:operandLeft": "https://w3id.org/edc/v0.0.1/ns/id",
"edc:operator": "=",
"edc:operandRight": f"{asset_id_}"
}
}
}
print(f"Query Catalog for notification assets {catalog_url_}")
return session_.request(method="POST", url=catalog_url_, headers=headers_, data=json.dumps(payload_))
if __name__ == "__main__":
timestamp_start = time.time()
parser = argparse.ArgumentParser(description="Script to upload testdata into CX-Network.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-f", "--file", type=str, help="Test data file location", required=True)
parser.add_argument("-s", "--submodel", type=str, nargs="*", help="Submodel server display URLs", required=True)
parser.add_argument("-su", "--submodelupload", type=str, nargs="*", help="Submodel server upload URLs",
required=False)
parser.add_argument("-a", "--aas", type=str, help="aas url", required=True)
parser.add_argument("-au", "--aasupload", type=str, help="aas url", required=False)
parser.add_argument("-edc", "--edc", type=str, nargs="*", help="EDC provider control plane display URLs",
required=True)
parser.add_argument("--edcBPN", type=str, nargs="*", help="The BPN of the provider EDC", required=True)
parser.add_argument("-eu", "--edcupload", type=str, nargs="*", help="EDC provider control plane upload URLs",
required=False)
parser.add_argument("-k", "--apikey", type=str, help="EDC provider api key", required=True)
parser.add_argument("-e", "--esr", type=str, help="ESR URL", required=False)
parser.add_argument("--ess", help="Enable ESS data creation with invalid EDC URL", action='store_true',
required=False)
parser.add_argument("--bpn", help="Faulty BPN which will create a non existing EDC endpoint", required=False)
parser.add_argument("-p", "--policy", help="Default Policy which should be used for EDC contract definitions",
required=False)
parser.add_argument("-bpns", "--bpns", type=str, nargs="*", help="Filter upload to upload only specific BPNs",
required=False)
parser.add_argument("--aas3", help="Create AAS assets in version 3.0", action='store_true', required=False)
parser.add_argument("-d", "--dataplane", type=str, nargs="*", help="EDC provider data plane display URLs",
required=False)
parser.add_argument("--allowedBPNs", type=str, nargs="*",
help="The allowed BPNs for digital twin registration in the registry.", required=False)
parser.add_argument("--essURL", type=str, help="The base URL of the ESS Service API", required=False)
args = parser.parse_args()
config = vars(args)
filepath = config.get("file")
submodel_server_urls = config.get("submodel")
submodel_server_upload_urls = config.get("submodelupload")
aas_url = config.get("aas")
aas_upload_url = config.get("aasupload")
edc_urls = config.get("edc")
edc_bpns = config.get("edcBPN")
edc_upload_urls = config.get("edcupload")
edc_api_key = config.get("apikey")
esr_url = config.get("esr")
is_ess = config.get("ess")
ess_base_url = config.get("essURL")
bpnl_fail = config.get("bpn")
default_policy = config.get("policy")
bpns_list = config.get("bpns")
is_aas3 = config.get("aas3")
dataplane_urls = config.get("dataplane")
allowedBPNs = config.get("allowedBPNs")
if is_aas3 and dataplane_urls is None:
raise ArgumentException("Dataplane URLs have to be specified with -d or --dataplane if --aas flag is set!")
if is_ess and ess_base_url is None:
raise ArgumentException("ESS base URL has to be specified with --essURL if --ess flag is set!")
if submodel_server_upload_urls is None:
submodel_server_upload_urls = submodel_server_urls
if edc_upload_urls is None:
edc_upload_urls = edc_urls
if default_policy is None:
default_policy = "default-policy"
if aas_upload_url is None:
aas_upload_url = aas_url
if allowedBPNs is None:
allowedBPNs = []
registry_path = "/registry/shell-descriptors"
if is_aas3:
registry_path = "/shell-descriptors"
check_url_args(submodel_server_upload_urls, submodel_server_urls, edc_upload_urls, edc_urls, dataplane_urls,
edc_bpns)
edc_asset_path = "/management/v3/assets"
edc_policy_path = "/management/v2/policydefinitions"
edc_contract_definition_path = "/management/v2/contractdefinitions"
edc_catalog_path = "/management/v2/catalog/request"
dataplane_public_path = "/api/public"
controlplane_public_path = "/api/v1/dsp"
registry_asset_id = "registry-asset"
headers = {
'Content-Type': 'application/json'
}
headers_with_api_key = {
'X-Api-Key': edc_api_key,
'Content-Type': 'application/json'
}
default_policy_definition = {
"default": {
"@context": {
"odrl": "http://www.w3.org/ns/odrl/2/"
},
"@id": "default-policy",
"policy": {
"@type": "odrl:Set",
"odrl:permission": []
}
}
}
# Opening JSON file
f = open(filepath)
data = json.load(f)
f.close()
testdata = data["https://catenax.io/schema/TestDataContainer/1.0.0"]
policies = default_policy_definition
if "policies" in data.keys():
policies.update(data["policies"])
contract_number = 1
retries = Retry(total=5,
backoff_factor=0.1)
session = requests.Session()
session.mount('https://', HTTPAdapter(max_retries=retries))
if policies:
for policy in policies.keys():
for url in edc_upload_urls:
create_policy(policies[policy], url, edc_policy_path, headers_with_api_key, session)
create_registry_asset(edc_upload_urls, edc_bpns, edc_asset_path, edc_contract_definition_path, edc_catalog_path,
headers_with_api_key, session, edc_urls, default_policy, registry_asset_id, aas_url)
if is_ess:
create_notification_assets(edc_upload_urls, edc_asset_path, edc_contract_definition_path, edc_catalog_path,
controlplane_public_path, headers_with_api_key, session, edc_urls, default_policy,
ess_base_url)
edc_asset_ids = []
for url in dataplane_urls:
edc_asset_ids.append(uuid.uuid4().urn)
print(edc_asset_ids)
esr = "urn:bamm:io.catenax.esr_certificates.esr_certificate_state_statistic:1.0.1#EsrCertificateStateStatistic"
apr = "urn:bamm:io.catenax.single_level_bom_as_built:1.0.0#SingleLevelBomAsBuilt"
for tmp_data in testdata:
if bpns_list is None or tmp_data["bpnl"] in bpns_list or not bpns_list:
catenax_id = tmp_data["catenaXId"]
identification = uuid.uuid4().urn
tmp_keys = tmp_data.keys()
specific_asset_ids = []
submodel_descriptors = []
name_at_manufacturer = ""
specific_asset_ids_temp = []
for tmp_key in tmp_keys:
if "Batch" in tmp_key or "SerialPart" in tmp_key:
specific_asset_ids_temp = copy(tmp_data[tmp_key][0]["localIdentifiers"])
name_at_manufacturer = tmp_data[tmp_key][0]["partTypeInformation"]["nameAtManufacturer"].replace(
" ",
"")
if "PartAsPlanned" in tmp_key:
name_at_manufacturer = tmp_data[tmp_key][0]["partTypeInformation"]["nameAtManufacturer"].replace(
" ",
"")
specific_asset_ids_temp.append({
"value": tmp_data[tmp_key][0]["partTypeInformation"]["manufacturerPartId"],
"key": "manufacturerPartId"
})
print(name_at_manufacturer)
name_at_manufacturer = name_at_manufacturer + "-" + uuid.uuid4().hex
print(name_at_manufacturer)
manufacturerId = {
"key": "manufacturerId",
"value": tmp_data["bpnl"]
}
if manufacturerId not in specific_asset_ids_temp:
specific_asset_ids_temp.append(manufacturerId)
if is_aas3:
keys = [{
"type": "GlobalReference",
"value": "PUBLIC_READABLE"
}]
for bpn in allowedBPNs:
keys.append({
"type": "GlobalReference",
"value": bpn
})
externalSubjectId = {
"type": "ExternalReference",
"keys": keys
}
for asset in specific_asset_ids_temp:
specific_asset_ids.append({
"name": asset.get("key"),
"value": asset.get("value"),
"externalSubjectId": externalSubjectId
})
else:
specific_asset_ids = specific_asset_ids_temp
if esr_url and apr in tmp_keys and "childItems" in tmp_data[apr][0] and tmp_data[apr][0]["childItems"]:
tmp_data.update({esr: ""})
policy_id = default_policy
if "policy" in tmp_keys:
policy_id = tmp_data["policy"]
print(f"Policy: {policy_id}")
for tmp_key in tmp_keys:
if "PlainObject" not in tmp_key and "catenaXId" not in tmp_key and "bpn" not in tmp_key \
and "policy" not in tmp_key and "urn:bamm:io.catenax.aas:1.0.0#AAS" not in tmp_key:
# Prepare submodel endpoint address
submodel_url = submodel_server_urls[contract_number % len(submodel_server_urls)]
submodel_upload_url = submodel_server_upload_urls[
contract_number % len(submodel_server_upload_urls)]
edc_url = edc_urls[contract_number % len(edc_urls)]
edc_upload_url = edc_upload_urls[contract_number % len(edc_upload_urls)]
dataplane_url = dataplane_urls[contract_number % len(dataplane_urls)]
edc_asset_id = edc_asset_ids[contract_number % len(edc_asset_ids)]
submodel_name = tmp_key[tmp_key.index("#") + 1: len(tmp_key)]
submodel_identification = uuid.uuid4().urn
semantic_id = tmp_key
if is_ess and tmp_data["bpnl"] in bpnl_fail:
endpoint_address = f"http://idonotexist/{catenax_id}-{submodel_identification}/submodel?content=value&extent=withBlobValue"
elif submodel_name == "EsrCertificateStateStatistic" and esr_url is not None:
endpoint_address = f"{esr_url}/{catenax_id}/asBuilt/ISO14001/submodel"
else:
endpoint_address = f"{edc_url}/{catenax_id}-{submodel_identification}/submodel?content=value&extent=withBlobValue"
if is_aas3:
endpoint_address = f"{dataplane_url}{dataplane_public_path}/{submodel_identification}"
if is_ess and tmp_data["bpnl"] in bpnl_fail:
endpoint_address = f"http://idonotexist/{dataplane_public_path}/data/{submodel_identification}"
descriptor = create_submodel_descriptor_3_0(submodel_name, submodel_identification, semantic_id,
endpoint_address,
edc_asset_id,
edc_url)
submodel_descriptors.append(json.loads(descriptor))
else:
descriptor = create_submodel_descriptor(submodel_name, submodel_identification, semantic_id,
endpoint_address)
submodel_descriptors.append(json.loads(descriptor))
if is_aas3:
asset_prop_id = edc_asset_id
else:
asset_prop_id = f"{catenax_id}-{submodel_identification}"
print("Create submodel on submodel server")
if tmp_data[tmp_key] != "":
payload = create_submodel_payload(tmp_data[tmp_key][0])
response = session.request(method="POST",
url=f"{submodel_upload_url}/{submodel_identification}",
headers=headers, data=payload)
print_response(response)
asset_path = edc_upload_url + edc_asset_path
print(f"Create edc asset on EDC {asset_path}")
if submodel_name == "EsrCertificateStateStatistic" and esr_url is not None:
payload = create_esr_edc_asset_payload(esr_url, asset_prop_id, catenax_id)
else:
payload = create_edc_asset_payload(submodel_url, asset_prop_id)
response = session.request(method="POST", url=asset_path, headers=headers_with_api_key,
data=payload)
print_response(response)
if response.status_code > 205:
print("Asset creation failed. Skipping creation of contract definition.")
else:
print("Create edc contract definition")
payload = create_edc_contract_definition_payload(policy_id, asset_prop_id)
response = session.request(method="POST", url=edc_upload_url + edc_contract_definition_path,
headers=headers_with_api_key,
data=payload)
print_response(response)
contract_number = contract_number + 1
if submodel_descriptors:
print("Create aas shell")
if is_aas3:
payload = create_aas_shell_3_0(catenax_id, name_at_manufacturer, identification, specific_asset_ids,
submodel_descriptors)
else:
payload = create_aas_shell(catenax_id, name_at_manufacturer, identification, specific_asset_ids,
submodel_descriptors)
response = session.request(method="POST", url=f"{aas_upload_url}{registry_path}",
headers=headers,
data=payload)
print_response(response)
timestamp_end = time.time()
duration = timestamp_end - timestamp_start
print(f"Test data upload completed in {math.ceil(duration)} Seconds")