Add lightynode support to testutils.py
[transportpce.git] / tests / transportpce_tests / common / test_utils.py
index 63ab18735479f8877df43371f7b41962324efde2..0ee48c2587e6f8a94b3efea998496dc29a539737 100644 (file)
@@ -29,7 +29,8 @@ import simulators
 SIMS = simulators.SIMS
 
 HONEYNODE_OK_START_MSG = 'Netconf SSH endpoint started successfully at 0.0.0.0'
-KARAF_OK_START_MSG = "Blueprint container for bundle org.opendaylight.netconf.restconf.* was successfully created"
+LIGHTYNODE_OK_START_MSG = 'Data tree change listeners registered'
+KARAF_OK_START_MSG = "Transportpce controller started"
 LIGHTY_OK_START_MSG = re.escape("lighty.io and RESTCONF-NETCONF started")
 
 ODL_LOGIN = 'admin'
@@ -59,6 +60,7 @@ else:
 
 RESTCONF_PATH_PREFIX = {'rfc8040': '/rests',
                         'draft-bierman02': '/restconf'}
+
 if 'USE_ODL_RESTCONF_VERSION' in os.environ:
     RESTCONF_VERSION = os.environ['USE_ODL_RESTCONF_VERSION']
     if RESTCONF_VERSION not in RESTCONF_PATH_PREFIX:
@@ -132,12 +134,53 @@ def post_request(url, data):
 #
 
 
+def start_honeynode(log_file: str, sim):
+    executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                              '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
+    sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                                    '..', '..', 'sample_configs', 'openroadm', sim[1])
+    if os.path.isfile(executable):
+        with open(log_file, 'w', encoding='utf-8') as outfile:
+            return subprocess.Popen(
+                [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
+                stdout=outfile, stderr=outfile)
+    return None
+
+
+def start_lightynode(log_file: str, sim):
+    executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                              '..', '..', 'lightynode', 'lightynode-openroadm-device', 'start-device.sh')
+    sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                                    '..', '..', 'sample_configs', 'openroadm', sim[1])
+    if os.path.isfile(executable):
+        with open(log_file, 'w', encoding='utf-8') as outfile:
+            return subprocess.Popen(
+                [executable, "-v" + sim[1], "-p" + SIMS[sim]['port'], "-f" + os.path.join(sample_directory,
+                                                                                          SIMS[sim]['configfile'])],
+                stdout=outfile, stderr=outfile)
+    return None
+
+
 def start_sims(sims_list):
+    if 'USE_SIMS' in os.environ:
+        sims_to_use = os.environ['USE_SIMS']
+        print(os.environ['USE_SIMS'] + ' ' + sims_to_use)
+    else:
+        sims_to_use = 'honeynode'
+    if sims_to_use == 'None':
+        return None
+    if sims_to_use == 'lightynode':
+        start_msg = LIGHTYNODE_OK_START_MSG
+        start_method = start_lightynode
+        print("lightynode used")
+    else:
+        start_msg = HONEYNODE_OK_START_MSG
+        start_method = start_honeynode
     for sim in sims_list:
         print('starting simulator ' + sim[0] + ' in OpenROADM device version ' + sim[1] + '...')
         log_file = os.path.join(SIM_LOG_DIRECTORY, SIMS[sim]['logfile'])
-        process = start_honeynode(log_file, sim)
-        if wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 100):
+        process = start_method(log_file, sim)
+        if wait_until_log_contains(log_file, start_msg, 100):
             print('simulator for ' + sim[0] + ' started')
         else:
             print('simulator for ' + sim[0] + ' failed to start')
@@ -150,6 +193,9 @@ def start_sims(sims_list):
 
 
 def start_tpce():
+    if 'NO_ODL_STARTUP' in os.environ:
+        print('No OpenDaylight instance to start!')
+        return []
     print('starting OpenDaylight...')
     if 'USE_LIGHTY' in os.environ and os.environ['USE_LIGHTY'] == 'True':
         process = start_lighty()
@@ -157,7 +203,7 @@ def start_tpce():
     else:
         process = start_karaf()
         start_msg = KARAF_OK_START_MSG
-    if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=300):
+    if wait_until_log_contains(TPCE_LOG, start_msg, time_to_wait=100):
         print('OpenDaylight started !')
     else:
         print('OpenDaylight failed to start !')
@@ -195,7 +241,9 @@ def install_karaf_feature(feature_name: str):
     executable = os.path.join(
         os.path.dirname(os.path.realpath(__file__)),
         '..', '..', '..', KARAF_INSTALLDIR, 'target', 'assembly', 'bin', 'client')
-    return subprocess.run([executable],
+# FIXME: https://jira.opendaylight.org/browse/TRNSPRTPCE-701
+# -b option needed below because of Karaf client bug reporte in the JIRA ticket mentioned above
+    return subprocess.run([executable, '-b'],
                           input='feature:install ' + feature_name + '\n feature:list | grep '
                           + feature_name + ' \n logout \n',
                           universal_newlines=True, check=False)
@@ -209,19 +257,6 @@ def shutdown_process(process):
         process.send_signal(signal.SIGINT)
 
 
-def start_honeynode(log_file: str, sim):
-    executable = os.path.join(os.path.dirname(os.path.realpath(__file__)),
-                              '..', '..', 'honeynode', sim[1], 'honeynode-simulator', 'honeycomb-tpce')
-    sample_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),
-                                    '..', '..', 'sample_configs', 'openroadm', sim[1])
-    if os.path.isfile(executable):
-        with open(log_file, 'w', encoding='utf-8') as outfile:
-            return subprocess.Popen(
-                [executable, SIMS[sim]['port'], os.path.join(sample_directory, SIMS[sim]['configfile'])],
-                stdout=outfile, stderr=outfile)
-    return None
-
-
 def wait_until_log_contains(log_file, regexp, time_to_wait=60):
     # pylint: disable=lost-exception
     # pylint: disable=consider-using-with
@@ -430,7 +465,7 @@ def get_portmapping_node_attr(node: str, attr: str, value: str):
 def get_ietf_network_request(network: str, content: str):
     url = {'rfc8040': '{}/data/ietf-network:networks/network={}?content={}',
            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
+    if RESTCONF_VERSION in ('rfc8040'):
         format_args = ('{}', network, content)
     elif content == 'config':
         format_args = ('{}', content, network)
@@ -466,7 +501,7 @@ def del_ietf_network(network: str):
 def get_ietf_network_link_request(network: str, link: str, content: str):
     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
+    if RESTCONF_VERSION in ('rfc8040'):
         format_args = ('{}', network, link, content)
     elif content == 'config':
         format_args = ('{}', content, network, link)
@@ -484,7 +519,7 @@ def get_ietf_network_link_request(network: str, link: str, content: str):
 def del_ietf_network_link_request(network: str, link: str, content: str):
     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/ietf-network-topology:link={}?content={}',
            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/ietf-network-topology:link/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
+    if RESTCONF_VERSION in ('rfc8040'):
         format_args = ('{}', network, link, content)
     elif content == 'config':
         format_args = ('{}', content, network, link)
@@ -515,7 +550,7 @@ def del_oms_attr_request(link: str,):
 def get_ietf_network_node_request(network: str, node: str, content: str):
     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
+    if RESTCONF_VERSION in ('rfc8040'):
         format_args = ('{}', network, node, content)
     elif content == 'config':
         format_args = ('{}', content, network, node)
@@ -536,7 +571,7 @@ def get_ietf_network_node_request(network: str, node: str, content: str):
 def del_ietf_network_node_request(network: str, node: str, content: str):
     url = {'rfc8040': '{}/data/ietf-network:networks/network={}/node={}?content={}',
            'draft-bierman02': '{}/{}/ietf-network:networks/network/{}/node/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
+    if RESTCONF_VERSION in ('rfc8040'):
         format_args = ('{}', network, node, content)
     elif content == 'config':
         format_args = ('{}', content, network, node)
@@ -569,10 +604,7 @@ def get_ordm_serv_list_request():
 def get_ordm_serv_list_attr_request(attribute: str, value: str):
     url = {'rfc8040': '{}/data/org-openroadm-service:service-list/{}={}?content=nonconfig',
            'draft-bierman02': '{}/operational/org-openroadm-service:service-list/{}/{}'}
-    if RESTCONF_VERSION == 'rfc8040':
-        format_args = ('{}', attribute, value)
-    else:
-        format_args = ('{}', attribute, value)
+    format_args = ('{}', attribute, value)
     response = get_request(url[RESTCONF_VERSION].format(*format_args))
     res = response.json()
     return_key = {'rfc8040': 'org-openroadm-service:' + attribute,