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