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"
37 URL_CONFIG_NETCONF_TOPO = "{}/config/network-topology:network-topology/topology/topology-netconf/"
38 URL_CONFIG_ORDM_TOPO = "{}/config/ietf-network:networks/network/openroadm-topology/"
39 URL_CONFIG_OTN_TOPO = "{}/config/ietf-network:networks/network/otn-topology/"
40 URL_CONFIG_CLLI_NET = "{}/config/ietf-network:networks/network/clli-network/"
41 URL_CONFIG_ORDM_NET = "{}/config/ietf-network:networks/network/openroadm-network/"
42 URL_PORTMAPPING = "{}/config/transportpce-portmapping:network/nodes/"
44 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
45 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
47 CODE_SHOULD_BE_200 = 'Http status code should be 200'
48 CODE_SHOULD_BE_201 = 'Http status code should be 201'
50 LOG_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
52 KARAF_LOG = os.path.join(
53 os.path.dirname(os.path.realpath(__file__)),
54 "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
58 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
64 def start_sims(sims_list):
66 print("starting simulator for " + sim + "...")
67 log_file = os.path.join(LOG_DIRECTORY, SIMS[sim]['logfile'])
68 process = start_honeynode(log_file, SIMS[sim]['port'], SIMS[sim]['configfile'])
69 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
70 print("simulator for " + sim + " started")
72 print("simulator for " + sim + " failed to start")
73 shutdown_process(process)
74 for pid in process_list:
77 process_list.append(process)
82 print("starting OpenDaylight...")
83 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
84 process = start_lighty()
85 # TODO: add some sort of health check similar to Karaf below
87 process = start_karaf()
88 if wait_until_log_contains(KARAF_LOG, KARAF_OK_START_MSG, time_to_wait=60):
89 print("OpenDaylight started !")
91 print("OpenDaylight failed to start !")
92 shutdown_process(process)
93 for pid in process_list:
96 process_list.append(process)
101 print("starting KARAF TransportPCE build...")
102 executable = os.path.join(
103 os.path.dirname(os.path.realpath(__file__)),
104 "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
105 with open('odl.log', 'w') as outfile:
106 return subprocess.Popen(
107 ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
111 print("starting LIGHTY.IO TransportPCE build...")
112 executable = os.path.join(
113 os.path.dirname(os.path.realpath(__file__)),
114 "..", "..", "..", "lighty", "target", "tpce",
115 "clean-start-controller.sh")
116 with open('odl.log', 'w') as outfile:
117 return subprocess.Popen(
118 ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
121 def install_karaf_feature(feature_name: str):
122 print("installing feature " + feature_name)
123 executable = os.path.join(
124 os.path.dirname(os.path.realpath(__file__)),
125 "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
126 return subprocess.run([executable],
127 input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
128 universal_newlines=True)
131 def get_request(url):
132 return requests.request(
133 "GET", url.format(RESTCONF_BASE_URL),
134 headers=TYPE_APPLICATION_JSON,
135 auth=(ODL_LOGIN, ODL_PWD))
138 def post_request(url, data):
140 return requests.request(
141 "POST", url.format(RESTCONF_BASE_URL),
142 data=json.dumps(data),
143 headers=TYPE_APPLICATION_JSON,
144 auth=(ODL_LOGIN, ODL_PWD))
146 return requests.request(
147 "POST", url.format(RESTCONF_BASE_URL),
148 headers=TYPE_APPLICATION_JSON,
149 auth=(ODL_LOGIN, ODL_PWD))
152 def post_xmlrequest(url, data):
154 return requests.request(
155 "POST", url.format(RESTCONF_BASE_URL),
157 headers=TYPE_APPLICATION_XML,
158 auth=(ODL_LOGIN, ODL_PWD))
161 def put_request(url, data):
162 return requests.request(
163 "PUT", url.format(RESTCONF_BASE_URL),
164 data=json.dumps(data),
165 headers=TYPE_APPLICATION_JSON,
166 auth=(ODL_LOGIN, ODL_PWD))
169 def put_xmlrequest(url, data):
170 return requests.request(
171 "PUT", url.format(RESTCONF_BASE_URL),
173 headers=TYPE_APPLICATION_XML,
174 auth=(ODL_LOGIN, ODL_PWD))
177 def rawput_request(url, data):
178 return requests.request(
179 "PUT", url.format(RESTCONF_BASE_URL),
181 headers=TYPE_APPLICATION_JSON,
182 auth=(ODL_LOGIN, ODL_PWD))
185 def delete_request(url):
186 return requests.request(
187 "DELETE", url.format(RESTCONF_BASE_URL),
188 headers=TYPE_APPLICATION_JSON,
189 auth=(ODL_LOGIN, ODL_PWD))
192 def mount_device(node_id, sim):
193 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
196 "netconf-node-topology:username": NODES_LOGIN,
197 "netconf-node-topology:password": NODES_PWD,
198 "netconf-node-topology:host": "127.0.0.1",
199 "netconf-node-topology:port": SIMS[sim]['port'],
200 "netconf-node-topology:tcp-only": "false",
201 "netconf-node-topology:pass-through": {}}]}
202 response = put_request(url, body)
203 if wait_until_log_contains(TPCE_LOG, re.escape("Triggering notification stream NETCONF for node "+node_id), 60):
204 print("Node "+node_id+" correctly added to tpce topology", end='... ', flush=True)
206 print("Node "+node_id+" still not added to tpce topology", end='... ', flush=True)
207 if response.status_code == requests.codes.ok:
208 print("It was probably loaded at start-up", end='... ', flush=True)
209 # TODO an else-clause to abort test would probably be nice here
213 def unmount_device(node_id):
214 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
215 response = delete_request(url)
216 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: "+node_id), 60):
217 print("Node "+node_id+" correctly deleted from tpce topology", end='... ', flush=True)
219 print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
223 def connect_xpdr_to_rdm_request(xpdr_node: str, xpdr_num: str, network_num: str,
224 rdm_node: str, srg_num: str, termination_num: str):
225 url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
227 "networkutils:input": {
228 "networkutils:links-input": {
229 "networkutils:xpdr-node": xpdr_node,
230 "networkutils:xpdr-num": xpdr_num,
231 "networkutils:network-num": network_num,
232 "networkutils:rdm-node": rdm_node,
233 "networkutils:srg-num": srg_num,
234 "networkutils:termination-point-num": termination_num
238 return post_request(url, data)
241 def connect_rdm_to_xpdr_request(xpdr_node: str, xpdr_num: str, network_num: str,
242 rdm_node: str, srg_num: str, termination_num: str):
243 url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
245 "networkutils:input": {
246 "networkutils:links-input": {
247 "networkutils:xpdr-node": xpdr_node,
248 "networkutils:xpdr-num": xpdr_num,
249 "networkutils:network-num": network_num,
250 "networkutils:rdm-node": rdm_node,
251 "networkutils:srg-num": srg_num,
252 "networkutils:termination-point-num": termination_num
256 return post_request(url, data)
259 def check_netconf_node_request(node: str, suffix: str):
260 url = URL_CONFIG_NETCONF_TOPO + (
261 "node/" + node + "/yang-ext:mount/org-openroadm-device:org-openroadm-device/" + suffix
263 return get_request(url)
266 def get_netconf_oper_request(node: str):
267 url = "{}/operational/network-topology:network-topology/topology/topology-netconf/node/" + node
268 return get_request(url)
271 def get_ordm_topo_request(suffix: str):
272 url = URL_CONFIG_ORDM_TOPO + suffix
273 return get_request(url)
276 def add_oms_attr_request(link: str, attr):
277 url = URL_CONFIG_ORDM_TOPO + (
278 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
280 return put_request(url, attr)
283 def del_oms_attr_request(link: str):
284 url = URL_CONFIG_ORDM_TOPO + (
285 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
287 return delete_request(url)
290 def get_clli_net_request():
291 return get_request(URL_CONFIG_CLLI_NET)
294 def get_ordm_net_request():
295 return get_request(URL_CONFIG_ORDM_NET)
298 def get_otn_topo_request():
299 return get_request(URL_CONFIG_OTN_TOPO)
302 def del_link_request(link: str):
303 url = URL_CONFIG_ORDM_TOPO + ("ietf-network-topology:link/" + link)
304 return delete_request(url)
307 def del_node_request(node: str):
308 url = URL_CONFIG_CLLI_NET + ("node/" + node)
309 return delete_request(url)
312 def portmapping_request(suffix: str):
313 url = URL_PORTMAPPING + suffix
314 return get_request(url)
317 def shutdown_process(process):
318 if process is not None:
319 for child in psutil.Process(process.pid).children():
320 child.send_signal(signal.SIGINT)
322 process.send_signal(signal.SIGINT)
325 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
326 if os.path.isfile(HONEYNODE_EXECUTABLE):
327 with open(log_file, 'w') as outfile:
328 return subprocess.Popen(
329 [HONEYNODE_EXECUTABLE, node_port, os.path.join(SAMPLES_DIRECTORY, node_config_file_name)],
330 stdout=outfile, stderr=outfile)
333 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
338 with TimeOut(seconds=time_to_wait):
339 while not os.path.exists(log_file):
341 filelogs = open(log_file, 'r')
344 print("Searching for pattern '"+regexp+"' in "+os.path.basename(log_file), end='... ', flush=True)
345 compiled_regexp = re.compile(regexp)
347 line = filelogs.readline()
348 if compiled_regexp.search(line):
349 print("Pattern found!", end=' ')
355 print("Pattern not found after "+str(time_to_wait), end=" seconds! ", flush=True)
356 except PermissionError:
357 print("Permission Error when trying to access the log file", end=" ... ", flush=True)
362 print("log file does not exist or is not accessible... ", flush=True)
367 def __init__(self, seconds=1, error_message='Timeout'):
368 self.seconds = seconds
369 self.error_message = error_message
371 def handle_timeout(self, signum, frame):
372 raise TimeoutError(self.error_message)
375 signal.signal(signal.SIGALRM, self.handle_timeout)
376 signal.alarm(self.seconds)
378 def __exit__(self, type, value, traceback):