Reintroduce nb-bierman02 support in new func tests
[transportpce.git] / tests / transportpce_tests / common / test_utils_rfc8040.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2021 Orange, Inc. and others.  All rights reserved.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # pylint: disable=no-member
13
14 import json
15 import os
16 # pylint: disable=wrong-import-order
17 import sys
18 import re
19 import signal
20 import subprocess
21 import time
22
23 import psutil
24 import requests
25
26 # pylint: disable=import-error
27 import simulators
28
29 SIMS = simulators.SIMS
30
31 HONEYNODE_OK_START_MSG = 'Netconf SSH endpoint started successfully at 0.0.0.0'
32 KARAF_OK_START_MSG = re.escape(
33     "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
34 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
35
36 ODL_LOGIN = 'admin'
37 ODL_PWD = 'admin'
38 NODES_LOGIN = 'admin'
39 NODES_PWD = 'admin'
40
41 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
42 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
43
44 CODE_SHOULD_BE_200 = 'Http status code should be 200'
45 CODE_SHOULD_BE_201 = 'Http status code should be 201'
46
47 SIM_LOG_DIRECTORY = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'log')
48
49 process_list = []
50
51 if 'USE_ODL_ALT_RESTCONF_PORT' in os.environ:
52     RESTCONF_PORT = os.environ['USE_ODL_ALT_RESTCONF_PORT']
53 else:
54     RESTCONF_PORT = 8181
55
56 RESTCONF_PATH_PREFIX = {'rfc8040': '/rests',
57                         'draft-bierman02': '/restconf'}
58 if 'USE_ODL_RESTCONF_VERSION' in os.environ:
59     RESTCONF_VERSION = os.environ['USE_ODL_RESTCONF_VERSION']
60     if RESTCONF_VERSION not in RESTCONF_PATH_PREFIX.keys():
61         print('unsupported RESTCONF version ' + RESTCONF_VERSION)
62         sys.exit(3)
63 else:
64     RESTCONF_VERSION = 'rfc8040'
65
66 RESTCONF_BASE_URL = 'http://localhost:' + RESTCONF_PORT + RESTCONF_PATH_PREFIX[RESTCONF_VERSION]
67
68 if 'USE_ODL_ALT_KARAF_INSTALL_DIR' in os.environ:
69     KARAF_INSTALLDIR = os.environ['USE_ODL_ALT_KARAF_INSTALL_DIR']
70 else:
71     KARAF_INSTALLDIR = 'karaf'
72
73 KARAF_LOG = os.path.join(
74     os.path.dirname(os.path.realpath(__file__)),
75     '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'data', 'log', 'karaf.log')
76
77 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
78     TPCE_LOG = 'odl-' + str(os.getpid()) + '.log'
79 else:
80     TPCE_LOG = KARAF_LOG
81
82 #
83 # Basic HTTP operations
84 #
85
86
87 def get_request(url):
88     return requests.request(
89         'GET', url.format(RESTCONF_BASE_URL),
90         headers=TYPE_APPLICATION_JSON,
91         auth=(ODL_LOGIN, ODL_PWD))
92
93
94 def put_request(url, data):
95     return requests.request(
96         'PUT', url.format(RESTCONF_BASE_URL),
97         data=json.dumps(data),
98         headers=TYPE_APPLICATION_JSON,
99         auth=(ODL_LOGIN, ODL_PWD))
100
101
102 def delete_request(url):
103     return requests.request(
104         'DELETE', url.format(RESTCONF_BASE_URL),
105         headers=TYPE_APPLICATION_JSON,
106         auth=(ODL_LOGIN, ODL_PWD))
107
108 #
109 # Process management
110 #
111
112
113 def start_sims(sims_list):
114     for sim in sims_list:
115         print('starting simulator ' + sim[0] + ' in OpenROADM device version ' + sim[1] + '...')
116         log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
117         process = start_honeynode(log_file, sim)
118         if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
119             print('simulator for ' + sim[0] + ' started')
120         else:
121             print('simulator for ' + sim[0] + ' failed to start')
122             shutdown_process(process)
123             for pid in process_list:
124                 shutdown_process(pid)
125             sys.exit(3)
126         process_list.append(process)
127     return process_list
128
129
130 def start_tpce():
131     print('starting OpenDaylight...')
132     if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
133         process = start_lighty()
134         start_msg = LIGHTY_OK_START_MSG
135     else:
136         process = start_karaf()
137         start_msg = KARAF_OK_START_MSG
138     if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=300):
139         print('OpenDaylight started !')
140     else:
141         print('OpenDaylight failed to start !')
142         shutdown_process(process)
143         for pid in process_list:
144             shutdown_process(pid)
145         sys.exit(1)
146     process_list.append(process)
147     return process_list
148
149
150 def start_karaf():
151     print('starting KARAF TransportPCE build...')
152     executable = os.path.join(
153         os.path.dirname(os.path.realpath(__file__)),
154         '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'karaf')
155     with open('odl.log', 'w', encoding='utf-8') as outfile:
156         return subprocess.Popen(
157             ['sh', executable, 'server'], stdout=outfile, stderr=outfile, stdin=None)
158
159
160 def start_lighty():
161     print('starting LIGHTY.IO TransportPCE build...')
162     executable = os.path.join(
163         os.path.dirname(os.path.realpath(__file__)),
164         '..', '..', '..', 'lighty', 'target', 'tpce',
165         'clean-start-controller.sh')
166     with open(TPCE_LOG, 'w', encoding='utf-8') as outfile:
167         return subprocess.Popen(
168             ['sh', executable], stdout=outfile, stderr=outfile, stdin=None)
169
170
171 def install_karaf_feature(feature_name: str):
172     print('installing feature ' + feature_name)
173     executable = os.path.join(
174         os.path.dirname(os.path.realpath(__file__)),
175         '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'client')
176     return subprocess.run([executable],
177                           input='feature:install ' + feature_name + '\n feature:list | grep '
178                           + feature_name + ' \n logout \n',
179                           universal_newlines=True, check=False)
180
181
182 def shutdown_process(process):
183     if process is not None:
184         for child in psutil.Process(process.pid).children():
185             child.send_signal(signal.SIGINT)
186             child.wait()
187         process.send_signal(signal.SIGINT)
188
189
190 def start_honeynode(log_file: str, sim):
191     executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
192                               '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
193     sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
194                                     '..', '..', 'sample_configs', 'openroadm', sim[1])
195     if os.path.isfile(executable):
196         with open(log_file, 'w', encoding='utf-8') as outfile:
197             return subprocess.Popen(
198                 [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
199                 stdout=outfile, stderr=outfile)
200     return None
201
202
203 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
204     # pylint: disable=lost-exception
205     # pylint: disable=consider-using-with
206     stringfound = False
207     filefound = False
208     line = None
209     try:
210         with TimeOut(seconds=time_to_wait):
211             while not os.path.exists(log_file):
212                 time.sleep(0.2)
213             filelogs = open(log_file, 'r', encoding='utf-8')
214             filelogs.seek(0, 2)
215             filefound = True
216             print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
217             compiled_regexp = re.compile(regexp)
218             while True:
219                 line = filelogs.readline()
220                 if compiled_regexp.search(line):
221                     print('Pattern found!', end=' ')
222                     stringfound = True
223                     break
224                 if not line:
225                     time.sleep(0.1)
226     except TimeoutError:
227         print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True)
228     except PermissionError:
229         print('Permission Error when trying to access the log file', end=' ... ', flush=True)
230     finally:
231         if filefound:
232             filelogs.close()
233         else:
234             print('log file does not exist or is not accessible... ', flush=True)
235         return stringfound
236
237
238 class TimeOut:
239     def __init__(self, seconds=1, error_message='Timeout'):
240         self.seconds = seconds
241         self.error_message = error_message
242
243     def handle_timeout(self, signum, frame):
244         raise TimeoutError(self.error_message)
245
246     def __enter__(self):
247         signal.signal(signal.SIGALRM, self.handle_timeout)
248         signal.alarm(self.seconds)
249
250     def __exit__(self, type, value, traceback):
251         # pylint: disable=W0622
252         signal.alarm(0)
253
254 #
255 # Basic NetCONF device operations
256 #
257
258
259 def mount_device(node: str, sim: str):
260     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
261            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
262     body = {'node': [{
263         'node-id': node,
264         'netconf-node-topology:username': NODES_LOGIN,
265         'netconf-node-topology:password': NODES_PWD,
266         'netconf-node-topology:host': '127.0.0.1',
267         'netconf-node-topology:port': SIMS[sim]['port'],
268         'netconf-node-topology:tcp-only': 'false',
269         'netconf-node-topology:pass-through': {}}]}
270     response = put_request(url[RESTCONF_VERSION].format('{}', node), body)
271     if wait_until_log_contains(TPCE_LOG, re.escape('Triggering notification stream NETCONF for node ' + node), 180):
272         print('Node ' + node + ' correctly added to tpce topology', end='... ', flush=True)
273     else:
274         print('Node ' + node + ' still not added to tpce topology', end='... ', flush=True)
275         if response.status_code == requests.codes.ok:
276             print('It was probably loaded at start-up', end='... ', flush=True)
277         # TODO an else-clause to abort test would probably be nice here
278     return response
279
280
281 def unmount_device(node: str):
282     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
283            'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
284     response = delete_request(url[RESTCONF_VERSION].format('{}', node))
285     if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: " + node), 180):
286         print('Node ' + node + ' correctly deleted from tpce topology', end='... ', flush=True)
287     else:
288         print('Node ' + node + ' still not deleted from tpce topology', end='... ', flush=True)
289     return response
290
291
292 def check_device_connection(node: str):
293     url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
294            'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}'}
295     response = get_request(url[RESTCONF_VERSION].format('{}', node))
296     res = response.json()
297     return_key = {'rfc8040': 'network-topology:node',
298                   'draft-bierman02': 'node'}
299     if return_key[RESTCONF_VERSION] in res.keys():
300         connection_status = res[return_key[RESTCONF_VERSION]][0]['netconf-node-topology:connection-status']
301     else:
302         connection_status = res['errors']['error'][0]
303     return {'status_code': response.status_code,
304             'connection-status': connection_status}
305
306 #
307 # Portmapping operations
308 #
309
310
311 def get_portmapping(node: str):
312     url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}',
313            'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}'}
314     response = get_request(url[RESTCONF_VERSION].format('{}', node))
315     res = response.json()
316     return_key = {'rfc8040': 'transportpce-portmapping:nodes',
317                   'draft-bierman02': 'nodes'}
318     nodes = res[return_key[RESTCONF_VERSION]]
319     return {'status_code': response.status_code,
320             'nodes': nodes}
321
322
323 def get_portmapping_node_info(node: str):
324     url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/node-info',
325            'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/node-info'}
326     response = get_request(url[RESTCONF_VERSION].format('{}', node))
327     res = response.json()
328     return_key = {'rfc8040': 'transportpce-portmapping:node-info',
329                   'draft-bierman02': 'node-info'}
330     if return_key[RESTCONF_VERSION] in res.keys():
331         node_info = res[return_key[RESTCONF_VERSION]]
332     else:
333         node_info = res['errors']['error'][0]
334     return {'status_code': response.status_code,
335             'node-info': node_info}
336
337
338 def portmapping_request(node: str, mapping: str):
339     url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/mapping={}',
340            'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/mapping/{}'}
341     response = get_request(url[RESTCONF_VERSION].format('{}', node, mapping))
342     res = response.json()
343     return_key = {'rfc8040': 'transportpce-portmapping:mapping',
344                   'draft-bierman02': 'mapping'}
345     mapping = res[return_key[RESTCONF_VERSION]]
346     return {'status_code': response.status_code,
347             'mapping': mapping}
348
349
350 def portmapping_mc_capa_request(node: str, mc_capa: str):
351     url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/mc-capabilities={}',
352            'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/mc-capabilities/{}'}
353     response = get_request(url[RESTCONF_VERSION].format('{}', node, mc_capa))
354     res = response.json()
355     return_key = {'rfc8040': 'transportpce-portmapping:mc-capabilities',
356                   'draft-bierman02': 'mc-capabilities'}
357     capabilities = res[return_key[RESTCONF_VERSION]]
358     return {'status_code': response.status_code,
359             'mc-capabilities': capabilities}
360
361 #
362 # Topology operations
363 #
364
365
366 def get_ietf_network_request(network: str, content: str):
367     url = {'rfc8040': '{}/data/ietf-network:networks/network={}?content={}',
368            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}'}
369     if RESTCONF_VERSION == 'rfc8040':
370         format_args = ('{}', network, content)
371     elif content == 'config':
372         format_args = ('{}', content, network)
373     else:
374         format_args = ('{}', 'operational', network)
375     response = get_request(url[RESTCONF_VERSION].format(*format_args))
376     res = response.json()
377     return_key = {'rfc8040': 'ietf-network:network',
378                   'draft-bierman02': 'network'}
379     networks = res[return_key[RESTCONF_VERSION]]
380     return {'status_code': response.status_code,
381             'network': networks}