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