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 ##############################################################################
23 SIMS = simulators.SIMS
24 HONEYNODE_EXECUTABLE = simulators.HONEYNODE_EXECUTABLE
25 SAMPLES_DIRECTORY = simulators.SAMPLES_DIRECTORY
27 HONEYNODE_OK_START_MSG = "Netconf SSH endpoint started successfully at 0.0.0.0"
28 KARAF_OK_START_MSG = re.escape(
29 "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
32 RESTCONF_BASE_URL = "http://localhost:8181/restconf"
38 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
39 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
41 CODE_SHOULD_BE_200 = 'Http status code should be 200'
42 CODE_SHOULD_BE_201 = 'Http status code should be 201'
44 LOG_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
46 KARAF_LOG = os.path.join(
47 os.path.dirname(os.path.realpath(__file__)),
48 "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
52 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
58 def start_sims(sims_list):
60 print("starting simulator for " + sim + "...")
61 log_file = os.path.join(LOG_DIRECTORY, SIMS[sim]['logfile'])
62 process = start_honeynode(log_file, SIMS[sim]['port'], SIMS[sim]['configfile'])
63 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
64 print("simulator for " + sim + " started")
66 print("simulator for " + sim + " failed to start")
67 shutdown_process(process)
68 for pid in process_list:
71 process_list.append(process)
76 print("starting OpenDaylight...")
77 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
78 process = start_lighty()
79 # TODO: add some sort of health check similar to Karaf below
81 process = start_karaf()
82 if wait_until_log_contains(KARAF_LOG, KARAF_OK_START_MSG, time_to_wait=60):
83 print("OpenDaylight started !")
85 print("OpenDaylight failed to start !")
86 shutdown_process(process)
87 for pid in process_list:
90 process_list.append(process)
95 print("starting KARAF TransportPCE build...")
96 executable = os.path.join(
97 os.path.dirname(os.path.realpath(__file__)),
98 "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
99 with open('odl.log', 'w') as outfile:
100 return subprocess.Popen(
101 ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
105 print("starting LIGHTY.IO TransportPCE build...")
106 executable = os.path.join(
107 os.path.dirname(os.path.realpath(__file__)),
108 "..", "..", "..", "lighty", "target", "tpce",
109 "clean-start-controller.sh")
110 with open('odl.log', 'w') as outfile:
111 return subprocess.Popen(
112 ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
115 def install_karaf_feature(feature_name: str):
116 print("installing feature " + feature_name)
117 executable = os.path.join(
118 os.path.dirname(os.path.realpath(__file__)),
119 "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
120 return subprocess.run([executable],
121 input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
122 universal_newlines=True)
125 def get_request(url):
126 return requests.request(
127 "GET", url.format(RESTCONF_BASE_URL),
128 headers=TYPE_APPLICATION_JSON,
129 auth=(ODL_LOGIN, ODL_PWD))
132 def post_request(url, data):
134 return requests.request(
135 "POST", url.format(RESTCONF_BASE_URL),
136 data=json.dumps(data),
137 headers=TYPE_APPLICATION_JSON,
138 auth=(ODL_LOGIN, ODL_PWD))
140 return requests.request(
141 "POST", url.format(RESTCONF_BASE_URL),
142 headers=TYPE_APPLICATION_JSON,
143 auth=(ODL_LOGIN, ODL_PWD))
146 def post_xmlrequest(url, data):
148 return requests.request(
149 "POST", url.format(RESTCONF_BASE_URL),
151 headers=TYPE_APPLICATION_XML,
152 auth=(ODL_LOGIN, ODL_PWD))
155 def put_request(url, data):
156 return requests.request(
157 "PUT", url.format(RESTCONF_BASE_URL),
158 data=json.dumps(data),
159 headers=TYPE_APPLICATION_JSON,
160 auth=(ODL_LOGIN, ODL_PWD))
163 def put_xmlrequest(url, data):
164 return requests.request(
165 "PUT", url.format(RESTCONF_BASE_URL),
167 headers=TYPE_APPLICATION_XML,
168 auth=(ODL_LOGIN, ODL_PWD))
171 def rawput_request(url, data):
172 return requests.request(
173 "PUT", url.format(RESTCONF_BASE_URL),
175 headers=TYPE_APPLICATION_JSON,
176 auth=(ODL_LOGIN, ODL_PWD))
179 def delete_request(url):
180 return requests.request(
181 "DELETE", url.format(RESTCONF_BASE_URL),
182 headers=TYPE_APPLICATION_JSON,
183 auth=(ODL_LOGIN, ODL_PWD))
186 def mount_device(node_id, sim):
187 url = "{}/config/network-topology:network-topology/topology/topology-netconf/node/"+node_id
190 "netconf-node-topology:username": NODES_LOGIN,
191 "netconf-node-topology:password": NODES_PWD,
192 "netconf-node-topology:host": "127.0.0.1",
193 "netconf-node-topology:port": SIMS[sim]['port'],
194 "netconf-node-topology:tcp-only": "false",
195 "netconf-node-topology:pass-through": {}}]}
196 response = put_request(url, body)
197 if wait_until_log_contains(TPCE_LOG, re.escape("Triggering notification stream NETCONF for node "+node_id), 60):
198 print("Node "+node_id+" correctly added to tpce topology", end='... ', flush=True)
200 print("Node "+node_id+" still not added to tpce topology", end='... ', flush=True)
201 if response.status_code == requests.codes.ok:
202 print("It was probably loaded at start-up", end='... ', flush=True)
203 # TODO an else-clause to abort test would probably be nice here
207 def unmount_device(node_id):
208 url = "{}/config/network-topology:network-topology/topology/topology-netconf/node/"+node_id
209 response = delete_request(url)
210 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: "+node_id), 60):
211 print("Node "+node_id+" correctly deleted from tpce topology", end='... ', flush=True)
213 print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
217 def connect_xpdr_to_rdm_request(xpdr_node: str, xpdr_num: str, network_num: str,
218 rdm_node: str, srg_num: str, termination_num: str):
219 url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
221 "networkutils:input": {
222 "networkutils:links-input": {
223 "networkutils:xpdr-node": xpdr_node,
224 "networkutils:xpdr-num": xpdr_num,
225 "networkutils:network-num": network_num,
226 "networkutils:rdm-node": rdm_node,
227 "networkutils:srg-num": srg_num,
228 "networkutils:termination-point-num": termination_num
232 return post_request(url, data)
235 def connect_rdm_to_xpdr_request(xpdr_node: str, xpdr_num: str, network_num: str,
236 rdm_node: str, srg_num: str, termination_num: str):
237 url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
239 "networkutils:input": {
240 "networkutils:links-input": {
241 "networkutils:xpdr-node": xpdr_node,
242 "networkutils:xpdr-num": xpdr_num,
243 "networkutils:network-num": network_num,
244 "networkutils:rdm-node": rdm_node,
245 "networkutils:srg-num": srg_num,
246 "networkutils:termination-point-num": termination_num
250 return post_request(url, data)
253 def shutdown_process(process):
254 if process is not None:
255 for child in psutil.Process(process.pid).children():
256 child.send_signal(signal.SIGINT)
258 process.send_signal(signal.SIGINT)
261 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
262 if os.path.isfile(HONEYNODE_EXECUTABLE):
263 with open(log_file, 'w') as outfile:
264 return subprocess.Popen(
265 [HONEYNODE_EXECUTABLE, node_port, os.path.join(SAMPLES_DIRECTORY, node_config_file_name)],
266 stdout=outfile, stderr=outfile)
269 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
274 with TimeOut(seconds=time_to_wait):
275 while not os.path.exists(log_file):
277 filelogs = open(log_file, 'r')
280 print("Searching for pattern '"+regexp+"' in "+os.path.basename(log_file), end='... ', flush=True)
281 compiled_regexp = re.compile(regexp)
283 line = filelogs.readline()
284 if compiled_regexp.search(line):
285 print("Pattern found!", end=' ')
291 print("Pattern not found after "+str(time_to_wait), end=" seconds! ", flush=True)
292 except PermissionError:
293 print("Permission Error when trying to access the log file", end=" ... ", flush=True)
298 print("log file does not exist or is not accessible... ", flush=True)
303 def __init__(self, seconds=1, error_message='Timeout'):
304 self.seconds = seconds
305 self.error_message = error_message
307 def handle_timeout(self, signum, frame):
308 raise TimeoutError(self.error_message)
311 signal.signal(signal.SIGALRM, self.handle_timeout)
312 signal.alarm(self.seconds)
314 def __exit__(self, type, value, traceback):