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