improve devices connection methods in tests
[transportpce.git] / tests / transportpce_tests / common / 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 import simulators
20
21 sims = simulators.sims
22 honeynode_executable = simulators.honeynode_executable
23 samples_directory = simulators.samples_directory
24
25 HONEYNODE_OK_START_MSG = "Netconf SSH endpoint started successfully at 0.0.0.0"
26 KARAF_OK_START_MSG = re.escape(
27     "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
28
29
30 RESTCONF_BASE_URL = "http://localhost:8181/restconf"
31 ODL_LOGIN = "admin"
32 ODL_PWD = "admin"
33 NODES_LOGIN = "admin"
34 NODES_PWD = "admin"
35
36 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
37 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
38
39 CODE_SHOULD_BE_200 = 'Http status code should be 200'
40 CODE_SHOULD_BE_201 = 'Http status code should be 201'
41
42 log_directory = os.path.dirname(os.path.realpath(__file__))
43
44 karaf_log = os.path.join(
45     os.path.dirname(os.path.realpath(__file__)),
46     "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
47
48 process_list = []
49
50 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
51     tpce_log = 'odl.log'
52 else:
53     tpce_log = karaf_log
54
55 def start_sims(sims_list):
56     for sim in sims_list:
57         print("starting simulator for " + sim + "...")
58         log_file = os.path.join(log_directory, sims[sim]['logfile'])
59         process = start_honeynode(log_file, sims[sim]['port'], sims[sim]['configfile'])
60         if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
61             print("simulator for " + sim + " started")
62         else:
63             print("simulator for " + sim + " failed to start")
64             shutdown_process(process)
65             for pid in process_list:
66                 shutdown_process(pid)
67             exit(3)
68         process_list.append(process)
69     return process_list
70
71
72 def start_tpce():
73     print("starting OpenDaylight...")
74     if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
75         process = start_lighty()
76         # TODO: add some sort of health check similar to Karaf below
77     else:
78         process = start_karaf()
79         if wait_until_log_contains(karaf_log, KARAF_OK_START_MSG, time_to_wait=60):
80             print("OpenDaylight started !")
81         else:
82             print("OpenDaylight failed to start !")
83             shutdown_process(process)
84             for pid in process_list:
85                 shutdown_process(pid)
86             exit(1)
87     process_list.append(process)
88     return process_list
89
90
91 def start_karaf():
92     print("starting KARAF TransportPCE build...")
93     executable = os.path.join(
94         os.path.dirname(os.path.realpath(__file__)),
95         "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
96     with open('odl.log', 'w') as outfile:
97         return subprocess.Popen(
98             ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
99
100
101 def start_lighty():
102     print("starting LIGHTY.IO TransportPCE build...")
103     executable = os.path.join(
104         os.path.dirname(os.path.realpath(__file__)),
105         "..", "..", "..", "lighty", "target", "tpce",
106         "clean-start-controller.sh")
107     with open('odl.log', 'w') as outfile:
108         return subprocess.Popen(
109             ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
110
111
112 def install_karaf_feature(feature_name: str):
113     print("installing feature " + feature_name)
114     executable = os.path.join(
115         os.path.dirname(os.path.realpath(__file__)),
116         "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
117     return subprocess.run([executable],
118                           input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
119                           universal_newlines=True)
120
121
122 def post_request(url, data, username, password):
123     return requests.request(
124         "POST", url, data=json.dumps(data),
125         headers=TYPE_APPLICATION_JSON, auth=(username, password))
126
127
128 def put_request(url, data, username, password):
129     return requests.request(
130         "PUT", url, data=json.dumps(data), headers=TYPE_APPLICATION_JSON,
131         auth=(username, password))
132
133
134 def delete_request(url, username, password):
135     return requests.request(
136         "DELETE", url, headers=TYPE_APPLICATION_JSON,
137         auth=(username, password))
138
139
140 def mount_device(node_id, sim):
141     url = ("{}/config/network-topology:network-topology/topology/topology-netconf/node/"
142            + node_id).format(RESTCONF_BASE_URL)
143     headers = {"node": [{
144         "node-id": node_id,
145         "netconf-node-topology:username": NODES_LOGIN,
146         "netconf-node-topology:password": NODES_PWD,
147         "netconf-node-topology:host": "127.0.0.1",
148         "netconf-node-topology:port": sims[sim]['port'],
149         "netconf-node-topology:tcp-only": "false",
150         "netconf-node-topology:pass-through": {}}]}
151     response = put_request(url, headers, ODL_LOGIN, ODL_PWD)
152     if wait_until_log_contains(tpce_log, re.escape("Triggering notification stream NETCONF for node "+node_id), 60):
153         print("Node "+node_id+" correctly added to tpce topology", end='... ', flush=True)
154     else:
155         print("Node "+node_id+" still not added to tpce topology", end='... ', flush=True)
156         if response.status_code == requests.codes.ok:
157             print("It was probably loaded at start-up", end='... ', flush=True)
158         # TODO an else-clause to abort test would probably be nice here
159     return response
160
161
162 def unmount_device(node_id):
163     url = ("{}/config/network-topology:network-topology/topology/topology-netconf/node/"
164            + node_id).format(RESTCONF_BASE_URL)
165     response = delete_request(url, ODL_LOGIN, ODL_PWD)
166     if wait_until_log_contains(tpce_log, re.escape("onDeviceDisConnected: "+node_id), 60):
167         print("Node "+node_id+" correctly deleted from tpce topology", end='... ', flush=True)
168     else:
169         print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
170     return response
171
172
173 def generate_link_data(xpdr_node: str, xpdr_num: str, network_num: str, rdm_node: str, srg_num: str,
174                        termination_num: str):
175     data = {
176         "networkutils:input": {
177             "networkutils:links-input": {
178                 "networkutils:xpdr-node": xpdr_node,
179                 "networkutils:xpdr-num": xpdr_num,
180                 "networkutils:network-num": network_num,
181                 "networkutils:rdm-node": rdm_node,
182                 "networkutils:srg-num": srg_num,
183                 "networkutils:termination-point-num": termination_num
184             }
185         }
186     }
187     return data
188
189
190 def shutdown_process(process):
191     if process is not None:
192         for child in psutil.Process(process.pid).children():
193             child.send_signal(signal.SIGINT)
194             child.wait()
195         process.send_signal(signal.SIGINT)
196
197
198 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
199     if os.path.isfile(honeynode_executable):
200         with open(log_file, 'w') as outfile:
201             return subprocess.Popen(
202                 [honeynode_executable, node_port, os.path.join(samples_directory, node_config_file_name)],
203                 stdout=outfile, stderr=outfile)
204
205
206 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
207     found = False
208     tail = None
209     try:
210         with timeout(seconds=time_to_wait):
211             print("Searching for pattern '"+regexp+"' in "+os.path.basename(log_file), end='... ', flush=True)
212             tail = subprocess.Popen(['tail', '-F', log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
213             compiled_regexp = re.compile(regexp)
214             while True:
215                 line = tail.stdout.readline().decode('utf-8')
216                 if compiled_regexp.search(line):
217                     print("String found!", end=' ')
218                     found = True
219                     break
220     except TimeoutError:
221         print("String not found after "+str(time_to_wait), end=" seconds! ", flush=True)
222     finally:
223         if tail is not None:
224             print("Stopping tail command", end='... ', flush=True)
225             tail.stderr.close()
226             tail.stdout.close()
227             tail.kill()
228             tail.wait()
229         return found
230 # TODO try to find an alternative to subprocess+tail -f (such as https://pypi.org/project/tailhead/)
231
232
233 class timeout:
234     def __init__(self, seconds=1, error_message='Timeout'):
235         self.seconds = seconds
236         self.error_message = error_message
237
238     def handle_timeout(self, signum, frame):
239         raise TimeoutError(self.error_message)
240
241     def __enter__(self):
242         signal.signal(signal.SIGALRM, self.handle_timeout)
243         signal.alarm(self.seconds)
244
245     def __exit__(self, type, value, traceback):
246         signal.alarm(0)