簡體   English   中英

無法使用networkx和d3創建圖鏈接/邊

[英]Not able to create graph links/edges using networkx and d3

我正在嘗試從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進行圖形可視化。 請指教。 謝謝

這個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的值012 ,...到的長度topology['link']當然,長度減去1)。 這就解釋了它在試圖查找整數0的條目['link']時第一次遇到的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在該塊的末尾沒有多大意義,因為下一次循環通過時,for循環會給i一個新值。 我也想知道在您的遍歷topology['node'] for循環中是否存在隱藏的錯誤,類似於給出錯誤的循環中的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM