From 628dc62b15e26071becfcc78a2749de10225a1c9 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sat, 3 Dec 2022 12:42:33 +0000 Subject: [PATCH] fix: move socket check to connect If there is no need to connect to a running VPP instance (for example, if the configuration is going to mock the VPPMessage()s rather than read them from the VPP dataplane), then there is no need to assert that a socket exists. Scan the JSON files in the constructor though, not at connect time, as other methods may want to use the JSON files without having to connect to the API. --- vppcfg/vpp/vppapi.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/vppcfg/vpp/vppapi.py b/vppcfg/vpp/vppapi.py index 05cbfa9..9eeb6f7 100644 --- a/vppcfg/vpp/vppapi.py +++ b/vppcfg/vpp/vppapi.py @@ -38,34 +38,39 @@ class VPPApi: self.logger = logging.getLogger("vppcfg.vppapi") self.logger.addHandler(logging.NullHandler()) - if vpp_json_dir is None: - vpp_json_dir = VPPApiJSONFiles.find_api_dir([]) - - if not os.path.exists(vpp_api_socket): - self.logger.error(f"VPP api socket file not found: {vpp_api_socket}") - if not os.path.isdir(vpp_json_dir): - self.logger.error(f"VPP api json directory not found: {vpp_json_dir}") - self.vpp_api_socket = vpp_api_socket self.vpp_json_dir = vpp_json_dir + self.vpp_jsonfiles = [] self.connected = False self.clientname = clientname self.vpp = None self.cache_clear() self.lcp_enabled = False + if self.vpp_json_dir is None: + self.vpp_json_dir = VPPApiJSONFiles.find_api_dir([]) + elif not os.path.isdir(self.vpp_json_dir): + self.logger.error(f"VPP api json directory not found: {self.vpp_json_dir}") + return False + + # construct a list of all the json api files + self.vpp_jsonfiles = VPPApiJSONFiles.find_api_files(api_dir=self.vpp_json_dir) + if not self.vpp_jsonfiles: + self.logger.error("no json api files found") + return False + def connect(self, retries=30): """Connect to the VPP Dataplane, if we're not already connected""" if self.connected: return True - # construct a list of all the json api files - jsonfiles = VPPApiJSONFiles.find_api_files(api_dir=self.vpp_json_dir) - if not jsonfiles: - self.logger.error("no json api files found") + if not os.path.exists(self.vpp_api_socket): + self.logger.error(f"VPP api socket file not found: {self.vpp_api_socket}") return False - self.vpp = VPPApiClient(apifiles=jsonfiles, server_address=self.vpp_api_socket) + self.vpp = VPPApiClient( + apifiles=self.vpp_jsonfiles, server_address=self.vpp_api_socket + ) self.logger.debug("Connecting to VPP") for i in range(retries): try: @@ -206,7 +211,6 @@ class VPPApi: self.cache_clear() - ## Workaround LCPng and linux-cp, in order. self.lcp_enabled = False try: self.logger.debug("Retrieving LCPs")