Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

End-to-End Path FIx #43

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions ipfabric/ipfabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,56 @@ def get_parsed_path_simulation(
"""
response = self.get_path_simulation(src_ip, dst_ip, src_port, dst_port, protocol, snapshot_id)
graph = response.get("graph", {})
nodes = {graph_node["id"]: graph_node for graph_node in graph.get("nodes", {})}
edges = {edge["id"]: edge for edge in graph.get("edges", {})}
path = []
nodes = {graph_node["id"]: graph_node for graph_node in graph.get("nodes", {})}

# ipfabric returns the source of the path as the last element in the nodes list
for idx, node in enumerate(graph.get("nodes", [])[::-1]):
edge_id = node["forwarding"][0]["dstIntList"][0]["id"]
edge = edges.get(edge_id)
if not edge or idx == len(nodes) - 1:
continue # don't add to path as the edge for the penultimate node will contain the 'target' node
for idx, edge in enumerate(graph.get("edges", [])[::-1]):
forwarding_type = ""
edge_id = edge.get("id")

src_name = edge.get("source")
src_node = nodes.get(src_name, {})
src_forwarding = src_node.get("forwarding")
src_type = src_node.get("devType")
src_fwd = edge.get("srcAddr", "")
if src_forwarding:
forwarding_type = src_forwarding[0].get("search")

dst_name = edge.get("target")
dst_node = nodes.get(dst_name, {})
dst_forwarding = dst_node.get("forwarding")
dst_type = dst_node.get("devType")
dst_fwd = edge.get("dstAddr", "")
if dst_forwarding:
forwarding_type = dst_forwarding[0].get("search")

if src_forwarding and forwarding_type == "mpls":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would group these 2 if statements in one:

if forwarding_type == "mpls":
    if src_forwarding:
    ...
    if dst_forwarding:
    ...

just to group all the mpls stuff together

# edge src tag
src_node_int_list = [*src_forwarding[0].get("srcIntList"), *src_forwarding[0].get("dstIntList")]
for intf in src_node_int_list:
if intf.get("id") == edge_id:
src_fwd = intf.get("labelStack")

if dst_forwarding and forwarding_type == "mpls":
# edge dst tag
dst_node_int_list = [*dst_forwarding[0].get("srcIntList"), *dst_forwarding[0].get("dstIntList")]
for intf in dst_node_int_list:
if edge.get("tlabel") and intf.get("int") == edge.get("tlabel"):
dst_fwd = intf.get("labelStack")

path.append(
(
idx + 1,
node.get("hostname"),
edge["slabel"],
edge["srcAddr"],
edge["dstAddr"],
edge["tlabel"],
nodes.get(edge["target"], {}).get("hostname"),
forwarding_type or "(empty)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use a CONSTANT instead of "(empty)" ?
so we could change it in one shoot later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, good one. Will update.

src_node.get("hostname") or "(empty)",
src_type or "(empty)",
edge.get("slabel") or "(empty)",
src_fwd or "(empty)",
dst_fwd or "(empty)",
edge.get("tlabel") or "(empty)",
dst_type or "(empty)",
dst_node.get("hostname") or "(empty)",
)
)
return path
Expand All @@ -201,21 +232,18 @@ def get_src_dst_endpoint(
"""
response = self.get_path_simulation(src_ip, dst_ip, src_port, dst_port, protocol, snapshot_id)
graph = response.get("graph", {})

endpoints = {}
src_intf = ""
dst_intf = ""
src_node = ""
dst_node = ""
endpoints["src"] = "Unknown"
endpoints["dst"] = "Unknown"

# ipfabric returns the source of the path as the last element in the nodes list
for idx, node in enumerate(graph.get("nodes", [])[::-1]):
if idx == 0:
src_intf = node["forwarding"][0]["srcIntList"][0]["int"]
src_node = node.get("hostname")
endpoints["src"] = f"{src_intf} - {src_node}"
if idx == len(graph.get("nodes", [])) - 1:
dst_intf = node["forwarding"][0]["dstIntList"][0]["int"]
dst_node = node.get("hostname")
endpoints["dst"] = f"{dst_intf} - {dst_node}"
node_forwarding = node.get("forwarding")
if node_forwarding:
if idx == 0:
src_intf = node_forwarding[0]["srcIntList"][0]["int"]
endpoints["src"] = f"{src_intf} -- {node.get('hostname')}"
elif idx == len(graph.get("nodes", [])) - 1:
dst_intf = node_forwarding[0]["dstIntList"][0]["int"]
endpoints["dst"] = f"{dst_intf} -- {node.get('hostname')}"
return endpoints
13 changes: 12 additions & 1 deletion ipfabric/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,18 @@ def end_to_end_path(
f"{dispatcher.bold('Destination: ')} {dst_ip} [{endpoints.get('dst')}]\n"
)
dispatcher.send_large_table(
["Hop", "Src Host", "Src Intf", "Src IP", "Dst IP", "Dst Intf", "Dst Host"],
[
"Hop",
"Fwd Type",
"Src Host",
"Src Type",
"Src Intf",
"Src Fwd",
"Dst Fwd",
"Dst Intf",
"Dst Type",
"Dst Host",
],
path,
)

Expand Down