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.
This commit is contained in:
Pim van Pelt
2022-12-03 12:42:33 +00:00
parent 1b0fa13f74
commit 628dc62b15

View File

@ -38,34 +38,39 @@ class VPPApi:
self.logger = logging.getLogger("vppcfg.vppapi") self.logger = logging.getLogger("vppcfg.vppapi")
self.logger.addHandler(logging.NullHandler()) 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_api_socket = vpp_api_socket
self.vpp_json_dir = vpp_json_dir self.vpp_json_dir = vpp_json_dir
self.vpp_jsonfiles = []
self.connected = False self.connected = False
self.clientname = clientname self.clientname = clientname
self.vpp = None self.vpp = None
self.cache_clear() self.cache_clear()
self.lcp_enabled = False 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): def connect(self, retries=30):
"""Connect to the VPP Dataplane, if we're not already connected""" """Connect to the VPP Dataplane, if we're not already connected"""
if self.connected: if self.connected:
return True return True
# construct a list of all the json api files if not os.path.exists(self.vpp_api_socket):
jsonfiles = VPPApiJSONFiles.find_api_files(api_dir=self.vpp_json_dir) self.logger.error(f"VPP api socket file not found: {self.vpp_api_socket}")
if not jsonfiles:
self.logger.error("no json api files found")
return False 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") self.logger.debug("Connecting to VPP")
for i in range(retries): for i in range(retries):
try: try:
@ -206,7 +211,6 @@ class VPPApi:
self.cache_clear() self.cache_clear()
## Workaround LCPng and linux-cp, in order.
self.lcp_enabled = False self.lcp_enabled = False
try: try:
self.logger.debug("Retrieving LCPs") self.logger.debug("Retrieving LCPs")