2 ##############################################################################
3 # Copyright (c) 2020 Orange, Inc. and others. All rights reserved.
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 ##############################################################################
21 sims = simulators.sims
22 honeynode_executable = simulators.honeynode_executable
23 samples_directory = simulators.samples_directory
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"
30 RESTCONF_BASE_URL = "http://localhost:8181/restconf"
36 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
37 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
39 CODE_SHOULD_BE_200 = 'Http status code should be 200'
40 CODE_SHOULD_BE_201 = 'Http status code should be 201'
42 log_directory = os.path.dirname(os.path.realpath(__file__))
44 karaf_log = os.path.join(
45 os.path.dirname(os.path.realpath(__file__)),
46 "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
50 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
55 def start_sims(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")
63 print("simulator for " + sim + " failed to start")
64 shutdown_process(process)
65 for pid in process_list:
68 process_list.append(process)
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
78 process = start_karaf()
79 if wait_until_log_contains(karaf_log, KARAF_OK_START_MSG, time_to_wait=60):
80 print("OpenDaylight started !")
82 print("OpenDaylight failed to start !")
83 shutdown_process(process)
84 for pid in process_list:
87 process_list.append(process)
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)
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)
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)
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))
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))
134 def delete_request(url, username, password):
135 return requests.request(
136 "DELETE", url, headers=TYPE_APPLICATION_JSON,
137 auth=(username, password))
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": [{
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)
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
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)
169 print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
173 def generate_link_data(xpdr_node: str, xpdr_num: str, network_num: str, rdm_node: str, srg_num: str,
174 termination_num: str):
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
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)
195 process.send_signal(signal.SIGINT)
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)
206 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
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)
215 line = tail.stdout.readline().decode('utf-8')
216 if compiled_regexp.search(line):
217 print("String found!", end=' ')
221 print("String not found after "+str(time_to_wait), end=" seconds! ", flush=True)
224 print("Stopping tail command", end='... ', flush=True)
230 # TODO try to find an alternative to subprocess+tail -f (such as https://pypi.org/project/tailhead/)
234 def __init__(self, seconds=1, error_message='Timeout'):
235 self.seconds = seconds
236 self.error_message = error_message
238 def handle_timeout(self, signum, frame):
239 raise TimeoutError(self.error_message)
242 signal.signal(signal.SIGALRM, self.handle_timeout)
243 signal.alarm(self.seconds)
245 def __exit__(self, type, value, traceback):