def getAllRings(graph, isMultigraph): """ Parameters: graph -> networkx graph or multigraph object, isMultigraph -> True if graph is multigraph else false Returns list of list of edges forming cycles in the graph. """ if isMultigraph: multigraph, graph = graph, nx.Graph(graph) all_cycle_edge_list = [] Debuglogger.info("Getting cycle basis") cycle_basis_list = getCycleBasis(graph) # get cycle basis of given graph Userlogger.info('\nGetting all the rings from physical topology') for i in range(1,len(cycle_basis_list)+1): # form combinations of cycle basis Debuglogger.debug("Getting combinations of basis cycle picking "+str(i)+" items") for combination in combinations(cycle_basis_list,i): if combination: Debuglogger.debug("combination exists") edge_set = {} for basis in combination: # find symmetric difference of edge_set = set(basis).symmetric_difference(edge_set)# members of each combination ring = nx.Graph() ring.add_edges_from(list(edge_set)) if isCycle(ring): #if the member is a connected cycle Debuglogger.debug("Is a connected cycle") all_cycle_edge_list.append(list(edge_set)) # add to list of all cycles Debuglogger.info("Ring "+str(len(all_cycle_edge_list))+" "+str(all_cycle_edge_list[-1])) Userlogger.info("Ring "+str(len(all_cycle_edge_list))+" "+str(all_cycle_edge_list[-1])) if isMultigraph: return getRingsMultigraph(multigraph, all_cycle_edge_list) return all_cycle_edge_list
def repp(graph): """ repp() method takes networkx object as input and enables and configures rep on all ports for all nodes in networkx object. """ Userlogger.info("enabling rep in network") Userlogger.info("\nEnabling rep on Selected logical topology") graph_nodes_list=graph.nodes() node_num=1 i=0 while i< len(graph_nodes_list): try: node_data=graph_nodes_list[i].split('\n') if node_num==1: Userlogger.info('Enabling rep on '+str(node_data[0])+ ' edge') run.Switch_commands.commandExecute(node_data[1],node_data[2],'config','rep segment 1','enable',graph.node[graph_nodes_list[i]]['node_port'],'edge') else: Userlogger.info('Enabling rep on '+str(node_data[0])) run.Switch_commands.commandExecute(node_data[1],node_data[2],'config','rep segment 1','enable',graph.node[graph_nodes_list[i]]['node_port'],' ') node_num+=1 i = i+1 #bar update bar.update(.4*barlen+( (.6*barlen*i)/len(graph_nodes_list) )) except: pass #end the bar bar.finish()
def switch_exec(ip,port): """ makes the switch in exec mode instead of config mode. """ Debuglogger.info("In switch_exec function") Debuglogger.info("makes switch ("+ip+' '+port+") in exec mode") Userlogger.info("makes switch ("+ip+' '+port+") in exec mode") child= pexpect.spawn('telnet '+ip+' '+port) child.expect('Escape.*') child.sendcontrol('m') child.expect('#') child.sendline('end') child.expect('#') child.sendline('quit') child.expect(' ') child.sendcontrol('m') child.expect('>')
def restore(graph): """ restore() method takes networkx object as input and enables all ports and disables rep on all ports for all nodes in networkx object. """ bar.start() Userlogger.info("restoring the previous network topology") Userlogger.info('\nRestoring prevoius Physical topology and diabling rep') graph_nodes_list=graph.nodes() i=0 while i< len(graph_nodes_list): try: node_data=graph_nodes_list[i].split('\n') run.Switch_commands.commandExecute(node_data[1],node_data[2],'config','no rep segment 1','restore',graph.node[graph_nodes_list[i]]['node_port']) i+=1 #bar update bar.update( (barlen*i)/len(graph_nodes_list) ) except: pass #end the bar bar.finish()
def showrepp(graph): " shows the rep topology" node=graph.nodes()[0] node_data=node.split('\n') Debuglogger.info('\nin show repp function') Userlogger.info('\nshowing the rep configuration') while 1: try: run.Switch_commands.commandExecute(node_data[1],node_data[2],'exec','show rep topology') templog=open('tempLog.txt','r') lines=templog.readlines() i=5 while i<len(lines): print lines[i] i+=1 templog.close() break except: pass
def getRingsMultigraph(multigraph, all_cycle_edge_list): """ Input -> multigraph object, list of cycles or rings got from simple graph Returns list of list of edges of a ring. Edge is represented as (node1.node2,edge_id) """ Debuglogger.info("In getRingsMultigraph") all_cycle_edge_list_multi = [] for ring in all_cycle_edge_list: Debuglogger.debug("Ring:"+str(ring)) cycle = [] multi_edge_list = [] edge_index = [] for edge in ring: Debuglogger.debug("Edge:"+str(edge)) node1 = edge[0] node2 = edge[1] if len(multigraph[node1][node2])>1: Debuglogger.info("this ia a multi edge") single_multi_edge = [] for edge_id in multigraph[node1][node2].keys(): single_multi_edge.append((node1, node2, edge_id)) edge_new = single_multi_edge[0] multi_edge_list.append(single_multi_edge) edge_index.append(ring.index(edge)) else: edge_new = edge + (0,) cycle.append(edge_new) all_cycle_edge_list_multi.append(cycle) Debuglogger.debug("multi_edge_list:"+str(multi_edge_list)) if multi_edge_list: for combination in product(*multi_edge_list): for i in range(len(combination)): idx = edge_index[i] temp = cycle[:idx]+[combination[i]] if i<len(cycle): temp+=cycle[idx+1:] cycle = temp #print cycle if temp not in all_cycle_edge_list_multi: all_cycle_edge_list_multi.append(temp) Userlogger.info("Ring "+str(len(all_cycle_edge_list_multi))+" "+str(temp)) Debuglogger.info("Ring "+str(len(all_cycle_edge_list_multi))+" "+str(temp)) return all_cycle_edge_list_multi
def clearline(ip,port): """ clear the line of the given switch with ip and port """ Debuglogger.info("In Clearline function") Debuglogger.info("Clear the line of switch with "+ip+' '+port) Userlogger.info("Clear the line of switch with "+ip+' '+port) child= pexpect.spawn('telnet '+ip) child.expect('Password:'******'lab') child.expect('>') child.sendline('en') child.expect('Password:'******'lab') child.expect('#') child.sendline('clear line '+str(int(port)%100)) child.sendcontrol('m') child.expect('#') child.close() print "line is cleared"
def addedge(self): """ addedge() finds the edge from the cdp data and calls eList() to add the edge entry (two switch name and corresponding interfaces and type of link) in EdgeList. """ Debuglogger.info("Finding Edges") fCDP=open('tempLog.txt','r') lines=fCDP.readlines() i=0 #to get to that line which has switch information for line in lines: if "Device ID" in lines[i]: i+=1 break i+=1 while i< len(lines): currentline_info=lines[i].split() Ln=len(currentline_info) if Ln==0: break #this is to include if switch name is too long then it interface information comes to next line elif Ln==1: i=i+1 nextline_info=lines[i].split() destSwitch=currentline_info[0] source_interface=nextline_info[0]+nextline_info[1] dest_interface=nextline_info[-2]+nextline_info[-1] i=i+1 else: destSwitch=currentline_info[0] source_interface=currentline_info[1]+currentline_info[2] dest_interface=currentline_info[-2]+currentline_info[-1] i=i+1 Network_topology.SwitchList[self.SwitchName+'\n'+self.host+'\n'+self.port].append(source_interface+' '+dest_interface) self.eList(destSwitch, source_interface, dest_interface) Userlogger.info(destSwitch) fCDP.close()
def cdpread(self): """ cdpread() method runs cdp command through commandExecute() defined in RunCommands module to find the neighbours. It calls findname() and addedge() to find the name of switch and finding edges corresponding to that switch. """ Debuglogger.info("In network topology") run.Switch_commands.commandExecute(self.host,self.port,'exec','show cdp ne') self.findname() Userlogger.info('\nLogged into : '+self.SwitchName+' '+self.host+' '+self.port) Userlogger.info(self.SwitchName+'is connected to :') Network_topology.SwitchList[self.SwitchName+'\n'+self.host+'\n'+self.port]=[] self.addedge() Userlogger.info('\n')
def disable_switch(graph): """ disable_switch() method takes networkx object as input and disables all ports for all nodes in networkx object. """ #start the progressbar bar.start() Userlogger.info("disable all the ports in network") Userlogger.info('\nDisabling all ports of Physical topology') graph_nodes_list=graph.nodes() i=0 while i< len(graph_nodes_list): try: node_data=graph_nodes_list[i].split('\n') Userlogger.info('\nDisabling all ports of '+str(node_data[0])) run.Switch_commands.commandExecute(node_data[1],node_data[2],'config','shutdown','disable',graph.node[graph_nodes_list[i]]['node_port']) i=i+1 #bar update bar.update((.4*barlen*i)/len(graph_nodes_list)) except: pass
#Entry point of the program try: opts, args = getopt.getopt(sys.argv[1:],"hel") except getopt.GetoptError: print 'To Take the help, Run command : \"python Self_Forming_Rings.py\" -h' sys.exit(2) if len(opts) > 0: get_args(opts) sys.exit(1) #start the progressbar bar.start() Userlogger.info('\n\n\t'+' ' + str(datetime.datetime.now()) ) # get topology fout=open('4.txt','r') lines=fout.readlines() i=0 while i<len(lines): try: line=lines[i] Switch = mt.Network_topology(line.split()[0],line.split()[1]) Switch.cdpread() i+=1 #bar update bar.update((.4*barlen)*(i)/len(lines)) except: pass