Best Python code snippet using lisa_python
topology_manager.py
Source:topology_manager.py
2class TopologyManager:3 def __init__(self):4 self.graph = nx.Graph()5 def add_switch(self, switch_identifier):6 if self.exists_switch(switch_identifier):7 return False8 description = {9 "type": "switch"10 }11 self.graph.add_node(switch_identifier, description = description)12 return True13 def remove_switch(self, switch_identifier):14 if not self.exists_switch(switch_identifier):15 return False16 self.graph.remove_node(switch_identifier)17 for host in self.hosts():18 host_description = self.graph.node[host]["description"]19 switch_identifier_connected_to = host_description["switch_identifier_connected_to"]20 if switch_identifier_connected_to == switch_identifier:21 self.graph.remove_node(host)22 return True23 def switches(self):24 switches = []25 for node in self.graph.nodes(data ="description"):26 node_identifier = node[0]27 node_type = node[1]["type"]28 if node_type == "switch":29 switches.append(node_identifier)30 return switches31 def add_host(self, host_identifier):32 if self.exists_host(host_identifier):33 return False34 description = {35 "type": "host"36 }37 self.graph.add_node(host_identifier, description = description)38 return True39 def update_host(self, host_identifier, **host_parameters):40 if not self.exists_host(host_identifier):41 return False42 for parameter, parameter_value in host_parameters.items():43 self.graph.node[host_identifier]["description"][parameter] = parameter_value44 return True45 def remove_host(self, host_identifier):46 if not self.exists_host(host_identifier):47 return False48 self.graph.remove_node(host_identifier)49 return True50 def hosts(self):51 hosts = []52 for node in self.graph.nodes(data ="description"):53 node_identifier = node[0]54 node_type = node[1]["type"]55 if node_type == "host":56 hosts.append(node_identifier)57 return hosts58 def add_link(self, node_from_identifier, node_to_identifier, node_from_port, node_to_port):59 if not self.graph.has_node(node_from_identifier) or not self.graph.has_node(node_to_identifier):60 return False61 if self.exists_link(node_from_identifier, node_to_identifier):62 return False63 description = {64 "type": "link",65 ("port_" + node_from_identifier): node_from_port,66 ("port_" + node_to_identifier): node_to_port67 }68 self.graph.add_edge(node_from_identifier, node_to_identifier, description = description)69 print(self.topology())70 return True71 def remove_link(self, node_from_identifier, node_to_identifier):72 if not self.graph.has_node(node_from_identifier) or not self.graph.has_node(node_to_identifier):73 return False74 if not self.exists_link(node_from_identifier, node_to_identifier):75 return False76 self.graph.remove_edge(node_from_identifier, node_to_identifier)77 return True78 def links(self):79 edges = []80 for edge in self.graph.edges():81 edges.append(edge)82 return edges83 def exists_switch(self, switch_identifier):84 exists = self.switches().__contains__(switch_identifier)85 return exists86 def exists_host(self, host_identifier):87 exists = self.hosts().__contains__(host_identifier)88 return exists89 def exists_link(self, node_from_identifier, node_to_identifier):90 exists = self.graph.has_edge(node_from_identifier, node_to_identifier)91 return exists92 def clear_topology(self):93 self.graph.clear()94 # REST API95 def topology(self):96 topology = {97 "nodes": {...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!