From f31d156bf8a054c6b3805fc0c5fb71f3035741ae Mon Sep 17 00:00:00 2001 From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk> Date: Tue, 15 Jan 2019 14:14:30 +0000 Subject: [PATCH] Added an API endpoint for deleting the network topology graph --- src/service/clmcservice/graphapi/tests.py | 10 ++++++ src/service/clmcservice/graphapi/utilities.py | 31 +++++++++++++++++++ src/service/clmcservice/graphapi/views.py | 16 +++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/service/clmcservice/graphapi/tests.py b/src/service/clmcservice/graphapi/tests.py index 1615b3c..12c6628 100644 --- a/src/service/clmcservice/graphapi/tests.py +++ b/src/service/clmcservice/graphapi/tests.py @@ -428,6 +428,16 @@ class TestGraphAPI(object): assert response == {"forward_latencies": forward_latencies, "reverse_latencies": reverse_latencies, "total_forward_latency": sum(forward_latencies), "total_reverse_latency": sum(reverse_latencies), "bandwidth": 104857600, "response_time": response_time, "global_tags": global_tags}, "Incorrect RTT response" + def test_delete_network_graph(self): + """ + Tests the delete network graph functionality. + """ + + request = testing.DummyRequest() + response = GraphAPI(request).delete_network_topology() + + assert response == {"deleted_switches_count": 6, "deleted_clusters_count": 6} + @staticmethod def check_exist_relationship(relationships_tuple, graph, uuid): """ diff --git a/src/service/clmcservice/graphapi/utilities.py b/src/service/clmcservice/graphapi/utilities.py index 4f4dfe3..95ff87e 100644 --- a/src/service/clmcservice/graphapi/utilities.py +++ b/src/service/clmcservice/graphapi/utilities.py @@ -366,3 +366,34 @@ def build_network_graph(graph, switches, links, clusters): find_or_create_edge(graph, "linkedTo", cluster_node, to_node, latency=0) return new_switches_count, new_clusters_count + + +def delete_network_graph(graph): + """ + A function used to delete all nodes of type Switch and Cluster in the neo4j graph. + + :param graph: the neo4j graph + :return: the number of deleted switches and clusters + """ + + log.info("Deleting Switch nodes.".format()) + + subgraph = graph.nodes.match("Switch") + deleted_switches = 0 + for node in subgraph: + graph.delete(node) + deleted_switches += 1 + + log.info("Deleted {0} Switch nodes.".format(deleted_switches)) + + log.info("Deleting Cluster nodes.") + + subgraph = graph.nodes.match("Cluster") + deleted_clusters = 0 + for node in subgraph: + graph.delete(node) + deleted_clusters += 1 + + log.info("Deleted {0} Cluster nodes.".format(deleted_clusters)) + + return deleted_switches, deleted_clusters diff --git a/src/service/clmcservice/graphapi/views.py b/src/service/clmcservice/graphapi/views.py index dfcb38a..c3441af 100644 --- a/src/service/clmcservice/graphapi/views.py +++ b/src/service/clmcservice/graphapi/views.py @@ -24,7 +24,7 @@ from clmcservice.graphapi.utilities import validate_json_queries_body, validate_graph_url_params, \ - build_network_graph, build_temporal_subgraph, delete_temporal_subgraph, validate_graph_rtt_params, RTT_CYPHER_QUERY_TEMPLATE + build_network_graph, delete_network_graph, build_temporal_subgraph, delete_temporal_subgraph, validate_graph_rtt_params, RTT_CYPHER_QUERY_TEMPLATE from uuid import uuid4 from influxdb import InfluxDBClient from py2neo import Graph @@ -276,3 +276,17 @@ class GraphAPI(object): switch_count, clusters_count = build_network_graph(graph, switches, links, clusters) return {"new_switches_count": switch_count, "new_clusters_count": clusters_count} + + @view_config(route_name='graph_network_topology', request_method='DELETE') + def delete_network_topology(self): + """ + An API endpoint to delete the network topology in the neo4j graph. + + :return: A JSON response with the number of switches and clusters that were deleted. + """ + + graph = Graph(host=self.request.registry.settings['neo4j_host'], password=self.request.registry.settings['neo4j_password']) # connect to the neo4j graph db + + deleted_switches, deleted_clusters = delete_network_graph(graph) + + return {"deleted_switches_count": deleted_switches, "deleted_clusters_count": deleted_clusters} -- GitLab