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