vppcfg: add args to specify location of vpp api files

- refactor to address review comments.

Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
This commit is contained in:
Dave Wallace
2022-04-26 21:04:31 -04:00
parent 71ea1823f4
commit b09773ae75
4 changed files with 80 additions and 13 deletions

View File

@ -31,8 +31,13 @@ class Dumper(VPPApi):
Note that not all running VPP configs are "valid" in vppcfg's eyes. It is not Note that not all running VPP configs are "valid" in vppcfg's eyes. It is not
guaranteed that the output of the Dumper() will stand validation.""" guaranteed that the output of the Dumper() will stand validation."""
def __init__(self, address="/run/vpp/api.sock", clientname="vppcfg"): def __init__(
VPPApi.__init__(self, address, clientname) self,
vpp_api_socket="/run/vpp/api.sock",
vpp_json_dir="/usr/share/vpp/api/",
clientname="vppcfg",
):
VPPApi.__init__(self, vpp_api_socket, vpp_json_dir, clientname)
def write(self, outfile): def write(self, outfile):
"""Emit the configuration to either stdout (outfile=='-') or a filename""" """Emit the configuration to either stdout (outfile=='-') or a filename"""

View File

@ -38,11 +38,11 @@ class Reconciler:
but not yet in the dataplane; and finally it syncs the configuration attributes of but not yet in the dataplane; and finally it syncs the configuration attributes of
objects that can be changed at runtime.""" objects that can be changed at runtime."""
def __init__(self, cfg): def __init__(self, cfg, vpp_api_socket='/run/vpp/api.sock', vpp_json_dir='/usr/share/vpp/api/'):
self.logger = logging.getLogger("vppcfg.reconciler") self.logger = logging.getLogger("vppcfg.reconciler")
self.logger.addHandler(logging.NullHandler()) self.logger.addHandler(logging.NullHandler())
self.vpp = VPPApi() self.vpp = VPPApi(vpp_api_socket, vpp_json_dir)
self.cfg = cfg self.cfg = cfg
## List of CLI calls emitted during the prune, create and sync phases. ## List of CLI calls emitted during the prune, create and sync phases.

View File

@ -28,11 +28,22 @@ from vpp_papi import VPPApiClient
class VPPApi: class VPPApi:
"""The VPPApi class is a base class that abstracts the vpp_papi.""" """The VPPApi class is a base class that abstracts the vpp_papi."""
def __init__(self, address="/run/vpp/api.sock", clientname="vppcfg"): def __init__(
self,
vpp_api_socket="/run/vpp/api.sock",
vpp_json_dir="/usr/share/vpp/api/",
clientname="vppcfg",
):
self.logger = logging.getLogger("vppcfg.vppapi") self.logger = logging.getLogger("vppcfg.vppapi")
self.logger.addHandler(logging.NullHandler()) self.logger.addHandler(logging.NullHandler())
self.address = address 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.connected = False self.connected = False
self.clientname = clientname self.clientname = clientname
self.vpp = None self.vpp = None
@ -45,12 +56,9 @@ class VPPApi:
if self.connected: if self.connected:
return True return True
vpp_json_dir = "/usr/share/vpp/api/"
## vpp_json_dir = "/home/pim/src/vpp/build-root/build-vpp_debug-native/vpp/CMakeFiles/"
# construct a list of all the json api files # construct a list of all the json api files
jsonfiles = [] jsonfiles = []
for root, _dirnames, filenames in os.walk(vpp_json_dir): for root, _dirnames, filenames in os.walk(self.vpp_json_dir):
for filename in fnmatch.filter(filenames, "*.api.json"): for filename in fnmatch.filter(filenames, "*.api.json"):
jsonfiles.append(os.path.join(root, filename)) jsonfiles.append(os.path.join(root, filename))
@ -58,7 +66,7 @@ class VPPApi:
self.logger.error("no json api files found") self.logger.error("no json api files found")
return False return False
self.vpp = VPPApiClient(apifiles=jsonfiles, server_address=self.address) self.vpp = VPPApiClient(apifiles=jsonfiles, server_address=self.vpp_api_socket)
try: try:
self.logger.debug("Connecting to VPP") self.logger.debug("Connecting to VPP")
self.vpp.connect(self.clientname) self.vpp.connect(self.clientname)

58
vppcfg
View File

@ -86,6 +86,22 @@ def main():
type=str, type=str,
help="""Output file for YAML config, default stdout""", help="""Output file for YAML config, default stdout""",
) )
dump_p.add_argument(
"-j",
"--vpp-json-dir",
dest="vpp_json_dir",
required=False,
type=str,
help="""Directory where VPP API JSON files are located""",
)
dump_p.add_argument(
"-a",
"--vpp-api-socket",
dest="vpp_api_socket",
required=False,
type=str,
help="""Pathname of VPP API socket file""",
)
plan_p = subparsers.add_parser( plan_p = subparsers.add_parser(
"plan", "plan",
@ -115,6 +131,22 @@ def main():
type=str, type=str,
help="""Output file for VPP CLI commands, default stdout""", help="""Output file for VPP CLI commands, default stdout""",
) )
plan_p.add_argument(
"-j",
"--vpp-json-dir",
dest="vpp_json_dir",
required=False,
type=str,
help="""Directory where VPP API JSON files are located""",
)
plan_p.add_argument(
"-a",
"--vpp-api-socket",
dest="vpp_api_socket",
required=False,
type=str,
help="""Pathname of VPP API socket file""",
)
apply_p = subparsers.add_parser( apply_p = subparsers.add_parser(
"apply", help="apply changes from current VPP dataplane to target config" "apply", help="apply changes from current VPP dataplane to target config"
@ -134,6 +166,22 @@ def main():
type=str, type=str,
help="""YAML configuration file for vppcfg""", help="""YAML configuration file for vppcfg""",
) )
apply_p.add_argument(
"-j",
"--vpp-json-dir",
dest="vpp_json_dir",
required=False,
type=str,
help="""Directory where VPP API JSON files are located""",
)
apply_p.add_argument(
"-a",
"--vpp-api-socket",
dest="vpp_api_socket",
required=False,
type=str,
help="""Pathname of VPP API socket file""",
)
args = parser.parse_args() args = parser.parse_args()
if not args.command: if not args.command:
@ -150,8 +198,14 @@ def main():
format="[%(levelname)-8s] %(name)s.%(funcName)s: %(message)s", level=level format="[%(levelname)-8s] %(name)s.%(funcName)s: %(message)s", level=level
) )
opt_kwargs = {}
if args.vpp_json_dir:
opt_kwargs["vpp_json_dir"] = args.vpp_json_dir
if args.vpp_api_socket:
opt_kwargs["vpp_api_socket"] = args.vpp_api_socket
if args.command == "dump": if args.command == "dump":
dumper = Dumper() dumper = Dumper(**opt_kwargs)
if not dumper.readconfig(): if not dumper.readconfig():
logging.error("Could not retrieve config from VPP") logging.error("Could not retrieve config from VPP")
sys.exit(-7) sys.exit(-7)
@ -175,7 +229,7 @@ def main():
if args.command == "check": if args.command == "check":
sys.exit(0) sys.exit(0)
reconciler = Reconciler(cfg) reconciler = Reconciler(cfg, **opt_kwargs)
if not reconciler.vpp.readconfig(): if not reconciler.vpp.readconfig():
sys.exit(-3) sys.exit(-3)