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/"
43 URL_OPER_SERV_LIST = "{}/operational/org-openroadm-service:service-list/"
44 URL_SERV_CREATE = "{}/operations/org-openroadm-service:service-create"
46 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
47 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
49 CODE_SHOULD_BE_200 = 'Http status code should be 200'
50 CODE_SHOULD_BE_201 = 'Http status code should be 201'
52 LOG_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
54 KARAF_LOG = os.path.join(
55 os.path.dirname(os.path.realpath(__file__)),
56 "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
60 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
66 def start_sims(sims_list):
68 print("starting simulator for " + sim + "...")
69 log_file = os.path.join(LOG_DIRECTORY, SIMS[sim]['logfile'])
70 process = start_honeynode(log_file, SIMS[sim]['port'], SIMS[sim]['configfile'])
71 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
72 print("simulator for " + sim + " started")
74 print("simulator for " + sim + " failed to start")
75 shutdown_process(process)
76 for pid in process_list:
79 process_list.append(process)
84 print("starting OpenDaylight...")
85 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
86 process = start_lighty()
87 # TODO: add some sort of health check similar to Karaf below
89 process = start_karaf()
90 if wait_until_log_contains(KARAF_LOG, KARAF_OK_START_MSG, time_to_wait=60):
91 print("OpenDaylight started !")
93 print("OpenDaylight failed to start !")
94 shutdown_process(process)
95 for pid in process_list:
98 process_list.append(process)
103 print("starting KARAF TransportPCE build...")
104 executable = os.path.join(
105 os.path.dirname(os.path.realpath(__file__)),
106 "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
107 with open('odl.log', 'w') as outfile:
108 return subprocess.Popen(
109 ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
113 print("starting LIGHTY.IO TransportPCE build...")
114 executable = os.path.join(
115 os.path.dirname(os.path.realpath(__file__)),
116 "..", "..", "..", "lighty", "target", "tpce",
117 "clean-start-controller.sh")
118 with open('odl.log', 'w') as outfile:
119 return subprocess.Popen(
120 ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
123 def install_karaf_feature(feature_name: str):
124 print("installing feature " + feature_name)
125 executable = os.path.join(
126 os.path.dirname(os.path.realpath(__file__)),
127 "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
128 return subprocess.run([executable],
129 input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
130 universal_newlines=True)
133 def get_request(url):
134 return requests.request(
135 "GET", url.format(RESTCONF_BASE_URL),
136 headers=TYPE_APPLICATION_JSON,
137 auth=(ODL_LOGIN, ODL_PWD))
140 def post_request(url, data):
142 return requests.request(
143 "POST", url.format(RESTCONF_BASE_URL),
144 data=json.dumps(data),
145 headers=TYPE_APPLICATION_JSON,
146 auth=(ODL_LOGIN, ODL_PWD))
148 return requests.request(
149 "POST", url.format(RESTCONF_BASE_URL),
150 headers=TYPE_APPLICATION_JSON,
151 auth=(ODL_LOGIN, ODL_PWD))
154 def post_xmlrequest(url, data):
156 return requests.request(
157 "POST", url.format(RESTCONF_BASE_URL),
159 headers=TYPE_APPLICATION_XML,
160 auth=(ODL_LOGIN, ODL_PWD))
163 def put_request(url, data):
164 return requests.request(
165 "PUT", url.format(RESTCONF_BASE_URL),
166 data=json.dumps(data),
167 headers=TYPE_APPLICATION_JSON,
168 auth=(ODL_LOGIN, ODL_PWD))
171 def put_xmlrequest(url, data):
172 return requests.request(
173 "PUT", url.format(RESTCONF_BASE_URL),
175 headers=TYPE_APPLICATION_XML,
176 auth=(ODL_LOGIN, ODL_PWD))
179 def rawput_request(url, data):
180 return requests.request(
181 "PUT", url.format(RESTCONF_BASE_URL),
183 headers=TYPE_APPLICATION_JSON,
184 auth=(ODL_LOGIN, ODL_PWD))
187 def delete_request(url):
188 return requests.request(
189 "DELETE", url.format(RESTCONF_BASE_URL),
190 headers=TYPE_APPLICATION_JSON,
191 auth=(ODL_LOGIN, ODL_PWD))
194 def mount_device(node_id, sim):
195 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
198 "netconf-node-topology:username": NODES_LOGIN,
199 "netconf-node-topology:password": NODES_PWD,
200 "netconf-node-topology:host": "127.0.0.1",
201 "netconf-node-topology:port": SIMS[sim]['port'],
202 "netconf-node-topology:tcp-only": "false",
203 "netconf-node-topology:pass-through": {}}]}
204 response = put_request(url, body)
205 if wait_until_log_contains(TPCE_LOG, re.escape("Triggering notification stream NETCONF for node "+node_id), 60):
206 print("Node "+node_id+" correctly added to tpce topology", end='... ', flush=True)
208 print("Node "+node_id+" still not added to tpce topology", end='... ', flush=True)
209 if response.status_code == requests.codes.ok:
210 print("It was probably loaded at start-up", end='... ', flush=True)
211 # TODO an else-clause to abort test would probably be nice here
215 def unmount_device(node_id):
216 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
217 response = delete_request(url)
218 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: "+node_id), 60):
219 print("Node "+node_id+" correctly deleted from tpce topology", end='... ', flush=True)
221 print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
225 def connect_xpdr_to_rdm_request(xpdr_node: str, xpdr_num: str, network_num: str,
226 rdm_node: str, srg_num: str, termination_num: str):
227 url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
229 "networkutils:input": {
230 "networkutils:links-input": {
231 "networkutils:xpdr-node": xpdr_node,
232 "networkutils:xpdr-num": xpdr_num,
233 "networkutils:network-num": network_num,
234 "networkutils:rdm-node": rdm_node,
235 "networkutils:srg-num": srg_num,
236 "networkutils:termination-point-num": termination_num
240 return post_request(url, data)
243 def connect_rdm_to_xpdr_request(xpdr_node: str, xpdr_num: str, network_num: str,
244 rdm_node: str, srg_num: str, termination_num: str):
245 url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
247 "networkutils:input": {
248 "networkutils:links-input": {
249 "networkutils:xpdr-node": xpdr_node,
250 "networkutils:xpdr-num": xpdr_num,
251 "networkutils:network-num": network_num,
252 "networkutils:rdm-node": rdm_node,
253 "networkutils:srg-num": srg_num,
254 "networkutils:termination-point-num": termination_num
258 return post_request(url, data)
261 def check_netconf_node_request(node: str, suffix: str):
262 url = URL_CONFIG_NETCONF_TOPO + (
263 "node/" + node + "/yang-ext:mount/org-openroadm-device:org-openroadm-device/" + suffix
265 return get_request(url)
268 def get_netconf_oper_request(node: str):
269 url = "{}/operational/network-topology:network-topology/topology/topology-netconf/node/" + node
270 return get_request(url)
273 def get_ordm_topo_request(suffix: str):
274 url = URL_CONFIG_ORDM_TOPO + suffix
275 return get_request(url)
278 def add_oms_attr_request(link: str, attr):
279 url = URL_CONFIG_ORDM_TOPO + (
280 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
282 return put_request(url, attr)
285 def del_oms_attr_request(link: str):
286 url = URL_CONFIG_ORDM_TOPO + (
287 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
289 return delete_request(url)
292 def get_clli_net_request():
293 return get_request(URL_CONFIG_CLLI_NET)
296 def get_ordm_net_request():
297 return get_request(URL_CONFIG_ORDM_NET)
300 def get_otn_topo_request():
301 return get_request(URL_CONFIG_OTN_TOPO)
304 def del_link_request(link: str):
305 url = URL_CONFIG_ORDM_TOPO + ("ietf-network-topology:link/" + link)
306 return delete_request(url)
309 def del_node_request(node: str):
310 url = URL_CONFIG_CLLI_NET + ("node/" + node)
311 return delete_request(url)
314 def portmapping_request(suffix: str):
315 url = URL_PORTMAPPING + suffix
316 return get_request(url)
318 def get_service_list_request(suffix: str):
319 url = URL_OPER_SERV_LIST + suffix
320 return get_request(url)
322 def service_create_request(attr):
323 return post_request(URL_SERV_CREATE, attr)
326 def shutdown_process(process):
327 if process is not None:
328 for child in psutil.Process(process.pid).children():
329 child.send_signal(signal.SIGINT)
331 process.send_signal(signal.SIGINT)
334 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
335 if os.path.isfile(HONEYNODE_EXECUTABLE):
336 with open(log_file, 'w') as outfile:
337 return subprocess.Popen(
338 [HONEYNODE_EXECUTABLE, node_port, os.path.join(SAMPLES_DIRECTORY, node_config_file_name)],
339 stdout=outfile, stderr=outfile)
342 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
347 with TimeOut(seconds=time_to_wait):
348 while not os.path.exists(log_file):
350 filelogs = open(log_file, 'r')
353 print("Searching for pattern '"+regexp+"' in "+os.path.basename(log_file), end='... ', flush=True)
354 compiled_regexp = re.compile(regexp)
356 line = filelogs.readline()
357 if compiled_regexp.search(line):
358 print("Pattern found!", end=' ')
364 print("Pattern not found after "+str(time_to_wait), end=" seconds! ", flush=True)
365 except PermissionError:
366 print("Permission Error when trying to access the log file", end=" ... ", flush=True)
371 print("log file does not exist or is not accessible... ", flush=True)
376 def __init__(self, seconds=1, error_message='Timeout'):
377 self.seconds = seconds
378 self.error_message = error_message
380 def handle_timeout(self, signum, frame):
381 raise TimeoutError(self.error_message)
384 signal.signal(signal.SIGALRM, self.handle_timeout)
385 signal.alarm(self.seconds)
387 def __exit__(self, type, value, traceback):