feat(terraform): Refactor ETL
[csit.git] / resources / libraries / python / PapiExecutor.py
index b629996..e685f87 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2023 Cisco and/or its affiliates.
+# Copyright (c) 2024 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
@@ -285,19 +285,36 @@ class PapiSocketExecutor:
             # It is right, we should refactor the code and move initialization
             # of package outside.
             from vpp_papi.vpp_papi import VPPApiClient as vpp_class
-
-            vpp_class.apidir = cls.api_json_path
-            # We need to create instance before removing from sys.path.
-            # Cannot use loglevel parameter, robot.api.logger lacks the support.
-            vpp_instance = vpp_class(
-                use_socket=True,
-                server_address="TBD",
-                async_thread=False,
-                # Large read timeout was originally there for VPP-1722,
-                # it may still be helping against AVF device creation failures.
-                read_timeout=14,
-                logger=FilteredLogger(logger, "INFO"),
-            )
+            try:
+                # The old way. Deduplicate when pre-2402 support is not needed.
+
+                vpp_class.apidir = cls.api_json_path
+                # We need to create instance before removing from sys.path.
+                # Cannot use loglevel parameter, robot.api.logger lacks the support.
+                vpp_instance = vpp_class(
+                    use_socket=True,
+                    server_address="TBD",
+                    async_thread=False,
+                    # Large read timeout was originally there for VPP-1722,
+                    # it may still be helping against AVF device creation failures.
+                    read_timeout=14,
+                    logger=FilteredLogger(logger, "INFO"),
+                )
+            except vpp_class.VPPRuntimeError:
+                # The 39871 way.
+
+                # We need to create instance before removing from sys.path.
+                # Cannot use loglevel parameter, robot.api.logger lacks the support.
+                vpp_instance = vpp_class(
+                    apidir=cls.api_json_path,
+                    use_socket=True,
+                    server_address="TBD",
+                    async_thread=False,
+                    # Large read timeout was originally there for VPP-1722,
+                    # it may still be helping against AVF device creation failures.
+                    read_timeout=14,
+                    logger=FilteredLogger(logger, "INFO"),
+                )
             # The following is needed to prevent union (e.g. Ip4) debug logging
             # of VPP part of PAPI from spamming robot logs.
             logging.getLogger("vpp_papi.serializer").setLevel(logging.INFO)
@@ -1034,6 +1051,10 @@ class PapiSocketExecutor:
                 PapiSocketExecutor._drain(vpp_instance, err_msg)
             # Process replies for this command.
             for reply in replies:
+                if reply is None:
+                    raise RuntimeError(
+                        f"{err_msg}\nNo reply to sync call. VPP crashed?"
+                    )
                 self.crc_checker.check_api_name(reply.__class__.__name__)
                 dictized_reply = dictize_and_check_retval(reply, err_msg)
                 ret_list.append(dictized_reply)
@@ -1044,6 +1065,8 @@ class PapiSocketExecutor:
 
         The messages were already sent by .add() in this mode,
         local_list is used just so we know how many replies to read.
+        Similarly to async .add, we do not check connection status here,
+        thus we avoid needless logging.
 
         Beware: It is not clear what to do when socket read fails
         in the middle of async processing.
@@ -1067,7 +1090,7 @@ class PapiSocketExecutor:
         :rtype: List[UserDict]
         :raises RuntimeError: If the replies are not all correct.
         """
-        vpp_instance = self.get_connected_client()
+        vpp_instance = self.get_connected_client(check_connected=False)
         ret_list = list()
         try:
             for index, _ in enumerate(local_list):