diff --git a/src/nbi/service/__main__.py b/src/nbi/service/__main__.py index 7e01b8de4464f4389125b3d62207b1352dfd2ab0..58fbb9625addc43c6b62d06d7a9caa3f648203d5 100644 --- a/src/nbi/service/__main__.py +++ b/src/nbi/service/__main__.py @@ -75,6 +75,14 @@ def main(): register_tfs_api(rest_server) rest_server.start() + LOGGER.debug('Configured Resources:') + for resource in rest_server.api.resources: + LOGGER.debug(' - {:s}'.format(str(resource))) + + LOGGER.debug('Configured Rules:') + for rule in rest_server.app.url_map.iter_rules(): + LOGGER.debug(' - {:s}'.format(str(rule))) + # Wait for Ctrl+C or termination signal while not terminate.wait(timeout=1.0): pass diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/__init__.py b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/__init__.py index 3d6ed94c8fc0bc11618dcc8af30180e8d11c677c..d556aa6480e8cdf51399122e06fd24f915fbfa02 100644 --- a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/__init__.py +++ b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/__init__.py @@ -31,7 +31,6 @@ def register_ietf_acl(rest_server: RestServer): rest_server, ACLs, "/device=<path:device_uuid>/ietf-access-control-list:acls", - "/device=<path:device_uuid>/ietf-access-control-list:acls", ) __add_resource( diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_service.py b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_service.py index 466a68efc8b6c966dd3a282fcd5a394f4dae70a8..99cbf09ba4cbe885bea7045d3794328614884d17 100644 --- a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_service.py +++ b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_service.py @@ -43,6 +43,7 @@ ACL_CONIG_RULE_KEY = r'\/device\[.+\]\/endpoint\[(.+)\]/acl_ruleset\[{}\]' class ACL(Resource): # @HTTP_AUTH.login_required def get(self, device_uuid: str, acl_name: str): + LOGGER.debug("GET device_uuid={:s}, acl_name={:s}".format(str(device_uuid), str(acl_name))) RE_ACL_CONIG_RULE_KEY = re.compile(ACL_CONIG_RULE_KEY.format(acl_name)) context_client = ContextClient() @@ -63,6 +64,7 @@ class ACL(Resource): # @HTTP_AUTH.login_required def delete(self, device_uuid: str, acl_name: str): + LOGGER.debug("DELETE device_uuid={:s}, acl_name={:s}".format(str(device_uuid), str(acl_name))) RE_ACL_CONIG_RULE_KEY = re.compile(ACL_CONIG_RULE_KEY.format(acl_name)) context_client = ContextClient() diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_services.py b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_services.py index 1ed7893b4cc12658e74b03ca28ff9b9f3373afa8..63095e294ea27ce80206dae4ec66291451c68c7a 100644 --- a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_services.py +++ b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/acl_services.py @@ -34,6 +34,7 @@ class ACLs(Resource): # @HTTP_AUTH.login_required def post(self, device_uuid: str): + LOGGER.debug("POST device_uuid={:s}, body={:s}".format(str(device_uuid), str(request.data))) if not request.is_json: raise UnsupportedMediaType("JSON pyload is required") request_data: Dict = request.json diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/ietf_acl_client.py b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/ietf_acl_client.py index 79ec388a2c8cb8e3b4352bfe17866e33c7763585..9bad3bec903770d5fa547a57c757d56c80a37ab7 100644 --- a/src/nbi/service/rest_server/nbi_plugins/ietf_acl/ietf_acl_client.py +++ b/src/nbi/service/rest_server/nbi_plugins/ietf_acl/ietf_acl_client.py @@ -12,58 +12,57 @@ # See the License for the specific language governing permissions and # limitations under the License. -import requests -import json -import time +import json, requests, time +from typing import Optional +from requests.auth import HTTPBasicAuth -BASE_URL = "/restconf/data" -POST_URL = "/device={}/ietf-access-control-list:acls" -DELETE_URL = "/device={}/ietf-access-control-list:acl={}" +BASE_URL = '{:s}://{:s}:{:d}/restconf/data' +ACLS_URL = '{:s}/device={:s}/ietf-access-control-list:acls' +ACL_URL = '{:s}/device={:s}/ietf-access-control-list:acl={:s}' -class IetfTfsClient: - def __init__(self, - tfs_host: str = "10.1.1.119", - tfs_port: int = 80, - username: str = "admin", - password: str = "admin", - timeout: int = 10, - allow_redirects: bool = True, - ) -> None: - self.host = tfs_host - self.port = tfs_port - self.username = username - self.password = password - self.timeout = timeout - self.allow_redirects = allow_redirects - - def post(self, device_uuid: str, ietf_acl_data: dict) -> str: - request_url = "http://{:s}:{:d}{:s}{:s}".format(self.host, self.port, BASE_URL, POST_URL.format(device_uuid)) - reply = requests.request("post", request_url, timeout=self.timeout, json=ietf_acl_data, allow_redirects=self.allow_redirects) +class TfsIetfAclClient: + def __init__( + self, host : str = 'localhost', port : int = 80, schema : str = 'http', + username : Optional[str] = 'admin', password : Optional[str] = 'admin', + timeout : int = 10, allow_redirects : bool = True, verify : bool = False + ) -> None: + self._base_url = BASE_URL.format(schema, host, port) + auth = HTTPBasicAuth(username, password) if username is not None and password is not None else None + self._settings = dict(auth=auth, timeout=timeout, allow_redirects=allow_redirects, verify=verify) + + def post(self, device_uuid : str, ietf_acl_data : dict) -> str: + request_url = ACLS_URL.format(self._base_url, device_uuid) + reply = requests.post(request_url, json=ietf_acl_data, **(self._settings)) return reply.text - def get(self, device_uuid: str, acl_name: str) -> str: - request_url = "http://{:s}:{:d}{:s}{:s}".format(self.host, self.port, BASE_URL, DELETE_URL.format(device_uuid, acl_name)) - reply = requests.request("get", request_url, timeout=self.timeout, allow_redirects=self.allow_redirects) + def get(self, device_uuid : str, acl_name : str) -> str: + request_url = ACL_URL.format(self._base_url, device_uuid, acl_name) + reply = requests.get(request_url, **(self._settings)) return reply.text - def delete(self, device_uuid: str, acl_name: str) -> str: - request_url = "http://{:s}:{:d}{:s}{:s}".format(self.host, self.port, BASE_URL, DELETE_URL.format(device_uuid, acl_name)) - reply = requests.request("delete", request_url, timeout=self.timeout, allow_redirects=self.allow_redirects) + def delete(self, device_uuid : str, acl_name : str) -> str: + request_url = ACL_URL.format(self._base_url, device_uuid, acl_name) + reply = requests.delete(request_url, **(self._settings)) return reply.text -if __name__ == "__main__": - csg1_device_uuid = 'b71fd62f-e3d4-5956-93b9-3139094836cf' +def main(): + csg1_device_uuid = '0392c251-b5d3-526b-8f3b-a3d4137829fa' acl_name = 'sample-ipv4-acl' acl_request_path = 'src/nbi/tests/data/ietf_acl.json' - with open(acl_request_path, 'r') as afile: - acl_request_data = json.load(afile) - ietf_tfs_client = IetfTfsClient() - post_response = ietf_tfs_client.post(csg1_device_uuid, acl_request_data) - print(f"post response: {post_response}") + with open(acl_request_path, 'r', encoding='UTF-8') as f: + acl_request_data = json.load(f) + print(acl_request_data) + + client = TfsIetfAclClient() + post_response = client.post(csg1_device_uuid, acl_request_data) + print(f'post response: {post_response}') time.sleep(.5) - get_response = ietf_tfs_client.get(csg1_device_uuid, acl_name) - print(f"get response: {get_response}") + get_response = client.get(csg1_device_uuid, acl_name) + print(f'get response: {get_response}') time.sleep(.5) - delete_response = ietf_tfs_client.delete(csg1_device_uuid, acl_name) - print(f"delete response: {delete_response}") \ No newline at end of file + delete_response = client.delete(csg1_device_uuid, acl_name) + print(f'delete response: {delete_response}') + +if __name__ == '__main__': + main()