-
Notifications
You must be signed in to change notification settings - Fork 1
/
weathermap2totem.py
executable file
·114 lines (97 loc) · 3.87 KB
/
weathermap2totem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
nodes = []
links = []
class Node:
""" Represents a node in the topology """
def __init__(self, name):
self.name = name
self.x = 0
self.y = 0
self.ifaces = []
def get_iface(self, node):
""" Create an interface to attach link to """
iface_id = len(self.ifaces)+1
self.ifaces.append({ "name": "{} to {}".format(self.name, node.name), "id": iface_id })
return iface_id
class Link:
""" Represents a link between two nodes in the topology """
def __init__(self, left, right):
self.left = left
self.right = right
self.metric = 0
self.bandwidth = 10000
self.left_iface = left.get_iface(right)
self.right_iface = right.get_iface(left)
def link_id(self):
""" Define a unique link id """
return "{}-{} to {}-{}".format(self.left.name,
self.left_iface,
self.right.name,
self.right_iface)
node = None
link = None
title = "Autogenerated Topology"
# Parse source weathermap config
with open("weathermap.conf", "r") as f:
for line in f:
if line.find("TITLE ") != -1:
pcs = line.split(" ")
title = " ".join(pcs[1:]).strip()
if line.find("NODES") != -1:
# Find the NODES attribute of link
pcs = line.split(" ")
left = [ node for node in nodes if node.name == pcs[1].strip() ][0]
right = [ node for node in nodes if node.name == pcs[2].strip() ][0]
link = Link(left, right)
links.append(link)
node = None
elif line.find("NODE") != -1:
# Find NODE definition
pcs = line.split(" ")
node = Node(pcs[1].strip())
nodes.append(node)
link = None
if node is not None:
# Find coordinates for the NODE
if line.find("POSITION") != -1:
pcs = line.split(" ")
node.x = int(pcs[1].strip())
node.y = int(pcs[2].strip())
if link is not None:
# Figure out link attributes
if line.find("BANDWIDTH") != -1:
# Link bandwidth
pcs = line.split(" ")
link.bandwidth = pcs[1].replace("M", "").strip()
if line.find("#LATENCY") != -1:
# Link latency (not used by network weathermap)
pcs = line.split(" ")
link.delay = int(pcs[1].strip())
if line.find("#METRIC") != -1:
# Link metric (not used by network weathermap)
pcs = line.split(" ")
link.metric = int(pcs[1].strip())
# Write XML data
import xml.etree.cElementTree as ET
root = ET.Element("domain", ASID="12434")
info = ET.SubElement(root, "info")
units = ET.SubElement(info, "units")
ET.SubElement(info, "title").text=title
ET.SubElement(info, "description").text="This was autogenerated by weathermap2totem"
ET.SubElement(units, "unit", type="bandwidth", value="mbps")
ET.SubElement(units, "unit", type="delay", value="ms")
topo = ET.SubElement(root, "topology")
nodes_ = ET.SubElement(topo, "nodes")
links_ = ET.SubElement(topo, "links")
for node in nodes:
elem = ET.SubElement(nodes_, "node", id=node.name)
ET.SubElement(elem, "location", latitude=str(float(node.y)), longitude=str(float(node.x)))
ifaces = ET.SubElement(elem, "interfaces")
for iface in node.ifaces:
ET.SubElement(ifaces, "interface", id=str(iface["id"]))
for link in links:
elem = ET.SubElement(links_, "link", id=link.link_id())
ET.SubElement(elem, "from", node=link.left.name).set("if", str(link.left_iface))
ET.SubElement(elem, "to", node=link.right.name).set("if", str(link.right_iface))
ET.SubElement(elem, "bw").text=str(link.bandwidth)
ET.dump(root)