3 ##############################################################################
4 # Copyright (c) 2021 Orange, Inc. and others. All rights reserved.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # pylint: disable=no-member
16 # pylint: disable=wrong-import-order
26 # pylint: disable=import-error
29 SIMS = simulators.SIMS
31 HONEYNODE_OK_START_MSG = 'Netconf SSH endpoint started successfully at 0.0.0.0'
32 KARAF_OK_START_MSG = re.escape(
33 "Blueprint container for bundle org.opendaylight.netconf.restconf")+".* was successfully created"
34 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
41 TYPE_APPLICATION_JSON = {'Content-Type': 'application/json', 'Accept': 'application/json'}
42 TYPE_APPLICATION_XML = {'Content-Type': 'application/xml', 'Accept': 'application/xml'}
44 CODE_SHOULD_BE_200 = 'Http status code should be 200'
45 CODE_SHOULD_BE_201 = 'Http status code should be 201'
47 SIM_LOG_DIRECTORY = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'log')
51 if 'USE_ODL_ALT_RESTCONF_PORT' in os.environ:
52 RESTCONF_PORT = os.environ['USE_ODL_ALT_RESTCONF_PORT']
56 RESTCONF_PATH_PREFIX = {'rfc8040': '/rests',
57 'draft-bierman02': '/restconf'}
58 if 'USE_ODL_RESTCONF_VERSION' in os.environ:
59 RESTCONF_VERSION = os.environ['USE_ODL_RESTCONF_VERSION']
60 if RESTCONF_VERSION not in RESTCONF_PATH_PREFIX.keys():
61 print('unsupported RESTCONF version ' + RESTCONF_VERSION)
64 RESTCONF_VERSION = 'rfc8040'
66 RESTCONF_BASE_URL = 'http://localhost:' + RESTCONF_PORT + RESTCONF_PATH_PREFIX[RESTCONF_VERSION]
68 if 'USE_ODL_ALT_KARAF_INSTALL_DIR' in os.environ:
69 KARAF_INSTALLDIR = os.environ['USE_ODL_ALT_KARAF_INSTALL_DIR']
71 KARAF_INSTALLDIR = 'karaf'
73 KARAF_LOG = os.path.join(
74 os.path.dirname(os.path.realpath(__file__)),
75 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'data', 'log', 'karaf.log')
77 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
78 TPCE_LOG = 'odl-' + str(os.getpid()) + '.log'
83 # Basic HTTP operations
88 return requests.request(
89 'GET', url.format(RESTCONF_BASE_URL),
90 headers=TYPE_APPLICATION_JSON,
91 auth=(ODL_LOGIN, ODL_PWD))
94 def put_request(url, data):
95 return requests.request(
96 'PUT', url.format(RESTCONF_BASE_URL),
97 data=json.dumps(data),
98 headers=TYPE_APPLICATION_JSON,
99 auth=(ODL_LOGIN, ODL_PWD))
102 def delete_request(url):
103 return requests.request(
104 'DELETE', url.format(RESTCONF_BASE_URL),
105 headers=TYPE_APPLICATION_JSON,
106 auth=(ODL_LOGIN, ODL_PWD))
113 def start_sims(sims_list):
114 for sim in sims_list:
115 print('starting simulator ' + sim[0] + ' in OpenROADM device version ' + sim[1] + '...')
116 log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
117 process = start_honeynode(log_file, sim)
118 if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
119 print('simulator for ' + sim[0] + ' started')
121 print('simulator for ' + sim[0] + ' failed to start')
122 shutdown_process(process)
123 for pid in process_list:
124 shutdown_process(pid)
126 process_list.append(process)
131 print('starting OpenDaylight...')
132 if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
133 process = start_lighty()
134 start_msg = LIGHTY_OK_START_MSG
136 process = start_karaf()
137 start_msg = KARAF_OK_START_MSG
138 if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=300):
139 print('OpenDaylight started !')
141 print('OpenDaylight failed to start !')
142 shutdown_process(process)
143 for pid in process_list:
144 shutdown_process(pid)
146 process_list.append(process)
151 print('starting KARAF TransportPCE build...')
152 executable = os.path.join(
153 os.path.dirname(os.path.realpath(__file__)),
154 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'karaf')
155 with open('odl.log', 'w', encoding='utf-8') as outfile:
156 return subprocess.Popen(
157 ['sh', executable, 'server'], stdout=outfile, stderr=outfile, stdin=None)
161 print('starting LIGHTY.IO TransportPCE build...')
162 executable = os.path.join(
163 os.path.dirname(os.path.realpath(__file__)),
164 '..', '..', '..', 'lighty', 'target', 'tpce',
165 'clean-start-controller.sh')
166 with open(TPCE_LOG, 'w', encoding='utf-8') as outfile:
167 return subprocess.Popen(
168 ['sh', executable], stdout=outfile, stderr=outfile, stdin=None)
171 def install_karaf_feature(feature_name: str):
172 print('installing feature ' + feature_name)
173 executable = os.path.join(
174 os.path.dirname(os.path.realpath(__file__)),
175 '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'client')
176 return subprocess.run([executable],
177 input='feature:install ' + feature_name + '\n feature:list | grep '
178 + feature_name + ' \n logout \n',
179 universal_newlines=True, check=False)
182 def shutdown_process(process):
183 if process is not None:
184 for child in psutil.Process(process.pid).children():
185 child.send_signal(signal.SIGINT)
187 process.send_signal(signal.SIGINT)
190 def start_honeynode(log_file: str, sim):
191 executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
192 '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
193 sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
194 '..', '..', 'sample_configs', 'openroadm', sim[1])
195 if os.path.isfile(executable):
196 with open(log_file, 'w', encoding='utf-8') as outfile:
197 return subprocess.Popen(
198 [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
199 stdout=outfile, stderr=outfile)
203 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
204 # pylint: disable=lost-exception
205 # pylint: disable=consider-using-with
210 with TimeOut(seconds=time_to_wait):
211 while not os.path.exists(log_file):
213 filelogs = open(log_file, 'r', encoding='utf-8')
216 print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True)
217 compiled_regexp = re.compile(regexp)
219 line = filelogs.readline()
220 if compiled_regexp.search(line):
221 print('Pattern found!', end=' ')
227 print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True)
228 except PermissionError:
229 print('Permission Error when trying to access the log file', end=' ... ', flush=True)
234 print('log file does not exist or is not accessible... ', flush=True)
239 def __init__(self, seconds=1, error_message='Timeout'):
240 self.seconds = seconds
241 self.error_message = error_message
243 def handle_timeout(self, signum, frame):
244 raise TimeoutError(self.error_message)
247 signal.signal(signal.SIGALRM, self.handle_timeout)
248 signal.alarm(self.seconds)
250 def __exit__(self, type, value, traceback):
251 # pylint: disable=W0622
255 # Basic NetCONF device operations
259 def mount_device(node: str, sim: str):
260 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
261 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
264 'netconf-node-topology:username': NODES_LOGIN,
265 'netconf-node-topology:password': NODES_PWD,
266 'netconf-node-topology:host': '127.0.0.1',
267 'netconf-node-topology:port': SIMS[sim]['port'],
268 'netconf-node-topology:tcp-only': 'false',
269 'netconf-node-topology:pass-through': {}}]}
270 response = put_request(url[RESTCONF_VERSION].format('{}', node), body)
271 if wait_until_log_contains(TPCE_LOG, re.escape('Triggering notification stream NETCONF for node ' + node), 180):
272 print('Node ' + node + ' correctly added to tpce topology', end='... ', flush=True)
274 print('Node ' + node + ' still not added to tpce topology', end='... ', flush=True)
275 if response.status_code == requests.codes.ok:
276 print('It was probably loaded at start-up', end='... ', flush=True)
277 # TODO an else-clause to abort test would probably be nice here
281 def unmount_device(node: str):
282 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
283 'draft-bierman02': '{}/config/network-topology:network-topology/topology/topology-netconf/node/{}'}
284 response = delete_request(url[RESTCONF_VERSION].format('{}', node))
285 if wait_until_log_contains(TPCE_LOG, re.escape("onDeviceDisConnected: " + node), 180):
286 print('Node ' + node + ' correctly deleted from tpce topology', end='... ', flush=True)
288 print('Node ' + node + ' still not deleted from tpce topology', end='... ', flush=True)
292 def check_device_connection(node: str):
293 url = {'rfc8040': '{}/data/network-topology:network-topology/topology=topology-netconf/node={}',
294 'draft-bierman02': '{}/operational/network-topology:network-topology/topology/topology-netconf/node/{}'}
295 response = get_request(url[RESTCONF_VERSION].format('{}', node))
296 res = response.json()
297 return_key = {'rfc8040': 'network-topology:node',
298 'draft-bierman02': 'node'}
299 if return_key[RESTCONF_VERSION] in res.keys():
300 connection_status = res[return_key[RESTCONF_VERSION]][0]['netconf-node-topology:connection-status']
302 connection_status = res['errors']['error'][0]
303 return {'status_code': response.status_code,
304 'connection-status': connection_status}
307 # Portmapping operations
311 def get_portmapping(node: str):
312 url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}',
313 'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}'}
314 response = get_request(url[RESTCONF_VERSION].format('{}', node))
315 res = response.json()
316 return_key = {'rfc8040': 'transportpce-portmapping:nodes',
317 'draft-bierman02': 'nodes'}
318 nodes = res[return_key[RESTCONF_VERSION]]
319 return {'status_code': response.status_code,
323 def get_portmapping_node_info(node: str):
324 url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/node-info',
325 'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/node-info'}
326 response = get_request(url[RESTCONF_VERSION].format('{}', node))
327 res = response.json()
328 return_key = {'rfc8040': 'transportpce-portmapping:node-info',
329 'draft-bierman02': 'node-info'}
330 if return_key[RESTCONF_VERSION] in res.keys():
331 node_info = res[return_key[RESTCONF_VERSION]]
333 node_info = res['errors']['error'][0]
334 return {'status_code': response.status_code,
335 'node-info': node_info}
338 def portmapping_request(node: str, mapping: str):
339 url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/mapping={}',
340 'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/mapping/{}'}
341 response = get_request(url[RESTCONF_VERSION].format('{}', node, mapping))
342 res = response.json()
343 return_key = {'rfc8040': 'transportpce-portmapping:mapping',
344 'draft-bierman02': 'mapping'}
345 mapping = res[return_key[RESTCONF_VERSION]]
346 return {'status_code': response.status_code,
350 def portmapping_mc_capa_request(node: str, mc_capa: str):
351 url = {'rfc8040': '{}/data/transportpce-portmapping:network/nodes={}/mc-capabilities={}',
352 'draft-bierman02': '{}/config/transportpce-portmapping:network/nodes/{}/mc-capabilities/{}'}
353 response = get_request(url[RESTCONF_VERSION].format('{}', node, mc_capa))
354 res = response.json()
355 return_key = {'rfc8040': 'transportpce-portmapping:mc-capabilities',
356 'draft-bierman02': 'mc-capabilities'}
357 capabilities = res[return_key[RESTCONF_VERSION]]
358 return {'status_code': response.status_code,
359 'mc-capabilities': capabilities}
362 # Topology operations
366 def get_ietf_network_request(network: str, content: str):
367 url = {'rfc8040': '{}/data/ietf-network:networks/network={}?content={}',
368 'draft-bierman02': '{}/{}/ietf-network:networks/network/{}'}
369 if RESTCONF_VERSION == 'rfc8040':
370 format_args = ('{}', network, content)
371 elif content == 'config':
372 format_args = ('{}', content, network)
374 format_args = ('{}', 'operational', network)
375 response = get_request(url[RESTCONF_VERSION].format(*format_args))
376 res = response.json()
377 return_key = {'rfc8040': 'ietf-network:network',
378 'draft-bierman02': 'network'}
379 networks = res[return_key[RESTCONF_VERSION]]
380 return {'status_code': response.status_code,