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_CLLI_NET = "{}/config/ietf-network:networks/network/clli-network/"
40 URL_CONFIG_ORDM_NET = "{}/config/ietf-network:networks/network/openroadm-network/"
42 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
43 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
45 CODE_SHOULD_BE_200 = 'Http status code should be 200'
46 CODE_SHOULD_BE_201 = 'Http status code should be 201'
48 LOG_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
50 KARAF_LOG = os.path.join(
51 os.path.dirname(os.path.realpath(__file__)),
52 "..", "..", "..", "karaf", "target", "assembly", "data", "log", "karaf.log")
56 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
62 def start_sims(sims_list):
64 print("starting simulator for " + sim + "...")
65 log_file = os.path.join(LOG_DIRECTORY, SIMS[sim]['logfile'])
66 process = start_honeynode(log_file, SIMS[sim]['port'], SIMS[sim]['configfile'])
67 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
68 print("simulator for " + sim + " started")
70 print("simulator for " + sim + " failed to start")
71 shutdown_process(process)
72 for pid in process_list:
75 process_list.append(process)
80 print("starting OpenDaylight...")
81 if "USE_LIGHTY" in os.environ and os.environ['USE_LIGHTY'] == 'True':
82 process = start_lighty()
83 # TODO: add some sort of health check similar to Karaf below
85 process = start_karaf()
86 if wait_until_log_contains(KARAF_LOG, KARAF_OK_START_MSG, time_to_wait=60):
87 print("OpenDaylight started !")
89 print("OpenDaylight failed to start !")
90 shutdown_process(process)
91 for pid in process_list:
94 process_list.append(process)
99 print("starting KARAF TransportPCE build...")
100 executable = os.path.join(
101 os.path.dirname(os.path.realpath(__file__)),
102 "..", "..", "..", "karaf", "target", "assembly", "bin", "karaf")
103 with open('odl.log', 'w') as outfile:
104 return subprocess.Popen(
105 ["sh", executable, "server"], stdout=outfile, stderr=outfile, stdin=None)
109 print("starting LIGHTY.IO TransportPCE build...")
110 executable = os.path.join(
111 os.path.dirname(os.path.realpath(__file__)),
112 "..", "..", "..", "lighty", "target", "tpce",
113 "clean-start-controller.sh")
114 with open('odl.log', 'w') as outfile:
115 return subprocess.Popen(
116 ["sh", executable], stdout=outfile, stderr=outfile, stdin=None)
119 def install_karaf_feature(feature_name: str):
120 print("installing feature " + feature_name)
121 executable = os.path.join(
122 os.path.dirname(os.path.realpath(__file__)),
123 "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
124 return subprocess.run([executable],
125 input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
126 universal_newlines=True)
129 def get_request(url):
130 return requests.request(
131 "GET", url.format(RESTCONF_BASE_URL),
132 headers=TYPE_APPLICATION_JSON,
133 auth=(ODL_LOGIN, ODL_PWD))
136 def post_request(url, data):
138 return requests.request(
139 "POST", url.format(RESTCONF_BASE_URL),
140 data=json.dumps(data),
141 headers=TYPE_APPLICATION_JSON,
142 auth=(ODL_LOGIN, ODL_PWD))
144 return requests.request(
145 "POST", url.format(RESTCONF_BASE_URL),
146 headers=TYPE_APPLICATION_JSON,
147 auth=(ODL_LOGIN, ODL_PWD))
150 def post_xmlrequest(url, data):
152 return requests.request(
153 "POST", url.format(RESTCONF_BASE_URL),
155 headers=TYPE_APPLICATION_XML,
156 auth=(ODL_LOGIN, ODL_PWD))
159 def put_request(url, data):
160 return requests.request(
161 "PUT", url.format(RESTCONF_BASE_URL),
162 data=json.dumps(data),
163 headers=TYPE_APPLICATION_JSON,
164 auth=(ODL_LOGIN, ODL_PWD))
167 def put_xmlrequest(url, data):
168 return requests.request(
169 "PUT", url.format(RESTCONF_BASE_URL),
171 headers=TYPE_APPLICATION_XML,
172 auth=(ODL_LOGIN, ODL_PWD))
175 def rawput_request(url, data):
176 return requests.request(
177 "PUT", url.format(RESTCONF_BASE_URL),
179 headers=TYPE_APPLICATION_JSON,
180 auth=(ODL_LOGIN, ODL_PWD))
183 def delete_request(url):
184 return requests.request(
185 "DELETE", url.format(RESTCONF_BASE_URL),
186 headers=TYPE_APPLICATION_JSON,
187 auth=(ODL_LOGIN, ODL_PWD))
190 def mount_device(node_id, sim):
191 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
194 "netconf-node-topology:username": NODES_LOGIN,
195 "netconf-node-topology:password": NODES_PWD,
196 "netconf-node-topology:host": "127.0.0.1",
197 "netconf-node-topology:port": SIMS[sim]['port'],
198 "netconf-node-topology:tcp-only": "false",
199 "netconf-node-topology:pass-through": {}}]}
200 response = put_request(url, body)
201 if wait_until_log_contains(TPCE_LOG, re.escape("Triggering notification stream NETCONF for node "+node_id), 60):
202 print("Node "+node_id+" correctly added to tpce topology", end='... ', flush=True)
204 print("Node "+node_id+" still not added to tpce topology", end='... ', flush=True)
205 if response.status_code == requests.codes.ok:
206 print("It was probably loaded at start-up", end='... ', flush=True)
207 # TODO an else-clause to abort test would probably be nice here
211 def unmount_device(node_id):
212 url = URL_CONFIG_NETCONF_TOPO+"node/"+node_id
213 response = delete_request(url)
214 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: "+node_id), 60):
215 print("Node "+node_id+" correctly deleted from tpce topology", end='... ', flush=True)
217 print("Node "+node_id+" still not deleted from tpce topology", end='... ', flush=True)
221 def connect_xpdr_to_rdm_request(xpdr_node: str, xpdr_num: str, network_num: str,
222 rdm_node: str, srg_num: str, termination_num: str):
223 url = "{}/operations/transportpce-networkutils:init-xpdr-rdm-links"
225 "networkutils:input": {
226 "networkutils:links-input": {
227 "networkutils:xpdr-node": xpdr_node,
228 "networkutils:xpdr-num": xpdr_num,
229 "networkutils:network-num": network_num,
230 "networkutils:rdm-node": rdm_node,
231 "networkutils:srg-num": srg_num,
232 "networkutils:termination-point-num": termination_num
236 return post_request(url, data)
239 def connect_rdm_to_xpdr_request(xpdr_node: str, xpdr_num: str, network_num: str,
240 rdm_node: str, srg_num: str, termination_num: str):
241 url = "{}/operations/transportpce-networkutils:init-rdm-xpdr-links"
243 "networkutils:input": {
244 "networkutils:links-input": {
245 "networkutils:xpdr-node": xpdr_node,
246 "networkutils:xpdr-num": xpdr_num,
247 "networkutils:network-num": network_num,
248 "networkutils:rdm-node": rdm_node,
249 "networkutils:srg-num": srg_num,
250 "networkutils:termination-point-num": termination_num
254 return post_request(url, data)
257 def check_netconf_node_request(node: str, suffix: str):
258 url = URL_CONFIG_NETCONF_TOPO + (
259 "node/" + node + "/yang-ext:mount/org-openroadm-device:org-openroadm-device/" + suffix
261 return get_request(url)
264 def get_netconf_oper_request(node: str):
265 url = "{}/operational/network-topology:network-topology/topology/topology-netconf/node/" + node
266 return get_request(url)
269 def get_ordm_topo_request(suffix: str):
270 url = URL_CONFIG_ORDM_TOPO + suffix
271 return get_request(url)
274 def add_oms_attr_request(link: str, attr):
275 url = URL_CONFIG_ORDM_TOPO + (
276 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
278 return put_request(url, attr)
281 def del_oms_attr_request(link: str):
282 url = URL_CONFIG_ORDM_TOPO + (
283 "ietf-network-topology:link/" + link + "/org-openroadm-network-topology:OMS-attributes/span"
285 return delete_request(url)
287 def get_clli_net_request():
288 return get_request(URL_CONFIG_CLLI_NET)
290 def get_ordm_net_request():
291 return get_request(URL_CONFIG_ORDM_NET)
293 def del_link_request(link: str):
294 url = URL_CONFIG_ORDM_TOPO + ("ietf-network-topology:link/" + link)
295 return delete_request(url)
297 def del_node_request(node: str):
298 url = URL_CONFIG_CLLI_NET + ("node/" + node)
299 return delete_request(url)
301 def shutdown_process(process):
302 if process is not None:
303 for child in psutil.Process(process.pid).children():
304 child.send_signal(signal.SIGINT)
306 process.send_signal(signal.SIGINT)
309 def start_honeynode(log_file: str, node_port: str, node_config_file_name: str):
310 if os.path.isfile(HONEYNODE_EXECUTABLE):
311 with open(log_file, 'w') as outfile:
312 return subprocess.Popen(
313 [HONEYNODE_EXECUTABLE, node_port, os.path.join(SAMPLES_DIRECTORY, node_config_file_name)],
314 stdout=outfile, stderr=outfile)
317 def wait_until_log_contains(log_file, regexp, time_to_wait=20):
322 with TimeOut(seconds=time_to_wait):
323 while not os.path.exists(log_file):
325 filelogs = open(log_file, 'r')
328 print("Searching for pattern '"+regexp+"' in "+os.path.basename(log_file), end='... ', flush=True)
329 compiled_regexp = re.compile(regexp)
331 line = filelogs.readline()
332 if compiled_regexp.search(line):
333 print("Pattern found!", end=' ')
339 print("Pattern not found after "+str(time_to_wait), end=" seconds! ", flush=True)
340 except PermissionError:
341 print("Permission Error when trying to access the log file", end=" ... ", flush=True)
346 print("log file does not exist or is not accessible... ", flush=True)
351 def __init__(self, seconds=1, error_message='Timeout'):
352 self.seconds = seconds
353 self.error_message = error_message
355 def handle_timeout(self, signum, frame):
356 raise TimeoutError(self.error_message)
359 signal.signal(signal.SIGALRM, self.handle_timeout)
360 signal.alarm(self.seconds)
362 def __exit__(self, type, value, traceback):