support networkx 2.1

add_node() and add_edge() parameter variable names changed.

Depends-On: Id583a698bffdd7d7f77ba0da40f7b11e4735940f
Change-Id: Id5dbd165a1e0220e4e24207e8d237f94415fc490
This commit is contained in:
Idan Hefetz 2018-07-23 08:10:08 +00:00
parent 12b9fa0b49
commit 83a370f70b
3 changed files with 30 additions and 12 deletions

View File

@ -23,9 +23,19 @@ class RootRestController(rest.RestController):
@staticmethod @staticmethod
def as_tree(graph, root=OPENSTACK_CLUSTER, reverse=False): 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): if 0 == nx.number_of_nodes(linked_graph):
return {} return {}
if reverse: if reverse:
linked_graph = linked_graph.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)

View File

@ -14,6 +14,7 @@
import json import json
import networkx as nx
from oslo_log import log from oslo_log import log
from oslo_utils.strutils import bool_from_string from oslo_utils.strutils import bool_from_string
@ -81,12 +82,20 @@ class TopologyController(RootRestController):
if graph_type == 'graph': if graph_type == 'graph':
return graph return graph
if graph_type == 'tree': if graph_type == 'tree':
node_id = CLUSTER_ID if nx.__version__ >= '2.0':
if root: node_id = ''
for node in graph['nodes']: for node in graph['nodes']:
if node[VProps.VITRAGE_ID] == root: if (root and node[VProps.VITRAGE_ID] == root) or \
node_id = node[VProps.ID] (not root and node[VProps.ID] == CLUSTER_ID):
node_id = node[VProps.GRAPH_INDEX]
break 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) return RootRestController.as_tree(graph, node_id)
except Exception: except Exception:

View File

@ -84,9 +84,9 @@ class NXGraph(Graph):
def _add_vertex(self, v): def _add_vertex(self, v):
properties_copy = copy.copy(v.properties) properties_copy = copy.copy(v.properties)
if properties_copy: if properties_copy:
self._g.add_node(n=v.vertex_id, **properties_copy) self._g.add_node(v.vertex_id, **properties_copy)
else: else:
self._g.add_node(n=v.vertex_id) self._g.add_node(v.vertex_id)
@Notifier.update_notify @Notifier.update_notify
def add_edge(self, e): def add_edge(self, e):
@ -100,11 +100,10 @@ class NXGraph(Graph):
def _add_edge(self, e): def _add_edge(self, e):
properties_copy = copy.copy(e.properties) properties_copy = copy.copy(e.properties)
if properties_copy: if properties_copy:
self._g.add_edge(u=e.source_id, v=e.target_id, self._g.add_edge(e.source_id, e.target_id,
key=e.label, **properties_copy) e.label, **properties_copy)
else: else:
self._g.add_edge(u=e.source_id, v=e.target_id, self._g.add_edge(e.source_id, e.target_id, e.label)
key=e.label)
def get_vertex(self, v_id): def get_vertex(self, v_id):
"""Fetch a vertex from the graph """Fetch a vertex from the graph