5402eae5b4610b236cb8de6a77e90a8932e1cc6c
[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 process_list = []
50
51 def start_sims(sims_list):
52     for sim in sims_list:
53         print("starting simulator for " + sim + "...")
54         log_file = os.path.join(log_directory, sims[sim]['logfile'])
55         process = start_honeynode(log_file, sims[sim]['port'], sims[sim]['configfile'])
56         if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
57             print("simulator for " + sim + " started")
58         else:
59             print("simulator for " + sim + " failed to start")
60             shutdown_process(process)
61             for pid in process_list:
62                 shutdown_process(pid)
63             exit(3)
64         process_list.append(process)
65     return process_list
66
67
68 def start_tpce():
69     print("starting opendaylight...")
70     if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
71         process = start_lighty()
72         # TODO: add some sort of health check similar to Karaf below
73     else:
74         process = start_karaf()
75         if wait_until_log_contains(karaf_log, KARAF_OK_START_MSG, time_to_wait=60):
76             print("opendaylight started")
77         else:
78             print("opendaylight failed to start")
79             shutdown_process(process)
80             for pid in process_list:
81                 shutdown_process(pid)
82             exit(1)
83     process_list.append(process)
84     return process_list
85
86
87 def start_karaf():
88     print("starting KARAF TransportPCE build...")
89     executable = os.path.join(
90         os.path.dirname(os.path.realpath(__file__)),
91         "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
92     with open('odl.log', 'w') as outfile:
93         return subprocess.Popen(
94             ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
95
96
97 def start_lighty():
98     print("starting LIGHTY.IO TransportPCE build...")
99     executable = os.path.join(
100         os.path.dirname(os.path.realpath(__file__)),
101         "..", "..", "..", "lighty", "target", "tpce",
102         "clean-start-controller.sh")
103     with open('odl.log', 'w') as outfile:
104         return subprocess.Popen(
105             ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
106
107
108 def install_karaf_feature(feature_name: str):
109     print("installing feature " + feature_name)
110     executable = os.path.join(
111         os.path.dirname(os.path.realpath(__file__)),
112         "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
113     return subprocess.run([executable],
114                           input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
115                           universal_newlines=True)
116
117
118 def post_request(url, data, username, password):
119     return requests.request(
120         "POST", url, data=json.dumps(data),
121         headers=TYPE_APPLICATION_JSON, auth=(username, password))
122
123
124 def put_request(url, data, username, password):
125     return requests.request(
126         "PUT", url, data=json.dumps(data), headers=TYPE_APPLICATION_JSON,
127         auth=(username, password))
128
129
130 def delete_request(url, username, password):
131     return requests.request(
132         "DELETE", url, headers=TYPE_APPLICATION_JSON,
133         auth=(username, password))
134
135
136 def generate_connect_data(node_id: str, node_port: str):
137     data = {"node": [{
138         "node-id": node_id,
139         "netconf-node-topology:username": "admin",
140         "netconf-node-topology:password": "admin",
141         "netconf-node-topology:host": "127.0.0.1",
142         "netconf-node-topology:port": node_port,
143         "netconf-node-topology:tcp-only": "false",
144         "netconf-node-topology:pass-through": {}}]}
145     return data
146
147
148 def generate_link_data(xpdr_node: str, xpdr_num: str, network_num: str, rdm_node: str, srg_num: str,
149                        termination_num: str):
150     data = {
151         "networkutils:input": {
152             "networkutils:links-input": {
153                 "networkutils:xpdr-node": xpdr_node,
154                 "networkutils:xpdr-num": xpdr_num,
155                 "networkutils:network-num": network_num,
156                 "networkutils:rdm-node": rdm_node,
157                 "networkutils:srg-num": srg_num,
158                 "networkutils:termination-point-num": termination_num
159             }
160         }
161     }
162     return data
163
164
165 def shutdown_process(process):
166     if process is not None:
167         for child in psutil.Process(process.pid).children():
168             child.send_signal(signal.SIGINT)
169             child.wait()
170         process.send_signal(signal.SIGINT)
171
172
173 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
174     if os.path.isfile(honeynode_executable):
175         with open(log_file, 'w') as outfile:
176             return subprocess.Popen(
177                 [honeynode_executable, node_port, os.path.join(samples_directory, node_config_file_name)],
178                 stdout=outfile, stderr=outfile)
179
180
181 def wait_until_log_contains(log_file, searched_string, time_to_wait=20):
182     found = False
183     tail = None
184     try:
185         with timeout(seconds=time_to_wait):
186             print("Waiting for " + searched_string)
187             tail = subprocess.Popen(['tail', '-F', log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
188             regexp = re.compile(searched_string)
189             while True:
190                 line = tail.stdout.readline().decode('utf-8')
191                 if regexp.search(line):
192                     print("Searched string found.")
193                     found = True
194                     break
195     except TimeoutError:
196         print("Cannot find string "+searched_string+" after waiting for "+str(time_to_wait))
197     finally:
198         if tail is not None:
199             print("Stopping tail command")
200             tail.stderr.close()
201             tail.stdout.close()
202             tail.kill()
203             tail.wait()
204         return found
205
206
207 class timeout:
208     def __init__(self, seconds=1, error_message='Timeout'):
209         self.seconds = seconds
210         self.error_message = error_message
211
212     def handle_timeout(self, signum, frame):
213         raise TimeoutError(self.error_message)
214
215     def __enter__(self):
216         signal.signal(signal.SIGALRM, self.handle_timeout)
217         signal.alarm(self.seconds)
218
219     def __exit__(self, type, value, traceback):
220         signal.alarm(0)