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