3 ##############################################################################
4 # Copyright (c) 2021 Orange, Inc. and others. All rights reserved.
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 ##############################################################################
12 # pylint: disable=no-member
16 # pylint: disable=wrong-import-order
26 # pylint: disable=import-error
29 SIMS = simulators.SIMS
31 HONEYNODE_OK_START_MSG = 'Netconf SSH endpoint started successfully at 0.0.0.0'
32 KARAF_OK_START_MSG = "Blueprint container for bundle org.opendaylight.transportpce.servicehandler.+" \
33 "was successfully created"
34 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
41 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
42 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
46 CODE_SHOULD_BE_200 = 'Http status code should be 200'
47 CODE_SHOULD_BE_201 = 'Http status code should be 201'
48 T100GE = 'Transponder 100GE'
49 T0_MULTILAYER_TOPO = 'T0 - Multi-layer topology'
50 T0_FULL_MULTILAYER_TOPO = 'T0 - Full Multi-layer topology'
52 SIM_LOG_DIRECTORY = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'log')
56 if 'USE_ODL_ALT_RESTCONF_PORT' in os.environ:
57 RESTCONF_PORT = os.environ['USE_ODL_ALT_RESTCONF_PORT']
61 RESTCONF_PATH_PREFIX = {'rfc8040': '/rests',
62 'draft-bierman02': '/restconf'}
63 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
64 RESTCONF_PATH_PREFIX['rfc8040'] = '/restconf'
66 if 'USE_ODL_RESTCONF_VERSION' in os.environ:
67 RESTCONF_VERSION = os.environ['USE_ODL_RESTCONF_VERSION']
68 if RESTCONF_VERSION not in RESTCONF_PATH_PREFIX:
69 print('unsupported RESTCONF version ' + RESTCONF_VERSION)
72 RESTCONF_VERSION = 'rfc8040'
74 RESTCONF_BASE_URL = 'http://localhost:' + str(RESTCONF_PORT) + RESTCONF_PATH_PREFIX[RESTCONF_VERSION]
76 if 'USE_ODL_ALT_KARAF_INSTALL_DIR' in os.environ:
77 KARAF_INSTALLDIR = os.environ['USE_ODL_ALT_KARAF_INSTALL_DIR']
79 KARAF_INSTALLDIR = 'karaf'
81 KARAF_LOG = os.path.join(
82 os.path.dirname(os.path.realpath(__file__)),
83 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'data', 'log', 'karaf.log')
85 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
86 TPCE_LOG = 'odl-' + str(os.getpid()) + '.log'
91 # Basic HTTP operations
96 return requests.request(
97 'GET', url.format(RESTCONF_BASE_URL),
98 headers=TYPE_APPLICATION_JSON,
99 auth=(ODL_LOGIN, ODL_PWD),
100 timeout=REQUEST_TIMEOUT)
103 def put_request(url, data):
104 return requests.request(
105 'PUT', url.format(RESTCONF_BASE_URL),
106 data=json.dumps(data),
107 headers=TYPE_APPLICATION_JSON,
108 auth=(ODL_LOGIN, ODL_PWD),
109 timeout=REQUEST_TIMEOUT)
112 def delete_request(url):
113 return requests.request(
114 'DELETE', url.format(RESTCONF_BASE_URL),
115 headers=TYPE_APPLICATION_JSON,
116 auth=(ODL_LOGIN, ODL_PWD),
117 timeout=REQUEST_TIMEOUT)
120 def post_request(url, data):
122 return requests.request(
123 "POST", url.format(RESTCONF_BASE_URL),
124 data=json.dumps(data),
125 headers=TYPE_APPLICATION_JSON,
126 auth=(ODL_LOGIN, ODL_PWD),
127 timeout=REQUEST_TIMEOUT)
128 return requests.request(
129 "POST", url.format(RESTCONF_BASE_URL),
130 headers=TYPE_APPLICATION_JSON,
131 auth=(ODL_LOGIN, ODL_PWD),
132 timeout=REQUEST_TIMEOUT)
139 def start_sims(sims_list):
140 for sim in sims_list:
141 print('starting simulator ' + sim[0] + ' in OpenROADM device version ' + sim[1] + '...')
142 log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
143 process = start_honeynode(log_file, sim)
144 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
145 print('simulator for ' + sim[0] + ' started')
147 print('simulator for ' + sim[0] + ' failed to start')
148 shutdown_process(process)
149 for pid in process_list:
150 shutdown_process(pid)
152 process_list.append(process)
157 if 'NO_ODL_STARTUP' in os.environ:
158 print('No OpenDaylight instance to start!')
160 print('starting OpenDaylight...')
161 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
162 process = start_lighty()
163 start_msg = LIGHTY_OK_START_MSG
165 process = start_karaf()
166 start_msg = KARAF_OK_START_MSG
167 if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=100):
168 print('OpenDaylight started !')
170 print('OpenDaylight failed to start !')
171 shutdown_process(process)
172 for pid in process_list:
173 shutdown_process(pid)
175 process_list.append(process)
180 print('starting KARAF TransportPCE build...')
181 executable = os.path.join(
182 os.path.dirname(os.path.realpath(__file__)),
183 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'karaf')
184 with open('odl.log', 'w', encoding='utf-8') as outfile:
185 return subprocess.Popen(
186 ['sh', executable, 'server'], stdout=outfile, stderr=outfile, stdin=None)
190 print('starting LIGHTY.IO TransportPCE build...')
191 executable = os.path.join(
192 os.path.dirname(os.path.realpath(__file__)),
193 '..', '..', '..', 'lighty', 'target', 'tpce',
194 'clean-start-controller.sh')
195 with open(TPCE_LOG, 'w', encoding='utf-8') as outfile:
196 return subprocess.Popen(
197 ['sh', executable], stdout=outfile, stderr=outfile, stdin=None)
200 def install_karaf_feature(feature_name: str):
201 print('installing feature ' + feature_name)
202 executable = os.path.join(
203 os.path.dirname(os.path.realpath(__file__)),
204 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'client')
205 # FIXME: https://jira.opendaylight.org/browse/TRNSPRTPCE-701
206 # -b option needed below because of Karaf client bug reporte in the JIRA ticket mentioned above
207 return subprocess.run([executable, '-b'],
208 input='feature:install ' + feature_name + '\n feature:list | grep '
209 + feature_name + ' \n logout \n',
210 universal_newlines=True, check=False)
213 def shutdown_process(process):
214 if process is not None:
215 for child in psutil.Process(process.pid).children():
216 child.send_signal(signal.SIGINT)
218 process.send_signal(signal.SIGINT)
221 def start_honeynode(log_file: str, sim):
222 executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
223 '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
224 sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
225 '..', '..', 'sample_configs', 'openroadm', sim[1])
226 if os.path.isfile(executable):
227 with open(log_file, 'w', encoding='utf-8') as outfile:
228 return subprocess.Popen(
229 [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
230 stdout=outfile, stderr=outfile)
234 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
235 # pylint: disable=lost-exception
236 # pylint: disable=consider-using-with
241 with TimeOut(seconds=time_to_wait):
242 while not os.path.exists(log_file):
244 filelogs = open(log_file, 'r', encoding='utf-8')
247 print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
248 compiled_regexp = re.compile(regexp)
250 line = filelogs.readline()
251 if compiled_regexp.search(line):
252 print('Pattern found!', end=' ')
258 print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True)
259 except PermissionError:
260 print('Permission Error when trying to access the log file', end=' ... ', flush=True)
265 print('log file does not exist or is not accessible... ', flush=True)
270 def __init__(self, seconds=1, error_message='Timeout'):
271 self.seconds = seconds
272 self.error_message = error_message
274 def handle_timeout(self, signum, frame):
275 raise TimeoutError(self.error_message)
278 signal.signal(signal.SIGALRM, self.handle_timeout)
279 signal.alarm(self.seconds)
281 def __exit__(self, type, value, traceback):
282 # pylint: disable=W0622
286 # Basic NetCONF device operations
290 def mount_device(node: str, sim: str):
291 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
292 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
295 'netconf-node-topology:username': NODES_LOGIN,
296 'netconf-node-topology:password': NODES_PWD,
297 'netconf-node-topology:host': '127.0.0.1',
298 'netconf-node-topology:port': SIMS[sim]['port'],
299 'netconf-node-topology:tcp-only': 'false',
300 'netconf-node-topology:pass-through': {}}]}
301 response = put_request(url[RESTCONF_VERSION].format('{}', node), body)
302 if wait_until_log_contains(TPCE_LOG, 'Triggering notification stream NETCONF for node ' + node, 180):
303 print('Node ' + node + ' correctly added to tpce topology', end='... ', flush=True)
305 print('Node ' + node + ' still not added to tpce topology', end='... ', flush=True)
306 if response.status_code == requests.codes.ok:
307 print('It was probably loaded at start-up', end='... ', flush=True)
308 # TODO an else-clause to abort test would probably be nice here
312 def unmount_device(node: str):
313 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
314 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
315 response = delete_request(url[RESTCONF_VERSION].format('{}', node))
316 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: " + node), 180):
317 print('Node ' + node + ' correctly deleted from tpce topology', end='... ', flush=True)
319 print('Node ' + node + ' still not deleted from tpce topology', end='... ', flush=True)
323 def check_device_connection(node: str):
324 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}?content=nonconfig',
325 'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}'}
326 response = get_request(url[RESTCONF_VERSION].format('{}', node))
327 res = response.json()
328 return_key = {'rfc8040': 'network-topology:node',
329 'draft-bierman02': 'node'}
330 if return_key[RESTCONF_VERSION] in res.keys():
331 connection_status = res[return_key[RESTCONF_VERSION]][0]['netconf-node-topology:connection-status']
333 connection_status = res['errors']['error'][0]
334 return {'status_code': response.status_code,
335 'connection-status': connection_status}
338 def check_node_request(node: str):
339 # pylint: disable=line-too-long
340 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device?content=config', # nopep8
341 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device'} # nopep8
342 response = get_request(url[RESTCONF_VERSION].format('{}', node))
343 res = response.json()
344 return_key = {'rfc8040': 'org-openroadm-device:org-openroadm-device',
345 'draft-bierman02': 'org-openroadm-device'}
346 if return_key[RESTCONF_VERSION] in res.keys():
347 response_attribute = res[return_key[RESTCONF_VERSION]]
349 response_attribute = res['errors']['error'][0]
350 return {'status_code': response.status_code,
351 'org-openroadm-device': response_attribute}
354 def check_node_attribute_request(node: str, attribute: str, attribute_value: str):
355 # pylint: disable=line-too-long
356 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}?content=nonconfig', # nopep8
357 'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}'} # nopep8
358 response = get_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value))
359 res = response.json()
360 return_key = {'rfc8040': 'org-openroadm-device:' + attribute,
361 'draft-bierman02': attribute}
362 if return_key[RESTCONF_VERSION] in res.keys():
363 response_attribute = res[return_key[RESTCONF_VERSION]]
365 response_attribute = res['errors']['error'][0]
366 return {'status_code': response.status_code,
367 attribute: response_attribute}
370 def check_node_attribute2_request(node: str, attribute: str, attribute_value: str, attribute2: str):
371 # pylint: disable=line-too-long
372 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}/{}?content=config', # nopep8
373 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}/{}'} # nopep8
374 response = get_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value, attribute2))
375 res = response.json()
376 if attribute2 in res.keys():
377 response_attribute = res[attribute2]
379 response_attribute = res['errors']['error'][0]
380 return {'status_code': response.status_code,
381 attribute2: response_attribute}
384 def del_node_attribute_request(node: str, attribute: str, attribute_value: str):
385 # pylint: disable=line-too-long
386 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}={}', # nopep8
387 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}/yang-ext:mount/org-openroadm-device:org-openroadm-device/{}/{}'} # nopep8
388 response = delete_request(url[RESTCONF_VERSION].format('{}', node, attribute, attribute_value))
392 # Portmapping operations
396 def post_portmapping(payload: str):
397 url = {'rfc8040': '{}/data/transportpce-portmapping:network',
398 'draft-bierman02': '{}/config/transportpce-portmapping:network'}
399 json_payload = json.loads(payload)
400 response = post_request(url[RESTCONF_VERSION].format('{}'), json_payload)
401 return {'status_code': response.status_code}
404 def del_portmapping():
405 url = {'rfc8040': '{}/data/transportpce-portmapping:network',
406 'draft-bierman02': '{}/config/transportpce-portmapping:network'}
407 response = delete_request(url[RESTCONF_VERSION].format('{}'))
408 return {'status_code': response.status_code}
411 def get_portmapping_node_attr(node: str, attr: str, value: str):
412 # pylint: disable=consider-using-f-string
413 url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}',
414 'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}'}
415 target_url = url[RESTCONF_VERSION].format('{}', node)
417 target_url = (target_url + '/{}').format('{}', attr)
418 if value is not None:
419 suffix = {'rfc8040': '={}', 'draft-bierman02': '/{}'}
420 target_url = (target_url + suffix[RESTCONF_VERSION]).format('{}', value)
423 response = get_request(target_url)
424 res = response.json()
425 return_key = {'rfc8040': 'transportpce-portmapping:' + attr,
426 'draft-bierman02': attr}
427 if return_key[RESTCONF_VERSION] in res.keys():
428 return_output = res[return_key[RESTCONF_VERSION]]
430 return_output = res['errors']['error'][0]
431 return {'status_code': response.status_code,
435 # Topology operations
439 def get_ietf_network_request(network: str, content: str):
440 url = {'rfc8040': '{}/data/ietf-network:networks/network={}?content={}',
441 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}'}
442 if RESTCONF_VERSION in ('rfc8040'):
443 format_args = ('{}', network, content)
444 elif content == 'config':
445 format_args = ('{}', content, network)
447 format_args = ('{}', 'operational', network)
448 response = get_request(url[RESTCONF_VERSION].format(*format_args))
450 res = response.json()
451 return_key = {'rfc8040': 'ietf-network:network',
452 'draft-bierman02': 'network'}
453 networks = res[return_key[RESTCONF_VERSION]]
456 return {'status_code': response.status_code,
460 def put_ietf_network(network: str, payload: str):
461 url = {'rfc8040': '{}/data/ietf-network:networks/network={}',
462 'draft-bierman02': '{}/config/ietf-network:networks/network/{}'}
463 json_payload = json.loads(payload)
464 response = put_request(url[RESTCONF_VERSION].format('{}', network), json_payload)
465 return {'status_code': response.status_code}
468 def del_ietf_network(network: str):
469 url = {'rfc8040': '{}/data/ietf-network:networks/network={}',
470 'draft-bierman02': '{}/config/ietf-network:networks/network/{}'}
471 response = delete_request(url[RESTCONF_VERSION].format('{}', network))
472 return {'status_code': response.status_code}
475 def get_ietf_network_link_request(network: str, link: str, content: str):
476 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
477 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
478 if RESTCONF_VERSION in ('rfc8040'):
479 format_args = ('{}', network, link, content)
480 elif content == 'config':
481 format_args = ('{}', content, network, link)
483 format_args = ('{}', 'operational', network, link)
484 response = get_request(url[RESTCONF_VERSION].format(*format_args))
485 res = response.json()
486 return_key = {'rfc8040': 'ietf-network-topology:link',
487 'draft-bierman02': 'ietf-network-topology:link'}
488 link = res[return_key[RESTCONF_VERSION]][0]
489 return {'status_code': response.status_code,
493 def del_ietf_network_link_request(network: str, link: str, content: str):
494 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
495 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
496 if RESTCONF_VERSION in ('rfc8040'):
497 format_args = ('{}', network, link, content)
498 elif content == 'config':
499 format_args = ('{}', content, network, link)
501 format_args = ('{}', 'operational', network, link)
502 response = delete_request(url[RESTCONF_VERSION].format(*format_args))
506 def add_oms_attr_request(link: str, oms_attr: str):
507 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
508 'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
509 url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
510 network = 'openroadm-topology'
511 response = put_request(url2.format('{}', network, link), oms_attr)
515 def del_oms_attr_request(link: str,):
516 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}',
517 'draft-bierman02': '{}/config/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
518 url2 = url[RESTCONF_VERSION] + '/org-openroadm-network-topology:OMS-attributes/span'
519 network = 'openroadm-topology'
520 response = delete_request(url2.format('{}', network, link))
524 def get_ietf_network_node_request(network: str, node: str, content: str):
525 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
526 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
527 if RESTCONF_VERSION in ('rfc8040'):
528 format_args = ('{}', network, node, content)
529 elif content == 'config':
530 format_args = ('{}', content, network, node)
532 format_args = ('{}', 'operational', network, node)
533 response = get_request(url[RESTCONF_VERSION].format(*format_args))
535 res = response.json()
536 return_key = {'rfc8040': 'ietf-network:node',
537 'draft-bierman02': 'node'}
538 node = res[return_key[RESTCONF_VERSION]][0]
541 return {'status_code': response.status_code,
545 def del_ietf_network_node_request(network: str, node: str, content: str):
546 url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
547 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
548 if RESTCONF_VERSION in ('rfc8040'):
549 format_args = ('{}', network, node, content)
550 elif content == 'config':
551 format_args = ('{}', content, network, node)
553 format_args = ('{}', 'operational', network, node)
554 response = delete_request(url[RESTCONF_VERSION].format(*format_args))
559 # Service list operations
563 def get_ordm_serv_list_request():
564 url = {'rfc8040': '{}/data/org-openroadm-service:service-list?content=nonconfig',
565 'draft-bierman02': '{}/operational/org-openroadm-service:service-list/'}
566 response = get_request(url[RESTCONF_VERSION])
567 res = response.json()
568 return_key = {'rfc8040': 'org-openroadm-service:service-list',
569 'draft-bierman02': 'service-list'}
570 if return_key[RESTCONF_VERSION] in res.keys():
571 response_attribute = res[return_key[RESTCONF_VERSION]]
573 response_attribute = res['errors']['error'][0]
574 return {'status_code': response.status_code,
575 'service-list': response_attribute}
578 def get_ordm_serv_list_attr_request(attribute: str, value: str):
579 url = {'rfc8040': '{}/data/org-openroadm-service:service-list/{}={}?content=nonconfig',
580 'draft-bierman02': '{}/operational/org-openroadm-service:service-list/{}/{}'}
581 format_args = ('{}', attribute, value)
582 response = get_request(url[RESTCONF_VERSION].format(*format_args))
583 res = response.json()
584 return_key = {'rfc8040': 'org-openroadm-service:' + attribute,
585 'draft-bierman02': attribute}
586 if return_key[RESTCONF_VERSION] in res.keys():
587 response_attribute = res[return_key[RESTCONF_VERSION]]
589 response_attribute = res['errors']['error'][0]
590 return {'status_code': response.status_code,
591 attribute: response_attribute}
594 def get_serv_path_list_attr(attribute: str, value: str):
595 url = {'rfc8040': '{}/data/transportpce-service-path:service-path-list/{}={}?content=nonconfig',
596 'draft-bierman02': '{}/operational/transportpce-service-path:service-path-list/{}/{}'}
597 response = get_request(url[RESTCONF_VERSION].format('{}', attribute, value))
598 res = response.json()
599 return_key = {'rfc8040': 'transportpce-service-path:' + attribute,
600 'draft-bierman02': attribute}
601 if return_key[RESTCONF_VERSION] in res.keys():
602 response_attribute = res[return_key[RESTCONF_VERSION]]
604 response_attribute = res['errors']['error'][0]
605 return {'status_code': response.status_code,
606 attribute: response_attribute}
610 # TransportPCE internal API RPCs
614 def prepend_dict_keys(input_dict: dict, prefix: str):
616 for key, value in input_dict.items():
617 newkey = prefix + key
618 if isinstance(value, dict):
619 return_dict[newkey] = prepend_dict_keys(value, prefix)
620 # TODO: perhaps some recursion depth limit or another solution has to be considered here
621 # even if recursion depth is given by the input_dict argument
622 # direct (self-)recursive functions may carry unwanted side-effects such as ressource consumptions
624 return_dict[newkey] = value
628 def transportpce_api_rpc_request(api_module: str, rpc: str, payload: dict):
629 # pylint: disable=consider-using-f-string
630 url = "{}/operations/{}:{}".format('{}', api_module, rpc)
633 elif RESTCONF_VERSION == 'draft-bierman02':
634 data = prepend_dict_keys({'input': payload}, api_module + ':')
636 data = {'input': payload}
637 response = post_request(url, data)
638 if response.status_code == requests.codes.no_content:
641 res = response.json()
642 return_key = {'rfc8040': api_module + ':output',
643 'draft-bierman02': 'output'}
644 if response.status_code == requests.codes.internal_server_error:
647 return_output = res[return_key[RESTCONF_VERSION]]
648 return {'status_code': response.status_code,
649 'output': return_output}