Я пытаюсь создать простой граф узлов и ребер из вызова 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 для визуализации графа. Пожалуйста, порекомендуйте. Спасибо