-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.py
109 lines (82 loc) · 3.22 KB
/
topology.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
class Topology():
def __init__(self):
self.nodes = dict()
def add_node(self, node_id, node_object):
if node_id in self.nodes:
print("Node Id already exists")
return
#Check if node_object is of type Node
self.nodes[node_id]=node_object
def display_topology(self):
print("Printing the topology")
if len(self.nodes) > 0:
for node_id in self.nodes.keys():
print("Node_id::"+node_id)
self.nodes[node_id].display_node()
def get_node_id_by_name(self, node_name):
if len(self.nodes) > 0:
for node_id in self.nodes.keys():
if self.nodes[node_id].name == node_name:
return node_id
return None
class Node:
def __init__(self, node_name):
self.name = node_name
self.ports = dict()
def add_port(self, port_id, port_object):
if port_id in self.ports:
print("Port Id already exists")
return
#Check if port_object is of type Port
self.ports[port_id]=port_object
def display_node(self):
print("Node::Name:"+self.name)
if len(self.ports) > 0:
for port_id in self.ports.keys():
print("Port_id::"+port_id)
self.ports[port_id].display_port()
class Port:
def __init__(self, port_name, ip_address):
self.name = port_name
self.ip_address = ip_address
self.link_id = None
self.connected_to_node_id = None
self.connected_to_port_id = None
def add_link(self, link_id, node_id, port_id):
self.link_id = link_id
self.connected_to_node_id = node_id
self.connected_to_port_id = port_id
def display_port(self):
print("Port::Name:"+self.name)
print("Port::ip_address:"+self.ip_address)
if self.link_id is not None:
print("Port::link_id:"+self.link_id)
print("Port::connected_to_node_id:"+self.connected_to_node_id)
print("Port::connected_to_port_id:"+self.connected_to_port_id)
class IntermediateNode:
def __init__(self):
self.inodes = []
def add_inode(self, inode_name):
if inode_name not in self.inodes:
self.inodes.append(self._get_empty_inode_dict(inode_name))
def _get_empty_inode_dict(self, inode_name):
node_dict = {inode_name : []}
return node_dict
def _get_link_dict(self, intf_name, node_name):
link_dict = {'intf_name': intf_name,
'node_name': node_name}
return link_dict
def add_link_to_inode(self, inode_name, intf_name, node_name):
if inode_name not in self.inodes:
self.add_inode(inode_name)
self.inodes[inode_name].append(self._get_link_dict(intf_name, node_name))
def __print_link(self, link_dict):
if 'intf_name' in link_dict:
print("Intf_name"+link_dict['intf_name'])
if 'node_name' in link_dict:
print("node_name"+link_dict['node_name'])
def print_inodes(self):
for inode_name in self.inodes:
print("INODE name:"+inode_name)
for link_dict in self.inodes[inode_name]:
self.__print_link(link_dict)