From 60c4324f7e88f5de61cbe3f928fc9ea73d74823a Mon Sep 17 00:00:00 2001
From: Adrian Pistol <vifino@tty.sh>
Date: Fri, 28 Oct 2022 03:02:28 +0200
Subject: [PATCH] vppapi: Use VPPApiJSONFiles instead of reinventing the wheel.

---
 setup.py                 |  1 -
 vppcfg/vpp/applier.py    |  2 +-
 vppcfg/vpp/dumper.py     |  2 +-
 vppcfg/vpp/reconciler.py |  2 +-
 vppcfg/vpp/vppapi.py     | 13 ++++++-------
 5 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/setup.py b/setup.py
index b8c4189..42ccd62 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,6 @@ setup(
         'importlib-metadata; python_version == "3.8"',
         "yamale",
         "netaddr",
-        "ipaddress",
         "vpp_papi",
     ],
     packages=["vppcfg", "vppcfg/config", "vppcfg/vpp"],
diff --git a/vppcfg/vpp/applier.py b/vppcfg/vpp/applier.py
index cac633d..9934bb8 100644
--- a/vppcfg/vpp/applier.py
+++ b/vppcfg/vpp/applier.py
@@ -30,7 +30,7 @@ class Applier(VPPApi):
     def __init__(
         self,
         vpp_api_socket="/run/vpp/api.sock",
-        vpp_json_dir="/usr/share/vpp/api/",
+        vpp_json_dir=None,
         clientname="vppcfg",
     ):
         VPPApi.__init__(self, vpp_api_socket, vpp_json_dir, clientname)
diff --git a/vppcfg/vpp/dumper.py b/vppcfg/vpp/dumper.py
index adfb5b6..1d31721 100644
--- a/vppcfg/vpp/dumper.py
+++ b/vppcfg/vpp/dumper.py
@@ -34,7 +34,7 @@ class Dumper(VPPApi):
     def __init__(
         self,
         vpp_api_socket="/run/vpp/api.sock",
-        vpp_json_dir="/usr/share/vpp/api/",
+        vpp_json_dir=None,
         clientname="vppcfg",
     ):
         VPPApi.__init__(self, vpp_api_socket, vpp_json_dir, clientname)
diff --git a/vppcfg/vpp/reconciler.py b/vppcfg/vpp/reconciler.py
index ca8aacd..b02fdf6 100644
--- a/vppcfg/vpp/reconciler.py
+++ b/vppcfg/vpp/reconciler.py
@@ -42,7 +42,7 @@ class Reconciler:
         self,
         cfg,
         vpp_api_socket="/run/vpp/api.sock",
-        vpp_json_dir="/usr/share/vpp/api/",
+        vpp_json_dir=None,
     ):
         self.logger = logging.getLogger("vppcfg.reconciler")
         self.logger.addHandler(logging.NullHandler())
diff --git a/vppcfg/vpp/vppapi.py b/vppcfg/vpp/vppapi.py
index 2317531..b23d8db 100644
--- a/vppcfg/vpp/vppapi.py
+++ b/vppcfg/vpp/vppapi.py
@@ -23,7 +23,7 @@ import fnmatch
 import logging
 import socket
 import time
-from vpp_papi import VPPApiClient
+from vpp_papi import VPPApiClient, VPPApiJSONFiles
 
 
 class VPPApi:
@@ -32,12 +32,15 @@ class VPPApi:
     def __init__(
         self,
         vpp_api_socket="/run/vpp/api.sock",
-        vpp_json_dir="/usr/share/vpp/api/",
+        vpp_json_dir=None,
         clientname="vppcfg",
     ):
         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):
@@ -58,11 +61,7 @@ class VPPApi:
             return True
 
         # construct a list of all the json api files
-        jsonfiles = []
-        for root, _dirnames, filenames in os.walk(self.vpp_json_dir):
-            for filename in fnmatch.filter(filenames, "*.api.json"):
-                jsonfiles.append(os.path.join(root, filename))
-
+        jsonfiles = VPPApiJSONFiles.find_api_files(api_dir=self.vpp_json_dir)
         if not jsonfiles:
             self.logger.error("no json api files found")
             return False