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