我正在尝试从rest api调用中创建节点和边缘的简单图形...到目前为止,它能够获取节点,将其作为json转储并发送到d3以进行可视化。 这里的问题我无法绘制边缘或链接的图形。下面给我一个错误
e = (link['link']['destination']['node']['node-id'], link['link']['source']['node']['node-id'])
TypeError: 'int' object has no attribute '__getitem__'
这是从ODL REST API调用的节点和链接的详细信息
Nodes List:
Node ID - openflow:1
Node ID - host:00:00:00:00:00:01
Node ID - openflow:2
Node ID - host:00:00:00:00:00:02
Links List:
Link ID - host:00:00:00:00:00:01/openflow:1:1
Link ID - openflow:2:1/host:00:00:00:00:00:02
Link ID - openflow:1:2
Link ID - openflow:2:2
Link ID - openflow:1:1/host:00:00:00:00:00:01
Link ID - host:00:00:00:00:00:02/openflow:2:1
这是从API调用的详细信息
{
u'network-topology': {
u'topology': [
{
u'node': [
{
u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']",
u'node-id': u'openflow:1',
u'termination-point': [
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:2']",
u'tp-id': u'openflow:1:2'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:1']",
u'tp-id': u'openflow:1:1'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:LOCAL']",
u'tp-id': u'openflow:1:LOCAL'
}
]
},
{
u'host-tracker-service:addresses': [
{
u'first-seen': 1562655393902,
u'ip': u'10.0.0.1',
u'mac': u'00:00:00:00:00:01',
u'id': 6,
u'last-seen': 1562655393902
}
],
u'host-tracker-service:id': u'00:00:00:00:00:01',
u'host-tracker-service:attachment-points': [
{
u'active': True,
u'corresponding-tp': u'host:00:00:00:00:00:01',
u'tp-id': u'openflow:1:1'
}
],
u'node-id': u'host:00:00:00:00:00:01',
u'termination-point': [
{
u'tp-id': u'host:00:00:00:00:00:01'
}
]
},
{
u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']",
u'node-id': u'openflow:2',
u'termination-point': [
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:LOCAL']",
u'tp-id': u'openflow:2:LOCAL'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:1']",
u'tp-id': u'openflow:2:1'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:2']",
u'tp-id': u'openflow:2:2'
}
]
},
{
u'host-tracker-service:addresses': [
{
u'first-seen': 1562655393906,
u'ip': u'10.0.0.2',
u'mac': u'00:00:00:00:00:02',
u'id': 7,
u'last-seen': 1562655393906
}
],
u'host-tracker-service:id': u'00:00:00:00:00:02',
u'host-tracker-service:attachment-points': [
{
u'active': True,
u'corresponding-tp': u'host:00:00:00:00:00:02',
u'tp-id': u'openflow:2:1'
}
],
u'node-id': u'host:00:00:00:00:00:02',
u'termination-point': [
{
u'tp-id': u'host:00:00:00:00:00:02'
}
]
}
],
u'link': [
{
u'link-id': u'host:00:00:00:00:00:01/openflow:1:1',
u'destination': {
u'dest-node': u'openflow:1',
u'dest-tp': u'openflow:1:1'
},
u'source': {
u'source-tp': u'host:00:00:00:00:00:01',
u'source-node': u'host:00:00:00:00:00:01'
}
},
{
u'link-id': u'openflow:2:1/host:00:00:00:00:00:02',
u'destination': {
u'dest-node': u'host:00:00:00:00:00:02',
u'dest-tp': u'host:00:00:00:00:00:02'
},
u'source': {
u'source-tp': u'openflow:2:1',
u'source-node': u'openflow:2'
}
},
{
u'link-id': u'openflow:1:2',
u'destination': {
u'dest-node': u'openflow:2',
u'dest-tp': u'openflow:2:2'
},
u'source': {
u'source-tp': u'openflow:1:2',
u'source-node': u'openflow:1'
}
},
{
u'link-id': u'openflow:2:2',
u'destination': {
u'dest-node': u'openflow:1',
u'dest-tp': u'openflow:1:2'
},
u'source': {
u'source-tp': u'openflow:2:2',
u'source-node': u'openflow:2'
}
},
{
u'link-id': u'openflow:1:1/host:00:00:00:00:00:01',
u'destination': {
u'dest-node': u'host:00:00:00:00:00:01',
u'dest-tp': u'host:00:00:00:00:00:01'
},
u'source': {
u'source-tp': u'openflow:1:1',
u'source-node': u'openflow:1'
}
},
{
u'link-id': u'host:00:00:00:00:00:02/openflow:2:1',
u'destination': {
u'dest-node': u'openflow:2',
u'dest-tp': u'openflow:2:1'
},
u'source': {
u'source-tp': u'host:00:00:00:00:00:02',
u'source-node': u'host:00:00:00:00:00:02'
}
}
],
u'topology-id': u'flow:1'
}
]
}
}
这是我测试过的代码,它只能获取节点,而不能获取链接/边...
# get nodes
resp, content = h.request(url, "GET")
nodeData = json.loads(content)
topology = nodeData['network-topology']['topology'][0]
print "Nodes List:"
for i in range(len(topology['node'])):
print "Node ID - ", topology['node'][i]['node-id']
i += 1
# Get links/edges
resp, content = h.request(url, "GET")
linkData = json.loads(content)
topology = linkData['network-topology']['topology'][0]
print "Links List:"
for i in range(len(topology['link'])):
print "Link ID - ", topology['link'][i]['link-id']
i += 1
# Add nodes and links into graph
graph = nx.Graph()
for node in range(len(topology['node'])):
graph.add_node(topology['node'][node]['node-id'])
for link in range(len(topology['link'])):
e = (link['link']['destination']['node']['node-id'], link['link']['source']['node']['node-id'])
graph.add_edge(*e)
print "Graph Nodes:"
print graph.nodes()
print "Graph Links:"
print graph.links()
# write json formatted data to use in visualization
d = json_graph.node_link_data(graph)
json.dump(d, open('topo.json','w'))
print('Wrote node-link JSON data')
我尝试通过删除与链接/边沿相关的代码并保留节点而没有问题..我可以在topo.json中获取节点的详细信息..但是当我为链接添加图形时遇到了问题
这里的问题是我无法绘制边缘或链接的图形。下面给我一个错误
e = (link['link']['destination']['node']['node-id'], link['link']['source']['node']['node-id'])
TypeError: 'int' object has no attribute '__getitem__'
我只是不知道如何解决...这里的专家们提供了宝贵的建议和帮助。.谢谢
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
我在下面进行了修改,但得到了不同的错误TypeError:字符串索引必须为整数
# Get links/edges
resp, content = h.request(url, "GET")
linkData = json.loads(content)
a = linkData['network-topology']['topology'][0]
print a
# Add links into graph
graph = nx.Graph()
for link in a:
e = (link['link']['destination']['node']['node-id'], link['link']['source']['node']['node-id'])
graph.add_edge(*e)
在上方打印出下面的内容
{
u'node': [
{
u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']",
u'node-id': u'openflow:1',
u'termination-point': [
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:2']",
u'tp-id': u'openflow:1:2'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:1']",
u'tp-id': u'openflow:1:1'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:LOCAL']",
u'tp-id': u'openflow:1:LOCAL'
}
]
},
{
u'host-tracker-service:addresses': [
{
u'first-seen': 1562655393902,
u'ip': u'10.0.0.1',
u'mac': u'00:00:00:00:00:01',
u'id': 6,
u'last-seen': 1562655393902
}
],
u'host-tracker-service:id': u'00:00:00:00:00:01',
u'host-tracker-service:attachment-points': [
{
u'active': True,
u'corresponding-tp': u'host:00:00:00:00:00:01',
u'tp-id': u'openflow:1:1'
}
],
u'node-id': u'host:00:00:00:00:00:01',
u'termination-point': [
{
u'tp-id': u'host:00:00:00:00:00:01'
}
]
},
{
u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']",
u'node-id': u'openflow:2',
u'termination-point': [
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:LOCAL']",
u'tp-id': u'openflow:2:LOCAL'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:1']",
u'tp-id': u'openflow:2:1'
},
{
u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:2']",
u'tp-id': u'openflow:2:2'
}
]
},
{
u'host-tracker-service:addresses': [
{
u'first-seen': 1562655393906,
u'ip': u'10.0.0.2',
u'mac': u'00:00:00:00:00:02',
u'id': 7,
u'last-seen': 1562655393906
}
],
u'host-tracker-service:id': u'00:00:00:00:00:02',
u'host-tracker-service:attachment-points': [
{
u'active': True,
u'corresponding-tp': u'host:00:00:00:00:00:02',
u'tp-id': u'openflow:2:1'
}
],
u'node-id': u'host:00:00:00:00:00:02',
u'termination-point': [
{
u'tp-id': u'host:00:00:00:00:00:02'
}
]
}
],
u'link': [
{
u'link-id': u'host:00:00:00:00:00:01/openflow:1:1',
u'destination': {
u'dest-node': u'openflow:1',
u'dest-tp': u'openflow:1:1'
},
u'source': {
u'source-tp': u'host:00:00:00:00:00:01',
u'source-node': u'host:00:00:00:00:00:01'
}
},
{
u'link-id': u'openflow:2:1/host:00:00:00:00:00:02',
u'destination': {
u'dest-node': u'host:00:00:00:00:00:02',
u'dest-tp': u'host:00:00:00:00:00:02'
},
u'source': {
u'source-tp': u'openflow:2:1',
u'source-node': u'openflow:2'
}
},
{
u'link-id': u'openflow:1:2',
u'destination': {
u'dest-node': u'openflow:2',
u'dest-tp': u'openflow:2:2'
},
u'source': {
u'source-tp': u'openflow:1:2',
u'source-node': u'openflow:1'
}
},
{
u'link-id': u'openflow:2:2',
u'destination': {
u'dest-node': u'openflow:1',
u'dest-tp': u'openflow:1:2'
},
u'source': {
u'source-tp': u'openflow:2:2',
u'source-node': u'openflow:2'
}
},
{
u'link-id': u'openflow:1:1/host:00:00:00:00:00:01',
u'destination': {
u'dest-node': u'host:00:00:00:00:00:01',
u'dest-tp': u'host:00:00:00:00:00:01'
},
u'source': {
u'source-tp': u'openflow:1:1',
u'source-node': u'openflow:1'
}
},
{
u'link-id': u'host:00:00:00:00:00:02/openflow:2:1',
u'destination': {
u'dest-node': u'openflow:2',
u'dest-tp': u'openflow:2:1'
},
u'source': {
u'source-tp': u'host:00:00:00:00:00:02',
u'source-node': u'host:00:00:00:00:00:02'
}
}
],
u'topology-id': u'flow:1'
}
仍然试图解决它...到目前为止还没有运气...
需要从API调用中获取所有节点和链接(源和目标),并将其保存到json文件中,然后发送到d3.js进行图形可视化。请指教。谢谢
答案 0 :(得分:0)
此for循环:
for link in range(len(topology['link'])):
e = (link['link']['destination']['node']['node-id'], link['link']['source']['node']['node-id'])
graph.add_edge(*e)
告诉Python为link
赋值0
,1
,2
,...,直至topology['link']
的长度(以及长度)减1)。这就解释了当它试图在整数['link']
中找到条目0
时第一次遇到的for循环第一行中的错误。整数没有此类条目。
您是要做for link in topology['link']:
还是其他事情?您似乎希望link
是某种形式的字典。
您的代码还有其他问题。
for i in range(len(topology['link'])):
print "Link ID - ", topology['link'][i]['link-id']
i += 1
i+=1
在块末尾没有多大意义,因为下一次循环通过时,i
从for循环中获得了一个新值。我还想知道您在topology['node']
上的for循环中是否存在隐藏错误,类似于给出错误的循环中的错误。