From b0eed16d009e8d6b2c7c8aa80c0bda87bcb57f49 Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 09:26:50 -0500 Subject: [PATCH 1/6] turn on request validation --- sdx_controller/handlers/connection_handler.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sdx_controller/handlers/connection_handler.py b/sdx_controller/handlers/connection_handler.py index 0c509b8..d0c15ee 100644 --- a/sdx_controller/handlers/connection_handler.py +++ b/sdx_controller/handlers/connection_handler.py @@ -6,7 +6,7 @@ from sdx_pce.load_balancing.te_solver import TESolver from sdx_pce.topology.temanager import TEManager -from sdx_pce.utils.exceptions import TEError +from sdx_pce.utils.exceptions import RequestValidationError, TEError from sdx_controller.messaging.topic_queue_producer import TopicQueueProducer from sdx_controller.models.simple_link import SimpleLink @@ -139,10 +139,15 @@ def place_connection( graph = te_manager.generate_graph_te() if graph is None: return "No SDX topology found", 424 + try: + traffic_matrix = te_manager.generate_traffic_matrix( + connection_request=connection_request + ) + except RequestValidationError as request_err: + err = traceback.format_exc().replace("\n", ", ") + logger.error(f"Error when parsing and validating request: {e} - {err}") + return f"Error: {e}", request_err.request_code - traffic_matrix = te_manager.generate_traffic_matrix( - connection_request=connection_request - ) if traffic_matrix is None: return "Could not generate a traffic matrix", 402 @@ -171,7 +176,7 @@ def place_connection( # We could probably return te_err.te_code instead of 400, # but I don't think PCE should use HTTP error codes, # because that violates abstraction boundaries. - return f"PCE error: {te_err}", 400 + return f"PCE error: {te_err}", te_err.te_code except Exception as e: err = traceback.format_exc().replace("\n", ", ") logger.error(f"Error when generating/publishing breakdown: {e} - {err}") From 03348ca20861d81232b1da9a9aae20cf60620968 Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 09:42:34 -0500 Subject: [PATCH 2/6] use pce@dev6: turn on the request validation test --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index d0dd1e1..c8cce76 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ dependencies = [ "pika >= 1.2.0", "dataset", "pymongo > 3.0", - "sdx-pce @ git+https://github.com/atlanticwave-sdx/pce@3.0.0.dev5", + "sdx-pce @ git+https://github.com/atlanticwave-sdx/pce@3.0.0.dev6", ] [project.optional-dependencies] From 45f72005916c7580b2e025147cacf0b20f30f5cd Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 09:46:19 -0500 Subject: [PATCH 3/6] error message --- sdx_controller/handlers/connection_handler.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sdx_controller/handlers/connection_handler.py b/sdx_controller/handlers/connection_handler.py index d0c15ee..291a6f0 100644 --- a/sdx_controller/handlers/connection_handler.py +++ b/sdx_controller/handlers/connection_handler.py @@ -145,8 +145,10 @@ def place_connection( ) except RequestValidationError as request_err: err = traceback.format_exc().replace("\n", ", ") - logger.error(f"Error when parsing and validating request: {e} - {err}") - return f"Error: {e}", request_err.request_code + logger.error( + f"Error when parsing and validating request: {request_err} - {err}" + ) + return f"Error: {request_err}", request_err.request_code if traffic_matrix is None: return "Could not generate a traffic matrix", 402 From 36f812dd876a1a9b5bf851295e70b28d2614e22d Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 10:05:33 -0500 Subject: [PATCH 4/6] error code 410 --- sdx_controller/test/test_l2vpn_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdx_controller/test/test_l2vpn_controller.py b/sdx_controller/test/test_l2vpn_controller.py index 06acc8c..8645eb5 100644 --- a/sdx_controller/test/test_l2vpn_controller.py +++ b/sdx_controller/test/test_l2vpn_controller.py @@ -609,7 +609,7 @@ def test_issue_356(self): print(f"POST response body is : {response.data.decode('utf-8')}") print(f"POST Response JSON is : {response.get_json()}") - self.assertStatus(response, 400) + self.assertStatus(response, 410) if __name__ == "__main__": From cdb5656e17fbff59d9052100eceea71ff64f1841 Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 10:21:48 -0500 Subject: [PATCH 5/6] erro code 400 instead of 402 --- sdx_controller/handlers/connection_handler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdx_controller/handlers/connection_handler.py b/sdx_controller/handlers/connection_handler.py index 291a6f0..fcf1211 100644 --- a/sdx_controller/handlers/connection_handler.py +++ b/sdx_controller/handlers/connection_handler.py @@ -151,7 +151,10 @@ def place_connection( return f"Error: {request_err}", request_err.request_code if traffic_matrix is None: - return "Could not generate a traffic matrix", 402 + return ( + "Request does not have a valid JSON or body is incomplete/incorrect", + 400, + ) logger.info(f"Generated graph: '{graph}', traffic matrix: '{traffic_matrix}'") From c1530a9872128f15c0e47e8e91045f35dfe0a777 Mon Sep 17 00:00:00 2001 From: Yufeng Xin Date: Tue, 14 Jan 2025 10:27:30 -0500 Subject: [PATCH 6/6] error message match in the test --- sdx_controller/test/test_l2vpn_controller.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdx_controller/test/test_l2vpn_controller.py b/sdx_controller/test/test_l2vpn_controller.py index 8645eb5..349abd1 100644 --- a/sdx_controller/test/test_l2vpn_controller.py +++ b/sdx_controller/test/test_l2vpn_controller.py @@ -333,7 +333,8 @@ def test_place_connection_v2_with_three_topologies_400_response(self): "Failure", ) self.assertEqual( - response.get_json().get("reason"), "Could not generate a traffic matrix" + response.get_json().get("reason"), + "Request does not have a valid JSON or body is incomplete/incorrect", ) # Returned connection ID should be different from the original