Fix new pylint issues in functional tests
[transportpce.git] / tests / transportpce_tests / common / test_utils.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2020 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 # pylint: disable=too-many-arguments
14
15 import json
16 import os
17 # pylint: disable=wrong-import-order
18 import sys
19 import re
20 import signal
21 import subprocess
22 import time
23
24 import psutil
25 import requests
26
27 # pylint: disable=import-error
28 import simulators
29
30 SIMS = simulators.SIMS
31
32 HONEYNODE_OK_START_MSG = "Netconf SSH endpoint started successfully at 0.0.0.0"
33 KARAF_OK_START_MSG = re.escape(
34     "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
35 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
36
37 ODL_LOGIN = "admin"
38 ODL_PWD = "admin"
39 NODES_LOGIN = "admin"
40 NODES_PWD = "admin"
41 URL_CONFIG_NETCONF_TOPO = "{}/config/network-topology:network-topology/topology/topology-netconf/"
42 URL_CONFIG_ORDM_TOPO = "{}/config/ietf-network:networks/network/openroadm-topology/"
43 URL_CONFIG_OTN_TOPO = "{}/config/ietf-network:networks/network/otn-topology/"
44 URL_CONFIG_CLLI_NET = "{}/config/ietf-network:networks/network/clli-network/"
45 URL_CONFIG_ORDM_NET = "{}/config/ietf-network:networks/network/openroadm-network/"
46 URL_PORTMAPPING = "{}/config/transportpce-portmapping:network/nodes/"
47 URL_OPER_SERV_LIST = "{}/operational/org-openroadm-service:service-list/"
48 URL_OPER_SERV_PATH_LIST = "{}/operational/transportpce-service-path:service-path-list/"
49 URL_GET_NBINOTIFICATIONS_PROCESS_SERV = "{}/operations/nbi-notifications:get-notifications-process-service/"
50 URL_GET_NBINOTIFICATIONS_ALARM_SERV = "{}/operations/nbi-notifications:get-notifications-alarm-service/"
51 URL_SERV_CREATE = "{}/operations/org-openroadm-service:service-create"
52 URL_SERV_DELETE = "{}/operations/org-openroadm-service:service-delete"
53 URL_SERVICE_PATH = "{}/operations/transportpce-device-renderer:service-path"
54 URL_OTN_SERVICE_PATH = "{}/operations/transportpce-device-renderer:otn-service-path"
55 URL_TAPI_CREATE_CONNECTIVITY = "{}/operations/tapi-connectivity:create-connectivity-service"
56 URL_TAPI_DELETE_CONNECTIVITY = "{}/operations/tapi-connectivity:delete-connectivity-service"
57 URL_TAPI_GET_CONNECTIVITY = "{}/operations/tapi-connectivity:get-connectivity-service-details"
58 URL_CREATE_OTS_OMS = "{}/operations/transportpce-device-renderer:create-ots-oms"
59 URL_PATH_COMPUTATION_REQUEST = "{}/operations/transportpce-pce:path-computation-request"
60 URL_FULL_PORTMAPPING = "{}/config/transportpce-portmapping:network"
61 URL_TAPI_TOPOLOGY_DETAILS = "{}/operations/tapi-topology:get-topology-details"
62 URL_TAPI_NODE_DETAILS = "{}/operations/tapi-topology:get-node-details"
63 URL_TAPI_NEP_DETAILS = "{}/operations/tapi-topology:get-node-edge-point-details"
64 URL_TAPI_SIP_LIST = "{}/operations/tapi-common:get-service-interface-point-list"
65 URL_TAPI_SERVICE_LIST = "{}/operations/tapi-connectivity:get-connectivity-service-list"
66 URL_TAPI_NOTIFICATION_SUBS_SERVICE = "{}/operations/tapi-notification:create-notification-subscription-service"
67 URL_TAPI_GET_NOTIFICATION_LIST = "{}/operations/tapi-notification:get-notification-list"
68
69 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
70 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
71
72 REQUEST_TIMEOUT = 10
73
74 CODE_SHOULD_BE_200 = 'Http status code should be 200'
75 CODE_SHOULD_BE_201 = 'Http status code should be 201'
76
77 SIM_LOG_DIRECTORY = os.path.join(os.path.dirname(os.path.realpath(__file__)), "log")
78
79 process_list = []
80
81
82 if "USE_ODL_ALT_RESTCONF_PORT" in os.environ:
83     RESTCONF_BASE_URL = "http://localhost:" + os.environ['USE_ODL_ALT_RESTCONF_PORT'] + "/restconf"
84 else:
85     RESTCONF_BASE_URL = "http://localhost:8181/restconf"
86
87 if "USE_ODL_ALT_KARAF_INSTALL_DIR" in os.environ:
88     KARAF_INSTALLDIR = os.environ['USE_ODL_ALT_KARAF_INSTALL_DIR']
89 else:
90     KARAF_INSTALLDIR = "karaf"
91
92 KARAF_LOG = os.path.join(
93     os.path.dirname(os.path.realpath(__file__)),
94     "..", "..", "..", KARAF_INSTALLDIR, "target", "assembly", "data", "log", "karaf.log")
95
96 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
97     TPCE_LOG = 'odl-' + str(os.getpid()) + '.log'
98 else:
99     TPCE_LOG = KARAF_LOG
100
101
102 def start_sims(sims_list):
103     for sim in sims_list:
104         print("starting simulator " + sim[0] + " in OpenROADM device version " + sim[1] + "...")
105         log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
106         process = start_honeynode(log_file, sim)
107         if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
108             print("simulator for " + sim[0] + " started")
109         else:
110             print("simulator for " + sim[0] + " failed to start")
111             shutdown_process(process)
112             for pid in process_list:
113                 shutdown_process(pid)
114             sys.exit(3)
115         process_list.append(process)
116     return process_list
117
118
119 def start_tpce():
120     print("starting OpenDaylight...")
121     if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
122         process = start_lighty()
123         start_msg = LIGHTY_OK_START_MSG
124     else:
125         process = start_karaf()
126         start_msg = KARAF_OK_START_MSG
127     if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=300):
128         print("OpenDaylight started !")
129     else:
130         print("OpenDaylight failed to start !")
131         shutdown_process(process)
132         for pid in process_list:
133             shutdown_process(pid)
134         sys.exit(1)
135     process_list.append(process)
136     return process_list
137
138
139 def start_karaf():
140     print("starting KARAF TransportPCE build...")
141     executable = os.path.join(
142         os.path.dirname(os.path.realpath(__file__)),
143         "..", "..", "..", KARAF_INSTALLDIR, "target", "assembly", "bin", "karaf")
144     with open('odl.log', 'w', encoding='utf-8') as outfile:
145         return subprocess.Popen(
146             ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
147
148
149 def start_lighty():
150     print("starting LIGHTY.IO TransportPCE build...")
151     executable = os.path.join(
152         os.path.dirname(os.path.realpath(__file__)),
153         "..", "..", "..", "lighty", "target", "tpce",
154         "clean-start-controller.sh")
155     with open(TPCE_LOG, 'w', encoding='utf-8') as outfile:
156         return subprocess.Popen(
157             ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
158
159
160 def install_karaf_feature(feature_name: str):
161     print("installing feature " + feature_name)
162     executable = os.path.join(
163         os.path.dirname(os.path.realpath(__file__)),
164         "..", "..", "..", KARAF_INSTALLDIR, "target", "assembly", "bin", "client")
165     return subprocess.run([executable],
166                           input='feature:install ' + feature_name + '\n feature:list | grep '
167                           + feature_name + ' \n logout \n',
168                           universal_newlines=True, check=False)
169
170
171 def get_request(url):
172     return requests.request(
173         "GET", url.format(RESTCONF_BASE_URL),
174         headers=TYPE_APPLICATION_JSON,
175         auth=(ODL_LOGIN, ODL_PWD),
176         timeout=REQUEST_TIMEOUT)
177
178
179 def post_request(url, data):
180     if data:
181         print(json.dumps(data))
182         return requests.request(
183             "POST", url.format(RESTCONF_BASE_URL),
184             data=json.dumps(data),
185             headers=TYPE_APPLICATION_JSON,
186             auth=(ODL_LOGIN, ODL_PWD),
187             timeout=REQUEST_TIMEOUT)
188
189     return requests.request(
190         "POST", url.format(RESTCONF_BASE_URL),
191         headers=TYPE_APPLICATION_JSON,
192         auth=(ODL_LOGIN, ODL_PWD),
193         timeout=REQUEST_TIMEOUT)
194
195
196 def post_xmlrequest(url, data):
197     if data:
198         return requests.request(
199             "POST", url.format(RESTCONF_BASE_URL),
200             data=data,
201             headers=TYPE_APPLICATION_XML,
202             auth=(ODL_LOGIN, ODL_PWD),
203             timeout=REQUEST_TIMEOUT)
204     return None
205
206
207 def put_request(url, data):
208     return requests.request(
209         "PUT", url.format(RESTCONF_BASE_URL),
210         data=json.dumps(data),
211         headers=TYPE_APPLICATION_JSON,
212         auth=(ODL_LOGIN, ODL_PWD),
213         timeout=REQUEST_TIMEOUT)
214
215
216 def put_xmlrequest(url, data):
217     return requests.request(
218         "PUT", url.format(RESTCONF_BASE_URL),
219         data=data,
220         headers=TYPE_APPLICATION_XML,
221         auth=(ODL_LOGIN, ODL_PWD),
222         timeout=REQUEST_TIMEOUT)
223
224
225 def put_jsonrequest(url, data):
226     return requests.request(
227         "PUT", url.format(RESTCONF_BASE_URL),
228         data=data,
229         headers=TYPE_APPLICATION_JSON,
230         auth=(ODL_LOGIN, ODL_PWD),
231         timeout=REQUEST_TIMEOUT)
232
233
234 def rawput_request(url, data):
235     return requests.request(
236         "PUT", url.format(RESTCONF_BASE_URL),
237         data=data,
238         headers=TYPE_APPLICATION_JSON,
239         auth=(ODL_LOGIN, ODL_PWD),
240         timeout=REQUEST_TIMEOUT)
241
242
243 def rawpost_request(url, data):
244     return requests.request(
245         "POST", url.format(RESTCONF_BASE_URL),
246         data=data,
247         headers=TYPE_APPLICATION_JSON,
248         auth=(ODL_LOGIN, ODL_PWD),
249         timeout=REQUEST_TIMEOUT)
250
251
252 def delete_request(url):
253     return requests.request(
254         "DELETE", url.format(RESTCONF_BASE_URL),
255         headers=TYPE_APPLICATION_JSON,
256         auth=(ODL_LOGIN, ODL_PWD),
257         timeout=REQUEST_TIMEOUT)
258
259
260 def mount_device(node_id, sim):
261     url = URL_CONFIG_NETCONF_TOPO + "node/" + node_id
262     body = {"node": [{
263         "node-id": node_id,
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, body)
271     if wait_until_log_contains(TPCE_LOG, re.escape("Triggering notification stream NETCONF for node " + node_id), 180):
272         print("Node " + node_id + " correctly added to tpce topology", end='... ', flush=True)
273     else:
274         print("Node " + node_id + " 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 mount_tapi_device(node_id, sim):
282     url = URL_CONFIG_NETCONF_TOPO + "node/" + node_id
283     body = {"node": [{
284         "node-id": node_id,
285         "netconf-node-topology:username": NODES_LOGIN,
286         "netconf-node-topology:password": NODES_PWD,
287         "netconf-node-topology:host": "127.0.0.1",
288         "netconf-node-topology:port": SIMS[sim]['port'],
289         "netconf-node-topology:tcp-only": "false",
290         "netconf-node-topology:pass-through": {}}]}
291     response = put_request(url, body)
292     if wait_until_log_contains(TPCE_LOG, re.escape(f"TAPI node for or node {node_id} successfully merged"), 200):
293         print("Node " + node_id + " correctly added to tpce topology", end='... ', flush=True)
294     else:
295         print("Node " + node_id + " still not added to tpce topology", end='... ', flush=True)
296         if response.status_code == requests.codes.ok:
297             print("It was probably loaded at start-up", end='... ', flush=True)
298         # TODO an else-clause to abort test would probably be nice here
299     return response
300
301
302 def unmount_device(node_id):
303     url = URL_CONFIG_NETCONF_TOPO + "node/" + node_id
304     response = delete_request(url)
305     if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: " + node_id), 180):
306         print("Node " + node_id + " correctly deleted from tpce topology", end='... ', flush=True)
307     else:
308         print("Node " + node_id + " still not deleted from tpce topology", end='... ', flush=True)
309     return response
310
311
312 def connect_xpdr_to_rdm_request(xpdr_node: str, xpdr_num: str, network_num: str,
313                                 rdm_node: str, srg_num: str, termination_num: str):
314     url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
315     data = {
316         "networkutils:input": {
317             "networkutils:links-input": {
318                 "networkutils:xpdr-node": xpdr_node,
319                 "networkutils:xpdr-num": xpdr_num,
320                 "networkutils:network-num": network_num,
321                 "networkutils:rdm-node": rdm_node,
322                 "networkutils:srg-num": srg_num,
323                 "networkutils:termination-point-num": termination_num
324             }
325         }
326     }
327     return post_request(url, data)
328
329
330 def connect_rdm_to_xpdr_request(xpdr_node: str, xpdr_num: str, network_num: str,
331                                 rdm_node: str, srg_num: str, termination_num: str):
332     url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
333     data = {
334         "networkutils:input": {
335             "networkutils:links-input": {
336                 "networkutils:xpdr-node": xpdr_node,
337                 "networkutils:xpdr-num": xpdr_num,
338                 "networkutils:network-num": network_num,
339                 "networkutils:rdm-node": rdm_node,
340                 "networkutils:srg-num": srg_num,
341                 "networkutils:termination-point-num": termination_num
342             }
343         }
344     }
345     return post_request(url, data)
346
347
348 def connect_xpdr_to_rdm_tapi_request(xpdr_node: str, xpdr_num: str, rdm_node: str, srg_num: str):
349     url = "{}/operations/transportpce-tapinetworkutils:init-xpdr-rdm-tapi-link"
350     data = {
351         "input": {
352             "xpdr-node": xpdr_node,
353             "network-tp": xpdr_num,
354             "rdm-node": rdm_node,
355             "add-drop-tp": srg_num
356         }
357     }
358     return post_request(url, data)
359
360
361 def check_netconf_node_request(node: str, suffix: str):
362     url = URL_CONFIG_NETCONF_TOPO + (
363         "node/" + node + "/yang-ext:mount/org-openroadm-device:org-openroadm-device/" + suffix
364     )
365     return get_request(url)
366
367
368 def get_netconf_oper_request(node: str):
369     url = "{}/operational/network-topology:network-topology/topology/topology-netconf/node/" + node
370     return get_request(url)
371
372
373 def get_ordm_topo_request(suffix: str):
374     url = URL_CONFIG_ORDM_TOPO + suffix
375     return get_request(url)
376
377
378 def add_oms_attr_request(link: str, attr):
379     url = URL_CONFIG_ORDM_TOPO + (
380         "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
381     )
382     return put_request(url, attr)
383
384
385 def del_oms_attr_request(link: str):
386     url = URL_CONFIG_ORDM_TOPO + (
387         "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
388     )
389     return delete_request(url)
390
391
392 def get_clli_net_request():
393     return get_request(URL_CONFIG_CLLI_NET)
394
395
396 def get_ordm_net_request():
397     return get_request(URL_CONFIG_ORDM_NET)
398
399
400 def get_otn_topo_request():
401     return get_request(URL_CONFIG_OTN_TOPO)
402
403
404 def del_link_request(link: str):
405     url = URL_CONFIG_ORDM_TOPO + ("ietf-network-topology:link/" + link)
406     return delete_request(url)
407
408
409 def del_node_request(node: str):
410     url = URL_CONFIG_CLLI_NET + ("node/" + node)
411     return delete_request(url)
412
413
414 def portmapping_request(suffix: str):
415     url = URL_PORTMAPPING + suffix
416     return get_request(url)
417
418
419 def get_notifications_process_service_request(attr):
420     return post_request(URL_GET_NBINOTIFICATIONS_PROCESS_SERV, attr)
421
422
423 def get_notifications_alarm_service_request(attr):
424     return post_request(URL_GET_NBINOTIFICATIONS_ALARM_SERV, attr)
425
426
427 def get_service_list_request(suffix: str):
428     return get_request(URL_OPER_SERV_LIST + suffix)
429
430
431 def get_service_path_list_request(suffix: str):
432     return get_request(URL_OPER_SERV_PATH_LIST + suffix)
433
434
435 def service_create_request(attr):
436     return post_request(URL_SERV_CREATE, attr)
437
438
439 def service_delete_request(servicename: str,
440                            requestid="e3028bae-a90f-4ddd-a83f-cf224eba0e58",
441                            notificationurl="http://localhost:8585/NotificationServer/notify"):
442     attr = {"input": {
443         "sdnc-request-header": {
444             "request-id": requestid,
445             "rpc-action": "service-delete",
446             "request-system-id": "appname",
447             "notification-url": notificationurl},
448         "service-delete-req-info": {
449             "service-name": servicename,
450             "tail-retention": "no"}}}
451     return post_request(URL_SERV_DELETE, attr)
452
453
454 def service_path_request(operation: str, servicename: str, wavenumber: str, nodes, centerfreq: str,
455                          slotwidth: int, minfreq: float, maxfreq: float, lowerslotnumber: int,
456                          higherslotnumber: int, modulation_format="dp-qpsk"):
457     attr = {"renderer:input": {
458         "renderer:service-name": servicename,
459         "renderer:wave-number": wavenumber,
460         "renderer:modulation-format": modulation_format,
461         "renderer:operation": operation,
462         "renderer:nodes": nodes,
463         "renderer:center-freq": centerfreq,
464         "renderer:nmc-width": slotwidth,
465         "renderer:min-freq": minfreq,
466         "renderer:max-freq": maxfreq,
467         "renderer:lower-spectral-slot-number": lowerslotnumber,
468         "renderer:higher-spectral-slot-number": higherslotnumber}}
469     return post_request(URL_SERVICE_PATH, attr)
470
471
472 def otn_service_path_request(operation: str, servicename: str, servicerate: str, serviceformat: str, nodes,
473                              eth_attr=None):
474     attr = {"service-name": servicename,
475             "operation": operation,
476             "service-rate": servicerate,
477             "service-format": serviceformat,
478             "nodes": nodes}
479     if eth_attr:
480         attr.update(eth_attr)
481     return post_request(URL_OTN_SERVICE_PATH, {"renderer:input": attr})
482
483
484 def create_ots_oms_request(nodeid: str, lcp: str):
485     attr = {"input": {
486         "node-id": nodeid,
487         "logical-connection-point": lcp}}
488     return post_request(URL_CREATE_OTS_OMS, attr)
489
490
491 def path_computation_request(requestid: str, servicename: str, serviceaend, servicezend,
492                              hardconstraints=None, softconstraints=None, metric="hop-count", other_attr=None):
493     attr = {"service-name": servicename,
494             "resource-reserve": "true",
495             "service-handler-header": {"request-id": requestid},
496             "service-a-end": serviceaend,
497             "service-z-end": servicezend,
498             "pce-routing-metric": metric}
499     if hardconstraints:
500         attr.update({"hard-constraints": hardconstraints})
501     if softconstraints:
502         attr.update({"soft-constraints": softconstraints})
503     if other_attr:
504         attr.update(other_attr)
505     return post_request(URL_PATH_COMPUTATION_REQUEST, {"input": attr})
506
507
508 def tapi_create_connectivity_request(topologyidorname):
509     return post_request(URL_TAPI_CREATE_CONNECTIVITY, topologyidorname)
510
511
512 def tapi_get_connectivity_request(serviceidorname):
513     attr = {
514         "input": {
515             "service-id-or-name": serviceidorname}}
516     return post_request(URL_TAPI_GET_CONNECTIVITY, attr)
517
518
519 def tapi_delete_connectivity_request(serviceidorname):
520     attr = {
521         "input": {
522             "service-id-or-name": serviceidorname}}
523     return post_request(URL_TAPI_DELETE_CONNECTIVITY, attr)
524
525
526 def tapi_get_topology_details_request(topologyidorname):
527     attr = {
528         "input": {
529             "topology-id-or-name": topologyidorname}}
530     return post_request(URL_TAPI_TOPOLOGY_DETAILS, attr)
531
532
533 def tapi_get_node_details_request(topologyidorname, nodeidorname):
534     attr = {
535         "input": {
536             "topology-id-or-name": topologyidorname,
537             "node-id-or-name": nodeidorname}}
538     return post_request(URL_TAPI_NODE_DETAILS, attr)
539
540
541 def tapi_get_node_edge_point_details_request(topologyidorname, nodeidorname, nepidorname):
542     attr = {
543         "input": {
544             "topology-id-or-name": topologyidorname,
545             "node-id-or-name": nodeidorname,
546             "ep-id-or-name": nepidorname}}
547     return post_request(URL_TAPI_NEP_DETAILS, attr)
548
549
550 def tapi_get_sip_details_request():
551     return post_request(URL_TAPI_SIP_LIST, "")
552
553
554 def tapi_get_service_list_request():
555     return post_request(URL_TAPI_SERVICE_LIST, "")
556
557
558 def tapi_create_notification_subscription_service_request(attr):
559     return post_request(URL_TAPI_NOTIFICATION_SUBS_SERVICE, attr)
560
561
562 def tapi_get_notifications_list_request(attr):
563     return post_request(URL_TAPI_GET_NOTIFICATION_LIST, attr)
564
565
566 def shutdown_process(process):
567     if process is not None:
568         for child in psutil.Process(process.pid).children():
569             child.send_signal(signal.SIGINT)
570             child.wait()
571         process.send_signal(signal.SIGINT)
572
573
574 def start_honeynode(log_file: str, sim):
575     executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
576                               "..", "..", "honeynode", sim[1], "honeynode-simulator", "honeycomb-tpce")
577     sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
578                                     "..", "..", "sample_configs", "openroadm", sim[1])
579     if os.path.isfile(executable):
580         with open(log_file, 'w', encoding='utf-8') as outfile:
581             return subprocess.Popen(
582                 [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
583                 stdout=outfile, stderr=outfile)
584     return None
585
586
587 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
588     # pylint: disable=lost-exception
589     # pylint: disable=consider-using-with
590     stringfound = False
591     filefound = False
592     line = None
593     try:
594         with TimeOut(seconds=time_to_wait):
595             while not os.path.exists(log_file):
596                 time.sleep(0.2)
597             filelogs = open(log_file, 'r', encoding='utf-8')
598             filelogs.seek(0, 2)
599             filefound = True
600             print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
601             compiled_regexp = re.compile(regexp)
602             while True:
603                 line = filelogs.readline()
604                 if compiled_regexp.search(line):
605                     print("Pattern found!", end=' ')
606                     stringfound = True
607                     break
608                 if not line:
609                     time.sleep(0.1)
610     except TimeoutError:
611         print("Pattern not found after " + str(time_to_wait), end=" seconds! ", flush=True)
612     except PermissionError:
613         print("Permission Error when trying to access the log file", end=" ... ", flush=True)
614     finally:
615         if filefound:
616             filelogs.close()
617         else:
618             print("log file does not exist or is not accessible... ", flush=True)
619         return stringfound
620
621
622 class TimeOut:
623     def __init__(self, seconds=1, error_message='Timeout'):
624         self.seconds = seconds
625         self.error_message = error_message
626
627     def handle_timeout(self, signum, frame):
628         raise TimeoutError(self.error_message)
629
630     def __enter__(self):
631         signal.signal(signal.SIGALRM, self.handle_timeout)
632         signal.alarm(self.seconds)
633
634     def __exit__(self, type, value, traceback):
635         # pylint: disable=W0622
636         signal.alarm(0)