minor changes in functional tests
[transportpce.git] / tests / transportpce_tests / 2.2.1 / test_utils.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2020 Orange, Inc. and others.  All rights reserved.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 import json
11 import os
12 import re
13 import signal
14 import subprocess
15
16 import psutil
17 import requests
18
19 sims = {
20     'xpdra': {'port': '17840', 'configfile': 'oper-XPDRA.xml', 'logfile': 'oper-XPDRA.log'},
21     'roadma': {'port': '17841', 'configfile': 'oper-ROADMA.xml', 'logfile': 'oper-ROADMA.log'},
22     'roadmb': {'port': '17842', 'configfile': 'oper-ROADMB.xml', 'logfile': 'oper-ROADMB.log'},
23     'roadmc': {'port': '17843', 'configfile': 'oper-ROADMC.xml', 'logfile': 'oper-ROADMC.log'},
24     'xpdrc': {'port': '17844', 'configfile': 'oper-XPDRC.xml', 'logfile': 'oper-XPDRC.log'},
25     'spdrav2': {'port': '17845', 'configfile': 'oper-SPDRAv2.xml', 'logfile': 'oper-SPDRAv2.log'},
26     'spdrav1': {'port': '17846', 'configfile': 'oper-SPDRAv1.xml', 'logfile': 'oper-SPDRAv1.log'}
27 }
28
29 HONEYNODE_OK_START_MSG = re.escape("Netconf SSH endpoint started successfully at 0.0.0.0")
30 KARAF_OK_START_MSG = re.escape("Blueprint container for bundle "
31                                "org.opendaylight.netconf.restconf") + ".* was successfully created"
32
33 TYPE_APPLICATION_JSON = {'content-type': 'application/json'}
34
35 honeynode_executable = os.path.join(
36     os.path.dirname(os.path.realpath(__file__)),
37     "..", "..", "honeynode", "2.2.1", "honeynode-simulator", "honeycomb-tpce")
38
39 samples_directory = os.path.join(
40     os.path.dirname(os.path.realpath(__file__)),
41     "..", "..", "sample_configs", "openroadm", "2.2.1")
42
43 log_directory = os.path.dirname(os.path.realpath(__file__))
44
45 karaf_log = os.path.join(
46     os.path.dirname(os.path.realpath(__file__)),
47     "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
48
49
50 def start_sim(sim):
51     print("starting simulator for " + sim + "...")
52     log_file = os.path.join(log_directory, sims[sim]['logfile'])
53     process = start_honeynode(log_file, sims[sim]['port'], sims[sim]['configfile'])
54     if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
55         print("simulator for " + sim + " started")
56     else:
57         print("simulator for " + sim + "failed to start")
58     return process
59
60
61 def start_tpce():
62     print("starting opendaylight...")
63     if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
64         process = start_lighty()
65         # TODO: add some sort of health check similar to Karaf below
66     else:
67         process = start_karaf()
68         if wait_until_log_contains(karaf_log, KARAF_OK_START_MSG, time_to_wait=60):
69             print("opendaylight started")
70         else:
71             print("opendaylight failed to start")
72             shutdown_process(process)
73             exit(1)
74     return process
75
76
77 def start_karaf():
78     print("starting KARAF TransportPCE build...")
79     executable = os.path.join(
80         os.path.dirname(os.path.realpath(__file__)),
81         "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
82     with open('odl.log', 'w') as outfile:
83         return subprocess.Popen(
84             ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
85
86
87 def start_lighty():
88     print("starting LIGHTY.IO TransportPCE build...")
89     executable = os.path.join(
90         os.path.dirname(os.path.realpath(__file__)),
91         "..", "..", "..", "lighty", "target", "tpce",
92         "clean-start-controller.sh")
93     with open('odl.log', 'w') as outfile:
94         return subprocess.Popen(
95             ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
96
97
98 def install_karaf_feature(feature_name: str):
99     print("installing feature " + feature_name)
100     executable = os.path.join(
101         os.path.dirname(os.path.realpath(__file__)),
102         "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
103     return subprocess.run([executable],
104                           input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
105                           universal_newlines=True)
106
107
108 def post_request(url, data, username, password):
109     return requests.request(
110         "POST", url, data=json.dumps(data),
111         headers=TYPE_APPLICATION_JSON, auth=(username, password))
112
113
114 def put_request(url, data, username, password):
115     return requests.request(
116         "PUT", url, data=json.dumps(data), headers=TYPE_APPLICATION_JSON,
117         auth=(username, password))
118
119
120 def delete_request(url, username, password):
121     return requests.request(
122         "DELETE", url, headers=TYPE_APPLICATION_JSON,
123         auth=(username, password))
124
125
126 def generate_connect_data(node_id: str, node_port: str):
127     data = {"node": [{
128         "node-id": node_id,
129         "netconf-node-topology:username": "admin",
130         "netconf-node-topology:password": "admin",
131         "netconf-node-topology:host": "127.0.0.1",
132         "netconf-node-topology:port": node_port,
133         "netconf-node-topology:tcp-only": "false",
134         "netconf-node-topology:pass-through": {}}]}
135     return data
136
137
138 def generate_link_data(xpdr_node: str, xpdr_num: str, network_num: str, rdm_node: str, srg_num: str,
139                        termination_num: str):
140     data = {
141         "networkutils:input": {
142             "networkutils:links-input": {
143                 "networkutils:xpdr-node": xpdr_node,
144                 "networkutils:xpdr-num": xpdr_num,
145                 "networkutils:network-num": network_num,
146                 "networkutils:rdm-node": rdm_node,
147                 "networkutils:srg-num": srg_num,
148                 "networkutils:termination-point-num": termination_num
149             }
150         }
151     }
152     return data
153
154
155 def shutdown_process(process):
156     if process is not None:
157         for child in psutil.Process(process.pid).children():
158             child.send_signal(signal.SIGINT)
159             child.wait()
160         process.send_signal(signal.SIGINT)
161
162
163 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
164     if os.path.isfile(honeynode_executable):
165         with open(log_file, 'w') as outfile:
166             return subprocess.Popen(
167                 [honeynode_executable, node_port, os.path.join(samples_directory, node_config_file_name)],
168                 stdout=outfile, stderr=outfile)
169
170
171 def wait_until_log_contains(log_file, searched_string, time_to_wait=20):
172     found = False
173     tail = None
174     try:
175         with timeout(seconds=time_to_wait):
176             print("Waiting for " + searched_string)
177             tail = subprocess.Popen(['tail', '-F', log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
178             regexp = re.compile(searched_string)
179             while True:
180                 line = tail.stdout.readline().decode('utf-8')
181                 if regexp.search(line):
182                     print("Searched string found.")
183                     found = True
184                     break
185     except TimeoutError:
186         print("Cannot find string "+searched_string+" after waiting for "+str(time_to_wait))
187     finally:
188         if tail is not None:
189             print("Stopping tail command")
190             tail.stderr.close()
191             tail.stdout.close()
192             tail.kill()
193             tail.wait()
194         return found
195
196
197 class timeout:
198     def __init__(self, seconds=1, error_message='Timeout'):
199         self.seconds = seconds
200         self.error_message = error_message
201
202     def handle_timeout(self, signum, frame):
203         raise TimeoutError(self.error_message)
204
205     def __enter__(self):
206         signal.signal(signal.SIGALRM, self.handle_timeout)
207         signal.alarm(self.seconds)
208
209     def __exit__(self, type, value, traceback):
210         signal.alarm(0)