Refactor test_utils_rfc8040.py portmapping methods
[transportpce.git] / tests / transportpce_tests / common / test_utils_rfc8040.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2021 Orange, Inc. and others.  All rights reserved.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # pylint: disable=no-member
13
14 import json
15 import os
16 # pylint: disable=wrong-import-order
17 import sys
18 import re
19 import signal
20 import subprocess
21 import time
22
23 import psutil
24 import requests
25
26 # pylint: disable=import-error
27 import simulators
28
29 SIMS = simulators.SIMS
30
31 HONEYNODE_OK_START_MSG = 'Netconf SSH endpoint started successfully at 0.0.0.0'
32 KARAF_OK_START_MSG = re.escape(
33     "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
34 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
35
36 ODL_LOGIN = 'admin'
37 ODL_PWD = 'admin'
38 NODES_LOGIN = 'admin'
39 NODES_PWD = 'admin'
40
41 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
42 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
43
44 REQUEST_TIMEOUT = 10
45
46 CODE_SHOULD_BE_200 = 'Http status code should be 200'
47 CODE_SHOULD_BE_201 = 'Http status code should be 201'
48
49 SIM_LOG_DIRECTORY = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'log')
50
51 process_list = []
52
53 if 'USE_ODL_ALT_RESTCONF_PORT' in os.environ:
54     RESTCONF_PORT = os.environ['USE_ODL_ALT_RESTCONF_PORT']
55 else:
56     RESTCONF_PORT = 8181
57
58 RESTCONF_PATH_PREFIX = {'rfc8040': '/rests',
59                         'draft-bierman02': '/restconf'}
60 if 'USE_ODL_RESTCONF_VERSION' in os.environ:
61     RESTCONF_VERSION = os.environ['USE_ODL_RESTCONF_VERSION']
62     if RESTCONF_VERSION not in RESTCONF_PATH_PREFIX:
63         print('unsupported RESTCONF version ' + RESTCONF_VERSION)
64         sys.exit(3)
65 else:
66     RESTCONF_VERSION = 'rfc8040'
67
68 RESTCONF_BASE_URL = 'http://localhost:' + str(RESTCONF_PORT) + RESTCONF_PATH_PREFIX[RESTCONF_VERSION]
69
70 if 'USE_ODL_ALT_KARAF_INSTALL_DIR' in os.environ:
71     KARAF_INSTALLDIR = os.environ['USE_ODL_ALT_KARAF_INSTALL_DIR']
72 else:
73     KARAF_INSTALLDIR = 'karaf'
74
75 KARAF_LOG = os.path.join(
76     os.path.dirname(os.path.realpath(__file__)),
77     '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'data', 'log', 'karaf.log')
78
79 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
80     TPCE_LOG = 'odl-' + str(os.getpid()) + '.log'
81 else:
82     TPCE_LOG = KARAF_LOG
83
84 #
85 # Basic HTTP operations
86 #
87
88
89 def get_request(url):
90     return requests.request(
91         'GET', url.format(RESTCONF_BASE_URL),
92         headers=TYPE_APPLICATION_JSON,
93         auth=(ODL_LOGIN, ODL_PWD),
94         timeout=REQUEST_TIMEOUT)
95
96
97 def put_request(url, data):
98     return requests.request(
99         'PUT', url.format(RESTCONF_BASE_URL),
100         data=json.dumps(data),
101         headers=TYPE_APPLICATION_JSON,
102         auth=(ODL_LOGIN, ODL_PWD),
103         timeout=REQUEST_TIMEOUT)
104
105
106 def delete_request(url):
107     return requests.request(
108         'DELETE', url.format(RESTCONF_BASE_URL),
109         headers=TYPE_APPLICATION_JSON,
110         auth=(ODL_LOGIN, ODL_PWD),
111         timeout=REQUEST_TIMEOUT)
112
113
114 def post_request(url, data):
115     if data:
116         return requests.request(
117             "POST", url.format(RESTCONF_BASE_URL),
118             data=json.dumps(data),
119             headers=TYPE_APPLICATION_JSON,
120             auth=(ODL_LOGIN, ODL_PWD),
121             timeout=REQUEST_TIMEOUT)
122     return requests.request(
123         "POST", url.format(RESTCONF_BASE_URL),
124         headers=TYPE_APPLICATION_JSON,
125         auth=(ODL_LOGIN, ODL_PWD),
126         timeout=REQUEST_TIMEOUT)
127
128 #
129 # Process management
130 #
131
132
133 def start_sims(sims_list):
134     for sim in sims_list:
135         print('starting simulator ' + sim[0] + ' in OpenROADM device version ' + sim[1] + '...')
136         log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
137         process = start_honeynode(log_file, sim)
138         if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
139             print('simulator for ' + sim[0] + ' started')
140         else:
141             print('simulator for ' + sim[0] + ' failed to start')
142             shutdown_process(process)
143             for pid in process_list:
144                 shutdown_process(pid)
145             sys.exit(3)
146         process_list.append(process)
147     return process_list
148
149
150 def start_tpce():
151     print('starting OpenDaylight...')
152     if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
153         process = start_lighty()
154         start_msg = LIGHTY_OK_START_MSG
155     else:
156         process = start_karaf()
157         start_msg = KARAF_OK_START_MSG
158     if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=300):
159         print('OpenDaylight started !')
160     else:
161         print('OpenDaylight failed to start !')
162         shutdown_process(process)
163         for pid in process_list:
164             shutdown_process(pid)
165         sys.exit(1)
166     process_list.append(process)
167     return process_list
168
169
170 def start_karaf():
171     print('starting KARAF TransportPCE build...')
172     executable = os.path.join(
173         os.path.dirname(os.path.realpath(__file__)),
174         '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'karaf')
175     with open('odl.log', 'w', encoding='utf-8') as outfile:
176         return subprocess.Popen(
177             ['sh', executable, 'server'], stdout=outfile, stderr=outfile, stdin=None)
178
179
180 def start_lighty():
181     print('starting LIGHTY.IO TransportPCE build...')
182     executable = os.path.join(
183         os.path.dirname(os.path.realpath(__file__)),
184         '..', '..', '..', 'lighty', 'target', 'tpce',
185         'clean-start-controller.sh')
186     with open(TPCE_LOG, 'w', encoding='utf-8') as outfile:
187         return subprocess.Popen(
188             ['sh', executable], stdout=outfile, stderr=outfile, stdin=None)
189
190
191 def install_karaf_feature(feature_name: str):
192     print('installing feature ' + feature_name)
193     executable = os.path.join(
194         os.path.dirname(os.path.realpath(__file__)),
195         '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'client')
196     return subprocess.run([executable],
197                           input='feature:install ' + feature_name + '\n feature:list | grep '
198                           + feature_name + ' \n logout \n',
199                           universal_newlines=True, check=False)
200
201
202 def shutdown_process(process):
203     if process is not None:
204         for child in psutil.Process(process.pid).children():
205             child.send_signal(signal.SIGINT)
206             child.wait()
207         process.send_signal(signal.SIGINT)
208
209
210 def start_honeynode(log_file: str, sim):
211     executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
212                               '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
213     sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
214                                     '..', '..', 'sample_configs', 'openroadm', sim[1])
215     if os.path.isfile(executable):
216         with open(log_file, 'w', encoding='utf-8') as outfile:
217             return subprocess.Popen(
218                 [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
219                 stdout=outfile, stderr=outfile)
220     return None
221
222
223 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
224     # pylint: disable=lost-exception
225     # pylint: disable=consider-using-with
226     stringfound = False
227     filefound = False
228     line = None
229     try:
230         with TimeOut(seconds=time_to_wait):
231             while not os.path.exists(log_file):
232                 time.sleep(0.2)
233             filelogs = open(log_file, 'r', encoding='utf-8')
234             filelogs.seek(0, 2)
235             filefound = True
236             print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
237             compiled_regexp = re.compile(regexp)
238             while True:
239                 line = filelogs.readline()
240                 if compiled_regexp.search(line):
241                     print('Pattern found!', end=' ')
242                     stringfound = True
243                     break
244                 if not line:
245                     time.sleep(0.1)
246     except TimeoutError:
247         print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True)
248     except PermissionError:
249         print('Permission Error when trying to access the log file', end=' ... ', flush=True)
250     finally:
251         if filefound:
252             filelogs.close()
253         else:
254             print('log file does not exist or is not accessible... ', flush=True)
255         return stringfound
256
257
258 class TimeOut:
259     def __init__(self, seconds=1, error_message='Timeout'):
260         self.seconds = seconds
261         self.error_message = error_message
262
263     def handle_timeout(self, signum, frame):
264         raise TimeoutError(self.error_message)
265
266     def __enter__(self):
267         signal.signal(signal.SIGALRM, self.handle_timeout)
268         signal.alarm(self.seconds)
269
270     def __exit__(self, type, value, traceback):
271         # pylint: disable=W0622
272         signal.alarm(0)
273
274 #
275 # Basic NetCONF device operations
276 #
277
278
279 def mount_device(node: str, sim: str):
280     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
281            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
282     body = {'node': [{
283         'node-id': node,
284         'netconf-node-topology:username': NODES_LOGIN,
285         'netconf-node-topology:password': NODES_PWD,
286         'netconf-node-topology:host': '127.0.0.1',
287         'netconf-node-topology:port': SIMS[sim]['port'],
288         'netconf-node-topology:tcp-only': 'false',
289         'netconf-node-topology:pass-through': {}}]}
290     response = put_request(url[RESTCONF_VERSION].format('{}', node), body)
291     if wait_until_log_contains(TPCE_LOG, 'Triggering notification stream NETCONF for node ' + node, 180):
292         print('Node ' + node + ' correctly added to tpce topology', end='... ', flush=True)
293     else:
294         print('Node ' + node + ' still not added to tpce topology', end='... ', flush=True)
295         if response.status_code == requests.codes.ok:
296             print('It was probably loaded at start-up', end='... ', flush=True)
297         # TODO an else-clause to abort test would probably be nice here
298     return response
299
300
301 def unmount_device(node: str):
302     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
303            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
304     response = delete_request(url[RESTCONF_VERSION].format('{}', node))
305     if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: " + node), 180):
306         print('Node ' + node + ' correctly deleted from tpce topology', end='... ', flush=True)
307     else:
308         print('Node ' + node + ' still not deleted from tpce topology', end='... ', flush=True)
309     return response
310
311
312 def check_device_connection(node: str):
313     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}?content=nonconfig',
314            'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}'}
315     response = get_request(url[RESTCONF_VERSION].format('{}', node))
316     res = response.json()
317     return_key = {'rfc8040': 'network-topology:node',
318                   'draft-bierman02': 'node'}
319     if return_key[RESTCONF_VERSION] in res.keys():
320         connection_status = res[return_key[RESTCONF_VERSION]][0]['netconf-node-topology:connection-status']
321     else:
322         connection_status = res['errors']['error'][0]
323     return {'status_code': response.status_code,
324             'connection-status': connection_status}
325
326
327 def check_node_request(node: str):
328     # pylint: disable=line-too-long
329     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device?content=config',  # nopep8
330            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device'}  # nopep8
331     response = get_request(url[RESTCONF_VERSION].format('{}', node))
332     res = response.json()
333     return_key = {'rfc8040': 'org-openroadm-device:org-openroadm-device',
334                   'draft-bierman02': 'org-openroadm-device'}
335     if return_key[RESTCONF_VERSION] in res.keys():
336         response_attribute = res[return_key[RESTCONF_VERSION]]
337     else:
338         response_attribute = res['errors']['error'][0]
339     return {'status_code': response.status_code,
340             'org-openroadm-device': response_attribute}
341
342
343 def check_node_attribute_request(node: str, attribute: str, attribute_value: str):
344     # pylint: disable=line-too-long
345     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}?content=nonconfig',  # nopep8
346            'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}'}  # nopep8
347     response = get_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value))
348     res = response.json()
349     return_key = {'rfc8040': 'org-openroadm-device:' + attribute,
350                   'draft-bierman02': attribute}
351     if return_key[RESTCONF_VERSION] in res.keys():
352         response_attribute = res[return_key[RESTCONF_VERSION]]
353     else:
354         response_attribute = res['errors']['error'][0]
355     return {'status_code': response.status_code,
356             attribute: response_attribute}
357
358
359 def check_node_attribute2_request(node: str, attribute: str, attribute_value: str, attribute2: str):
360     # pylint: disable=line-too-long
361     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}/{}?content=config',  # nopep8
362            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}/{}'}  # nopep8
363     response = get_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value, attribute2))
364     res = response.json()
365     if attribute2 in res.keys():
366         response_attribute = res[attribute2]
367     else:
368         response_attribute = res['errors']['error'][0]
369     return {'status_code': response.status_code,
370             attribute2: response_attribute}
371
372
373 def del_node_attribute_request(node: str, attribute: str, attribute_value: str):
374     # pylint: disable=line-too-long
375     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}',  # nopep8
376            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}'}  # nopep8
377     response = delete_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value))
378     return response
379
380 #
381 # Portmapping operations
382 #
383
384
385 def get_portmapping_node_attr(node: str, attr: str, value: str):
386     # pylint: disable=consider-using-f-string
387     url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}',
388            'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}'}
389     target_url = url[RESTCONF_VERSION].format('{}', node)
390     if attr is not None:
391         target_url = (target_url + '/{}').format('{}', attr)
392         if value is not None:
393             suffix = {'rfc8040': '={}', 'draft-bierman02': '/{}'}
394             target_url = (target_url + suffix[RESTCONF_VERSION]).format('{}', value)
395     else:
396         attr = 'nodes'
397     response = get_request(target_url)
398     res = response.json()
399     return_key = {'rfc8040': 'transportpce-portmapping:' + attr,
400                   'draft-bierman02': attr}
401     if return_key[RESTCONF_VERSION] in res.keys():
402         return_output = res[return_key[RESTCONF_VERSION]]
403     else:
404         return_output = res['errors']['error'][0]
405     return {'status_code': response.status_code,
406             attr: return_output}
407
408 #
409 # Topology operations
410 #
411
412
413 def get_ietf_network_request(network: str, content: str):
414     url = {'rfc8040': '{}/data/ietf-network:networks/network={}?content={}',
415            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}'}
416     if RESTCONF_VERSION == 'rfc8040':
417         format_args = ('{}', network, content)
418     elif content == 'config':
419         format_args = ('{}', content, network)
420     else:
421         format_args = ('{}', 'operational', network)
422     response = get_request(url[RESTCONF_VERSION].format(*format_args))
423     res = response.json()
424     return_key = {'rfc8040': 'ietf-network:network',
425                   'draft-bierman02': 'network'}
426     networks = res[return_key[RESTCONF_VERSION]]
427     return {'status_code': response.status_code,
428             'network': networks}
429
430
431 def get_ietf_network_link_request(network: str, link: str, content: str):
432     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
433            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
434     if RESTCONF_VERSION == 'rfc8040':
435         format_args = ('{}', network, link, content)
436     elif content == 'config':
437         format_args = ('{}', content, network, link)
438     else:
439         format_args = ('{}', 'operational', network, link)
440     response = get_request(url[RESTCONF_VERSION].format(*format_args))
441     res = response.json()
442     return_key = {'rfc8040': 'ietf-network-topology:link',
443                   'draft-bierman02': 'ietf-network-topology:link'}
444     link = res[return_key[RESTCONF_VERSION]][0]
445     return {'status_code': response.status_code,
446             'link': link}
447
448
449 def del_ietf_network_link_request(network: str, link: str, content: str):
450     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
451            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
452     if RESTCONF_VERSION == 'rfc8040':
453         format_args = ('{}', network, link, content)
454     elif content == 'config':
455         format_args = ('{}', content, network, link)
456     else:
457         format_args = ('{}', 'operational', network, link)
458     response = delete_request(url[RESTCONF_VERSION].format(*format_args))
459     return response
460
461
462 def add_oms_attr_request(link: str, oms_attr: str):
463     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
464            'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
465     url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
466     network = 'openroadm-topology'
467     response = put_request(url2.format('{}', network, link), oms_attr)
468     return response
469
470
471 def del_oms_attr_request(link: str,):
472     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
473            'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
474     url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
475     network = 'openroadm-topology'
476     response = delete_request(url2.format('{}', network, link))
477     return response
478
479
480 def get_ietf_network_node_request(network: str, node: str, content: str):
481     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
482            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
483     if RESTCONF_VERSION == 'rfc8040':
484         format_args = ('{}', network, node, content)
485     elif content == 'config':
486         format_args = ('{}', content, network, node)
487     else:
488         format_args = ('{}', 'operational', network, node)
489     response = get_request(url[RESTCONF_VERSION].format(*format_args))
490     res = response.json()
491     return_key = {'rfc8040': 'ietf-network:node',
492                   'draft-bierman02': 'node'}
493     node = res[return_key[RESTCONF_VERSION]][0]
494     return {'status_code': response.status_code,
495             'node': node}
496
497
498 def del_ietf_network_node_request(network: str, node: str, content: str):
499     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
500            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
501     if RESTCONF_VERSION == 'rfc8040':
502         format_args = ('{}', network, node, content)
503     elif content == 'config':
504         format_args = ('{}', content, network, node)
505     else:
506         format_args = ('{}', 'operational', network, node)
507     response = delete_request(url[RESTCONF_VERSION].format(*format_args))
508     return response
509
510
511 #
512 # Service list operations
513 #
514
515
516 def get_ordm_serv_list_request():
517     url = {'rfc8040': '{}/data/org-openroadm-service:service-list?content=nonconfig',
518            'draft-bierman02': '{}/operational/org-openroadm-service:service-list/'}
519     response = get_request(url[RESTCONF_VERSION])
520     res = response.json()
521     return_key = {'rfc8040': 'org-openroadm-service:service-list',
522                   'draft-bierman02': 'service-list'}
523     if return_key[RESTCONF_VERSION] in res.keys():
524         response_attribute = res[return_key[RESTCONF_VERSION]]
525     else:
526         response_attribute = res['errors']['error'][0]
527     return {'status_code': response.status_code,
528             'service-list': response_attribute}
529
530
531 def get_ordm_serv_list_attr_request(attribute: str, value: str):
532     url = {'rfc8040': '{}/data/org-openroadm-service:service-list/{}={}?content=nonconfig',
533            'draft-bierman02': '{}/operational/org-openroadm-service:service-list/{}/{}'}
534     if RESTCONF_VERSION == 'rfc8040':
535         format_args = ('{}', attribute, value)
536     else:
537         format_args = ('{}', attribute, value)
538     response = get_request(url[RESTCONF_VERSION].format(*format_args))
539     res = response.json()
540     return_key = {'rfc8040': 'org-openroadm-service:' + attribute,
541                   'draft-bierman02': attribute}
542     if return_key[RESTCONF_VERSION] in res.keys():
543         response_attribute = res[return_key[RESTCONF_VERSION]]
544     else:
545         response_attribute = res['errors']['error'][0]
546     return {'status_code': response.status_code,
547             attribute: response_attribute}
548
549
550 #
551 # TransportPCE internal API RPCs
552 #
553
554
555 def prepend_dict_keys(input_dict: dict, prefix: str):
556     return_dict = {}
557     for key, value in input_dict.items():
558         newkey = prefix + key
559         if isinstance(value, dict):
560             return_dict[newkey] = prepend_dict_keys(value, prefix)
561             # TODO: perhaps some recursion depth limit or another solution has to be considered here
562             # even if recursion depth is given by the input_dict argument
563             # direct (self-)recursive functions may carry unwanted side-effects such as ressource consumptions
564         else:
565             return_dict[newkey] = value
566     return return_dict
567
568
569 def transportpce_api_rpc_request(api_module: str, rpc: str, payload: dict):
570     # pylint: disable=consider-using-f-string
571     url = "{}/operations/{}:{}".format('{}', api_module, rpc)
572     if payload is None:
573         data = None
574     elif RESTCONF_VERSION == 'draft-bierman02':
575         data = prepend_dict_keys({'input': payload}, api_module + ':')
576     else:
577         data = {'input': payload}
578     response = post_request(url, data)
579     res = response.json()
580     return_key = {'rfc8040': api_module + ':output',
581                   'draft-bierman02': 'output'}
582     return_output = res[return_key[RESTCONF_VERSION]]
583     return {'status_code': response.status_code,
584             'output': return_output}