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