From 83a370f70baba6d0d9cccb59345aebeb5e07aca8 Mon Sep 17 00:00:00 2001 From: Idan Hefetz Date: Mon, 23 Jul 2018 08:10:08 +0000 Subject: [PATCH] support networkx 2.1 add_node() and add_edge() parameter variable names changed. Depends-On: Id583a698bffdd7d7f77ba0da40f7b11e4735940f Change-Id: Id5dbd165a1e0220e4e24207e8d237f94415fc490 --- vitrage/api/controllers/rest.py | 14 ++++++++++++-- vitrage/api/controllers/v1/topology.py | 17 +++++++++++++---- vitrage/graph/driver/networkx_graph.py | 11 +++++------ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/vitrage/api/controllers/rest.py b/vitrage/api/controllers/rest.py index 76eabec83..3cb9ecd77 100644 --- a/vitrage/api/controllers/rest.py +++ b/vitrage/api/controllers/rest.py @@ -23,9 +23,19 @@ class RootRestController(rest.RestController): @staticmethod def as_tree(graph, root=OPENSTACK_CLUSTER, reverse=False): - linked_graph = json_graph.node_link_graph(graph) + if nx.__version__ >= '2.0': + linked_graph = json_graph.node_link_graph( + graph, attrs={'name': 'graph_index'}) + else: + linked_graph = json_graph.node_link_graph(graph) if 0 == nx.number_of_nodes(linked_graph): return {} if reverse: linked_graph = linked_graph.reverse() - return json_graph.tree_data(linked_graph, root=root) + if nx.__version__ >= '2.0': + return json_graph.tree_data( + linked_graph, + root=root, + attrs={'id': 'graph_index', 'children': 'children'}) + else: + return json_graph.tree_data(linked_graph, root=root) diff --git a/vitrage/api/controllers/v1/topology.py b/vitrage/api/controllers/v1/topology.py index cacaae36a..a0de9361d 100644 --- a/vitrage/api/controllers/v1/topology.py +++ b/vitrage/api/controllers/v1/topology.py @@ -14,6 +14,7 @@ import json +import networkx as nx from oslo_log import log from oslo_utils.strutils import bool_from_string @@ -81,12 +82,20 @@ class TopologyController(RootRestController): if graph_type == 'graph': return graph if graph_type == 'tree': - node_id = CLUSTER_ID - if root: + if nx.__version__ >= '2.0': + node_id = '' for node in graph['nodes']: - if node[VProps.VITRAGE_ID] == root: - node_id = node[VProps.ID] + if (root and node[VProps.VITRAGE_ID] == root) or \ + (not root and node[VProps.ID] == CLUSTER_ID): + node_id = node[VProps.GRAPH_INDEX] break + else: + node_id = CLUSTER_ID + if root: + for node in graph['nodes']: + if node[VProps.VITRAGE_ID] == root: + node_id = node[VProps.ID] + break return RootRestController.as_tree(graph, node_id) except Exception: diff --git a/vitrage/graph/driver/networkx_graph.py b/vitrage/graph/driver/networkx_graph.py index 9c2fb02d0..be6749139 100644 --- a/vitrage/graph/driver/networkx_graph.py +++ b/vitrage/graph/driver/networkx_graph.py @@ -84,9 +84,9 @@ class NXGraph(Graph): def _add_vertex(self, v): properties_copy = copy.copy(v.properties) if properties_copy: - self._g.add_node(n=v.vertex_id, **properties_copy) + self._g.add_node(v.vertex_id, **properties_copy) else: - self._g.add_node(n=v.vertex_id) + self._g.add_node(v.vertex_id) @Notifier.update_notify def add_edge(self, e): @@ -100,11 +100,10 @@ class NXGraph(Graph): def _add_edge(self, e): properties_copy = copy.copy(e.properties) if properties_copy: - self._g.add_edge(u=e.source_id, v=e.target_id, - key=e.label, **properties_copy) + self._g.add_edge(e.source_id, e.target_id, + e.label, **properties_copy) else: - self._g.add_edge(u=e.source_id, v=e.target_id, - key=e.label) + self._g.add_edge(e.source_id, e.target_id, e.label) def get_vertex(self, v_id): """Fetch a vertex from the graph