创建表示网络拓扑的拓扑类。
创建类的实例时,描述拓扑的字典将作为参数传递。字典可能包含“重复”连接。“重复”连接是指字典中有两个连接的情况:
("R1", "Eth0/0"): ("SW1", "Eth0/1")
("SW1", "Eth0/1"): ("R1", "Eth0/0")
任务是在最终字典中只留下其中一个链接,无论哪个链接。
在每个实例中,必须创建一个拓扑实例变量,其中包含拓扑字典,但已经没有“重复项”。拓扑实例变量应在创建实例后立即包含没有“重复项”的字典。
from rich import print
from pprint import pprint
class Topology:
def __init__(self, topology_dict):
self.topology = self._normalize(topology_dict)
def _normalize(self, topology_dict):
# 消重方法 #1:
return {
min(local, remote): max(local, remote)
for local, remote in topology_dict.items()
}
# 消重方法 #2:
# topo = {}
# for local, remote in topology_dict.items():
# if not topo.get(remote) == local:
# topo[local] = remote
# return topo
# 消重方法 #3:
# normalized_topology = {}
# for box, neighbor in topology_dict.items():
# if not neighbor in normalized_topology:
# normalized_topology[box] =neighbor
# return normalized_topology
def delete_link(self, from_port, to_port):
if self.topology.get(from_port) == to_port:
del self.topology[from_port]
elif self.topology.get(to_port) == from_port:
del self.topology[to_port]
else:
print('There is no such link')
def delete_node(self, node):
original_size = len(self.topology)
for src, dest in list(self.topology.items()):
if node in src or node in dest:
del self.topology[src]
if original_size == len(self.topology):
print("There is no such device")
def add_link(self, src, dest):
keys_and_values = self.topology.keys() | self.topology.values()
if self.topology.get(src) == dest:
print("Such a connection already exists.")
elif src in keys_and_values or dest in keys_and_values:
print("A link to one of the port exists")
else:
self.topology[src] = dest
# 1、初始化拓扑图
topology_example = {
("R1", "Eth0/0"): ("SW1", "Eth0/1"),
("R2", "Eth0/0"): ("SW1", "Eth0/2"),
("R2", "Eth0/1"): ("SW2", "Eth0/11"),
("R3", "Eth0/0"): ("SW1", "Eth0/3"),
("R3", "Eth0/1"): ("R4", "Eth0/0"),
("R3", "Eth0/2"): ("R5", "Eth0/0"),
("SW1", "Eth0/1"): ("R1", "Eth0/0"),
("SW1", "Eth0/2"): ("R2", "Eth0/0"),
("SW1", "Eth0/3"): ("R3", "Eth0/0"),
}
# 2、创建拓扑图,删除冗余部分
print('2、创建拓扑图,删除冗余部分'.center(80, '#'))
top = Topology(topology_example)
print(top.topology)
# 3、删除链接
print('3、删除链接'.center(80, '#'))
top.delete_link(("R3", "Eth0/1"), ("R4", "Eth0/0"))
pprint(top.topology)
top.delete_link(('R5', 'Eth0/0'), ('R3', 'Eth0/2'))
pprint(top.topology)
top.delete_link(('R5', 'Eth0/0'), ('R3', 'Eth0/2'))
# 4、删除节点设备
print('4、删除节点设备'.center(80, '#'))
top = Topology(topology_example)
top.delete_node('SW1')
print(top.topology)
top.delete_node('SW1')
# 5、添加连接
print('5、添加连接'.center(80, '#'))
top = Topology(topology_example)
top.add_link(('R1', 'Eth0/4'), ('R7', 'Eth0/0'))
print(top.topology)
top.add_link(('R1', 'Eth0/4'), ('R7', 'Eth0/0'))
top.add_link(('R1', 'Eth0/4'), ('R7', 'Eth0/5'))
输出:
#################################2、创建拓扑图,删除冗余部分#################################
{
('R1', 'Eth0/0'): ('SW1', 'Eth0/1'),
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'),
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'),
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'),
('R3', 'Eth0/1'): ('R4', 'Eth0/0'),
('R3', 'Eth0/2'): ('R5', 'Eth0/0')
}
#####################################3、删除链接#####################################
{('R1', 'Eth0/0'): ('SW1', 'Eth0/1'),
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'),
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'),
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'),
('R3', 'Eth0/2'): ('R5', 'Eth0/0')}
{('R1', 'Eth0/0'): ('SW1', 'Eth0/1'),
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'),
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'),
('R3', 'Eth0/0'): ('SW1', 'Eth0/3')}
There is no such link
####################################4、删除节点设备####################################
{('R2', 'Eth0/1'): ('SW2', 'Eth0/11'), ('R3', 'Eth0/1'): ('R4', 'Eth0/0'), ('R3', 'Eth0/2'): ('R5', 'Eth0/0')}
There is no such device
#####################################5、添加连接#####################################
{
('R1', 'Eth0/0'): ('SW1', 'Eth0/1'),
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'),
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'),
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'),
('R3', 'Eth0/1'): ('R4', 'Eth0/0'),
('R3', 'Eth0/2'): ('R5', 'Eth0/0'),
('R1', 'Eth0/4'): ('R7', 'Eth0/0')
}
Such a connection already exists.
A link to one of the port exists
文章评论