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