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