Replace the pyagentx threaded version with a much simpler, non-threaded version.

This commit is contained in:
Pim van Pelt
2021-09-11 12:19:38 +00:00
parent 842bce9d6e
commit 8c9c1e2b4a
13 changed files with 507 additions and 1089 deletions

View File

@ -7,7 +7,6 @@ from vpp_papi import VPPApiClient
import os
import fnmatch
import logging
import threading
class NullHandler(logging.Handler):
@ -16,18 +15,16 @@ class NullHandler(logging.Handler):
class VPPApi():
def __init__(self, address='/run/vpp/api.sock'):
def __init__(self, address='/run/vpp/api.sock', clientname='vppapi-client'):
self.address = address
self.lock = threading.Lock()
self.connected = False
self.logger = logging.getLogger('pyagentx.vppapi')
self.logger = logging.getLogger('agentx.vppapi')
self.logger.addHandler(NullHandler())
self.clientname = clientname
self.vpp = None
def connect(self):
self.lock.acquire()
if self.connected:
self.lock.release()
return True
vpp_json_dir = '/usr/share/vpp/api/'
@ -40,23 +37,20 @@ class VPPApi():
if not jsonfiles:
self.logger.error('no json api files found')
self.lock.release()
return False
self.vpp = VPPApiClient(apifiles=jsonfiles,
server_address=self.address)
try:
self.logger.info('Connecting to VPP')
self.vpp.connect('vpp-snmp-agent')
self.vpp.connect(self.clientname)
except:
self.lock.release()
return False
v = self.vpp.api.show_version()
self.logger.info('VPP version is %s' % v.version)
self.connected = True
self.lock.release()
return True
def disconnect(self):
@ -71,23 +65,19 @@ class VPPApi():
if not self.connected:
return ret
self.lock.acquire()
try:
iface_list = self.vpp.api.sw_interface_dump()
except Exception as e:
self.logger.error("VPP communication error, disconnecting", e)
self.vpp.disconnect()
self.connected = False
self.lock.release()
return ret
if not iface_list:
self.logger.error("Can't get interface list")
self.lock.release()
return ret
for iface in iface_list:
ret[iface.interface_name] = iface
self.lock.release()
return ret